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