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