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_is_local (const gbp_endpoint_t * ge)
101 return (!(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE));
105 gbp_endpoint_is_external (const gbp_endpoint_t * ge)
107 return (! !(ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_EXTERNAL));
111 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
112 mac_address_t * mac, u32 * sw_if_index)
114 mac_address_from_u64 (key->key[0], mac);
115 *sw_if_index = key->key[1];
119 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
120 ip46_address_t * ip, u32 * sw_if_index)
122 ip->as_u64[0] = key->key[0];
123 ip->as_u64[1] = key->key[1];
124 *sw_if_index = key->key[2];
128 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
130 clib_bihash_kv_24_8_t key, value;
133 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
135 rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
140 return (gbp_endpoint_get (value.value));
144 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
146 vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
148 gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
152 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
154 clib_bihash_kv_16_8_t key;
157 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
160 rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
167 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
169 clib_bihash_kv_24_8_t key;
172 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
175 rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
181 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
183 clib_bihash_kv_16_8_t key;
185 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
187 clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
191 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
193 clib_bihash_kv_24_8_t key;
195 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
197 clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
201 gbp_endpoint_index (const gbp_endpoint_t * ge)
203 return (ge - gbp_endpoint_pool);
207 ip46_address_get_type (const ip46_address_t * a)
209 return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
213 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
215 return (ip46_address_is_equal (ip, &fp->fp_addr));
219 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
220 const ip46_address_t * ips,
221 const gbp_route_domain_t * grd)
223 const ip46_address_t *ip;
226 gei = gbp_endpoint_index (ge);
227 grdi = gbp_route_domain_index (grd);
229 ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
230 (ge->ge_key.gek_grd == grdi));
232 vec_foreach (ip, ips)
234 if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
235 gbp_endpoint_ip_is_equal))
239 vec_add2 (ge->ge_key.gek_ips, pfx, 1);
240 fib_prefix_from_ip46_addr (ip, pfx);
242 gbp_endpoint_add_ip (&pfx->fp_addr,
243 grd->grd_fib_index[pfx->fp_proto], gei);
245 ge->ge_key.gek_grd = grdi;
249 static gbp_endpoint_t *
250 gbp_endpoint_alloc (const ip46_address_t * ips,
251 const gbp_route_domain_t * grd,
252 const mac_address_t * mac,
253 const gbp_bridge_domain_t * gbd)
258 pool_get_zero (gbp_endpoint_pool, ge);
260 fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
261 gei = gbp_endpoint_index (ge);
263 ge->ge_key.gek_grd = ge->ge_fwd.gef_itf = INDEX_INVALID;
264 ge->ge_last_time = vlib_time_now (vlib_get_main ());
265 ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
269 mac_address_copy (&ge->ge_key.gek_mac, mac);
270 gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
272 gbp_endpoint_ips_update (ge, ips, grd);
278 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
280 return (a->gel_src == b->gel_src);
284 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
286 return (a->gel_src - b->gel_src);
289 static gbp_endpoint_loc_t *
290 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
292 gbp_endpoint_loc_t gel = {
297 pos = vec_search_with_function (ge->ge_locs, &gel,
298 gbp_endpoint_loc_is_equal);
301 return (&ge->ge_locs[pos]);
307 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
313 if (0 == gel->gel_locks)
315 pos = gel - ge->ge_locs;
317 vec_del1 (ge->ge_locs, pos);
318 if (vec_len (ge->ge_locs) > 1)
319 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
321 /* This could be the last lock, so don't access the EP from
323 fib_node_unlock (&ge->ge_node);
331 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
333 gbp_endpoint_group_unlock (gel->gel_epg);
335 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
337 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
341 static gbp_endpoint_loc_t *
342 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
344 gbp_endpoint_loc_t gel = {
346 .gel_epg = INDEX_INVALID,
347 .gel_sw_if_index = INDEX_INVALID,
352 pos = vec_search_with_function (ge->ge_locs, &gel,
353 gbp_endpoint_loc_is_equal);
357 vec_add1 (ge->ge_locs, gel);
359 if (vec_len (ge->ge_locs) > 1)
361 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
363 pos = vec_search_with_function (ge->ge_locs, &gel,
364 gbp_endpoint_loc_is_equal);
370 * it's the sources and children that lock the endpoints
372 fib_node_lock (&ge->ge_node);
375 return (&ge->ge_locs[pos]);
379 * Find an EP inthe DBs and check that if we find it in the L2 DB
380 * it has the same IPs as this update
383 gbp_endpoint_find_for_update (const ip46_address_t * ips,
384 const gbp_route_domain_t * grd,
385 const mac_address_t * mac,
386 const gbp_bridge_domain_t * gbd,
387 gbp_endpoint_t ** ge)
389 gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
391 l2_ge = l3_ge = NULL;
393 if (NULL != mac && !mac_address_is_zero (mac))
396 l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
398 if (NULL != ips && !ip46_address_is_zero (ips))
400 const ip46_address_t *ip;
401 fib_protocol_t fproto;
404 vec_foreach (ip, ips)
406 fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
408 tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
413 else if (NULL == l3_ge)
414 /* first match against an IP address */
416 else if (tmp == l3_ge)
417 /* another match against IP address that is the same endpoint */
422 * a match agains a different endpoint.
423 * this means the KEY of the EP is changing which is not allowed
430 if (NULL == l2_ge && NULL == l3_ge)
433 else if (NULL == l2_ge)
436 else if (NULL == l3_ge)
441 /* found both L3 and L2 - they must be the same else the KEY
453 static gbp_endpoint_src_t
454 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
456 if (0 == vec_len (ge->ge_locs))
457 return (GBP_ENDPOINT_SRC_MAX);
459 return (ge->ge_locs[0].gel_src);
463 gbp_endpoint_n_learned (int n)
465 gbp_n_learnt_endpoints += n;
467 if (n > 0 && 1 == gbp_n_learnt_endpoints)
469 vlib_process_signal_event (vlib_get_main (),
470 gbp_scanner_node.index,
471 GBP_ENDPOINT_SCAN_START, 0);
473 if (n < 0 && 0 == gbp_n_learnt_endpoints)
475 vlib_process_signal_event (vlib_get_main (),
476 gbp_scanner_node.index,
477 GBP_ENDPOINT_SCAN_STOP, 0);
482 gbp_endpoint_loc_update (gbp_endpoint_loc_t * gel,
485 gbp_endpoint_flags_t flags,
486 const ip46_address_t * tun_src,
487 const ip46_address_t * tun_dst)
489 int was_learnt, is_learnt;
492 was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
493 gel->gel_flags = flags;
494 is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
496 gbp_endpoint_n_learned (is_learnt - was_learnt);
498 if (INDEX_INVALID == gel->gel_epg)
501 if (INDEX_INVALID != gel->gel_epg)
503 gbp_endpoint_group_lock (gel->gel_epg);
508 ASSERT (gel->gel_epg == ggi);
511 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
514 ip46_address_copy (&gel->tun.gel_src, tun_src);
516 ip46_address_copy (&gel->tun.gel_dst, tun_dst);
519 * the input interface may be the parent GBP-vxlan interface,
520 * create a child vlxan-gbp tunnel and use that as the endpoint's
523 if (~0 != gel->gel_sw_if_index)
524 vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
526 switch (gbp_vxlan_tunnel_get_type (sw_if_index))
528 case GBP_VXLAN_TEMPLATE_TUNNEL:
529 gel->tun.gel_parent_sw_if_index = sw_if_index;
530 gel->gel_sw_if_index =
531 gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
535 case VXLAN_GBP_TUNNEL:
536 gel->tun.gel_parent_sw_if_index =
537 vxlan_gbp_tunnel_get_parent (sw_if_index);
538 gel->gel_sw_if_index = sw_if_index;
539 vxlan_gbp_tunnel_lock (gel->gel_sw_if_index);
545 gel->gel_sw_if_index = sw_if_index;
550 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
552 const gbp_route_domain_t *grd;
553 const gbp_bridge_domain_t *gbd;
554 gbp_endpoint_fwd_t *gef;
555 const fib_prefix_t *pfx;
559 gei = gbp_endpoint_index (ge);
560 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
563 vec_foreach (pfx, ge->ge_key.gek_ips)
567 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
568 fib_index = grd->grd_fib_index[pfx->fp_proto];
570 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
571 &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
574 * remove a host route
576 if (gbp_endpoint_is_remote (ge))
578 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
581 fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
583 vec_foreach (ai, gef->gef_adjs)
588 if (INDEX_INVALID != gef->gef_itf)
590 l2fib_del_entry (ge->ge_key.gek_mac.bytes,
591 gbd->gb_bd_index, gef->gef_itf);
592 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, (L2INPUT_FEAT_NONE));
593 gbp_itf_set_l2_output_feature (gef->gef_itf, gei, L2OUTPUT_FEAT_NONE);
595 gbp_itf_unlock (gef->gef_itf);
596 gef->gef_itf = INDEX_INVALID;
599 vec_free (gef->gef_adjs);
603 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
605 const gbp_route_domain_t *grd;
606 const gbp_bridge_domain_t *gbd;
607 const gbp_endpoint_group_t *gg;
608 gbp_endpoint_loc_t *gel;
609 gbp_endpoint_fwd_t *gef;
610 const fib_prefix_t *pfx;
614 * locations are sort in source priority order
616 gei = gbp_endpoint_index (ge);
617 gel = &ge->ge_locs[0];
619 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
621 gef->gef_flags = gel->gel_flags;
623 if (INDEX_INVALID != gel->gel_epg)
625 gg = gbp_endpoint_group_get (gel->gel_epg);
626 gef->gef_epg_id = gg->gg_id;
633 gef->gef_itf = gbp_itf_add_and_lock (gel->gel_sw_if_index,
636 if (!mac_address_is_zero (&ge->ge_key.gek_mac))
638 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, L2INPUT_FEAT_GBP_FWD);
640 if (gbp_endpoint_is_remote (ge) || gbp_endpoint_is_external (ge))
643 * bridged packets to external endpoints should be classifed
644 * based on the EP's/BD's EPG
646 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
647 L2OUTPUT_FEAT_GBP_POLICY_MAC);
651 gbp_endpoint_add_itf (gef->gef_itf, gei);
652 gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
653 L2OUTPUT_FEAT_GBP_POLICY_PORT);
655 l2fib_add_entry (ge->ge_key.gek_mac.bytes,
657 gef->gef_itf, L2FIB_ENTRY_RESULT_FLAG_STATIC);
660 vec_foreach (pfx, ge->ge_key.gek_ips)
662 ethernet_header_t *eth;
669 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
670 fib_index = grd->grd_fib_index[pfx->fp_proto];
672 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
673 &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
676 * add a host route via the EPG's BVI we need this because the
677 * adj fib does not install, due to cover refinement check, since
678 * the BVI's prefix is /32
680 vec_validate (rewrite, sizeof (*eth) - 1);
681 eth = (ethernet_header_t *) rewrite;
683 eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
687 if (gbp_endpoint_is_remote (ge))
690 * for dynamic EPs we must add the IP adjacency via the learned
691 * tunnel since the BD will not contain the EP's MAC since it was
692 * L3 learned. The dst MAC address used is the 'BD's MAC'.
694 ip_sw_if_index = gef->gef_itf;
696 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
698 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
704 * for the static EPs we add the IP adjacency via the BVI
705 * knowing that the BD has the MAC address to route to and
706 * that policy will be applied on egress to the EP's port
708 ip_sw_if_index = gbd->gb_bvi_sw_if_index;
710 clib_memcpy (eth->src_address,
711 vnet_sw_interface_get_hw_address (vnet_get_main (),
713 sizeof (eth->src_address));
714 mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
717 fib_table_entry_path_add (fib_index, pfx,
718 FIB_SOURCE_PLUGIN_LOW,
720 fib_proto_to_dpo (pfx->fp_proto),
721 &pfx->fp_addr, ip_sw_if_index,
722 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
724 ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
725 fib_proto_to_link (pfx->fp_proto),
727 ip_sw_if_index, rewrite);
728 vec_add1 (gef->gef_adjs, ai);
731 * if the endpoint is external then routed packet to it must be
732 * classifed to the BD's EPG. but this will happen anyway with
733 * the GBP_MAC classification.
738 if (gbp_endpoint_is_remote (ge))
740 dpo_id_t policy_dpo = DPO_INVALID;
743 * interpose a policy DPO from the endpoint so that policy
746 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
747 gg->gg_id, ~0, &policy_dpo);
749 fib_table_entry_special_dpo_add (fib_index, pfx,
750 FIB_SOURCE_PLUGIN_HI,
751 FIB_ENTRY_FLAG_INTERPOSE,
753 dpo_reset (&policy_dpo);
757 * send a gratuitous ARP on the EPG's uplink. this is done so
758 * that if this EP has moved from some other place in the
759 * 'fabric', upstream devices are informed
761 if (gbp_endpoint_is_local (ge) && ~0 != gg->gg_uplink_sw_if_index)
763 gbp_endpoint_add_itf (gef->gef_itf, gei);
764 if (FIB_PROTOCOL_IP4 == pfx->fp_proto)
765 send_ip4_garp_w_addr (vlib_get_main (),
767 gg->gg_uplink_sw_if_index);
769 send_ip6_na_w_addr (vlib_get_main (),
771 gg->gg_uplink_sw_if_index);
776 if (gbp_endpoint_is_local (ge) && !gbp_endpoint_is_external (ge))
779 * non-remote endpoints (i.e. those not arriving on iVXLAN
780 * tunnels) need to be classifed based on the the input interface.
781 * We enable the GBP-FWD feature only if the group has an uplink
782 * interface (on which the GBP-FWD feature would send UU traffic).
783 * External endpoints get classified based on an LPM match
785 l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
787 if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
788 feats |= L2INPUT_FEAT_GBP_FWD;
789 gbp_itf_set_l2_input_feature (gef->gef_itf, gei, feats);
793 * update children with the new forwarding info
795 fib_node_back_walk_ctx_t bw_ctx = {
796 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
797 .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
800 fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
804 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
806 const ip46_address_t * ips,
807 const mac_address_t * mac,
808 index_t gbdi, index_t grdi, epg_id_t epg_id,
809 gbp_endpoint_flags_t flags,
810 const ip46_address_t * tun_src,
811 const ip46_address_t * tun_dst, u32 * handle)
813 gbp_bridge_domain_t *gbd;
814 gbp_endpoint_group_t *gg;
815 gbp_endpoint_src_t best;
816 gbp_route_domain_t *grd;
817 gbp_endpoint_loc_t *gel;
822 if (~0 == sw_if_index)
823 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
829 * we need to determine the bridge-domain, either from the EPG or
832 if (EPG_INVALID != epg_id)
834 ggi = gbp_endpoint_group_find (epg_id);
836 if (INDEX_INVALID == ggi)
837 return (VNET_API_ERROR_NO_SUCH_ENTRY);
839 gg = gbp_endpoint_group_get (ggi);
845 if (INDEX_INVALID == gbdi)
846 return (VNET_API_ERROR_NO_SUCH_ENTRY);
847 if (INDEX_INVALID == grdi)
848 return (VNET_API_ERROR_NO_SUCH_FIB);
852 gbd = gbp_bridge_domain_get (gbdi);
853 grd = gbp_route_domain_get (grdi);
854 rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
861 ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
865 gbp_endpoint_ips_update (ge, ips, grd);
868 best = gbp_endpoint_get_best_src (ge);
869 gei = gbp_endpoint_index (ge);
870 gel = gbp_endpoint_loc_find_or_add (ge, src);
872 gbp_endpoint_loc_update (gel, sw_if_index, ggi, flags, tun_src, tun_dst);
877 * either the best source has been updated or we have a new best source
879 gbb_endpoint_fwd_reset (ge);
880 gbb_endpoint_fwd_recalc (ge);
885 * an update to a lower priority source, so we need do nothing
892 GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
898 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
900 gbp_endpoint_loc_t *gel, gel_copy;
901 gbp_endpoint_src_t best;
905 if (pool_is_free_index (gbp_endpoint_pool, gei))
908 GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
910 ge = gbp_endpoint_get (gei);
912 gel = gbp_endpoint_loc_find (ge, src);
918 * lock the EP so we can control when it is deleted
920 fib_node_lock (&ge->ge_node);
921 best = gbp_endpoint_get_best_src (ge);
924 * copy the location info since we'll lose it when it's removed from
927 clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
930 * remove the source we no longer need
932 removed = gbp_endpoint_loc_unlock (ge, gel);
937 * we have removed the old best source => recalculate fwding
939 if (0 == vec_len (ge->ge_locs))
942 * if there are no more sources left, then we need only release
943 * the fwding resources held and then this EP is gawn.
945 gbb_endpoint_fwd_reset (ge);
950 * else there are more sources. release the old and get new
953 gbb_endpoint_fwd_reset (ge);
954 gbb_endpoint_fwd_recalc (ge);
959 * we removed a lower priority source so we need to do nothing
963 * clear up any resources held by the source
966 gbp_endpoint_loc_destroy (&gel_copy);
969 * remove the lock taken above
971 fib_node_unlock (&ge->ge_node);
973 * We may have removed the last source and so this EP is now TOAST
974 * DO NOTHING BELOW HERE
979 gbp_endpoint_child_add (index_t gei,
980 fib_node_type_t type, fib_node_index_t index)
982 return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
986 gbp_endpoint_child_remove (index_t gei, u32 sibling)
988 return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
991 typedef struct gbp_endpoint_flush_ctx_t_
994 gbp_endpoint_src_t src;
996 } gbp_endpoint_flush_ctx_t;
999 gbp_endpoint_flush_cb (index_t gei, void *args)
1001 gbp_endpoint_flush_ctx_t *ctx = args;
1002 gbp_endpoint_loc_t *gel;
1005 ge = gbp_endpoint_get (gei);
1006 gel = gbp_endpoint_loc_find (ge, ctx->src);
1008 if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
1010 vec_add1 (ctx->geis, gei);
1013 return (WALK_CONTINUE);
1017 * remove all learnt endpoints using the interface
1020 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
1022 gbp_endpoint_flush_ctx_t ctx = {
1023 .sw_if_index = sw_if_index,
1028 GBP_ENDPOINT_INFO ("flush: %U %U",
1029 format_gbp_endpoint_src, src,
1030 format_vnet_sw_if_index_name, vnet_get_main (),
1032 gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1034 vec_foreach (gei, ctx.geis)
1036 gbp_endpoint_unlock (src, *gei);
1039 vec_free (ctx.geis);
1043 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1048 pool_foreach_index(index, gbp_endpoint_pool,
1050 if (!cb(index, ctx))
1056 static clib_error_t *
1057 gbp_endpoint_cli (vlib_main_t * vm,
1058 unformat_input_t * input, vlib_cli_command_t * cmd)
1060 ip46_address_t ip = ip46_address_initializer, *ips = NULL;
1061 mac_address_t mac = ZERO_MAC_ADDRESS;
1062 vnet_main_t *vnm = vnet_get_main ();
1063 u32 epg_id = EPG_INVALID;
1064 u32 handle = INDEX_INVALID;
1065 u32 sw_if_index = ~0;
1069 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1071 ip46_address_reset (&ip);
1073 if (unformat (input, "%U", unformat_vnet_sw_interface,
1076 else if (unformat (input, "add"))
1078 else if (unformat (input, "del"))
1080 else if (unformat (input, "epg %d", &epg_id))
1082 else if (unformat (input, "handle %d", &handle))
1084 else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1086 else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1088 else if (unformat (input, "mac %U", unformat_mac_address, &mac))
1096 if (~0 == sw_if_index)
1097 return clib_error_return (0, "interface must be specified");
1098 if (EPG_INVALID == epg_id)
1099 return clib_error_return (0, "EPG-ID must be specified");
1102 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1103 sw_if_index, ips, &mac,
1104 INDEX_INVALID, INDEX_INVALID,
1106 GBP_ENDPOINT_FLAG_NONE,
1107 NULL, NULL, &handle);
1110 return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1112 vlib_cli_output (vm, "handle %d\n", handle);
1116 if (INDEX_INVALID == handle)
1117 return clib_error_return (0, "handle must be specified");
1119 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1128 * Configure a GBP Endpoint
1131 * @cliexstart{set gbp endpoint [del] <interface> epg <ID> ip <IP>}
1135 VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
1136 .path = "gbp endpoint",
1137 .short_help = "gbp endpoint [del] <interface> epg <ID> ip <IP> mac <MAC>",
1138 .function = gbp_endpoint_cli,
1143 format_gbp_endpoint_src (u8 * s, va_list * args)
1145 gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1149 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1150 foreach_gbp_endpoint_src
1154 return (format (s, "unknown"));
1158 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1160 gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
1162 s = format (s, "fwd:");
1163 s = format (s, "\n itf:[%U]", format_gbp_itf, gef->gef_itf);
1164 if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
1166 s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1173 format_gbp_endpoint_key (u8 * s, va_list * args)
1175 gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1176 const fib_prefix_t *pfx;
1178 s = format (s, "ips:[");
1180 vec_foreach (pfx, gek->gek_ips)
1182 s = format (s, "%U, ", format_fib_prefix, pfx);
1184 s = format (s, "]");
1186 s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1192 format_gbp_endpoint_loc (u8 * s, va_list * args)
1194 gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1196 s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
1198 format (s, "\n %U", format_vnet_sw_if_index_name, vnet_get_main (),
1199 gel->gel_sw_if_index);
1200 s = format (s, " EPG:%d", gel->gel_epg);
1202 if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1204 s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1206 if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
1208 s = format (s, " tun:[");
1209 s = format (s, "parent:%U", format_vnet_sw_if_index_name,
1210 vnet_get_main (), gel->tun.gel_parent_sw_if_index);
1211 s = format (s, " {%U,%U}]",
1212 format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
1213 format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
1220 format_gbp_endpoint (u8 * s, va_list * args)
1222 index_t gei = va_arg (*args, index_t);
1223 gbp_endpoint_loc_t *gel;
1226 ge = gbp_endpoint_get (gei);
1228 s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
1229 s = format (s, " last-time:[%f]", ge->ge_last_time);
1231 vec_foreach (gel, ge->ge_locs)
1233 s = format (s, "\n %U", format_gbp_endpoint_loc, gel);
1235 s = format (s, "\n %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1241 gbp_endpoint_show_one (index_t gei, void *ctx)
1246 vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1248 return (WALK_CONTINUE);
1252 gbp_endpoint_walk_ip_itf (const clib_bihash_kv_24_8_t * kvp, void *arg)
1260 gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
1262 vlib_cli_output (vm, " {%U, %U} -> %d",
1263 format_ip46_address, &ip, IP46_TYPE_ANY,
1264 format_vnet_sw_if_index_name, vnet_get_main (),
1265 sw_if_index, kvp->value);
1269 gbp_endpoint_walk_mac_itf (const clib_bihash_kv_16_8_t * kvp, void *arg)
1277 gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
1279 vlib_cli_output (vm, " {%U, %U} -> %d",
1280 format_mac_address_t, &mac,
1281 format_vnet_sw_if_index_name, vnet_get_main (),
1282 sw_if_index, kvp->value);
1285 static clib_error_t *
1286 gbp_endpoint_show (vlib_main_t * vm,
1287 unformat_input_t * input, vlib_cli_command_t * cmd)
1289 u32 show_dbs, handle;
1291 handle = INDEX_INVALID;
1294 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1296 if (unformat (input, "%d", &handle))
1298 else if (unformat (input, "db"))
1304 if (INDEX_INVALID != handle)
1306 vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
1310 vlib_cli_output (vm, "\nDatabases:");
1311 clib_bihash_foreach_key_value_pair_24_8 (&gbp_ep_db.ged_by_ip_rd,
1312 gbp_endpoint_walk_ip_itf, vm);
1313 clib_bihash_foreach_key_value_pair_16_8
1314 (&gbp_ep_db.ged_by_mac_bd, gbp_endpoint_walk_mac_itf, vm);
1318 vlib_cli_output (vm, "Endpoints:");
1319 gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1326 * Show Group Based Policy Endpoints and derived information
1329 * @cliexstart{show gbp endpoint}
1333 VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
1334 .path = "show gbp endpoint",
1335 .short_help = "show gbp endpoint\n",
1336 .function = gbp_endpoint_show,
1341 gbp_endpoint_check (index_t gei, f64 start_time)
1343 gbp_endpoint_loc_t *gel;
1346 ge = gbp_endpoint_get (gei);
1347 gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1349 if ((NULL != gel) &&
1350 ((start_time - ge->ge_last_time) > GBP_ENDPOINT_INACTIVE_TIME))
1352 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1357 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1359 clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1360 f64 last_start, start_time, delta_t;
1364 last_start = start_time = vlib_time_now (vm);
1366 for (i = 0; i < gte_table->nbuckets; i++)
1368 clib_bihash_bucket_16_8_t *b;
1369 clib_bihash_value_16_8_t *v;
1371 /* allow no more than 20us without a pause */
1372 delta_t = vlib_time_now (vm) - last_start;
1373 if (delta_t > 20e-6)
1375 /* suspend for 100 us */
1376 vlib_process_suspend (vm, 100e-6);
1377 last_start = vlib_time_now (vm);
1380 b = >e_table->buckets[i];
1383 v = clib_bihash_get_value_16_8 (gte_table, b->offset);
1385 for (j = 0; j < (1 << b->log2_pages); j++)
1387 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1389 if (clib_bihash_is_free_16_8 (&v->kvp[k]))
1392 gbp_endpoint_check (v->kvp[k].value, start_time);
1395 * Note: we may have just freed the bucket's backing
1396 * storage, so check right here...
1409 gbp_endpoint_scan_l3 (vlib_main_t * vm)
1411 clib_bihash_24_8_t *gte_table = &gbp_ep_db.ged_by_ip_rd;
1412 f64 last_start, start_time, delta_t;
1416 last_start = start_time = vlib_time_now (vm);
1418 for (i = 0; i < gte_table->nbuckets; i++)
1420 clib_bihash_bucket_24_8_t *b;
1421 clib_bihash_value_24_8_t *v;
1423 /* allow no more than 20us without a pause */
1424 delta_t = vlib_time_now (vm) - last_start;
1425 if (delta_t > 20e-6)
1427 /* suspend for 100 us */
1428 vlib_process_suspend (vm, 100e-6);
1429 last_start = vlib_time_now (vm);
1432 b = >e_table->buckets[i];
1435 v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1437 for (j = 0; j < (1 << b->log2_pages); j++)
1439 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1441 if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1444 gbp_endpoint_check (v->kvp[k].value, start_time);
1447 * Note: we may have just freed the bucket's backing
1448 * storage, so check right here...
1461 gbp_endpoint_scan (vlib_main_t * vm)
1463 gbp_endpoint_scan_l2 (vm);
1464 gbp_endpoint_scan_l3 (vm);
1468 gbp_learn_set_inactive_threshold (u32 threshold)
1470 GBP_ENDPOINT_INACTIVE_TIME = threshold;
1472 vlib_process_signal_event (vlib_get_main (),
1473 gbp_scanner_node.index,
1474 GBP_ENDPOINT_SCAN_SET_TIME, 0);
1478 gbp_endpoint_scan_threshold (void)
1480 return (GBP_ENDPOINT_INACTIVE_TIME);
1484 gbp_endpoint_get_node (fib_node_index_t index)
1488 ge = gbp_endpoint_get (index);
1490 return (&ge->ge_node);
1493 static gbp_endpoint_t *
1494 gbp_endpoint_from_fib_node (fib_node_t * node)
1496 ASSERT (gbp_endpoint_fib_type == node->fn_type);
1497 return ((gbp_endpoint_t *) node);
1501 gbp_endpoint_last_lock_gone (fib_node_t * node)
1503 const gbp_bridge_domain_t *gbd;
1504 const gbp_route_domain_t *grd;
1505 const fib_prefix_t *pfx;
1508 ge = gbp_endpoint_from_fib_node (node);
1510 ASSERT (0 == vec_len (ge->ge_locs));
1512 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1515 * we have removed the last source. this EP is toast
1517 if (INDEX_INVALID != ge->ge_key.gek_gbd)
1519 gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1521 vec_foreach (pfx, ge->ge_key.gek_ips)
1523 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
1524 gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
1526 pool_put (gbp_endpoint_pool, ge);
1529 static fib_node_back_walk_rc_t
1530 gbp_endpoint_back_walk_notify (fib_node_t * node,
1531 fib_node_back_walk_ctx_t * ctx)
1535 return (FIB_NODE_BACK_WALK_CONTINUE);
1539 * The FIB path's graph node virtual function table
1541 static const fib_node_vft_t gbp_endpoint_vft = {
1542 .fnv_get = gbp_endpoint_get_node,
1543 .fnv_last_lock = gbp_endpoint_last_lock_gone,
1544 .fnv_back_walk = gbp_endpoint_back_walk_notify,
1545 // .fnv_mem_show = fib_path_memory_show,
1548 static clib_error_t *
1549 gbp_endpoint_init (vlib_main_t * vm)
1551 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1552 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
1554 clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
1555 "GBP Endpoints - IP/RD",
1556 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1558 clib_bihash_init_16_8 (&gbp_ep_db.ged_by_mac_bd,
1559 "GBP Endpoints - MAC/BD",
1560 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1562 gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1563 gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1568 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1571 * fd.io coding-style-patch-verification: ON
1574 * eval: (c-set-style "gnu")