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