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