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