gbp: Ownership of dynamically created vxlan-gbp tunnels managed via gbp_itf
[vpp.git] / src / plugins / gbp / gbp_endpoint.c
1 /*
2  * gbp.h : Group Based Policy
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <plugins/gbp/gbp_endpoint.h>
19 #include <plugins/gbp/gbp_endpoint_group.h>
20 #include <plugins/gbp/gbp_itf.h>
21 #include <plugins/gbp/gbp_scanner.h>
22 #include <plugins/gbp/gbp_bridge_domain.h>
23 #include <plugins/gbp/gbp_route_domain.h>
24 #include <plugins/gbp/gbp_policy_dpo.h>
25 #include <plugins/gbp/gbp_vxlan.h>
26
27 #include <vnet/ethernet/arp.h>
28 #include <vnet/l2/l2_input.h>
29 #include <vnet/l2/l2_output.h>
30 #include <vnet/l2/feat_bitmap.h>
31 #include <vnet/l2/l2_fib.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vnet/ip/ip_neighbor.h>
34 #include <vnet/fib/fib_walk.h>
35 #include <vnet/vxlan-gbp/vxlan_gbp.h>
36
37 static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
38
39 /**
40  * EP DBs
41  */
42 gbp_ep_db_t gbp_ep_db;
43
44 fib_node_type_t gbp_endpoint_fib_type;
45
46 vlib_log_class_t gbp_ep_logger;
47
48 #define GBP_ENDPOINT_DBG(...)                           \
49     vlib_log_debug (gbp_ep_logger, __VA_ARGS__);
50
51 #define GBP_ENDPOINT_INFO(...)                          \
52     vlib_log_notice (gbp_ep_logger, __VA_ARGS__);
53
54 /**
55  * Pool of GBP endpoints
56  */
57 gbp_endpoint_t *gbp_endpoint_pool;
58
59 /**
60  * A count of the number of dynamic entries
61  */
62 static u32 gbp_n_learnt_endpoints;
63
64 #define FOR_EACH_GBP_ENDPOINT_ATTR(_item)               \
65     for (_item = GBP_ENDPOINT_ATTR_FIRST;               \
66          _item < GBP_ENDPOINT_ATTR_LAST;                \
67          _item++)
68
69 u8 *
70 format_gbp_endpoint_flags (u8 * s, va_list * args)
71 {
72   gbp_endpoint_attr_t attr;
73   gbp_endpoint_flags_t flags = va_arg (*args, gbp_endpoint_flags_t);
74
75   FOR_EACH_GBP_ENDPOINT_ATTR (attr)
76   {
77     if ((1 << attr) & flags)
78       {
79         s = format (s, "%s,", gbp_endpoint_attr_names[attr]);
80       }
81   }
82
83   return (s);
84 }
85
86 int
87 gbp_endpoint_is_remote (const gbp_endpoint_t * ge)
88 {
89   return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
90 }
91
92 int
93 gbp_endpoint_is_local (const gbp_endpoint_t * ge)
94 {
95   return (!(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
96 }
97
98 int
99 gbp_endpoint_is_external (const gbp_endpoint_t * ge)
100 {
101   return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_EXTERNAL));
102 }
103
104 int
105 gbp_endpoint_is_learnt (const gbp_endpoint_t * ge)
106 {
107   if (0 == vec_len (ge->ge_locs))
108     return 0;
109
110   /* DP is the highest source so if present it will be first */
111   return (ge->ge_locs[0].gel_src == GBP_ENDPOINT_SRC_DP);
112 }
113
114 static void
115 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
116                                   mac_address_t * mac, u32 * sw_if_index)
117 {
118   mac_address_from_u64 (mac, key->key[0]);
119   *sw_if_index = key->key[1];
120 }
121
122 static void
123 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
124                                  ip46_address_t * ip, u32 * sw_if_index)
125 {
126   ip->as_u64[0] = key->key[0];
127   ip->as_u64[1] = key->key[1];
128   *sw_if_index = key->key[2];
129 }
130
131 gbp_endpoint_t *
132 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
133 {
134   clib_bihash_kv_24_8_t key, value;
135   int rv;
136
137   gbp_endpoint_mk_key_ip (ip, fib_index, &key);
138
139   rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
140
141   if (0 != rv)
142     return NULL;
143
144   return (gbp_endpoint_get (value.value));
145 }
146
147 static void
148 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
149 {
150   vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
151
152   gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
153 }
154
155 static bool
156 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
157 {
158   clib_bihash_kv_16_8_t key;
159   int rv;
160
161   gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
162   key.value = gei;
163
164   rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
165
166
167   return (0 == rv);
168 }
169
170 static bool
171 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
172 {
173   clib_bihash_kv_24_8_t key;
174   int rv;
175
176   gbp_endpoint_mk_key_ip (ip, fib_index, &key);
177   key.value = gei;
178
179   rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
180
181   return (0 == rv);
182 }
183
184 static void
185 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
186 {
187   clib_bihash_kv_16_8_t key;
188
189   gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
190
191   clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
192 }
193
194 static void
195 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
196 {
197   clib_bihash_kv_24_8_t key;
198
199   gbp_endpoint_mk_key_ip (ip, fib_index, &key);
200
201   clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
202 }
203
204 static index_t
205 gbp_endpoint_index (const gbp_endpoint_t * ge)
206 {
207   return (ge - gbp_endpoint_pool);
208 }
209
210 static ip46_type_t
211 ip46_address_get_type (const ip46_address_t * a)
212 {
213   return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
214 }
215
216 static int
217 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
218 {
219   return (ip46_address_is_equal (ip, &fp->fp_addr));
220 }
221
222 static void
223 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
224                          const ip46_address_t * ips,
225                          const gbp_route_domain_t * grd)
226 {
227   const ip46_address_t *ip;
228   index_t gei, grdi;
229
230   gei = gbp_endpoint_index (ge);
231   grdi = gbp_route_domain_index (grd);
232
233   ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
234           (ge->ge_key.gek_grd == grdi));
235
236   vec_foreach (ip, ips)
237   {
238     if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
239                                         gbp_endpoint_ip_is_equal))
240       {
241         fib_prefix_t *pfx;
242
243         vec_add2 (ge->ge_key.gek_ips, pfx, 1);
244         fib_prefix_from_ip46_addr (ip, pfx);
245
246         gbp_endpoint_add_ip (&pfx->fp_addr,
247                              grd->grd_fib_index[pfx->fp_proto], gei);
248       }
249     ge->ge_key.gek_grd = grdi;
250   }
251 }
252
253 static gbp_endpoint_t *
254 gbp_endpoint_alloc (const ip46_address_t * ips,
255                     const gbp_route_domain_t * grd,
256                     const mac_address_t * mac,
257                     const gbp_bridge_domain_t * gbd)
258 {
259   gbp_endpoint_t *ge;
260   index_t gei;
261
262   pool_get_zero (gbp_endpoint_pool, ge);
263
264   fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
265   gei = gbp_endpoint_index (ge);
266   ge->ge_key.gek_gbd =
267     ge->ge_key.gek_grd = ge->ge_fwd.gef_fib_index = INDEX_INVALID;
268   gbp_itf_hdl_reset (&ge->ge_fwd.gef_itf);
269   ge->ge_last_time = vlib_time_now (vlib_get_main ());
270   ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
271
272   if (NULL != mac)
273     {
274       mac_address_copy (&ge->ge_key.gek_mac, mac);
275       gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
276     }
277   gbp_endpoint_ips_update (ge, ips, grd);
278
279   return (ge);
280 }
281
282 static int
283 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
284 {
285   return (a->gel_src == b->gel_src);
286 }
287
288 static int
289 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
290 {
291   return (a->gel_src - b->gel_src);
292 }
293
294 static gbp_endpoint_loc_t *
295 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
296 {
297   gbp_endpoint_loc_t gel = {
298     .gel_src = src,
299   };
300   u32 pos;
301
302   pos = vec_search_with_function (ge->ge_locs, &gel,
303                                   gbp_endpoint_loc_is_equal);
304
305   if (~0 != pos)
306     return (&ge->ge_locs[pos]);
307
308   return NULL;
309 }
310
311 static int
312 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
313 {
314   u32 pos;
315
316   gel->gel_locks--;
317
318   if (0 == gel->gel_locks)
319     {
320       pos = gel - ge->ge_locs;
321
322       vec_del1 (ge->ge_locs, pos);
323       if (vec_len (ge->ge_locs) > 1)
324         vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
325
326       /* This could be the last lock, so don't access the EP from
327        * this point on */
328       fib_node_unlock (&ge->ge_node);
329
330       return (1);
331     }
332   return (0);
333 }
334
335 static void
336 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
337 {
338   gbp_endpoint_group_unlock (gel->gel_epg);
339   gbp_itf_unlock (&gel->gel_itf);
340 }
341
342 static gbp_endpoint_loc_t *
343 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
344 {
345   gbp_endpoint_loc_t gel = {
346     .gel_src = src,
347     .gel_epg = INDEX_INVALID,
348     .gel_itf = GBP_ITF_HDL_INVALID,
349     .gel_locks = 0,
350   };
351   u32 pos;
352
353   pos = vec_search_with_function (ge->ge_locs, &gel,
354                                   gbp_endpoint_loc_is_equal);
355
356   if (~0 == pos)
357     {
358       vec_add1 (ge->ge_locs, gel);
359
360       if (vec_len (ge->ge_locs) > 1)
361         {
362           vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
363
364           pos = vec_search_with_function (ge->ge_locs, &gel,
365                                           gbp_endpoint_loc_is_equal);
366         }
367       else
368         pos = 0;
369
370       /*
371        * it's the sources and children that lock the endpoints
372        */
373       fib_node_lock (&ge->ge_node);
374     }
375
376   return (&ge->ge_locs[pos]);
377 }
378
379 /**
380  * Find an EP inthe DBs and check that if we find it in the L2 DB
381  * it has the same IPs as this update
382  */
383 static int
384 gbp_endpoint_find_for_update (const ip46_address_t * ips,
385                               const gbp_route_domain_t * grd,
386                               const mac_address_t * mac,
387                               const gbp_bridge_domain_t * gbd,
388                               gbp_endpoint_t ** ge)
389 {
390   gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
391
392   l2_ge = l3_ge = NULL;
393
394   if (NULL != mac && !mac_address_is_zero (mac))
395     {
396       ASSERT (gbd);
397       l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
398     }
399   if (NULL != ips && !ip46_address_is_zero (ips))
400     {
401       const ip46_address_t *ip;
402       fib_protocol_t fproto;
403
404       ASSERT (grd);
405       vec_foreach (ip, ips)
406       {
407         fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
408
409         tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
410
411         if (NULL == tmp)
412           /* not found */
413           continue;
414         else if (NULL == l3_ge)
415           /* first match against an IP address */
416           l3_ge = tmp;
417         else if (tmp == l3_ge)
418           /* another match against IP address that is the same endpoint */
419           continue;
420         else
421           {
422             /*
423              *  a match agains a different endpoint.
424              * this means the KEY of the EP is changing which is not allowed
425              */
426             return (-1);
427           }
428       }
429     }
430
431   if (NULL == l2_ge && NULL == l3_ge)
432     /* not found */
433     *ge = NULL;
434   else if (NULL == l2_ge)
435     /* found at L3 */
436     *ge = l3_ge;
437   else if (NULL == l3_ge)
438     /* found at L2 */
439     *ge = l2_ge;
440   else
441     {
442       /* found both L3 and L2 - they must be the same else the KEY
443        * is changing
444        */
445       if (l2_ge == l3_ge)
446         *ge = l2_ge;
447       else
448         return (-1);
449     }
450
451   return (0);
452 }
453
454 static gbp_endpoint_src_t
455 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
456 {
457   if (0 == vec_len (ge->ge_locs))
458     return (GBP_ENDPOINT_SRC_MAX);
459
460   return (ge->ge_locs[0].gel_src);
461 }
462
463 static void
464 gbp_endpoint_n_learned (int n)
465 {
466   gbp_n_learnt_endpoints += n;
467
468   if (n > 0 && 1 == gbp_n_learnt_endpoints)
469     {
470       vlib_process_signal_event (vlib_get_main (),
471                                  gbp_scanner_node.index,
472                                  GBP_ENDPOINT_SCAN_START, 0);
473     }
474   if (n < 0 && 0 == gbp_n_learnt_endpoints)
475     {
476       vlib_process_signal_event (vlib_get_main (),
477                                  gbp_scanner_node.index,
478                                  GBP_ENDPOINT_SCAN_STOP, 0);
479     }
480 }
481
482 static void
483 gbp_endpoint_loc_update (const gbp_endpoint_t * ge,
484                          gbp_endpoint_loc_t * gel,
485                          const gbp_bridge_domain_t * gb,
486                          u32 sw_if_index,
487                          index_t ggi,
488                          gbp_endpoint_flags_t flags,
489                          const ip46_address_t * tun_src,
490                          const ip46_address_t * tun_dst)
491 {
492   int was_learnt, is_learnt;
493
494   gel->gel_locks++;
495   was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
496   gel->gel_flags = flags;
497   is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
498
499   gbp_endpoint_n_learned (is_learnt - was_learnt);
500
501   if (INDEX_INVALID == gel->gel_epg)
502     {
503       gel->gel_epg = ggi;
504       if (INDEX_INVALID != gel->gel_epg)
505         {
506           gbp_endpoint_group_lock (gel->gel_epg);
507         }
508     }
509   else
510     {
511       ASSERT (gel->gel_epg == ggi);
512     }
513
514   if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
515     {
516       if (NULL != tun_src)
517         ip46_address_copy (&gel->tun.gel_src, tun_src);
518       if (NULL != tun_dst)
519         ip46_address_copy (&gel->tun.gel_dst, tun_dst);
520
521       if (ip46_address_is_multicast (&gel->tun.gel_src))
522         {
523           /*
524            * we learnt the EP from the multicast tunnel.
525            * Create a unicast TEP from the packet's source
526            * and the fixed address of the BD's parent tunnel
527            */
528           const gbp_vxlan_tunnel_t *gt;
529
530           gt = gbp_vxlan_tunnel_get (gb->gb_vni);
531
532           if (NULL != gt)
533             {
534               ip46_address_copy (&gel->tun.gel_src, &gt->gt_src);
535               sw_if_index = gt->gt_sw_if_index;
536             }
537         }
538
539       /*
540        * the input interface may be the parent GBP-vxlan interface,
541        * create a child vlxan-gbp tunnel and use that as the endpoint's
542        * interface.
543        */
544       gbp_itf_hdl_t old = gel->gel_itf;
545
546       switch (gbp_vxlan_tunnel_get_type (sw_if_index))
547         {
548         case GBP_VXLAN_TEMPLATE_TUNNEL:
549           gel->tun.gel_parent_sw_if_index = sw_if_index;
550           gel->gel_itf = gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
551                                                           &gel->tun.gel_src,
552                                                           &gel->tun.gel_dst);
553           break;
554         case VXLAN_GBP_TUNNEL:
555           gel->tun.gel_parent_sw_if_index =
556             vxlan_gbp_tunnel_get_parent (sw_if_index);
557           gel->gel_itf = vxlan_gbp_tunnel_lock_itf (sw_if_index);
558           break;
559         }
560
561       gbp_itf_unlock (&old);
562     }
563   else
564     {
565       gel->gel_itf = gbp_itf_l2_add_and_lock (sw_if_index,
566                                               ge->ge_key.gek_gbd);
567     }
568 }
569
570 static void
571 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
572 {
573   const gbp_route_domain_t *grd;
574   const gbp_bridge_domain_t *gbd;
575   gbp_endpoint_fwd_t *gef;
576   const fib_prefix_t *pfx;
577   index_t *ai;
578
579   gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
580   gef = &ge->ge_fwd;
581
582   vec_foreach (pfx, ge->ge_key.gek_ips)
583   {
584     u32 fib_index;
585
586     grd = gbp_route_domain_get (ge->ge_key.gek_grd);
587     fib_index = grd->grd_fib_index[pfx->fp_proto];
588
589     bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
590                        &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
591
592     /*
593      * remove a host route
594      */
595     if (gbp_endpoint_is_remote (ge))
596       {
597         fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
598       }
599
600     fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
601   }
602   vec_foreach (ai, gef->gef_adjs)
603   {
604     adj_unlock (*ai);
605   }
606
607   if (gbp_itf_hdl_is_valid (gef->gef_itf))
608     {
609       l2fib_del_entry (ge->ge_key.gek_mac.bytes,
610                        gbd->gb_bd_index,
611                        gbp_itf_get_sw_if_index (gef->gef_itf));
612     }
613
614   gbp_itf_unlock (&gef->gef_itf);
615   vec_free (gef->gef_adjs);
616 }
617
618 static void
619 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
620 {
621   const gbp_bridge_domain_t *gbd;
622   const gbp_endpoint_group_t *gg;
623   const gbp_route_domain_t *grd;
624   gbp_endpoint_loc_t *gel;
625   gbp_endpoint_fwd_t *gef;
626   const fib_prefix_t *pfx;
627   index_t gei;
628
629   /*
630    * locations are sort in source priority order
631    */
632   gei = gbp_endpoint_index (ge);
633   gel = &ge->ge_locs[0];
634   gef = &ge->ge_fwd;
635   gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
636
637   gef->gef_flags = gel->gel_flags;
638
639   if (INDEX_INVALID != gel->gel_epg)
640     {
641       gg = gbp_endpoint_group_get (gel->gel_epg);
642       gef->gef_sclass = gg->gg_sclass;
643     }
644   else
645     {
646       gg = NULL;
647     }
648
649   gef->gef_itf = gbp_itf_clone_and_lock (gel->gel_itf);
650
651   if (!mac_address_is_zero (&ge->ge_key.gek_mac))
652     {
653       gbp_itf_l2_set_input_feature (gef->gef_itf, L2INPUT_FEAT_GBP_FWD);
654
655       if (gbp_endpoint_is_remote (ge) || gbp_endpoint_is_external (ge))
656         {
657           /*
658            * bridged packets to external endpoints should be classifed
659            * based on the EP's/BD's EPG
660            */
661           gbp_itf_l2_set_output_feature (gef->gef_itf,
662                                          L2OUTPUT_FEAT_GBP_POLICY_MAC);
663         }
664       else
665         {
666           gbp_endpoint_add_itf (gbp_itf_get_sw_if_index (gef->gef_itf), gei);
667           gbp_itf_l2_set_output_feature (gef->gef_itf,
668                                          L2OUTPUT_FEAT_GBP_POLICY_PORT);
669         }
670       l2fib_add_entry (ge->ge_key.gek_mac.bytes,
671                        gbd->gb_bd_index,
672                        gbp_itf_get_sw_if_index (gef->gef_itf),
673                        L2FIB_ENTRY_RESULT_FLAG_STATIC);
674     }
675
676   vec_foreach (pfx, ge->ge_key.gek_ips)
677   {
678     ethernet_header_t *eth;
679     u32 ip_sw_if_index;
680     u32 fib_index;
681     u8 *rewrite;
682     index_t ai;
683
684     rewrite = NULL;
685     grd = gbp_route_domain_get (ge->ge_key.gek_grd);
686     fib_index = grd->grd_fib_index[pfx->fp_proto];
687     gef->gef_fib_index = fib_index;
688
689     bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
690                        &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
691
692     /*
693      * add a host route via the EPG's BVI we need this because the
694      * adj fib does not install, due to cover refinement check, since
695      * the BVI's prefix is /32
696      */
697     vec_validate (rewrite, sizeof (*eth) - 1);
698     eth = (ethernet_header_t *) rewrite;
699
700     eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
701                                        ETHERNET_TYPE_IP4 :
702                                        ETHERNET_TYPE_IP6));
703
704     if (gbp_endpoint_is_remote (ge))
705       {
706         /*
707          * for dynamic EPs we must add the IP adjacency via the learned
708          * tunnel since the BD will not contain the EP's MAC since it was
709          * L3 learned. The dst MAC address used is the 'BD's MAC'.
710          */
711         ip_sw_if_index = gbp_itf_get_sw_if_index (gef->gef_itf);
712
713         mac_address_to_bytes (gbp_route_domain_get_local_mac (),
714                               eth->src_address);
715         mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
716                               eth->dst_address);
717       }
718     else
719       {
720         /*
721          * for the static EPs we add the IP adjacency via the BVI
722          * knowing that the BD has the MAC address to route to and
723          * that policy will be applied on egress to the EP's port
724          */
725         ip_sw_if_index = gbd->gb_bvi_sw_if_index;
726
727         clib_memcpy (eth->src_address,
728                      vnet_sw_interface_get_hw_address (vnet_get_main (),
729                                                        ip_sw_if_index),
730                      sizeof (eth->src_address));
731         mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
732       }
733
734     fib_table_entry_path_add (fib_index, pfx,
735                               FIB_SOURCE_PLUGIN_LOW,
736                               FIB_ENTRY_FLAG_NONE,
737                               fib_proto_to_dpo (pfx->fp_proto),
738                               &pfx->fp_addr, ip_sw_if_index,
739                               ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
740
741     ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
742                                         fib_proto_to_link (pfx->fp_proto),
743                                         &pfx->fp_addr,
744                                         ip_sw_if_index, rewrite);
745     vec_add1 (gef->gef_adjs, ai);
746
747     /*
748      * if the endpoint is external then routed packet to it must be
749      * classifed to the BD's EPG. but this will happen anyway with
750      * the GBP_MAC classification.
751      */
752
753     if (NULL != gg)
754       {
755         if (gbp_endpoint_is_remote (ge))
756           {
757             dpo_id_t policy_dpo = DPO_INVALID;
758
759             /*
760              * interpose a policy DPO from the endpoint so that policy
761              * is applied
762              */
763             gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
764                                         grd->grd_scope,
765                                         gg->gg_sclass, ~0, &policy_dpo);
766
767             fib_table_entry_special_dpo_add (fib_index, pfx,
768                                              FIB_SOURCE_PLUGIN_HI,
769                                              FIB_ENTRY_FLAG_INTERPOSE,
770                                              &policy_dpo);
771             dpo_reset (&policy_dpo);
772           }
773
774         /*
775          * send a gratuitous ARP on the EPG's uplink. this is done so
776          * that if this EP has moved from some other place in the
777          * 'fabric', upstream devices are informed
778          */
779         if (gbp_endpoint_is_local (ge) && ~0 != gg->gg_uplink_sw_if_index)
780           {
781             gbp_endpoint_add_itf (gbp_itf_get_sw_if_index (gef->gef_itf),
782                                   gei);
783             if (FIB_PROTOCOL_IP4 == pfx->fp_proto)
784               send_ip4_garp_w_addr (vlib_get_main (),
785                                     &pfx->fp_addr.ip4,
786                                     gg->gg_uplink_sw_if_index);
787             else
788               send_ip6_na_w_addr (vlib_get_main (),
789                                   &pfx->fp_addr.ip6,
790                                   gg->gg_uplink_sw_if_index);
791           }
792       }
793   }
794
795   if (gbp_endpoint_is_external (ge))
796     {
797       gbp_itf_l2_set_input_feature (gef->gef_itf,
798                                     L2INPUT_FEAT_GBP_LPM_CLASSIFY);
799     }
800   else if (gbp_endpoint_is_local (ge))
801     {
802       /*
803        * non-remote endpoints (i.e. those not arriving on iVXLAN
804        * tunnels) need to be classifed based on the the input interface.
805        * We enable the GBP-FWD feature only if the group has an uplink
806        * interface (on which the GBP-FWD feature would send UU traffic).
807        * External endpoints get classified based on an LPM match
808        */
809       l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
810
811       if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
812         feats |= L2INPUT_FEAT_GBP_FWD;
813       gbp_itf_l2_set_input_feature (gef->gef_itf, feats);
814     }
815
816   /*
817    * update children with the new forwarding info
818    */
819   fib_node_back_walk_ctx_t bw_ctx = {
820     .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
821     .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
822   };
823
824   fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
825 }
826
827 int
828 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
829                               u32 sw_if_index,
830                               const ip46_address_t * ips,
831                               const mac_address_t * mac,
832                               index_t gbdi, index_t grdi,
833                               sclass_t sclass,
834                               gbp_endpoint_flags_t flags,
835                               const ip46_address_t * tun_src,
836                               const ip46_address_t * tun_dst, u32 * handle)
837 {
838   gbp_bridge_domain_t *gbd;
839   gbp_endpoint_group_t *gg;
840   gbp_endpoint_src_t best;
841   gbp_route_domain_t *grd;
842   gbp_endpoint_loc_t *gel;
843   gbp_endpoint_t *ge;
844   index_t ggi, gei;
845   int rv;
846
847   if (~0 == sw_if_index)
848     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
849
850   ge = NULL;
851   gg = NULL;
852
853   /*
854    * we need to determine the bridge-domain, either from the EPG or
855    * the BD passed
856    */
857   if (SCLASS_INVALID != sclass)
858     {
859       ggi = gbp_endpoint_group_find (sclass);
860
861       if (INDEX_INVALID == ggi)
862         return (VNET_API_ERROR_NO_SUCH_ENTRY);
863
864       gg = gbp_endpoint_group_get (ggi);
865       gbdi = gg->gg_gbd;
866       grdi = gg->gg_rd;
867     }
868   else
869     {
870       if (INDEX_INVALID == gbdi)
871         return (VNET_API_ERROR_NO_SUCH_ENTRY);
872       if (INDEX_INVALID == grdi)
873         return (VNET_API_ERROR_NO_SUCH_FIB);
874       ggi = INDEX_INVALID;
875     }
876
877   gbd = gbp_bridge_domain_get (gbdi);
878   grd = gbp_route_domain_get (grdi);
879   rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
880
881   if (0 != rv)
882     return (rv);
883
884   if (NULL == ge)
885     {
886       ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
887     }
888   else
889     {
890       gbp_endpoint_ips_update (ge, ips, grd);
891     }
892
893   best = gbp_endpoint_get_best_src (ge);
894   gei = gbp_endpoint_index (ge);
895   gel = gbp_endpoint_loc_find_or_add (ge, src);
896
897   gbp_endpoint_loc_update (ge, gel, gbd, sw_if_index, ggi, flags,
898                            tun_src, tun_dst);
899
900   if (src <= best)
901     {
902       /*
903        * either the best source has been updated or we have a new best source
904        */
905       gbb_endpoint_fwd_reset (ge);
906       gbb_endpoint_fwd_recalc (ge);
907     }
908   else
909     {
910       /*
911        * an update to a lower priority source, so we need do nothing
912        */
913     }
914
915   if (handle)
916     *handle = gei;
917
918   GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
919
920   return (0);
921 }
922
923 void
924 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
925 {
926   gbp_endpoint_loc_t *gel, gel_copy;
927   gbp_endpoint_src_t best;
928   gbp_endpoint_t *ge;
929   int removed;
930
931   if (pool_is_free_index (gbp_endpoint_pool, gei))
932     return;
933
934   GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
935
936   ge = gbp_endpoint_get (gei);
937
938   gel = gbp_endpoint_loc_find (ge, src);
939
940   if (NULL == gel)
941     return;
942
943   /*
944    * lock the EP so we can control when it is deleted
945    */
946   fib_node_lock (&ge->ge_node);
947   best = gbp_endpoint_get_best_src (ge);
948
949   /*
950    * copy the location info since we'll lose it when it's removed from
951    * the vector
952    */
953   clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
954
955   /*
956    * remove the source we no longer need
957    */
958   removed = gbp_endpoint_loc_unlock (ge, gel);
959
960   if (src == best)
961     {
962       /*
963        * we have removed the old best source => recalculate fwding
964        */
965       if (0 == vec_len (ge->ge_locs))
966         {
967           /*
968            * if there are no more sources left, then we need only release
969            * the fwding resources held and then this EP is gawn.
970            */
971           gbb_endpoint_fwd_reset (ge);
972         }
973       else
974         {
975           /*
976            * else there are more sources. release the old and get new
977            * fwding objects
978            */
979           gbb_endpoint_fwd_reset (ge);
980           gbb_endpoint_fwd_recalc (ge);
981         }
982     }
983   /*
984    * else
985    *  we removed a lower priority source so we need to do nothing
986    */
987
988   /*
989    * clear up any resources held by the source
990    */
991   if (removed)
992     gbp_endpoint_loc_destroy (&gel_copy);
993
994   /*
995    * remove the lock taken above
996    */
997   fib_node_unlock (&ge->ge_node);
998   /*
999    *  We may have removed the last source and so this EP is now TOAST
1000    *  DO NOTHING BELOW HERE
1001    */
1002 }
1003
1004 u32
1005 gbp_endpoint_child_add (index_t gei,
1006                         fib_node_type_t type, fib_node_index_t index)
1007 {
1008   return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
1009 }
1010
1011 void
1012 gbp_endpoint_child_remove (index_t gei, u32 sibling)
1013 {
1014   return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
1015 }
1016
1017 typedef struct gbp_endpoint_flush_ctx_t_
1018 {
1019   u32 sw_if_index;
1020   gbp_endpoint_src_t src;
1021   index_t *geis;
1022 } gbp_endpoint_flush_ctx_t;
1023
1024 static walk_rc_t
1025 gbp_endpoint_flush_cb (index_t gei, void *args)
1026 {
1027   gbp_endpoint_flush_ctx_t *ctx = args;
1028   gbp_endpoint_loc_t *gel;
1029   gbp_endpoint_t *ge;
1030
1031   ge = gbp_endpoint_get (gei);
1032   gel = gbp_endpoint_loc_find (ge, ctx->src);
1033
1034   if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
1035     {
1036       vec_add1 (ctx->geis, gei);
1037     }
1038
1039   return (WALK_CONTINUE);
1040 }
1041
1042 /**
1043  * remove all learnt endpoints using the interface
1044  */
1045 void
1046 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
1047 {
1048   gbp_endpoint_flush_ctx_t ctx = {
1049     .sw_if_index = sw_if_index,
1050     .src = src,
1051   };
1052   index_t *gei;
1053
1054   GBP_ENDPOINT_INFO ("flush: %U %U",
1055                      format_gbp_endpoint_src, src,
1056                      format_vnet_sw_if_index_name, vnet_get_main (),
1057                      sw_if_index);
1058   gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1059
1060   vec_foreach (gei, ctx.geis)
1061   {
1062     gbp_endpoint_unlock (src, *gei);
1063   }
1064
1065   vec_free (ctx.geis);
1066 }
1067
1068 void
1069 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1070 {
1071   u32 index;
1072
1073   /* *INDENT-OFF* */
1074   pool_foreach_index(index, gbp_endpoint_pool,
1075   {
1076     if (!cb(index, ctx))
1077       break;
1078   });
1079   /* *INDENT-ON* */
1080 }
1081
1082 static clib_error_t *
1083 gbp_endpoint_cli (vlib_main_t * vm,
1084                   unformat_input_t * input, vlib_cli_command_t * cmd)
1085 {
1086   ip46_address_t ip = ip46_address_initializer, *ips = NULL;
1087   mac_address_t mac = ZERO_MAC_ADDRESS;
1088   vnet_main_t *vnm = vnet_get_main ();
1089   u32 sclass = SCLASS_INVALID;
1090   u32 handle = INDEX_INVALID;
1091   u32 sw_if_index = ~0;
1092   u8 add = 1;
1093   int rv;
1094
1095   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1096     {
1097       ip46_address_reset (&ip);
1098
1099       if (unformat (input, "%U", unformat_vnet_sw_interface,
1100                     vnm, &sw_if_index))
1101         ;
1102       else if (unformat (input, "add"))
1103         add = 1;
1104       else if (unformat (input, "del"))
1105         add = 0;
1106       else if (unformat (input, "sclass %d", &sclass))
1107         ;
1108       else if (unformat (input, "handle %d", &handle))
1109         ;
1110       else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1111         vec_add1 (ips, ip);
1112       else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1113         vec_add1 (ips, ip);
1114       else if (unformat (input, "mac %U", unformat_mac_address, &mac))
1115         ;
1116       else
1117         break;
1118     }
1119
1120   if (add)
1121     {
1122       if (~0 == sw_if_index)
1123         return clib_error_return (0, "interface must be specified");
1124       if (SCLASS_INVALID == sclass)
1125         return clib_error_return (0, "SCLASS must be specified");
1126
1127       rv =
1128         gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1129                                       sw_if_index, ips, &mac,
1130                                       INDEX_INVALID, INDEX_INVALID,
1131                                       sclass,
1132                                       GBP_ENDPOINT_FLAG_NONE,
1133                                       NULL, NULL, &handle);
1134
1135       if (rv)
1136         return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1137       else
1138         vlib_cli_output (vm, "handle %d\n", handle);
1139     }
1140   else
1141     {
1142       if (INDEX_INVALID == handle)
1143         return clib_error_return (0, "handle must be specified");
1144
1145       gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1146     }
1147
1148   vec_free (ips);
1149
1150   return (NULL);
1151 }
1152
1153 /*?
1154  * Configure a GBP Endpoint
1155  *
1156  * @cliexpar
1157  * @cliexstart{set gbp endpoint [del] <interface> epg <ID> ip <IP>}
1158  * @cliexend
1159  ?*/
1160 /* *INDENT-OFF* */
1161 VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
1162   .path = "gbp endpoint",
1163   .short_help = "gbp endpoint [del] <interface> epg <ID> ip <IP> mac <MAC>",
1164   .function = gbp_endpoint_cli,
1165 };
1166 /* *INDENT-ON* */
1167
1168 u8 *
1169 format_gbp_endpoint_src (u8 * s, va_list * args)
1170 {
1171   gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1172
1173   switch (action)
1174     {
1175 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1176       foreach_gbp_endpoint_src
1177 #undef _
1178     }
1179
1180   return (format (s, "unknown"));
1181 }
1182
1183 static u8 *
1184 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1185 {
1186   gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
1187
1188   s = format (s, "fwd:");
1189   s = format (s, "\n   itf:[%U]", format_gbp_itf_hdl, gef->gef_itf);
1190   if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
1191     {
1192       s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1193     }
1194
1195   return (s);
1196 }
1197
1198 static u8 *
1199 format_gbp_endpoint_key (u8 * s, va_list * args)
1200 {
1201   gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1202   const fib_prefix_t *pfx;
1203
1204   s = format (s, "ips:[");
1205
1206   vec_foreach (pfx, gek->gek_ips)
1207   {
1208     s = format (s, "%U, ", format_fib_prefix, pfx);
1209   }
1210   s = format (s, "]");
1211
1212   s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1213
1214   return (s);
1215 }
1216
1217 static u8 *
1218 format_gbp_endpoint_loc (u8 * s, va_list * args)
1219 {
1220   gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1221
1222   s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
1223   s = format (s, "\n    EPG:%d [%U]", gel->gel_epg,
1224               format_gbp_itf_hdl, gel->gel_itf);
1225
1226   if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1227     {
1228       s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1229     }
1230   if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
1231     {
1232       s = format (s, " tun:[");
1233       s = format (s, "parent:%U", format_vnet_sw_if_index_name,
1234                   vnet_get_main (), gel->tun.gel_parent_sw_if_index);
1235       s = format (s, " {%U,%U}]",
1236                   format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
1237                   format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
1238     }
1239
1240   return (s);
1241 }
1242
1243 u8 *
1244 format_gbp_endpoint (u8 * s, va_list * args)
1245 {
1246   index_t gei = va_arg (*args, index_t);
1247   gbp_endpoint_loc_t *gel;
1248   gbp_endpoint_t *ge;
1249
1250   ge = gbp_endpoint_get (gei);
1251
1252   s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
1253   s = format (s, " last-time:[%f]", ge->ge_last_time);
1254
1255   vec_foreach (gel, ge->ge_locs)
1256   {
1257     s = format (s, "\n  %U", format_gbp_endpoint_loc, gel);
1258   }
1259   s = format (s, "\n  %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1260
1261   return s;
1262 }
1263
1264 static walk_rc_t
1265 gbp_endpoint_show_one (index_t gei, void *ctx)
1266 {
1267   vlib_main_t *vm;
1268
1269   vm = ctx;
1270   vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1271
1272   return (WALK_CONTINUE);
1273 }
1274
1275 static void
1276 gbp_endpoint_walk_ip_itf (const clib_bihash_kv_24_8_t * kvp, void *arg)
1277 {
1278   ip46_address_t ip;
1279   vlib_main_t *vm;
1280   u32 sw_if_index;
1281
1282   vm = arg;
1283
1284   gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
1285
1286   vlib_cli_output (vm, " {%U, %U} -> %d",
1287                    format_ip46_address, &ip, IP46_TYPE_ANY,
1288                    format_vnet_sw_if_index_name, vnet_get_main (),
1289                    sw_if_index, kvp->value);
1290 }
1291
1292 static void
1293 gbp_endpoint_walk_mac_itf (const clib_bihash_kv_16_8_t * kvp, void *arg)
1294 {
1295   mac_address_t mac;
1296   vlib_main_t *vm;
1297   u32 sw_if_index;
1298
1299   vm = arg;
1300
1301   gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
1302
1303   vlib_cli_output (vm, " {%U, %U} -> %d",
1304                    format_mac_address_t, &mac,
1305                    format_vnet_sw_if_index_name, vnet_get_main (),
1306                    sw_if_index, kvp->value);
1307 }
1308
1309 static clib_error_t *
1310 gbp_endpoint_show (vlib_main_t * vm,
1311                    unformat_input_t * input, vlib_cli_command_t * cmd)
1312 {
1313   u32 show_dbs, handle;
1314
1315   handle = INDEX_INVALID;
1316   show_dbs = 0;
1317
1318   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1319     {
1320       if (unformat (input, "%d", &handle))
1321         ;
1322       else if (unformat (input, "db"))
1323         show_dbs = 1;
1324       else
1325         break;
1326     }
1327
1328   if (INDEX_INVALID != handle)
1329     {
1330       vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
1331     }
1332   else if (show_dbs)
1333     {
1334       vlib_cli_output (vm, "\nDatabases:");
1335       clib_bihash_foreach_key_value_pair_24_8 (&gbp_ep_db.ged_by_ip_rd,
1336                                                gbp_endpoint_walk_ip_itf, vm);
1337       clib_bihash_foreach_key_value_pair_16_8
1338         (&gbp_ep_db.ged_by_mac_bd, gbp_endpoint_walk_mac_itf, vm);
1339     }
1340   else
1341     {
1342       vlib_cli_output (vm, "Endpoints:");
1343       gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1344     }
1345
1346   return (NULL);
1347 }
1348
1349 /*?
1350  * Show Group Based Policy Endpoints and derived information
1351  *
1352  * @cliexpar
1353  * @cliexstart{show gbp endpoint}
1354  * @cliexend
1355  ?*/
1356 /* *INDENT-OFF* */
1357 VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
1358   .path = "show gbp endpoint",
1359   .short_help = "show gbp endpoint\n",
1360   .function = gbp_endpoint_show,
1361 };
1362 /* *INDENT-ON* */
1363
1364 static void
1365 gbp_endpoint_check (index_t gei, f64 start_time)
1366 {
1367   gbp_endpoint_group_t *gg;
1368   gbp_endpoint_loc_t *gel;
1369   gbp_endpoint_t *ge;
1370
1371   ge = gbp_endpoint_get (gei);
1372   gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1373
1374   if (NULL != gel)
1375     {
1376       gg = gbp_endpoint_group_get (gel->gel_epg);
1377
1378       if ((start_time - ge->ge_last_time) >
1379           gg->gg_retention.remote_ep_timeout)
1380         {
1381           gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1382         }
1383     }
1384 }
1385
1386 static void
1387 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1388 {
1389   clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1390   f64 last_start, start_time, delta_t;
1391   int i, j, k;
1392
1393   delta_t = 0;
1394   last_start = start_time = vlib_time_now (vm);
1395
1396   for (i = 0; i < gte_table->nbuckets; i++)
1397     {
1398       clib_bihash_bucket_16_8_t *b;
1399       clib_bihash_value_16_8_t *v;
1400
1401       /* allow no more than 20us without a pause */
1402       delta_t = vlib_time_now (vm) - last_start;
1403       if (delta_t > 20e-6)
1404         {
1405           /* suspend for 100 us */
1406           vlib_process_suspend (vm, 100e-6);
1407           last_start = vlib_time_now (vm);
1408         }
1409
1410       b = &gte_table->buckets[i];
1411       if (b->offset == 0)
1412         continue;
1413       v = clib_bihash_get_value_16_8 (gte_table, b->offset);
1414
1415       for (j = 0; j < (1 << b->log2_pages); j++)
1416         {
1417           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1418             {
1419               if (clib_bihash_is_free_16_8 (&v->kvp[k]))
1420                 continue;
1421
1422               gbp_endpoint_check (v->kvp[k].value, start_time);
1423
1424               /*
1425                * Note: we may have just freed the bucket's backing
1426                * storage, so check right here...
1427                */
1428               if (b->offset == 0)
1429                 goto doublebreak;
1430             }
1431           v++;
1432         }
1433     doublebreak:
1434       ;
1435     }
1436 }
1437
1438 static void
1439 gbp_endpoint_scan_l3 (vlib_main_t * vm)
1440 {
1441   clib_bihash_24_8_t *gte_table = &gbp_ep_db.ged_by_ip_rd;
1442   f64 last_start, start_time, delta_t;
1443   int i, j, k;
1444
1445   delta_t = 0;
1446   last_start = start_time = vlib_time_now (vm);
1447
1448   for (i = 0; i < gte_table->nbuckets; i++)
1449     {
1450       clib_bihash_bucket_24_8_t *b;
1451       clib_bihash_value_24_8_t *v;
1452
1453       /* allow no more than 20us without a pause */
1454       delta_t = vlib_time_now (vm) - last_start;
1455       if (delta_t > 20e-6)
1456         {
1457           /* suspend for 100 us */
1458           vlib_process_suspend (vm, 100e-6);
1459           last_start = vlib_time_now (vm);
1460         }
1461
1462       b = &gte_table->buckets[i];
1463       if (b->offset == 0)
1464         continue;
1465       v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1466
1467       for (j = 0; j < (1 << b->log2_pages); j++)
1468         {
1469           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1470             {
1471               if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1472                 continue;
1473
1474               gbp_endpoint_check (v->kvp[k].value, start_time);
1475
1476               /*
1477                * Note: we may have just freed the bucket's backing
1478                * storage, so check right here...
1479                */
1480               if (b->offset == 0)
1481                 goto doublebreak;
1482             }
1483           v++;
1484         }
1485     doublebreak:
1486       ;
1487     }
1488 }
1489
1490 void
1491 gbp_endpoint_scan (vlib_main_t * vm)
1492 {
1493   gbp_endpoint_scan_l2 (vm);
1494   gbp_endpoint_scan_l3 (vm);
1495 }
1496
1497 static fib_node_t *
1498 gbp_endpoint_get_node (fib_node_index_t index)
1499 {
1500   gbp_endpoint_t *ge;
1501
1502   ge = gbp_endpoint_get (index);
1503
1504   return (&ge->ge_node);
1505 }
1506
1507 static gbp_endpoint_t *
1508 gbp_endpoint_from_fib_node (fib_node_t * node)
1509 {
1510   ASSERT (gbp_endpoint_fib_type == node->fn_type);
1511   return ((gbp_endpoint_t *) node);
1512 }
1513
1514 static void
1515 gbp_endpoint_last_lock_gone (fib_node_t * node)
1516 {
1517   const gbp_bridge_domain_t *gbd;
1518   const gbp_route_domain_t *grd;
1519   const fib_prefix_t *pfx;
1520   gbp_endpoint_t *ge;
1521
1522   ge = gbp_endpoint_from_fib_node (node);
1523
1524   ASSERT (0 == vec_len (ge->ge_locs));
1525
1526   gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1527
1528   /*
1529    * we have removed the last source. this EP is toast
1530    */
1531   if (INDEX_INVALID != ge->ge_key.gek_gbd)
1532     {
1533       gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1534     }
1535   vec_foreach (pfx, ge->ge_key.gek_ips)
1536   {
1537     grd = gbp_route_domain_get (ge->ge_key.gek_grd);
1538     gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
1539   }
1540   pool_put (gbp_endpoint_pool, ge);
1541 }
1542
1543 static fib_node_back_walk_rc_t
1544 gbp_endpoint_back_walk_notify (fib_node_t * node,
1545                                fib_node_back_walk_ctx_t * ctx)
1546 {
1547   ASSERT (0);
1548
1549   return (FIB_NODE_BACK_WALK_CONTINUE);
1550 }
1551
1552 /*
1553  * The FIB path's graph node virtual function table
1554  */
1555 static const fib_node_vft_t gbp_endpoint_vft = {
1556   .fnv_get = gbp_endpoint_get_node,
1557   .fnv_last_lock = gbp_endpoint_last_lock_gone,
1558   .fnv_back_walk = gbp_endpoint_back_walk_notify,
1559   // .fnv_mem_show = fib_path_memory_show,
1560 };
1561
1562 static clib_error_t *
1563 gbp_endpoint_init (vlib_main_t * vm)
1564 {
1565 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1566 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
1567
1568   clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
1569                          "GBP Endpoints - IP/RD",
1570                          GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1571
1572   clib_bihash_init_16_8 (&gbp_ep_db.ged_by_mac_bd,
1573                          "GBP Endpoints - MAC/BD",
1574                          GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1575
1576   gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1577   gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1578
1579   return (NULL);
1580 }
1581
1582 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1583
1584 /*
1585  * fd.io coding-style-patch-verification: ON
1586  *
1587  * Local Variables:
1588  * eval: (c-set-style "gnu")
1589  * End:
1590  */