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