2 * gbp.h : Group Based Policy
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
36 static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
41 gbp_ep_db_t gbp_ep_db;
43 fib_node_type_t gbp_endpoint_fib_type;
45 vlib_log_class_t gbp_ep_logger;
47 #define GBP_ENDPOINT_DBG(...) \
48 vlib_log_debug (gbp_ep_logger, __VA_ARGS__);
50 #define GBP_ENDPOINT_INFO(...) \
51 vlib_log_notice (gbp_ep_logger, __VA_ARGS__);
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
58 static u32 GBP_ENDPOINT_INACTIVE_TIME = 30;
61 * Pool of GBP endpoints
63 gbp_endpoint_t *gbp_endpoint_pool;
66 * A count of the number of dynamic entries
68 static u32 gbp_n_learnt_endpoints;
70 #define FOR_EACH_GBP_ENDPOINT_ATTR(_item) \
71 for (_item = GBP_ENDPOINT_ATTR_FIRST; \
72 _item < GBP_ENDPOINT_ATTR_LAST; \
76 format_gbp_endpoint_flags (u8 * s, va_list * args)
78 gbp_endpoint_attr_t attr;
79 gbp_endpoint_flags_t flags = va_arg (*args, gbp_endpoint_flags_t);
81 FOR_EACH_GBP_ENDPOINT_ATTR (attr)
83 if ((1 << attr) & flags)
85 s = format (s, "%s,", gbp_endpoint_attr_names[attr]);
93 gbp_endpoint_is_remote (const gbp_endpoint_t * ge)
95 return (ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE);
99 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
100 mac_address_t * mac, u32 * sw_if_index)
102 mac_address_from_u64 (key->key[0], mac);
103 *sw_if_index = key->key[1];
107 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
108 ip46_address_t * ip, u32 * sw_if_index)
110 ip->as_u64[0] = key->key[0];
111 ip->as_u64[1] = key->key[1];
112 *sw_if_index = key->key[2];
116 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
118 clib_bihash_kv_24_8_t key, value;
121 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
123 rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
128 return (gbp_endpoint_get (value.value));
132 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
134 vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
136 gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
140 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
142 clib_bihash_kv_16_8_t key;
145 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
148 rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
155 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
157 clib_bihash_kv_24_8_t key;
160 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
163 rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
169 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
171 clib_bihash_kv_16_8_t key;
173 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
175 clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
179 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
181 clib_bihash_kv_24_8_t key;
183 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
185 clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
189 gbp_endpoint_index (const gbp_endpoint_t * ge)
191 return (ge - gbp_endpoint_pool);
195 ip46_address_get_type (const ip46_address_t * a)
197 return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
201 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
203 return (ip46_address_is_equal (ip, &fp->fp_addr));
207 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
208 const ip46_address_t * ips,
209 const gbp_route_domain_t * grd)
211 const ip46_address_t *ip;
214 gei = gbp_endpoint_index (ge);
215 grdi = gbp_route_domain_index (grd);
217 ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
218 (ge->ge_key.gek_grd == grdi));
220 vec_foreach (ip, ips)
222 if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
223 gbp_endpoint_ip_is_equal))
227 vec_add2 (ge->ge_key.gek_ips, pfx, 1);
228 fib_prefix_from_ip46_addr (ip, pfx);
230 gbp_endpoint_add_ip (&pfx->fp_addr,
231 grd->grd_fib_index[pfx->fp_proto], gei);
233 ge->ge_key.gek_grd = grdi;
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)
246 pool_get_zero (gbp_endpoint_pool, ge);
248 fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
249 gei = gbp_endpoint_index (ge);
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);
257 mac_address_copy (&ge->ge_key.gek_mac, mac);
258 gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
260 gbp_endpoint_ips_update (ge, ips, grd);
266 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
268 return (a->gel_src == b->gel_src);
272 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
274 return (a->gel_src - b->gel_src);
277 static gbp_endpoint_loc_t *
278 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
280 gbp_endpoint_loc_t gel = {
285 pos = vec_search_with_function (ge->ge_locs, &gel,
286 gbp_endpoint_loc_is_equal);
289 return (&ge->ge_locs[pos]);
295 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
301 if (0 == gel->gel_locks)
303 pos = gel - ge->ge_locs;
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);
309 /* This could be the last lock, so don't access the EP from
311 fib_node_unlock (&ge->ge_node);
319 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
321 gbp_endpoint_group_unlock (gel->gel_epg);
323 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
325 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
329 static gbp_endpoint_loc_t *
330 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
332 gbp_endpoint_loc_t gel = {
334 .gel_epg = INDEX_INVALID,
335 .gel_sw_if_index = INDEX_INVALID,
340 pos = vec_search_with_function (ge->ge_locs, &gel,
341 gbp_endpoint_loc_is_equal);
345 vec_add1 (ge->ge_locs, gel);
347 if (vec_len (ge->ge_locs) > 1)
349 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
351 pos = vec_search_with_function (ge->ge_locs, &gel,
352 gbp_endpoint_loc_is_equal);
358 * it's the sources and children that lock the endpoints
360 fib_node_lock (&ge->ge_node);
363 return (&ge->ge_locs[pos]);
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
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)
377 gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
379 l2_ge = l3_ge = NULL;
381 if (NULL != mac && !mac_address_is_zero (mac))
384 l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
386 if (NULL != ips && !ip46_address_is_zero (ips))
388 const ip46_address_t *ip;
389 fib_protocol_t fproto;
392 vec_foreach (ip, ips)
394 fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
396 tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
401 else if (NULL == l3_ge)
402 /* first match against an IP address */
404 else if (tmp == l3_ge)
405 /* another match against IP address that is the same endpoint */
410 * a match agains a different endpoint.
411 * this means the KEY of the EP is changing which is not allowed
418 if (NULL == l2_ge && NULL == l3_ge)
421 else if (NULL == l2_ge)
424 else if (NULL == l3_ge)
429 /* found both L3 and L2 - they must be the same else the KEY
441 static gbp_endpoint_src_t
442 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
444 if (0 == vec_len (ge->ge_locs))
445 return (GBP_ENDPOINT_SRC_MAX);
447 return (ge->ge_locs[0].gel_src);
451 gbp_endpoint_n_learned (int n)
453 gbp_n_learnt_endpoints += n;
455 if (n > 0 && 1 == gbp_n_learnt_endpoints)
457 vlib_process_signal_event (vlib_get_main (),
458 gbp_scanner_node.index,
459 GBP_ENDPOINT_SCAN_START, 0);
461 if (n < 0 && 0 == gbp_n_learnt_endpoints)
463 vlib_process_signal_event (vlib_get_main (),
464 gbp_scanner_node.index,
465 GBP_ENDPOINT_SCAN_STOP, 0);
470 gbp_endpoint_loc_update (gbp_endpoint_loc_t * gel,
473 gbp_endpoint_flags_t flags,
474 const ip46_address_t * tun_src,
475 const ip46_address_t * tun_dst)
477 int was_learnt, is_learnt;
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);
484 gbp_endpoint_n_learned (is_learnt - was_learnt);
486 if (INDEX_INVALID == gel->gel_epg)
489 if (INDEX_INVALID != gel->gel_epg)
491 gbp_endpoint_group_lock (gel->gel_epg);
496 ASSERT (gel->gel_epg == ggi);
499 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
502 ip46_address_copy (&gel->tun.gel_src, tun_src);
504 ip46_address_copy (&gel->tun.gel_dst, tun_dst);
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
511 if (~0 != gel->gel_sw_if_index)
512 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
514 switch (gbp_vxlan_tunnel_get_type (sw_if_index))
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,
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);
533 gel->gel_sw_if_index = sw_if_index;
538 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
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;
547 gei = gbp_endpoint_index (ge);
548 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
551 vec_foreach (pfx, ge->ge_key.gek_ips)
555 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
556 fib_index = grd->grd_fib_index[pfx->fp_proto];
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);
562 * remove a host route
564 if (gbp_endpoint_is_remote (ge))
566 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
569 fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
571 vec_foreach (ai, gef->gef_adjs)
576 if (INDEX_INVALID != gef->gef_itf)
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);
583 gbp_itf_unlock (gef->gef_itf);
584 gef->gef_itf = INDEX_INVALID;
587 vec_free (gef->gef_adjs);
591 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
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;
602 * locations are sort in source priority order
604 gei = gbp_endpoint_index (ge);
605 gel = &ge->ge_locs[0];
607 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
609 gef->gef_flags = gel->gel_flags;
611 if (INDEX_INVALID != gel->gel_epg)
613 gg = gbp_endpoint_group_get (gel->gel_epg);
614 gef->gef_epg_id = gg->gg_id;
621 gef->gef_itf = gbp_itf_add_and_lock (gel->gel_sw_if_index,
624 if (!mac_address_is_zero (&ge->ge_key.gek_mac))
626 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, L2INPUT_FEAT_GBP_FWD);
628 if (gbp_endpoint_is_remote (ge))
630 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
631 L2OUTPUT_FEAT_GBP_POLICY_MAC);
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);
639 l2fib_add_entry (ge->ge_key.gek_mac.bytes,
641 gef->gef_itf, L2FIB_ENTRY_RESULT_FLAG_STATIC);
644 vec_foreach (pfx, ge->ge_key.gek_ips)
646 ethernet_header_t *eth;
653 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
654 fib_index = grd->grd_fib_index[pfx->fp_proto];
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);
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
664 vec_validate (rewrite, sizeof (*eth) - 1);
665 eth = (ethernet_header_t *) rewrite;
667 eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
671 if (gbp_endpoint_is_remote (ge))
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'.
678 ip_sw_if_index = gef->gef_itf;
680 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
682 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
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
692 ip_sw_if_index = gbd->gb_bvi_sw_if_index;
694 clib_memcpy (eth->src_address,
695 vnet_sw_interface_get_hw_address (vnet_get_main (),
697 sizeof (eth->src_address));
698 mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
701 fib_table_entry_path_add (fib_index, pfx,
702 FIB_SOURCE_PLUGIN_LOW,
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);
708 ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
709 fib_proto_to_link (pfx->fp_proto),
711 ip_sw_if_index, rewrite);
712 vec_add1 (gef->gef_adjs, ai);
716 if (gbp_endpoint_is_remote (ge))
718 dpo_id_t policy_dpo = DPO_INVALID;
721 * interpose a policy DPO from the endpoint so that policy
724 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
725 gg->gg_id, ~0, &policy_dpo);
727 fib_table_entry_special_dpo_add (fib_index, pfx,
728 FIB_SOURCE_PLUGIN_HI,
729 FIB_ENTRY_FLAG_INTERPOSE,
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
738 if (!gbp_endpoint_is_remote (ge) && ~0 != gg->gg_uplink_sw_if_index)
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 (),
744 gg->gg_uplink_sw_if_index);
746 send_ip6_na_w_addr (vlib_get_main (),
748 gg->gg_uplink_sw_if_index);
753 if (!gbp_endpoint_is_remote (ge))
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).
761 l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
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);
769 * update children with the new forwarding info
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,
776 fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
780 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
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)
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;
798 if (~0 == sw_if_index)
799 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
805 * we need to determine the bridge-domain, either from the EPG or
808 if (EPG_INVALID != epg_id)
810 ggi = gbp_endpoint_group_find (epg_id);
812 if (INDEX_INVALID == ggi)
813 return (VNET_API_ERROR_NO_SUCH_ENTRY);
815 gg = gbp_endpoint_group_get (ggi);
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);
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);
837 ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
841 gbp_endpoint_ips_update (ge, ips, grd);
844 best = gbp_endpoint_get_best_src (ge);
845 gei = gbp_endpoint_index (ge);
846 gel = gbp_endpoint_loc_find_or_add (ge, src);
848 gbp_endpoint_loc_update (gel, sw_if_index, ggi, flags, tun_src, tun_dst);
853 * either the best source has been updated or we have a new best source
855 gbb_endpoint_fwd_reset (ge);
856 gbb_endpoint_fwd_recalc (ge);
861 * an update to a lower priority source, so we need do nothing
868 GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
874 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
876 gbp_endpoint_loc_t *gel, gel_copy;
877 gbp_endpoint_src_t best;
881 if (pool_is_free_index (gbp_endpoint_pool, gei))
884 GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
886 ge = gbp_endpoint_get (gei);
888 gel = gbp_endpoint_loc_find (ge, src);
894 * lock the EP so we can control when it is deleted
896 fib_node_lock (&ge->ge_node);
897 best = gbp_endpoint_get_best_src (ge);
900 * copy the location info since we'll lose it when it's removed from
903 clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
906 * remove the source we no longer need
908 removed = gbp_endpoint_loc_unlock (ge, gel);
913 * we have removed the old best source => recalculate fwding
915 if (0 == vec_len (ge->ge_locs))
918 * if there are no more sources left, then we need only release
919 * the fwding resources held and then this EP is gawn.
921 gbb_endpoint_fwd_reset (ge);
926 * else there are more sources. release the old and get new
929 gbb_endpoint_fwd_reset (ge);
930 gbb_endpoint_fwd_recalc (ge);
935 * we removed a lower priority source so we need to do nothing
939 * clear up any resources held by the source
942 gbp_endpoint_loc_destroy (&gel_copy);
945 * remove the lock taken above
947 fib_node_unlock (&ge->ge_node);
949 * We may have removed the last source and so this EP is now TOAST
950 * DO NOTHING BELOW HERE
955 gbp_endpoint_child_add (index_t gei,
956 fib_node_type_t type, fib_node_index_t index)
958 return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
962 gbp_endpoint_child_remove (index_t gei, u32 sibling)
964 return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
967 typedef struct gbp_endpoint_flush_ctx_t_
970 gbp_endpoint_src_t src;
972 } gbp_endpoint_flush_ctx_t;
975 gbp_endpoint_flush_cb (index_t gei, void *args)
977 gbp_endpoint_flush_ctx_t *ctx = args;
978 gbp_endpoint_loc_t *gel;
981 ge = gbp_endpoint_get (gei);
982 gel = gbp_endpoint_loc_find (ge, ctx->src);
984 if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
986 vec_add1 (ctx->geis, gei);
989 return (WALK_CONTINUE);
993 * remove all learnt endpoints using the interface
996 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
998 gbp_endpoint_flush_ctx_t ctx = {
999 .sw_if_index = sw_if_index,
1004 GBP_ENDPOINT_INFO ("flush: %U %U",
1005 format_gbp_endpoint_src, src,
1006 format_vnet_sw_if_index_name, vnet_get_main (),
1008 gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1010 vec_foreach (gei, ctx.geis)
1012 gbp_endpoint_unlock (src, *gei);
1015 vec_free (ctx.geis);
1019 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1024 pool_foreach_index(index, gbp_endpoint_pool,
1026 if (!cb(index, ctx))
1032 static clib_error_t *
1033 gbp_endpoint_cli (vlib_main_t * vm,
1034 unformat_input_t * input, vlib_cli_command_t * cmd)
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;
1045 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1047 ip46_address_reset (&ip);
1049 if (unformat (input, "%U", unformat_vnet_sw_interface,
1052 else if (unformat (input, "add"))
1054 else if (unformat (input, "del"))
1056 else if (unformat (input, "epg %d", &epg_id))
1058 else if (unformat (input, "handle %d", &handle))
1060 else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1062 else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1064 else if (unformat (input, "mac %U", unformat_mac_address, &mac))
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");
1078 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1079 sw_if_index, ips, &mac,
1080 INDEX_INVALID, INDEX_INVALID,
1082 GBP_ENDPOINT_FLAG_NONE,
1083 NULL, NULL, &handle);
1086 return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1088 vlib_cli_output (vm, "handle %d\n", handle);
1092 if (INDEX_INVALID == handle)
1093 return clib_error_return (0, "handle must be specified");
1095 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1104 * Configure a GBP Endpoint
1107 * @cliexstart{set gbp endpoint [del] <interface> epg <ID> ip <IP>}
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,
1119 format_gbp_endpoint_src (u8 * s, va_list * args)
1121 gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1125 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1126 foreach_gbp_endpoint_src
1130 return (format (s, "unknown"));
1134 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1136 gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
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)
1142 s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1149 format_gbp_endpoint_key (u8 * s, va_list * args)
1151 gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1152 const fib_prefix_t *pfx;
1154 s = format (s, "ips:[");
1156 vec_foreach (pfx, gek->gek_ips)
1158 s = format (s, "%U, ", format_fib_prefix, pfx);
1160 s = format (s, "]");
1162 s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1168 format_gbp_endpoint_loc (u8 * s, va_list * args)
1170 gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1172 s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
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);
1178 if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1180 s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1182 if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
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);
1196 format_gbp_endpoint (u8 * s, va_list * args)
1198 index_t gei = va_arg (*args, index_t);
1199 gbp_endpoint_loc_t *gel;
1202 ge = gbp_endpoint_get (gei);
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);
1207 vec_foreach (gel, ge->ge_locs)
1209 s = format (s, "\n %U", format_gbp_endpoint_loc, gel);
1211 s = format (s, "\n %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1217 gbp_endpoint_show_one (index_t gei, void *ctx)
1222 vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1224 return (WALK_CONTINUE);
1228 gbp_endpoint_walk_ip_itf (const clib_bihash_kv_24_8_t * kvp, void *arg)
1236 gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
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);
1245 gbp_endpoint_walk_mac_itf (const clib_bihash_kv_16_8_t * kvp, void *arg)
1253 gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
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);
1261 static clib_error_t *
1262 gbp_endpoint_show (vlib_main_t * vm,
1263 unformat_input_t * input, vlib_cli_command_t * cmd)
1265 u32 show_dbs, handle;
1267 handle = INDEX_INVALID;
1270 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1272 if (unformat (input, "%d", &handle))
1274 else if (unformat (input, "db"))
1280 if (INDEX_INVALID != handle)
1282 vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
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);
1294 vlib_cli_output (vm, "Endpoints:");
1295 gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1302 * Show Group Based Policy Endpoints and derived information
1305 * @cliexstart{show gbp endpoint}
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,
1317 gbp_endpoint_check (index_t gei, f64 start_time)
1319 gbp_endpoint_loc_t *gel;
1322 ge = gbp_endpoint_get (gei);
1323 gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1325 if ((NULL != gel) &&
1326 ((start_time - ge->ge_last_time) > GBP_ENDPOINT_INACTIVE_TIME))
1328 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1333 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1335 clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1336 f64 last_start, start_time, delta_t;
1340 last_start = start_time = vlib_time_now (vm);
1342 for (i = 0; i < gte_table->nbuckets; i++)
1344 clib_bihash_bucket_16_8_t *b;
1345 clib_bihash_value_16_8_t *v;
1347 /* allow no more than 20us without a pause */
1348 delta_t = vlib_time_now (vm) - last_start;
1349 if (delta_t > 20e-6)
1351 /* suspend for 100 us */
1352 vlib_process_suspend (vm, 100e-6);
1353 last_start = vlib_time_now (vm);
1356 b = >e_table->buckets[i];
1359 v = clib_bihash_get_value_16_8 (gte_table, b->offset);
1361 for (j = 0; j < (1 << b->log2_pages); j++)
1363 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1365 if (clib_bihash_is_free_16_8 (&v->kvp[k]))
1368 gbp_endpoint_check (v->kvp[k].value, start_time);
1371 * Note: we may have just freed the bucket's backing
1372 * storage, so check right here...
1385 gbp_endpoint_scan_l3 (vlib_main_t * vm)
1387 clib_bihash_24_8_t *gte_table = &gbp_ep_db.ged_by_ip_rd;
1388 f64 last_start, start_time, delta_t;
1392 last_start = start_time = vlib_time_now (vm);
1394 for (i = 0; i < gte_table->nbuckets; i++)
1396 clib_bihash_bucket_24_8_t *b;
1397 clib_bihash_value_24_8_t *v;
1399 /* allow no more than 20us without a pause */
1400 delta_t = vlib_time_now (vm) - last_start;
1401 if (delta_t > 20e-6)
1403 /* suspend for 100 us */
1404 vlib_process_suspend (vm, 100e-6);
1405 last_start = vlib_time_now (vm);
1408 b = >e_table->buckets[i];
1411 v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1413 for (j = 0; j < (1 << b->log2_pages); j++)
1415 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1417 if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1420 gbp_endpoint_check (v->kvp[k].value, start_time);
1423 * Note: we may have just freed the bucket's backing
1424 * storage, so check right here...
1437 gbp_endpoint_scan (vlib_main_t * vm)
1439 gbp_endpoint_scan_l2 (vm);
1440 gbp_endpoint_scan_l3 (vm);
1444 gbp_learn_set_inactive_threshold (u32 threshold)
1446 GBP_ENDPOINT_INACTIVE_TIME = threshold;
1450 gbp_endpoint_scan_threshold (void)
1452 return (GBP_ENDPOINT_INACTIVE_TIME);
1456 gbp_endpoint_get_node (fib_node_index_t index)
1460 ge = gbp_endpoint_get (index);
1462 return (&ge->ge_node);
1465 static gbp_endpoint_t *
1466 gbp_endpoint_from_fib_node (fib_node_t * node)
1468 ASSERT (gbp_endpoint_fib_type == node->fn_type);
1469 return ((gbp_endpoint_t *) node);
1473 gbp_endpoint_last_lock_gone (fib_node_t * node)
1475 const gbp_bridge_domain_t *gbd;
1476 const gbp_route_domain_t *grd;
1477 const fib_prefix_t *pfx;
1480 ge = gbp_endpoint_from_fib_node (node);
1482 ASSERT (0 == vec_len (ge->ge_locs));
1484 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1487 * we have removed the last source. this EP is toast
1489 if (INDEX_INVALID != ge->ge_key.gek_gbd)
1491 gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1493 vec_foreach (pfx, ge->ge_key.gek_ips)
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]);
1498 pool_put (gbp_endpoint_pool, ge);
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)
1507 return (FIB_NODE_BACK_WALK_CONTINUE);
1511 * The FIB path's graph node virtual function table
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,
1520 static clib_error_t *
1521 gbp_endpoint_init (vlib_main_t * vm)
1523 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1524 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
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);
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);
1534 gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1535 gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1540 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1543 * fd.io coding-style-patch-verification: ON
1546 * eval: (c-set-style "gnu")