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