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>
35 #include <vnet/vxlan-gbp/vxlan_gbp.h>
37 static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
42 gbp_ep_db_t gbp_ep_db;
44 fib_node_type_t gbp_endpoint_fib_type;
46 vlib_log_class_t gbp_ep_logger;
48 #define GBP_ENDPOINT_DBG(...) \
49 vlib_log_debug (gbp_ep_logger, __VA_ARGS__);
51 #define GBP_ENDPOINT_INFO(...) \
52 vlib_log_notice (gbp_ep_logger, __VA_ARGS__);
55 * Pool of GBP endpoints
57 gbp_endpoint_t *gbp_endpoint_pool;
60 * A count of the number of dynamic entries
62 static u32 gbp_n_learnt_endpoints;
64 #define FOR_EACH_GBP_ENDPOINT_ATTR(_item) \
65 for (_item = GBP_ENDPOINT_ATTR_FIRST; \
66 _item < GBP_ENDPOINT_ATTR_LAST; \
70 format_gbp_endpoint_flags (u8 * s, va_list * args)
72 gbp_endpoint_attr_t attr;
73 gbp_endpoint_flags_t flags = va_arg (*args, gbp_endpoint_flags_t);
75 FOR_EACH_GBP_ENDPOINT_ATTR (attr)
77 if ((1 << attr) & flags)
79 s = format (s, "%s,", gbp_endpoint_attr_names[attr]);
87 gbp_endpoint_is_remote (const gbp_endpoint_t * ge)
89 return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
93 gbp_endpoint_is_local (const gbp_endpoint_t * ge)
95 return (!(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
99 gbp_endpoint_is_external (const gbp_endpoint_t * ge)
101 return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_EXTERNAL));
105 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
106 mac_address_t * mac, u32 * sw_if_index)
108 mac_address_from_u64 (mac, key->key[0]);
109 *sw_if_index = key->key[1];
113 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
114 ip46_address_t * ip, u32 * sw_if_index)
116 ip->as_u64[0] = key->key[0];
117 ip->as_u64[1] = key->key[1];
118 *sw_if_index = key->key[2];
122 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
124 clib_bihash_kv_24_8_t key, value;
127 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
129 rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
134 return (gbp_endpoint_get (value.value));
138 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
140 vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
142 gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
146 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
148 clib_bihash_kv_16_8_t key;
151 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
154 rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
161 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
163 clib_bihash_kv_24_8_t key;
166 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
169 rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
175 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
177 clib_bihash_kv_16_8_t key;
179 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
181 clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
185 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
187 clib_bihash_kv_24_8_t key;
189 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
191 clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
195 gbp_endpoint_index (const gbp_endpoint_t * ge)
197 return (ge - gbp_endpoint_pool);
201 ip46_address_get_type (const ip46_address_t * a)
203 return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
207 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
209 return (ip46_address_is_equal (ip, &fp->fp_addr));
213 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
214 const ip46_address_t * ips,
215 const gbp_route_domain_t * grd)
217 const ip46_address_t *ip;
220 gei = gbp_endpoint_index (ge);
221 grdi = gbp_route_domain_index (grd);
223 ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
224 (ge->ge_key.gek_grd == grdi));
226 vec_foreach (ip, ips)
228 if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
229 gbp_endpoint_ip_is_equal))
233 vec_add2 (ge->ge_key.gek_ips, pfx, 1);
234 fib_prefix_from_ip46_addr (ip, pfx);
236 gbp_endpoint_add_ip (&pfx->fp_addr,
237 grd->grd_fib_index[pfx->fp_proto], gei);
239 ge->ge_key.gek_grd = grdi;
243 static gbp_endpoint_t *
244 gbp_endpoint_alloc (const ip46_address_t * ips,
245 const gbp_route_domain_t * grd,
246 const mac_address_t * mac,
247 const gbp_bridge_domain_t * gbd)
252 pool_get_zero (gbp_endpoint_pool, ge);
254 fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
255 gei = gbp_endpoint_index (ge);
257 ge->ge_key.gek_grd = ge->ge_fwd.gef_itf = INDEX_INVALID;
258 ge->ge_last_time = vlib_time_now (vlib_get_main ());
259 ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
263 mac_address_copy (&ge->ge_key.gek_mac, mac);
264 gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
266 gbp_endpoint_ips_update (ge, ips, grd);
272 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
274 return (a->gel_src == b->gel_src);
278 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
280 return (a->gel_src - b->gel_src);
283 static gbp_endpoint_loc_t *
284 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
286 gbp_endpoint_loc_t gel = {
291 pos = vec_search_with_function (ge->ge_locs, &gel,
292 gbp_endpoint_loc_is_equal);
295 return (&ge->ge_locs[pos]);
301 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
307 if (0 == gel->gel_locks)
309 pos = gel - ge->ge_locs;
311 vec_del1 (ge->ge_locs, pos);
312 if (vec_len (ge->ge_locs) > 1)
313 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
315 /* This could be the last lock, so don't access the EP from
317 fib_node_unlock (&ge->ge_node);
325 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
327 gbp_endpoint_group_unlock (gel->gel_epg);
329 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
331 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
335 static gbp_endpoint_loc_t *
336 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
338 gbp_endpoint_loc_t gel = {
340 .gel_epg = INDEX_INVALID,
341 .gel_sw_if_index = INDEX_INVALID,
346 pos = vec_search_with_function (ge->ge_locs, &gel,
347 gbp_endpoint_loc_is_equal);
351 vec_add1 (ge->ge_locs, gel);
353 if (vec_len (ge->ge_locs) > 1)
355 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
357 pos = vec_search_with_function (ge->ge_locs, &gel,
358 gbp_endpoint_loc_is_equal);
364 * it's the sources and children that lock the endpoints
366 fib_node_lock (&ge->ge_node);
369 return (&ge->ge_locs[pos]);
373 * Find an EP inthe DBs and check that if we find it in the L2 DB
374 * it has the same IPs as this update
377 gbp_endpoint_find_for_update (const ip46_address_t * ips,
378 const gbp_route_domain_t * grd,
379 const mac_address_t * mac,
380 const gbp_bridge_domain_t * gbd,
381 gbp_endpoint_t ** ge)
383 gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
385 l2_ge = l3_ge = NULL;
387 if (NULL != mac && !mac_address_is_zero (mac))
390 l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
392 if (NULL != ips && !ip46_address_is_zero (ips))
394 const ip46_address_t *ip;
395 fib_protocol_t fproto;
398 vec_foreach (ip, ips)
400 fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
402 tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
407 else if (NULL == l3_ge)
408 /* first match against an IP address */
410 else if (tmp == l3_ge)
411 /* another match against IP address that is the same endpoint */
416 * a match agains a different endpoint.
417 * this means the KEY of the EP is changing which is not allowed
424 if (NULL == l2_ge && NULL == l3_ge)
427 else if (NULL == l2_ge)
430 else if (NULL == l3_ge)
435 /* found both L3 and L2 - they must be the same else the KEY
447 static gbp_endpoint_src_t
448 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
450 if (0 == vec_len (ge->ge_locs))
451 return (GBP_ENDPOINT_SRC_MAX);
453 return (ge->ge_locs[0].gel_src);
457 gbp_endpoint_n_learned (int n)
459 gbp_n_learnt_endpoints += n;
461 if (n > 0 && 1 == gbp_n_learnt_endpoints)
463 vlib_process_signal_event (vlib_get_main (),
464 gbp_scanner_node.index,
465 GBP_ENDPOINT_SCAN_START, 0);
467 if (n < 0 && 0 == gbp_n_learnt_endpoints)
469 vlib_process_signal_event (vlib_get_main (),
470 gbp_scanner_node.index,
471 GBP_ENDPOINT_SCAN_STOP, 0);
476 gbp_endpoint_loc_update (gbp_endpoint_loc_t * gel,
477 const gbp_bridge_domain_t * gb,
480 gbp_endpoint_flags_t flags,
481 const ip46_address_t * tun_src,
482 const ip46_address_t * tun_dst)
484 int was_learnt, is_learnt;
487 was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
488 gel->gel_flags = flags;
489 is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
491 gbp_endpoint_n_learned (is_learnt - was_learnt);
493 if (INDEX_INVALID == gel->gel_epg)
496 if (INDEX_INVALID != gel->gel_epg)
498 gbp_endpoint_group_lock (gel->gel_epg);
503 ASSERT (gel->gel_epg == ggi);
506 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
509 ip46_address_copy (&gel->tun.gel_src, tun_src);
511 ip46_address_copy (&gel->tun.gel_dst, tun_dst);
513 if (ip46_address_is_multicast (&gel->tun.gel_src))
516 * we learnt the EP from the multicast tunnel.
517 * Create a unicast TEP from the packet's source
518 * and the fixed address of the BD's parent tunnel
520 const gbp_vxlan_tunnel_t *gt;
522 gt = gbp_vxlan_tunnel_get (gb->gb_vni);
526 ip46_address_copy (&gel->tun.gel_src, >->gt_src);
527 sw_if_index = gt->gt_sw_if_index;
532 * the input interface may be the parent GBP-vxlan interface,
533 * create a child vlxan-gbp tunnel and use that as the endpoint's
536 if (~0 != gel->gel_sw_if_index)
537 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
539 switch (gbp_vxlan_tunnel_get_type (sw_if_index))
541 case GBP_VXLAN_TEMPLATE_TUNNEL:
542 gel->tun.gel_parent_sw_if_index = sw_if_index;
543 gel->gel_sw_if_index =
544 gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
548 case VXLAN_GBP_TUNNEL:
549 gel->tun.gel_parent_sw_if_index =
550 vxlan_gbp_tunnel_get_parent (sw_if_index);
551 gel->gel_sw_if_index = sw_if_index;
552 vxlan_gbp_tunnel_lock (gel->gel_sw_if_index);
558 gel->gel_sw_if_index = sw_if_index;
563 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
565 const gbp_route_domain_t *grd;
566 const gbp_bridge_domain_t *gbd;
567 gbp_endpoint_fwd_t *gef;
568 const fib_prefix_t *pfx;
572 gei = gbp_endpoint_index (ge);
573 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
576 vec_foreach (pfx, ge->ge_key.gek_ips)
580 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
581 fib_index = grd->grd_fib_index[pfx->fp_proto];
583 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
584 &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
587 * remove a host route
589 if (gbp_endpoint_is_remote (ge))
591 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
594 fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
596 vec_foreach (ai, gef->gef_adjs)
601 if (INDEX_INVALID != gef->gef_itf)
603 l2fib_del_entry (ge->ge_key.gek_mac.bytes,
604 gbd->gb_bd_index, gef->gef_itf);
605 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, (L2INPUT_FEAT_NONE));
606 gbp_itf_set_l2_output_feature (gef->gef_itf, gei, L2OUTPUT_FEAT_NONE);
608 gbp_itf_unlock (gef->gef_itf);
609 gef->gef_itf = INDEX_INVALID;
612 vec_free (gef->gef_adjs);
616 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
618 const gbp_route_domain_t *grd;
619 const gbp_bridge_domain_t *gbd;
620 const gbp_endpoint_group_t *gg;
621 gbp_endpoint_loc_t *gel;
622 gbp_endpoint_fwd_t *gef;
623 const fib_prefix_t *pfx;
627 * locations are sort in source priority order
629 gei = gbp_endpoint_index (ge);
630 gel = &ge->ge_locs[0];
632 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
634 gef->gef_flags = gel->gel_flags;
636 if (INDEX_INVALID != gel->gel_epg)
638 gg = gbp_endpoint_group_get (gel->gel_epg);
639 gef->gef_sclass = gg->gg_sclass;
646 gef->gef_itf = gbp_itf_add_and_lock (gel->gel_sw_if_index,
649 if (!mac_address_is_zero (&ge->ge_key.gek_mac))
651 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, L2INPUT_FEAT_GBP_FWD);
653 if (gbp_endpoint_is_remote (ge) || gbp_endpoint_is_external (ge))
656 * bridged packets to external endpoints should be classifed
657 * based on the EP's/BD's EPG
659 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
660 L2OUTPUT_FEAT_GBP_POLICY_MAC);
664 gbp_endpoint_add_itf (gef->gef_itf, gei);
665 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
666 L2OUTPUT_FEAT_GBP_POLICY_PORT);
668 l2fib_add_entry (ge->ge_key.gek_mac.bytes,
670 gef->gef_itf, L2FIB_ENTRY_RESULT_FLAG_STATIC);
673 vec_foreach (pfx, ge->ge_key.gek_ips)
675 ethernet_header_t *eth;
682 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
683 fib_index = grd->grd_fib_index[pfx->fp_proto];
685 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
686 &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
689 * add a host route via the EPG's BVI we need this because the
690 * adj fib does not install, due to cover refinement check, since
691 * the BVI's prefix is /32
693 vec_validate (rewrite, sizeof (*eth) - 1);
694 eth = (ethernet_header_t *) rewrite;
696 eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
700 if (gbp_endpoint_is_remote (ge))
703 * for dynamic EPs we must add the IP adjacency via the learned
704 * tunnel since the BD will not contain the EP's MAC since it was
705 * L3 learned. The dst MAC address used is the 'BD's MAC'.
707 ip_sw_if_index = gef->gef_itf;
709 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
711 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
717 * for the static EPs we add the IP adjacency via the BVI
718 * knowing that the BD has the MAC address to route to and
719 * that policy will be applied on egress to the EP's port
721 ip_sw_if_index = gbd->gb_bvi_sw_if_index;
723 clib_memcpy (eth->src_address,
724 vnet_sw_interface_get_hw_address (vnet_get_main (),
726 sizeof (eth->src_address));
727 mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
730 fib_table_entry_path_add (fib_index, pfx,
731 FIB_SOURCE_PLUGIN_LOW,
733 fib_proto_to_dpo (pfx->fp_proto),
734 &pfx->fp_addr, ip_sw_if_index,
735 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
737 ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
738 fib_proto_to_link (pfx->fp_proto),
740 ip_sw_if_index, rewrite);
741 vec_add1 (gef->gef_adjs, ai);
744 * if the endpoint is external then routed packet to it must be
745 * classifed to the BD's EPG. but this will happen anyway with
746 * the GBP_MAC classification.
751 if (gbp_endpoint_is_remote (ge))
753 dpo_id_t policy_dpo = DPO_INVALID;
756 * interpose a policy DPO from the endpoint so that policy
759 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
760 gg->gg_sclass, ~0, &policy_dpo);
762 fib_table_entry_special_dpo_add (fib_index, pfx,
763 FIB_SOURCE_PLUGIN_HI,
764 FIB_ENTRY_FLAG_INTERPOSE,
766 dpo_reset (&policy_dpo);
770 * send a gratuitous ARP on the EPG's uplink. this is done so
771 * that if this EP has moved from some other place in the
772 * 'fabric', upstream devices are informed
774 if (gbp_endpoint_is_local (ge) && ~0 != gg->gg_uplink_sw_if_index)
776 gbp_endpoint_add_itf (gef->gef_itf, gei);
777 if (FIB_PROTOCOL_IP4 == pfx->fp_proto)
778 send_ip4_garp_w_addr (vlib_get_main (),
780 gg->gg_uplink_sw_if_index);
782 send_ip6_na_w_addr (vlib_get_main (),
784 gg->gg_uplink_sw_if_index);
789 if (gbp_endpoint_is_local (ge) && !gbp_endpoint_is_external (ge))
792 * non-remote endpoints (i.e. those not arriving on iVXLAN
793 * tunnels) need to be classifed based on the the input interface.
794 * We enable the GBP-FWD feature only if the group has an uplink
795 * interface (on which the GBP-FWD feature would send UU traffic).
796 * External endpoints get classified based on an LPM match
798 l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
800 if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
801 feats |= L2INPUT_FEAT_GBP_FWD;
802 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, feats);
806 * update children with the new forwarding info
808 fib_node_back_walk_ctx_t bw_ctx = {
809 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
810 .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
813 fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
817 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
819 const ip46_address_t * ips,
820 const mac_address_t * mac,
821 index_t gbdi, index_t grdi,
823 gbp_endpoint_flags_t flags,
824 const ip46_address_t * tun_src,
825 const ip46_address_t * tun_dst, u32 * handle)
827 gbp_bridge_domain_t *gbd;
828 gbp_endpoint_group_t *gg;
829 gbp_endpoint_src_t best;
830 gbp_route_domain_t *grd;
831 gbp_endpoint_loc_t *gel;
836 if (~0 == sw_if_index)
837 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
843 * we need to determine the bridge-domain, either from the EPG or
846 if (SCLASS_INVALID != sclass)
848 ggi = gbp_endpoint_group_find (sclass);
850 if (INDEX_INVALID == ggi)
851 return (VNET_API_ERROR_NO_SUCH_ENTRY);
853 gg = gbp_endpoint_group_get (ggi);
859 if (INDEX_INVALID == gbdi)
860 return (VNET_API_ERROR_NO_SUCH_ENTRY);
861 if (INDEX_INVALID == grdi)
862 return (VNET_API_ERROR_NO_SUCH_FIB);
866 gbd = gbp_bridge_domain_get (gbdi);
867 grd = gbp_route_domain_get (grdi);
868 rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
875 ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
879 gbp_endpoint_ips_update (ge, ips, grd);
882 best = gbp_endpoint_get_best_src (ge);
883 gei = gbp_endpoint_index (ge);
884 gel = gbp_endpoint_loc_find_or_add (ge, src);
886 gbp_endpoint_loc_update (gel, gbd, sw_if_index, ggi, flags, tun_src,
892 * either the best source has been updated or we have a new best source
894 gbb_endpoint_fwd_reset (ge);
895 gbb_endpoint_fwd_recalc (ge);
900 * an update to a lower priority source, so we need do nothing
907 GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
913 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
915 gbp_endpoint_loc_t *gel, gel_copy;
916 gbp_endpoint_src_t best;
920 if (pool_is_free_index (gbp_endpoint_pool, gei))
923 GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
925 ge = gbp_endpoint_get (gei);
927 gel = gbp_endpoint_loc_find (ge, src);
933 * lock the EP so we can control when it is deleted
935 fib_node_lock (&ge->ge_node);
936 best = gbp_endpoint_get_best_src (ge);
939 * copy the location info since we'll lose it when it's removed from
942 clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
945 * remove the source we no longer need
947 removed = gbp_endpoint_loc_unlock (ge, gel);
952 * we have removed the old best source => recalculate fwding
954 if (0 == vec_len (ge->ge_locs))
957 * if there are no more sources left, then we need only release
958 * the fwding resources held and then this EP is gawn.
960 gbb_endpoint_fwd_reset (ge);
965 * else there are more sources. release the old and get new
968 gbb_endpoint_fwd_reset (ge);
969 gbb_endpoint_fwd_recalc (ge);
974 * we removed a lower priority source so we need to do nothing
978 * clear up any resources held by the source
981 gbp_endpoint_loc_destroy (&gel_copy);
984 * remove the lock taken above
986 fib_node_unlock (&ge->ge_node);
988 * We may have removed the last source and so this EP is now TOAST
989 * DO NOTHING BELOW HERE
994 gbp_endpoint_child_add (index_t gei,
995 fib_node_type_t type, fib_node_index_t index)
997 return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
1001 gbp_endpoint_child_remove (index_t gei, u32 sibling)
1003 return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
1006 typedef struct gbp_endpoint_flush_ctx_t_
1009 gbp_endpoint_src_t src;
1011 } gbp_endpoint_flush_ctx_t;
1014 gbp_endpoint_flush_cb (index_t gei, void *args)
1016 gbp_endpoint_flush_ctx_t *ctx = args;
1017 gbp_endpoint_loc_t *gel;
1020 ge = gbp_endpoint_get (gei);
1021 gel = gbp_endpoint_loc_find (ge, ctx->src);
1023 if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
1025 vec_add1 (ctx->geis, gei);
1028 return (WALK_CONTINUE);
1032 * remove all learnt endpoints using the interface
1035 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
1037 gbp_endpoint_flush_ctx_t ctx = {
1038 .sw_if_index = sw_if_index,
1043 GBP_ENDPOINT_INFO ("flush: %U %U",
1044 format_gbp_endpoint_src, src,
1045 format_vnet_sw_if_index_name, vnet_get_main (),
1047 gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1049 vec_foreach (gei, ctx.geis)
1051 gbp_endpoint_unlock (src, *gei);
1054 vec_free (ctx.geis);
1058 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1063 pool_foreach_index(index, gbp_endpoint_pool,
1065 if (!cb(index, ctx))
1071 static clib_error_t *
1072 gbp_endpoint_cli (vlib_main_t * vm,
1073 unformat_input_t * input, vlib_cli_command_t * cmd)
1075 ip46_address_t ip = ip46_address_initializer, *ips = NULL;
1076 mac_address_t mac = ZERO_MAC_ADDRESS;
1077 vnet_main_t *vnm = vnet_get_main ();
1078 u32 sclass = SCLASS_INVALID;
1079 u32 handle = INDEX_INVALID;
1080 u32 sw_if_index = ~0;
1084 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1086 ip46_address_reset (&ip);
1088 if (unformat (input, "%U", unformat_vnet_sw_interface,
1091 else if (unformat (input, "add"))
1093 else if (unformat (input, "del"))
1095 else if (unformat (input, "sclass %d", &sclass))
1097 else if (unformat (input, "handle %d", &handle))
1099 else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1101 else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1103 else if (unformat (input, "mac %U", unformat_mac_address, &mac))
1111 if (~0 == sw_if_index)
1112 return clib_error_return (0, "interface must be specified");
1113 if (SCLASS_INVALID == sclass)
1114 return clib_error_return (0, "SCLASS must be specified");
1117 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1118 sw_if_index, ips, &mac,
1119 INDEX_INVALID, INDEX_INVALID,
1121 GBP_ENDPOINT_FLAG_NONE,
1122 NULL, NULL, &handle);
1125 return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1127 vlib_cli_output (vm, "handle %d\n", handle);
1131 if (INDEX_INVALID == handle)
1132 return clib_error_return (0, "handle must be specified");
1134 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1143 * Configure a GBP Endpoint
1146 * @cliexstart{set gbp endpoint [del] <interface> epg <ID> ip <IP>}
1150 VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
1151 .path = "gbp endpoint",
1152 .short_help = "gbp endpoint [del] <interface> epg <ID> ip <IP> mac <MAC>",
1153 .function = gbp_endpoint_cli,
1158 format_gbp_endpoint_src (u8 * s, va_list * args)
1160 gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1164 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1165 foreach_gbp_endpoint_src
1169 return (format (s, "unknown"));
1173 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1175 gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
1177 s = format (s, "fwd:");
1178 s = format (s, "\n itf:[%U]", format_gbp_itf, gef->gef_itf);
1179 if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
1181 s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1188 format_gbp_endpoint_key (u8 * s, va_list * args)
1190 gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1191 const fib_prefix_t *pfx;
1193 s = format (s, "ips:[");
1195 vec_foreach (pfx, gek->gek_ips)
1197 s = format (s, "%U, ", format_fib_prefix, pfx);
1199 s = format (s, "]");
1201 s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1207 format_gbp_endpoint_loc (u8 * s, va_list * args)
1209 gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1211 s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
1213 format (s, "\n %U", format_vnet_sw_if_index_name, vnet_get_main (),
1214 gel->gel_sw_if_index);
1215 s = format (s, " EPG:%d", gel->gel_epg);
1217 if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1219 s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1221 if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
1223 s = format (s, " tun:[");
1224 s = format (s, "parent:%U", format_vnet_sw_if_index_name,
1225 vnet_get_main (), gel->tun.gel_parent_sw_if_index);
1226 s = format (s, " {%U,%U}]",
1227 format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
1228 format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
1235 format_gbp_endpoint (u8 * s, va_list * args)
1237 index_t gei = va_arg (*args, index_t);
1238 gbp_endpoint_loc_t *gel;
1241 ge = gbp_endpoint_get (gei);
1243 s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
1244 s = format (s, " last-time:[%f]", ge->ge_last_time);
1246 vec_foreach (gel, ge->ge_locs)
1248 s = format (s, "\n %U", format_gbp_endpoint_loc, gel);
1250 s = format (s, "\n %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1256 gbp_endpoint_show_one (index_t gei, void *ctx)
1261 vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1263 return (WALK_CONTINUE);
1267 gbp_endpoint_walk_ip_itf (const clib_bihash_kv_24_8_t * kvp, void *arg)
1275 gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
1277 vlib_cli_output (vm, " {%U, %U} -> %d",
1278 format_ip46_address, &ip, IP46_TYPE_ANY,
1279 format_vnet_sw_if_index_name, vnet_get_main (),
1280 sw_if_index, kvp->value);
1284 gbp_endpoint_walk_mac_itf (const clib_bihash_kv_16_8_t * kvp, void *arg)
1292 gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
1294 vlib_cli_output (vm, " {%U, %U} -> %d",
1295 format_mac_address_t, &mac,
1296 format_vnet_sw_if_index_name, vnet_get_main (),
1297 sw_if_index, kvp->value);
1300 static clib_error_t *
1301 gbp_endpoint_show (vlib_main_t * vm,
1302 unformat_input_t * input, vlib_cli_command_t * cmd)
1304 u32 show_dbs, handle;
1306 handle = INDEX_INVALID;
1309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1311 if (unformat (input, "%d", &handle))
1313 else if (unformat (input, "db"))
1319 if (INDEX_INVALID != handle)
1321 vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
1325 vlib_cli_output (vm, "\nDatabases:");
1326 clib_bihash_foreach_key_value_pair_24_8 (&gbp_ep_db.ged_by_ip_rd,
1327 gbp_endpoint_walk_ip_itf, vm);
1328 clib_bihash_foreach_key_value_pair_16_8
1329 (&gbp_ep_db.ged_by_mac_bd, gbp_endpoint_walk_mac_itf, vm);
1333 vlib_cli_output (vm, "Endpoints:");
1334 gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1341 * Show Group Based Policy Endpoints and derived information
1344 * @cliexstart{show gbp endpoint}
1348 VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
1349 .path = "show gbp endpoint",
1350 .short_help = "show gbp endpoint\n",
1351 .function = gbp_endpoint_show,
1356 gbp_endpoint_check (index_t gei, f64 start_time)
1358 gbp_endpoint_group_t *gg;
1359 gbp_endpoint_loc_t *gel;
1362 ge = gbp_endpoint_get (gei);
1363 gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1367 gg = gbp_endpoint_group_get (gel->gel_epg);
1369 if ((start_time - ge->ge_last_time) >
1370 gg->gg_retention.remote_ep_timeout)
1372 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1378 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1380 clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1381 f64 last_start, start_time, delta_t;
1385 last_start = start_time = vlib_time_now (vm);
1387 for (i = 0; i < gte_table->nbuckets; i++)
1389 clib_bihash_bucket_16_8_t *b;
1390 clib_bihash_value_16_8_t *v;
1392 /* allow no more than 20us without a pause */
1393 delta_t = vlib_time_now (vm) - last_start;
1394 if (delta_t > 20e-6)
1396 /* suspend for 100 us */
1397 vlib_process_suspend (vm, 100e-6);
1398 last_start = vlib_time_now (vm);
1401 b = >e_table->buckets[i];
1404 v = clib_bihash_get_value_16_8 (gte_table, b->offset);
1406 for (j = 0; j < (1 << b->log2_pages); j++)
1408 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1410 if (clib_bihash_is_free_16_8 (&v->kvp[k]))
1413 gbp_endpoint_check (v->kvp[k].value, start_time);
1416 * Note: we may have just freed the bucket's backing
1417 * storage, so check right here...
1430 gbp_endpoint_scan_l3 (vlib_main_t * vm)
1432 clib_bihash_24_8_t *gte_table = &gbp_ep_db.ged_by_ip_rd;
1433 f64 last_start, start_time, delta_t;
1437 last_start = start_time = vlib_time_now (vm);
1439 for (i = 0; i < gte_table->nbuckets; i++)
1441 clib_bihash_bucket_24_8_t *b;
1442 clib_bihash_value_24_8_t *v;
1444 /* allow no more than 20us without a pause */
1445 delta_t = vlib_time_now (vm) - last_start;
1446 if (delta_t > 20e-6)
1448 /* suspend for 100 us */
1449 vlib_process_suspend (vm, 100e-6);
1450 last_start = vlib_time_now (vm);
1453 b = >e_table->buckets[i];
1456 v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1458 for (j = 0; j < (1 << b->log2_pages); j++)
1460 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1462 if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1465 gbp_endpoint_check (v->kvp[k].value, start_time);
1468 * Note: we may have just freed the bucket's backing
1469 * storage, so check right here...
1482 gbp_endpoint_scan (vlib_main_t * vm)
1484 gbp_endpoint_scan_l2 (vm);
1485 gbp_endpoint_scan_l3 (vm);
1489 gbp_endpoint_get_node (fib_node_index_t index)
1493 ge = gbp_endpoint_get (index);
1495 return (&ge->ge_node);
1498 static gbp_endpoint_t *
1499 gbp_endpoint_from_fib_node (fib_node_t * node)
1501 ASSERT (gbp_endpoint_fib_type == node->fn_type);
1502 return ((gbp_endpoint_t *) node);
1506 gbp_endpoint_last_lock_gone (fib_node_t * node)
1508 const gbp_bridge_domain_t *gbd;
1509 const gbp_route_domain_t *grd;
1510 const fib_prefix_t *pfx;
1513 ge = gbp_endpoint_from_fib_node (node);
1515 ASSERT (0 == vec_len (ge->ge_locs));
1517 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1520 * we have removed the last source. this EP is toast
1522 if (INDEX_INVALID != ge->ge_key.gek_gbd)
1524 gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1526 vec_foreach (pfx, ge->ge_key.gek_ips)
1528 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
1529 gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
1531 pool_put (gbp_endpoint_pool, ge);
1534 static fib_node_back_walk_rc_t
1535 gbp_endpoint_back_walk_notify (fib_node_t * node,
1536 fib_node_back_walk_ctx_t * ctx)
1540 return (FIB_NODE_BACK_WALK_CONTINUE);
1544 * The FIB path's graph node virtual function table
1546 static const fib_node_vft_t gbp_endpoint_vft = {
1547 .fnv_get = gbp_endpoint_get_node,
1548 .fnv_last_lock = gbp_endpoint_last_lock_gone,
1549 .fnv_back_walk = gbp_endpoint_back_walk_notify,
1550 // .fnv_mem_show = fib_path_memory_show,
1553 static clib_error_t *
1554 gbp_endpoint_init (vlib_main_t * vm)
1556 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1557 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
1559 clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
1560 "GBP Endpoints - IP/RD",
1561 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1563 clib_bihash_init_16_8 (&gbp_ep_db.ged_by_mac_bd,
1564 "GBP Endpoints - MAC/BD",
1565 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1567 gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1568 gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1573 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1576 * fd.io coding-style-patch-verification: ON
1579 * eval: (c-set-style "gnu")