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 * Pool of GBP endpoints
56 gbp_endpoint_t *gbp_endpoint_pool;
59 * A count of the number of dynamic entries
61 static u32 gbp_n_learnt_endpoints;
63 #define FOR_EACH_GBP_ENDPOINT_ATTR(_item) \
64 for (_item = GBP_ENDPOINT_ATTR_FIRST; \
65 _item < GBP_ENDPOINT_ATTR_LAST; \
69 format_gbp_endpoint_flags (u8 * s, va_list * args)
71 gbp_endpoint_attr_t attr;
72 gbp_endpoint_flags_t flags = va_arg (*args, gbp_endpoint_flags_t);
74 FOR_EACH_GBP_ENDPOINT_ATTR (attr)
76 if ((1 << attr) & flags)
78 s = format (s, "%s,", gbp_endpoint_attr_names[attr]);
86 gbp_endpoint_is_remote (const gbp_endpoint_t * ge)
88 return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
92 gbp_endpoint_is_local (const gbp_endpoint_t * ge)
94 return (!(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
98 gbp_endpoint_is_external (const gbp_endpoint_t * ge)
100 return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_EXTERNAL));
104 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
105 mac_address_t * mac, u32 * sw_if_index)
107 mac_address_from_u64 (mac, key->key[0]);
108 *sw_if_index = key->key[1];
112 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
113 ip46_address_t * ip, u32 * sw_if_index)
115 ip->as_u64[0] = key->key[0];
116 ip->as_u64[1] = key->key[1];
117 *sw_if_index = key->key[2];
121 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
123 clib_bihash_kv_24_8_t key, value;
126 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
128 rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
133 return (gbp_endpoint_get (value.value));
137 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
139 vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
141 gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
145 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
147 clib_bihash_kv_16_8_t key;
150 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
153 rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
160 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
162 clib_bihash_kv_24_8_t key;
165 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
168 rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
174 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
176 clib_bihash_kv_16_8_t key;
178 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
180 clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
184 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
186 clib_bihash_kv_24_8_t key;
188 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
190 clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
194 gbp_endpoint_index (const gbp_endpoint_t * ge)
196 return (ge - gbp_endpoint_pool);
200 ip46_address_get_type (const ip46_address_t * a)
202 return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
206 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
208 return (ip46_address_is_equal (ip, &fp->fp_addr));
212 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
213 const ip46_address_t * ips,
214 const gbp_route_domain_t * grd)
216 const ip46_address_t *ip;
219 gei = gbp_endpoint_index (ge);
220 grdi = gbp_route_domain_index (grd);
222 ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
223 (ge->ge_key.gek_grd == grdi));
225 vec_foreach (ip, ips)
227 if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
228 gbp_endpoint_ip_is_equal))
232 vec_add2 (ge->ge_key.gek_ips, pfx, 1);
233 fib_prefix_from_ip46_addr (ip, pfx);
235 gbp_endpoint_add_ip (&pfx->fp_addr,
236 grd->grd_fib_index[pfx->fp_proto], gei);
238 ge->ge_key.gek_grd = grdi;
242 static gbp_endpoint_t *
243 gbp_endpoint_alloc (const ip46_address_t * ips,
244 const gbp_route_domain_t * grd,
245 const mac_address_t * mac,
246 const gbp_bridge_domain_t * gbd)
251 pool_get_zero (gbp_endpoint_pool, ge);
253 fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
254 gei = gbp_endpoint_index (ge);
256 ge->ge_key.gek_grd = ge->ge_fwd.gef_itf = INDEX_INVALID;
257 ge->ge_last_time = vlib_time_now (vlib_get_main ());
258 ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
262 mac_address_copy (&ge->ge_key.gek_mac, mac);
263 gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
265 gbp_endpoint_ips_update (ge, ips, grd);
271 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
273 return (a->gel_src == b->gel_src);
277 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
279 return (a->gel_src - b->gel_src);
282 static gbp_endpoint_loc_t *
283 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
285 gbp_endpoint_loc_t gel = {
290 pos = vec_search_with_function (ge->ge_locs, &gel,
291 gbp_endpoint_loc_is_equal);
294 return (&ge->ge_locs[pos]);
300 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
306 if (0 == gel->gel_locks)
308 pos = gel - ge->ge_locs;
310 vec_del1 (ge->ge_locs, pos);
311 if (vec_len (ge->ge_locs) > 1)
312 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
314 /* This could be the last lock, so don't access the EP from
316 fib_node_unlock (&ge->ge_node);
324 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
326 gbp_endpoint_group_unlock (gel->gel_epg);
328 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
330 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
334 static gbp_endpoint_loc_t *
335 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
337 gbp_endpoint_loc_t gel = {
339 .gel_epg = INDEX_INVALID,
340 .gel_sw_if_index = INDEX_INVALID,
345 pos = vec_search_with_function (ge->ge_locs, &gel,
346 gbp_endpoint_loc_is_equal);
350 vec_add1 (ge->ge_locs, gel);
352 if (vec_len (ge->ge_locs) > 1)
354 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
356 pos = vec_search_with_function (ge->ge_locs, &gel,
357 gbp_endpoint_loc_is_equal);
363 * it's the sources and children that lock the endpoints
365 fib_node_lock (&ge->ge_node);
368 return (&ge->ge_locs[pos]);
372 * Find an EP inthe DBs and check that if we find it in the L2 DB
373 * it has the same IPs as this update
376 gbp_endpoint_find_for_update (const ip46_address_t * ips,
377 const gbp_route_domain_t * grd,
378 const mac_address_t * mac,
379 const gbp_bridge_domain_t * gbd,
380 gbp_endpoint_t ** ge)
382 gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
384 l2_ge = l3_ge = NULL;
386 if (NULL != mac && !mac_address_is_zero (mac))
389 l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
391 if (NULL != ips && !ip46_address_is_zero (ips))
393 const ip46_address_t *ip;
394 fib_protocol_t fproto;
397 vec_foreach (ip, ips)
399 fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
401 tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
406 else if (NULL == l3_ge)
407 /* first match against an IP address */
409 else if (tmp == l3_ge)
410 /* another match against IP address that is the same endpoint */
415 * a match agains a different endpoint.
416 * this means the KEY of the EP is changing which is not allowed
423 if (NULL == l2_ge && NULL == l3_ge)
426 else if (NULL == l2_ge)
429 else if (NULL == l3_ge)
434 /* found both L3 and L2 - they must be the same else the KEY
446 static gbp_endpoint_src_t
447 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
449 if (0 == vec_len (ge->ge_locs))
450 return (GBP_ENDPOINT_SRC_MAX);
452 return (ge->ge_locs[0].gel_src);
456 gbp_endpoint_n_learned (int n)
458 gbp_n_learnt_endpoints += n;
460 if (n > 0 && 1 == gbp_n_learnt_endpoints)
462 vlib_process_signal_event (vlib_get_main (),
463 gbp_scanner_node.index,
464 GBP_ENDPOINT_SCAN_START, 0);
466 if (n < 0 && 0 == gbp_n_learnt_endpoints)
468 vlib_process_signal_event (vlib_get_main (),
469 gbp_scanner_node.index,
470 GBP_ENDPOINT_SCAN_STOP, 0);
475 gbp_endpoint_loc_update (gbp_endpoint_loc_t * gel,
478 gbp_endpoint_flags_t flags,
479 const ip46_address_t * tun_src,
480 const ip46_address_t * tun_dst)
482 int was_learnt, is_learnt;
485 was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
486 gel->gel_flags = flags;
487 is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
489 gbp_endpoint_n_learned (is_learnt - was_learnt);
491 if (INDEX_INVALID == gel->gel_epg)
494 if (INDEX_INVALID != gel->gel_epg)
496 gbp_endpoint_group_lock (gel->gel_epg);
501 ASSERT (gel->gel_epg == ggi);
504 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
507 ip46_address_copy (&gel->tun.gel_src, tun_src);
509 ip46_address_copy (&gel->tun.gel_dst, tun_dst);
512 * the input interface may be the parent GBP-vxlan interface,
513 * create a child vlxan-gbp tunnel and use that as the endpoint's
516 if (~0 != gel->gel_sw_if_index)
517 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
519 switch (gbp_vxlan_tunnel_get_type (sw_if_index))
521 case GBP_VXLAN_TEMPLATE_TUNNEL:
522 gel->tun.gel_parent_sw_if_index = sw_if_index;
523 gel->gel_sw_if_index =
524 gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
528 case VXLAN_GBP_TUNNEL:
529 gel->tun.gel_parent_sw_if_index =
530 vxlan_gbp_tunnel_get_parent (sw_if_index);
531 gel->gel_sw_if_index = sw_if_index;
532 vxlan_gbp_tunnel_lock (gel->gel_sw_if_index);
538 gel->gel_sw_if_index = sw_if_index;
543 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
545 const gbp_route_domain_t *grd;
546 const gbp_bridge_domain_t *gbd;
547 gbp_endpoint_fwd_t *gef;
548 const fib_prefix_t *pfx;
552 gei = gbp_endpoint_index (ge);
553 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
556 vec_foreach (pfx, ge->ge_key.gek_ips)
560 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
561 fib_index = grd->grd_fib_index[pfx->fp_proto];
563 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
564 &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
567 * remove a host route
569 if (gbp_endpoint_is_remote (ge))
571 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
574 fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
576 vec_foreach (ai, gef->gef_adjs)
581 if (INDEX_INVALID != gef->gef_itf)
583 l2fib_del_entry (ge->ge_key.gek_mac.bytes,
584 gbd->gb_bd_index, gef->gef_itf);
585 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, (L2INPUT_FEAT_NONE));
586 gbp_itf_set_l2_output_feature (gef->gef_itf, gei, L2OUTPUT_FEAT_NONE);
588 gbp_itf_unlock (gef->gef_itf);
589 gef->gef_itf = INDEX_INVALID;
592 vec_free (gef->gef_adjs);
596 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
598 const gbp_route_domain_t *grd;
599 const gbp_bridge_domain_t *gbd;
600 const gbp_endpoint_group_t *gg;
601 gbp_endpoint_loc_t *gel;
602 gbp_endpoint_fwd_t *gef;
603 const fib_prefix_t *pfx;
607 * locations are sort in source priority order
609 gei = gbp_endpoint_index (ge);
610 gel = &ge->ge_locs[0];
612 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
614 gef->gef_flags = gel->gel_flags;
616 if (INDEX_INVALID != gel->gel_epg)
618 gg = gbp_endpoint_group_get (gel->gel_epg);
619 gef->gef_epg_id = gg->gg_id;
626 gef->gef_itf = gbp_itf_add_and_lock (gel->gel_sw_if_index,
629 if (!mac_address_is_zero (&ge->ge_key.gek_mac))
631 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, L2INPUT_FEAT_GBP_FWD);
633 if (gbp_endpoint_is_remote (ge) || gbp_endpoint_is_external (ge))
636 * bridged packets to external endpoints should be classifed
637 * based on the EP's/BD's EPG
639 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
640 L2OUTPUT_FEAT_GBP_POLICY_MAC);
644 gbp_endpoint_add_itf (gef->gef_itf, gei);
645 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
646 L2OUTPUT_FEAT_GBP_POLICY_PORT);
648 l2fib_add_entry (ge->ge_key.gek_mac.bytes,
650 gef->gef_itf, L2FIB_ENTRY_RESULT_FLAG_STATIC);
653 vec_foreach (pfx, ge->ge_key.gek_ips)
655 ethernet_header_t *eth;
662 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
663 fib_index = grd->grd_fib_index[pfx->fp_proto];
665 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
666 &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
669 * add a host route via the EPG's BVI we need this because the
670 * adj fib does not install, due to cover refinement check, since
671 * the BVI's prefix is /32
673 vec_validate (rewrite, sizeof (*eth) - 1);
674 eth = (ethernet_header_t *) rewrite;
676 eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
680 if (gbp_endpoint_is_remote (ge))
683 * for dynamic EPs we must add the IP adjacency via the learned
684 * tunnel since the BD will not contain the EP's MAC since it was
685 * L3 learned. The dst MAC address used is the 'BD's MAC'.
687 ip_sw_if_index = gef->gef_itf;
689 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
691 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
697 * for the static EPs we add the IP adjacency via the BVI
698 * knowing that the BD has the MAC address to route to and
699 * that policy will be applied on egress to the EP's port
701 ip_sw_if_index = gbd->gb_bvi_sw_if_index;
703 clib_memcpy (eth->src_address,
704 vnet_sw_interface_get_hw_address (vnet_get_main (),
706 sizeof (eth->src_address));
707 mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
710 fib_table_entry_path_add (fib_index, pfx,
711 FIB_SOURCE_PLUGIN_LOW,
713 fib_proto_to_dpo (pfx->fp_proto),
714 &pfx->fp_addr, ip_sw_if_index,
715 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
717 ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
718 fib_proto_to_link (pfx->fp_proto),
720 ip_sw_if_index, rewrite);
721 vec_add1 (gef->gef_adjs, ai);
724 * if the endpoint is external then routed packet to it must be
725 * classifed to the BD's EPG. but this will happen anyway with
726 * the GBP_MAC classification.
731 if (gbp_endpoint_is_remote (ge))
733 dpo_id_t policy_dpo = DPO_INVALID;
736 * interpose a policy DPO from the endpoint so that policy
739 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
740 gg->gg_id, ~0, &policy_dpo);
742 fib_table_entry_special_dpo_add (fib_index, pfx,
743 FIB_SOURCE_PLUGIN_HI,
744 FIB_ENTRY_FLAG_INTERPOSE,
746 dpo_reset (&policy_dpo);
750 * send a gratuitous ARP on the EPG's uplink. this is done so
751 * that if this EP has moved from some other place in the
752 * 'fabric', upstream devices are informed
754 if (gbp_endpoint_is_local (ge) && ~0 != gg->gg_uplink_sw_if_index)
756 gbp_endpoint_add_itf (gef->gef_itf, gei);
757 if (FIB_PROTOCOL_IP4 == pfx->fp_proto)
758 send_ip4_garp_w_addr (vlib_get_main (),
760 gg->gg_uplink_sw_if_index);
762 send_ip6_na_w_addr (vlib_get_main (),
764 gg->gg_uplink_sw_if_index);
769 if (gbp_endpoint_is_local (ge) && !gbp_endpoint_is_external (ge))
772 * non-remote endpoints (i.e. those not arriving on iVXLAN
773 * tunnels) need to be classifed based on the the input interface.
774 * We enable the GBP-FWD feature only if the group has an uplink
775 * interface (on which the GBP-FWD feature would send UU traffic).
776 * External endpoints get classified based on an LPM match
778 l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
780 if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
781 feats |= L2INPUT_FEAT_GBP_FWD;
782 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, feats);
786 * update children with the new forwarding info
788 fib_node_back_walk_ctx_t bw_ctx = {
789 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
790 .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
793 fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
797 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
799 const ip46_address_t * ips,
800 const mac_address_t * mac,
801 index_t gbdi, index_t grdi, epg_id_t epg_id,
802 gbp_endpoint_flags_t flags,
803 const ip46_address_t * tun_src,
804 const ip46_address_t * tun_dst, u32 * handle)
806 gbp_bridge_domain_t *gbd;
807 gbp_endpoint_group_t *gg;
808 gbp_endpoint_src_t best;
809 gbp_route_domain_t *grd;
810 gbp_endpoint_loc_t *gel;
815 if (~0 == sw_if_index)
816 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
822 * we need to determine the bridge-domain, either from the EPG or
825 if (EPG_INVALID != epg_id)
827 ggi = gbp_endpoint_group_find (epg_id);
829 if (INDEX_INVALID == ggi)
830 return (VNET_API_ERROR_NO_SUCH_ENTRY);
832 gg = gbp_endpoint_group_get (ggi);
838 if (INDEX_INVALID == gbdi)
839 return (VNET_API_ERROR_NO_SUCH_ENTRY);
840 if (INDEX_INVALID == grdi)
841 return (VNET_API_ERROR_NO_SUCH_FIB);
845 gbd = gbp_bridge_domain_get (gbdi);
846 grd = gbp_route_domain_get (grdi);
847 rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
854 ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
858 gbp_endpoint_ips_update (ge, ips, grd);
861 best = gbp_endpoint_get_best_src (ge);
862 gei = gbp_endpoint_index (ge);
863 gel = gbp_endpoint_loc_find_or_add (ge, src);
865 gbp_endpoint_loc_update (gel, sw_if_index, ggi, flags, tun_src, tun_dst);
870 * either the best source has been updated or we have a new best source
872 gbb_endpoint_fwd_reset (ge);
873 gbb_endpoint_fwd_recalc (ge);
878 * an update to a lower priority source, so we need do nothing
885 GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
891 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
893 gbp_endpoint_loc_t *gel, gel_copy;
894 gbp_endpoint_src_t best;
898 if (pool_is_free_index (gbp_endpoint_pool, gei))
901 GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
903 ge = gbp_endpoint_get (gei);
905 gel = gbp_endpoint_loc_find (ge, src);
911 * lock the EP so we can control when it is deleted
913 fib_node_lock (&ge->ge_node);
914 best = gbp_endpoint_get_best_src (ge);
917 * copy the location info since we'll lose it when it's removed from
920 clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
923 * remove the source we no longer need
925 removed = gbp_endpoint_loc_unlock (ge, gel);
930 * we have removed the old best source => recalculate fwding
932 if (0 == vec_len (ge->ge_locs))
935 * if there are no more sources left, then we need only release
936 * the fwding resources held and then this EP is gawn.
938 gbb_endpoint_fwd_reset (ge);
943 * else there are more sources. release the old and get new
946 gbb_endpoint_fwd_reset (ge);
947 gbb_endpoint_fwd_recalc (ge);
952 * we removed a lower priority source so we need to do nothing
956 * clear up any resources held by the source
959 gbp_endpoint_loc_destroy (&gel_copy);
962 * remove the lock taken above
964 fib_node_unlock (&ge->ge_node);
966 * We may have removed the last source and so this EP is now TOAST
967 * DO NOTHING BELOW HERE
972 gbp_endpoint_child_add (index_t gei,
973 fib_node_type_t type, fib_node_index_t index)
975 return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
979 gbp_endpoint_child_remove (index_t gei, u32 sibling)
981 return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
984 typedef struct gbp_endpoint_flush_ctx_t_
987 gbp_endpoint_src_t src;
989 } gbp_endpoint_flush_ctx_t;
992 gbp_endpoint_flush_cb (index_t gei, void *args)
994 gbp_endpoint_flush_ctx_t *ctx = args;
995 gbp_endpoint_loc_t *gel;
998 ge = gbp_endpoint_get (gei);
999 gel = gbp_endpoint_loc_find (ge, ctx->src);
1001 if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
1003 vec_add1 (ctx->geis, gei);
1006 return (WALK_CONTINUE);
1010 * remove all learnt endpoints using the interface
1013 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
1015 gbp_endpoint_flush_ctx_t ctx = {
1016 .sw_if_index = sw_if_index,
1021 GBP_ENDPOINT_INFO ("flush: %U %U",
1022 format_gbp_endpoint_src, src,
1023 format_vnet_sw_if_index_name, vnet_get_main (),
1025 gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1027 vec_foreach (gei, ctx.geis)
1029 gbp_endpoint_unlock (src, *gei);
1032 vec_free (ctx.geis);
1036 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1041 pool_foreach_index(index, gbp_endpoint_pool,
1043 if (!cb(index, ctx))
1049 static clib_error_t *
1050 gbp_endpoint_cli (vlib_main_t * vm,
1051 unformat_input_t * input, vlib_cli_command_t * cmd)
1053 ip46_address_t ip = ip46_address_initializer, *ips = NULL;
1054 mac_address_t mac = ZERO_MAC_ADDRESS;
1055 vnet_main_t *vnm = vnet_get_main ();
1056 u32 epg_id = EPG_INVALID;
1057 u32 handle = INDEX_INVALID;
1058 u32 sw_if_index = ~0;
1062 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1064 ip46_address_reset (&ip);
1066 if (unformat (input, "%U", unformat_vnet_sw_interface,
1069 else if (unformat (input, "add"))
1071 else if (unformat (input, "del"))
1073 else if (unformat (input, "epg %d", &epg_id))
1075 else if (unformat (input, "handle %d", &handle))
1077 else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1079 else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1081 else if (unformat (input, "mac %U", unformat_mac_address, &mac))
1089 if (~0 == sw_if_index)
1090 return clib_error_return (0, "interface must be specified");
1091 if (EPG_INVALID == epg_id)
1092 return clib_error_return (0, "EPG-ID must be specified");
1095 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1096 sw_if_index, ips, &mac,
1097 INDEX_INVALID, INDEX_INVALID,
1099 GBP_ENDPOINT_FLAG_NONE,
1100 NULL, NULL, &handle);
1103 return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1105 vlib_cli_output (vm, "handle %d\n", handle);
1109 if (INDEX_INVALID == handle)
1110 return clib_error_return (0, "handle must be specified");
1112 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1121 * Configure a GBP Endpoint
1124 * @cliexstart{set gbp endpoint [del] <interface> epg <ID> ip <IP>}
1128 VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
1129 .path = "gbp endpoint",
1130 .short_help = "gbp endpoint [del] <interface> epg <ID> ip <IP> mac <MAC>",
1131 .function = gbp_endpoint_cli,
1136 format_gbp_endpoint_src (u8 * s, va_list * args)
1138 gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1142 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1143 foreach_gbp_endpoint_src
1147 return (format (s, "unknown"));
1151 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1153 gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
1155 s = format (s, "fwd:");
1156 s = format (s, "\n itf:[%U]", format_gbp_itf, gef->gef_itf);
1157 if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
1159 s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1166 format_gbp_endpoint_key (u8 * s, va_list * args)
1168 gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1169 const fib_prefix_t *pfx;
1171 s = format (s, "ips:[");
1173 vec_foreach (pfx, gek->gek_ips)
1175 s = format (s, "%U, ", format_fib_prefix, pfx);
1177 s = format (s, "]");
1179 s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1185 format_gbp_endpoint_loc (u8 * s, va_list * args)
1187 gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1189 s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
1191 format (s, "\n %U", format_vnet_sw_if_index_name, vnet_get_main (),
1192 gel->gel_sw_if_index);
1193 s = format (s, " EPG:%d", gel->gel_epg);
1195 if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1197 s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1199 if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
1201 s = format (s, " tun:[");
1202 s = format (s, "parent:%U", format_vnet_sw_if_index_name,
1203 vnet_get_main (), gel->tun.gel_parent_sw_if_index);
1204 s = format (s, " {%U,%U}]",
1205 format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
1206 format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
1213 format_gbp_endpoint (u8 * s, va_list * args)
1215 index_t gei = va_arg (*args, index_t);
1216 gbp_endpoint_loc_t *gel;
1219 ge = gbp_endpoint_get (gei);
1221 s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
1222 s = format (s, " last-time:[%f]", ge->ge_last_time);
1224 vec_foreach (gel, ge->ge_locs)
1226 s = format (s, "\n %U", format_gbp_endpoint_loc, gel);
1228 s = format (s, "\n %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1234 gbp_endpoint_show_one (index_t gei, void *ctx)
1239 vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1241 return (WALK_CONTINUE);
1245 gbp_endpoint_walk_ip_itf (const clib_bihash_kv_24_8_t * kvp, void *arg)
1253 gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
1255 vlib_cli_output (vm, " {%U, %U} -> %d",
1256 format_ip46_address, &ip, IP46_TYPE_ANY,
1257 format_vnet_sw_if_index_name, vnet_get_main (),
1258 sw_if_index, kvp->value);
1262 gbp_endpoint_walk_mac_itf (const clib_bihash_kv_16_8_t * kvp, void *arg)
1270 gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
1272 vlib_cli_output (vm, " {%U, %U} -> %d",
1273 format_mac_address_t, &mac,
1274 format_vnet_sw_if_index_name, vnet_get_main (),
1275 sw_if_index, kvp->value);
1278 static clib_error_t *
1279 gbp_endpoint_show (vlib_main_t * vm,
1280 unformat_input_t * input, vlib_cli_command_t * cmd)
1282 u32 show_dbs, handle;
1284 handle = INDEX_INVALID;
1287 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1289 if (unformat (input, "%d", &handle))
1291 else if (unformat (input, "db"))
1297 if (INDEX_INVALID != handle)
1299 vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
1303 vlib_cli_output (vm, "\nDatabases:");
1304 clib_bihash_foreach_key_value_pair_24_8 (&gbp_ep_db.ged_by_ip_rd,
1305 gbp_endpoint_walk_ip_itf, vm);
1306 clib_bihash_foreach_key_value_pair_16_8
1307 (&gbp_ep_db.ged_by_mac_bd, gbp_endpoint_walk_mac_itf, vm);
1311 vlib_cli_output (vm, "Endpoints:");
1312 gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1319 * Show Group Based Policy Endpoints and derived information
1322 * @cliexstart{show gbp endpoint}
1326 VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
1327 .path = "show gbp endpoint",
1328 .short_help = "show gbp endpoint\n",
1329 .function = gbp_endpoint_show,
1334 gbp_endpoint_check (index_t gei, f64 start_time)
1336 gbp_endpoint_group_t *gg;
1337 gbp_endpoint_loc_t *gel;
1340 ge = gbp_endpoint_get (gei);
1341 gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1345 gg = gbp_endpoint_group_get (gel->gel_epg);
1347 if ((start_time - ge->ge_last_time) >
1348 gg->gg_retention.remote_ep_timeout)
1350 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1356 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1358 clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1359 f64 last_start, start_time, delta_t;
1363 last_start = start_time = vlib_time_now (vm);
1365 for (i = 0; i < gte_table->nbuckets; i++)
1367 clib_bihash_bucket_16_8_t *b;
1368 clib_bihash_value_16_8_t *v;
1370 /* allow no more than 20us without a pause */
1371 delta_t = vlib_time_now (vm) - last_start;
1372 if (delta_t > 20e-6)
1374 /* suspend for 100 us */
1375 vlib_process_suspend (vm, 100e-6);
1376 last_start = vlib_time_now (vm);
1379 b = >e_table->buckets[i];
1382 v = clib_bihash_get_value_16_8 (gte_table, b->offset);
1384 for (j = 0; j < (1 << b->log2_pages); j++)
1386 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1388 if (clib_bihash_is_free_16_8 (&v->kvp[k]))
1391 gbp_endpoint_check (v->kvp[k].value, start_time);
1394 * Note: we may have just freed the bucket's backing
1395 * storage, so check right here...
1408 gbp_endpoint_scan_l3 (vlib_main_t * vm)
1410 clib_bihash_24_8_t *gte_table = &gbp_ep_db.ged_by_ip_rd;
1411 f64 last_start, start_time, delta_t;
1415 last_start = start_time = vlib_time_now (vm);
1417 for (i = 0; i < gte_table->nbuckets; i++)
1419 clib_bihash_bucket_24_8_t *b;
1420 clib_bihash_value_24_8_t *v;
1422 /* allow no more than 20us without a pause */
1423 delta_t = vlib_time_now (vm) - last_start;
1424 if (delta_t > 20e-6)
1426 /* suspend for 100 us */
1427 vlib_process_suspend (vm, 100e-6);
1428 last_start = vlib_time_now (vm);
1431 b = >e_table->buckets[i];
1434 v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1436 for (j = 0; j < (1 << b->log2_pages); j++)
1438 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1440 if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1443 gbp_endpoint_check (v->kvp[k].value, start_time);
1446 * Note: we may have just freed the bucket's backing
1447 * storage, so check right here...
1460 gbp_endpoint_scan (vlib_main_t * vm)
1462 gbp_endpoint_scan_l2 (vm);
1463 gbp_endpoint_scan_l3 (vm);
1467 gbp_endpoint_get_node (fib_node_index_t index)
1471 ge = gbp_endpoint_get (index);
1473 return (&ge->ge_node);
1476 static gbp_endpoint_t *
1477 gbp_endpoint_from_fib_node (fib_node_t * node)
1479 ASSERT (gbp_endpoint_fib_type == node->fn_type);
1480 return ((gbp_endpoint_t *) node);
1484 gbp_endpoint_last_lock_gone (fib_node_t * node)
1486 const gbp_bridge_domain_t *gbd;
1487 const gbp_route_domain_t *grd;
1488 const fib_prefix_t *pfx;
1491 ge = gbp_endpoint_from_fib_node (node);
1493 ASSERT (0 == vec_len (ge->ge_locs));
1495 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1498 * we have removed the last source. this EP is toast
1500 if (INDEX_INVALID != ge->ge_key.gek_gbd)
1502 gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1504 vec_foreach (pfx, ge->ge_key.gek_ips)
1506 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
1507 gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
1509 pool_put (gbp_endpoint_pool, ge);
1512 static fib_node_back_walk_rc_t
1513 gbp_endpoint_back_walk_notify (fib_node_t * node,
1514 fib_node_back_walk_ctx_t * ctx)
1518 return (FIB_NODE_BACK_WALK_CONTINUE);
1522 * The FIB path's graph node virtual function table
1524 static const fib_node_vft_t gbp_endpoint_vft = {
1525 .fnv_get = gbp_endpoint_get_node,
1526 .fnv_last_lock = gbp_endpoint_last_lock_gone,
1527 .fnv_back_walk = gbp_endpoint_back_walk_notify,
1528 // .fnv_mem_show = fib_path_memory_show,
1531 static clib_error_t *
1532 gbp_endpoint_init (vlib_main_t * vm)
1534 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1535 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
1537 clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
1538 "GBP Endpoints - IP/RD",
1539 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1541 clib_bihash_init_16_8 (&gbp_ep_db.ged_by_mac_bd,
1542 "GBP Endpoints - MAC/BD",
1543 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1545 gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1546 gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1551 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1554 * fd.io coding-style-patch-verification: ON
1557 * eval: (c-set-style "gnu")