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/l2/l2_input.h>
28 #include <vnet/l2/l2_output.h>
29 #include <vnet/l2/feat_bitmap.h>
30 #include <vnet/l2/l2_fib.h>
31 #include <vnet/fib/fib_table.h>
32 #include <vnet/ip-neighbor/ip_neighbor.h>
33 #include <vnet/fib/fib_walk.h>
34 #include <vnet/vxlan-gbp/vxlan_gbp.h>
36 static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
41 gbp_ep_db_t gbp_ep_db;
43 static fib_source_t gbp_fib_source_hi;
44 static fib_source_t gbp_fib_source_low;
45 static fib_node_type_t gbp_endpoint_fib_type;
46 static 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_is_learnt (const gbp_endpoint_t * ge)
107 if (0 == vec_len (ge->ge_locs))
110 /* DP is the highest source so if present it will be first */
111 return (ge->ge_locs[0].gel_src == GBP_ENDPOINT_SRC_DP);
115 gbp_endpoint_extract_key_mac_itf (const clib_bihash_kv_16_8_t * key,
116 mac_address_t * mac, u32 * sw_if_index)
118 mac_address_from_u64 (mac, key->key[0]);
119 *sw_if_index = key->key[1];
123 gbp_endpoint_extract_key_ip_itf (const clib_bihash_kv_24_8_t * key,
124 ip46_address_t * ip, u32 * sw_if_index)
126 ip->as_u64[0] = key->key[0];
127 ip->as_u64[1] = key->key[1];
128 *sw_if_index = key->key[2];
132 gbp_endpoint_find_ip (const ip46_address_t * ip, u32 fib_index)
134 clib_bihash_kv_24_8_t key, value;
137 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
139 rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
144 return (gbp_endpoint_get (value.value));
148 gbp_endpoint_add_itf (u32 sw_if_index, index_t gei)
150 vec_validate_init_empty (gbp_ep_db.ged_by_sw_if_index, sw_if_index, ~0);
152 gbp_ep_db.ged_by_sw_if_index[sw_if_index] = gei;
156 gbp_endpoint_add_mac (const mac_address_t * mac, u32 bd_index, index_t gei)
158 clib_bihash_kv_16_8_t key;
161 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
164 rv = clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 1);
171 gbp_endpoint_add_ip (const ip46_address_t * ip, u32 fib_index, index_t gei)
173 clib_bihash_kv_24_8_t key;
176 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
179 rv = clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 1);
185 gbp_endpoint_del_mac (const mac_address_t * mac, u32 bd_index)
187 clib_bihash_kv_16_8_t key;
189 gbp_endpoint_mk_key_mac (mac->bytes, bd_index, &key);
191 clib_bihash_add_del_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, 0);
195 gbp_endpoint_del_ip (const ip46_address_t * ip, u32 fib_index)
197 clib_bihash_kv_24_8_t key;
199 gbp_endpoint_mk_key_ip (ip, fib_index, &key);
201 clib_bihash_add_del_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, 0);
205 gbp_endpoint_index (const gbp_endpoint_t * ge)
207 return (ge - gbp_endpoint_pool);
211 gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
213 return (ip46_address_is_equal (ip, &fp->fp_addr));
217 gbp_endpoint_ips_update (gbp_endpoint_t * ge,
218 const ip46_address_t * ips,
219 const gbp_route_domain_t * grd)
221 const ip46_address_t *ip;
224 gei = gbp_endpoint_index (ge);
225 grdi = gbp_route_domain_index (grd);
227 ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
228 (ge->ge_key.gek_grd == grdi));
230 vec_foreach (ip, ips)
232 if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
233 gbp_endpoint_ip_is_equal))
237 vec_add2 (ge->ge_key.gek_ips, pfx, 1);
238 fib_prefix_from_ip46_addr (ip, pfx);
240 gbp_endpoint_add_ip (&pfx->fp_addr,
241 grd->grd_fib_index[pfx->fp_proto], gei);
243 ge->ge_key.gek_grd = grdi;
247 static gbp_endpoint_t *
248 gbp_endpoint_alloc (const ip46_address_t * ips,
249 const gbp_route_domain_t * grd,
250 const mac_address_t * mac,
251 const gbp_bridge_domain_t * gbd)
256 pool_get_zero (gbp_endpoint_pool, ge);
258 fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
259 gei = gbp_endpoint_index (ge);
261 ge->ge_key.gek_grd = ge->ge_fwd.gef_fib_index = INDEX_INVALID;
262 gbp_itf_hdl_reset (&ge->ge_fwd.gef_itf);
263 ge->ge_last_time = vlib_time_now (vlib_get_main ());
264 ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
268 mac_address_copy (&ge->ge_key.gek_mac, mac);
269 gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
271 gbp_endpoint_ips_update (ge, ips, grd);
277 gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
279 return (a->gel_src == b->gel_src);
283 gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
285 return (a->gel_src - b->gel_src);
288 static gbp_endpoint_loc_t *
289 gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
291 gbp_endpoint_loc_t gel = {
296 pos = vec_search_with_function (ge->ge_locs, &gel,
297 gbp_endpoint_loc_is_equal);
300 return (&ge->ge_locs[pos]);
306 gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
312 if (0 == gel->gel_locks)
314 pos = gel - ge->ge_locs;
316 vec_del1 (ge->ge_locs, pos);
317 if (vec_len (ge->ge_locs) > 1)
318 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
320 /* This could be the last lock, so don't access the EP from
322 fib_node_unlock (&ge->ge_node);
330 gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
332 gbp_endpoint_group_unlock (gel->gel_epg);
333 gbp_itf_unlock (&gel->gel_itf);
336 static gbp_endpoint_loc_t *
337 gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
339 gbp_endpoint_loc_t gel = {
341 .gel_epg = INDEX_INVALID,
342 .gel_itf = GBP_ITF_HDL_INVALID,
347 pos = vec_search_with_function (ge->ge_locs, &gel,
348 gbp_endpoint_loc_is_equal);
352 vec_add1 (ge->ge_locs, gel);
354 if (vec_len (ge->ge_locs) > 1)
356 vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
358 pos = vec_search_with_function (ge->ge_locs, &gel,
359 gbp_endpoint_loc_is_equal);
365 * it's the sources and children that lock the endpoints
367 fib_node_lock (&ge->ge_node);
370 return (&ge->ge_locs[pos]);
374 * Find an EP inthe DBs and check that if we find it in the L2 DB
375 * it has the same IPs as this update
378 gbp_endpoint_find_for_update (const ip46_address_t * ips,
379 const gbp_route_domain_t * grd,
380 const mac_address_t * mac,
381 const gbp_bridge_domain_t * gbd,
382 gbp_endpoint_t ** ge)
384 gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
386 l2_ge = l3_ge = NULL;
388 if (NULL != mac && !mac_address_is_zero (mac))
391 l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
393 if (NULL != ips && !ip46_address_is_zero (ips))
395 const ip46_address_t *ip;
396 fib_protocol_t fproto;
399 vec_foreach (ip, ips)
401 fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
403 tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
408 else if (NULL == l3_ge)
409 /* first match against an IP address */
411 else if (tmp == l3_ge)
412 /* another match against IP address that is the same endpoint */
417 * a match agains a different endpoint.
418 * this means the KEY of the EP is changing which is not allowed
425 if (NULL == l2_ge && NULL == l3_ge)
428 else if (NULL == l2_ge)
431 else if (NULL == l3_ge)
436 /* found both L3 and L2 - they must be the same else the KEY
448 static gbp_endpoint_src_t
449 gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
451 if (0 == vec_len (ge->ge_locs))
452 return (GBP_ENDPOINT_SRC_MAX);
454 return (ge->ge_locs[0].gel_src);
458 gbp_endpoint_n_learned (int n)
460 gbp_n_learnt_endpoints += n;
462 if (n > 0 && 1 == gbp_n_learnt_endpoints)
464 vlib_process_signal_event (vlib_get_main (),
465 gbp_scanner_node.index,
466 GBP_ENDPOINT_SCAN_START, 0);
468 if (n < 0 && 0 == gbp_n_learnt_endpoints)
470 vlib_process_signal_event (vlib_get_main (),
471 gbp_scanner_node.index,
472 GBP_ENDPOINT_SCAN_STOP, 0);
477 gbp_endpoint_loc_update (const gbp_endpoint_t * ge,
478 gbp_endpoint_loc_t * gel,
479 const gbp_bridge_domain_t * gb,
482 gbp_endpoint_flags_t flags,
483 const ip46_address_t * tun_src,
484 const ip46_address_t * tun_dst)
486 int was_learnt, is_learnt;
489 was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
490 gel->gel_flags = flags;
491 is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
493 gbp_endpoint_n_learned (is_learnt - was_learnt);
498 gbp_endpoint_group_lock (ggi);
499 gbp_endpoint_group_unlock (gel->gel_epg);
502 if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
505 ip46_address_copy (&gel->tun.gel_src, tun_src);
507 ip46_address_copy (&gel->tun.gel_dst, tun_dst);
509 if (ip46_address_is_multicast (&gel->tun.gel_src))
512 * we learnt the EP from the multicast tunnel.
513 * Create a unicast TEP from the packet's source
514 * and the fixed address of the BD's parent tunnel
516 const gbp_vxlan_tunnel_t *gt;
518 gt = gbp_vxlan_tunnel_get (gb->gb_vni);
522 ip46_address_copy (&gel->tun.gel_src, >->gt_src);
523 sw_if_index = gt->gt_sw_if_index;
528 * the input interface may be the parent GBP-vxlan interface,
529 * create a child vlxan-gbp tunnel and use that as the endpoint's
532 gbp_itf_hdl_t old = gel->gel_itf;
534 switch (gbp_vxlan_tunnel_get_type (sw_if_index))
536 case GBP_VXLAN_TEMPLATE_TUNNEL:
537 gel->tun.gel_parent_sw_if_index = sw_if_index;
538 gel->gel_itf = gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
542 case VXLAN_GBP_TUNNEL:
543 gel->tun.gel_parent_sw_if_index =
544 vxlan_gbp_tunnel_get_parent (sw_if_index);
545 gel->gel_itf = vxlan_gbp_tunnel_lock_itf (sw_if_index);
549 gbp_itf_unlock (&old);
553 gel->gel_itf = gbp_itf_l2_add_and_lock (sw_if_index,
559 gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
561 const gbp_route_domain_t *grd;
562 const gbp_bridge_domain_t *gbd;
563 gbp_endpoint_fwd_t *gef;
564 const fib_prefix_t *pfx;
567 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
570 vec_foreach (pfx, ge->ge_key.gek_ips)
574 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
575 fib_index = grd->grd_fib_index[pfx->fp_proto];
577 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
578 &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
581 * remove a host route
583 if (gbp_endpoint_is_remote (ge))
585 fib_table_entry_special_remove (fib_index, pfx, gbp_fib_source_hi);
588 fib_table_entry_delete (fib_index, pfx, gbp_fib_source_low);
590 vec_foreach (ai, gef->gef_adjs)
595 if (gbp_itf_hdl_is_valid (gef->gef_itf))
597 l2fib_del_entry (ge->ge_key.gek_mac.bytes,
599 gbp_itf_get_sw_if_index (gef->gef_itf));
602 gbp_itf_unlock (&gef->gef_itf);
603 vec_free (gef->gef_adjs);
607 gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
609 const gbp_bridge_domain_t *gbd;
610 const gbp_endpoint_group_t *gg;
611 const gbp_route_domain_t *grd;
612 gbp_endpoint_loc_t *gel;
613 gbp_endpoint_fwd_t *gef;
614 const fib_prefix_t *pfx;
618 * locations are sort in source priority order
620 gei = gbp_endpoint_index (ge);
621 gel = &ge->ge_locs[0];
623 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
625 gef->gef_flags = gel->gel_flags;
627 if (INDEX_INVALID != gel->gel_epg)
629 gg = gbp_endpoint_group_get (gel->gel_epg);
630 gef->gef_sclass = gg->gg_sclass;
637 gef->gef_itf = gbp_itf_clone_and_lock (gel->gel_itf);
639 if (!mac_address_is_zero (&ge->ge_key.gek_mac))
641 gbp_itf_l2_set_input_feature (gef->gef_itf, L2INPUT_FEAT_GBP_FWD);
643 if (gbp_endpoint_is_remote (ge) || gbp_endpoint_is_external (ge))
646 * bridged packets to external endpoints should be classifed
647 * based on the EP's/BD's EPG
649 gbp_itf_l2_set_output_feature (gef->gef_itf,
650 L2OUTPUT_FEAT_GBP_POLICY_MAC);
654 gbp_endpoint_add_itf (gbp_itf_get_sw_if_index (gef->gef_itf), gei);
655 gbp_itf_l2_set_output_feature (gef->gef_itf,
656 L2OUTPUT_FEAT_GBP_POLICY_PORT);
658 l2fib_add_entry (ge->ge_key.gek_mac.bytes,
660 gbp_itf_get_sw_if_index (gef->gef_itf),
661 L2FIB_ENTRY_RESULT_FLAG_STATIC);
664 vec_foreach (pfx, ge->ge_key.gek_ips)
666 ethernet_header_t *eth;
673 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
674 fib_index = grd->grd_fib_index[pfx->fp_proto];
675 gef->gef_fib_index = fib_index;
677 bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
678 &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
681 * add a host route via the EPG's BVI we need this because the
682 * adj fib does not install, due to cover refinement check, since
683 * the BVI's prefix is /32
685 vec_validate (rewrite, sizeof (*eth) - 1);
686 eth = (ethernet_header_t *) rewrite;
688 eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
692 if (gbp_endpoint_is_remote (ge))
695 * for dynamic EPs we must add the IP adjacency via the learned
696 * tunnel since the BD will not contain the EP's MAC since it was
697 * L3 learned. The dst MAC address used is the 'BD's MAC'.
699 ip_sw_if_index = gbp_itf_get_sw_if_index (gef->gef_itf);
701 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
703 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
709 * for the static EPs we add the IP adjacency via the BVI
710 * knowing that the BD has the MAC address to route to and
711 * that policy will be applied on egress to the EP's port
713 ip_sw_if_index = gbd->gb_bvi_sw_if_index;
715 clib_memcpy (eth->src_address,
716 vnet_sw_interface_get_hw_address (vnet_get_main (),
718 sizeof (eth->src_address));
719 mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
722 fib_table_entry_path_add (fib_index, pfx,
725 fib_proto_to_dpo (pfx->fp_proto),
726 &pfx->fp_addr, ip_sw_if_index,
727 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
729 ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
730 fib_proto_to_link (pfx->fp_proto),
732 ip_sw_if_index, rewrite);
733 vec_add1 (gef->gef_adjs, ai);
736 * if the endpoint is external then routed packet to it must be
737 * classifed to the BD's EPG. but this will happen anyway with
738 * the GBP_MAC classification.
743 if (gbp_endpoint_is_remote (ge))
745 dpo_id_t policy_dpo = DPO_INVALID;
748 * interpose a policy DPO from the endpoint so that policy
751 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
753 gg->gg_sclass, ~0, &policy_dpo);
755 fib_table_entry_special_dpo_add (fib_index, pfx,
757 FIB_ENTRY_FLAG_INTERPOSE,
759 dpo_reset (&policy_dpo);
763 * send a gratuitous ARP on the EPG's uplink. this is done so
764 * that if this EP has moved from some other place in the
765 * 'fabric', upstream devices are informed
767 if (gbp_endpoint_is_local (ge) && ~0 != gg->gg_uplink_sw_if_index)
769 gbp_endpoint_add_itf (gbp_itf_get_sw_if_index (gef->gef_itf),
771 ip_neighbor_advertise (vlib_get_main (),
772 (FIB_PROTOCOL_IP4 == pfx->fp_proto ?
775 &pfx->fp_addr, gg->gg_uplink_sw_if_index);
780 if (gbp_endpoint_is_external (ge))
782 gbp_itf_l2_set_input_feature (gef->gef_itf,
783 L2INPUT_FEAT_GBP_LPM_CLASSIFY);
785 else if (gbp_endpoint_is_local (ge))
788 * non-remote endpoints (i.e. those not arriving on iVXLAN
789 * tunnels) need to be classifed based on the the input interface.
790 * We enable the GBP-FWD feature only if the group has an uplink
791 * interface (on which the GBP-FWD feature would send UU traffic).
792 * External endpoints get classified based on an LPM match
794 l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
796 if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
797 feats |= L2INPUT_FEAT_GBP_FWD;
798 gbp_itf_l2_set_input_feature (gef->gef_itf, feats);
802 * update children with the new forwarding info
804 fib_node_back_walk_ctx_t bw_ctx = {
805 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
806 .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
809 fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
813 gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
815 const ip46_address_t * ips,
816 const mac_address_t * mac,
817 index_t gbdi, index_t grdi,
819 gbp_endpoint_flags_t flags,
820 const ip46_address_t * tun_src,
821 const ip46_address_t * tun_dst, u32 * handle)
823 gbp_bridge_domain_t *gbd;
824 gbp_endpoint_group_t *gg;
825 gbp_endpoint_src_t best;
826 gbp_route_domain_t *grd;
827 gbp_endpoint_loc_t *gel;
832 if (~0 == sw_if_index)
833 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
839 * we need to determine the bridge-domain, either from the EPG or
842 if (SCLASS_INVALID != sclass)
844 ggi = gbp_endpoint_group_find (sclass);
846 if (INDEX_INVALID == ggi)
847 return (VNET_API_ERROR_NO_SUCH_ENTRY);
849 gg = gbp_endpoint_group_get (ggi);
855 if (INDEX_INVALID == gbdi)
856 return (VNET_API_ERROR_NO_SUCH_ENTRY);
857 if (INDEX_INVALID == grdi)
858 return (VNET_API_ERROR_NO_SUCH_FIB);
862 gbd = gbp_bridge_domain_get (gbdi);
863 grd = gbp_route_domain_get (grdi);
864 rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
871 ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
875 gbp_endpoint_ips_update (ge, ips, grd);
878 best = gbp_endpoint_get_best_src (ge);
879 gei = gbp_endpoint_index (ge);
880 gel = gbp_endpoint_loc_find_or_add (ge, src);
882 gbp_endpoint_loc_update (ge, gel, gbd, sw_if_index, ggi, flags,
888 * either the best source has been updated or we have a new best source
890 gbb_endpoint_fwd_reset (ge);
891 gbb_endpoint_fwd_recalc (ge);
896 * an update to a lower priority source, so we need do nothing
903 GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
909 gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
911 gbp_endpoint_loc_t *gel, gel_copy;
912 gbp_endpoint_src_t best;
916 if (pool_is_free_index (gbp_endpoint_pool, gei))
919 GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
921 ge = gbp_endpoint_get (gei);
923 gel = gbp_endpoint_loc_find (ge, src);
929 * lock the EP so we can control when it is deleted
931 fib_node_lock (&ge->ge_node);
932 best = gbp_endpoint_get_best_src (ge);
935 * copy the location info since we'll lose it when it's removed from
938 clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
941 * remove the source we no longer need
943 removed = gbp_endpoint_loc_unlock (ge, gel);
948 * we have removed the old best source => recalculate fwding
950 if (0 == vec_len (ge->ge_locs))
953 * if there are no more sources left, then we need only release
954 * the fwding resources held and then this EP is gawn.
956 gbb_endpoint_fwd_reset (ge);
961 * else there are more sources. release the old and get new
964 gbb_endpoint_fwd_reset (ge);
965 gbb_endpoint_fwd_recalc (ge);
970 * we removed a lower priority source so we need to do nothing
974 * clear up any resources held by the source
977 gbp_endpoint_loc_destroy (&gel_copy);
980 * remove the lock taken above
982 fib_node_unlock (&ge->ge_node);
984 * We may have removed the last source and so this EP is now TOAST
985 * DO NOTHING BELOW HERE
990 gbp_endpoint_child_add (index_t gei,
991 fib_node_type_t type, fib_node_index_t index)
993 return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
997 gbp_endpoint_child_remove (index_t gei, u32 sibling)
999 return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
1002 typedef struct gbp_endpoint_flush_ctx_t_
1005 gbp_endpoint_src_t src;
1007 } gbp_endpoint_flush_ctx_t;
1010 gbp_endpoint_flush_cb (index_t gei, void *args)
1012 gbp_endpoint_flush_ctx_t *ctx = args;
1013 gbp_endpoint_loc_t *gel;
1016 ge = gbp_endpoint_get (gei);
1017 gel = gbp_endpoint_loc_find (ge, ctx->src);
1019 if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
1021 vec_add1 (ctx->geis, gei);
1024 return (WALK_CONTINUE);
1028 * remove all learnt endpoints using the interface
1031 gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
1033 gbp_endpoint_flush_ctx_t ctx = {
1034 .sw_if_index = sw_if_index,
1039 GBP_ENDPOINT_INFO ("flush: %U %U",
1040 format_gbp_endpoint_src, src,
1041 format_vnet_sw_if_index_name, vnet_get_main (),
1043 gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
1045 vec_foreach (gei, ctx.geis)
1047 gbp_endpoint_unlock (src, *gei);
1050 vec_free (ctx.geis);
1054 gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx)
1059 pool_foreach_index(index, gbp_endpoint_pool,
1061 if (!cb(index, ctx))
1067 static clib_error_t *
1068 gbp_endpoint_cli (vlib_main_t * vm,
1069 unformat_input_t * input, vlib_cli_command_t * cmd)
1071 ip46_address_t ip = ip46_address_initializer, *ips = NULL;
1072 mac_address_t mac = ZERO_MAC_ADDRESS;
1073 vnet_main_t *vnm = vnet_get_main ();
1074 u32 sclass = SCLASS_INVALID;
1075 u32 handle = INDEX_INVALID;
1076 u32 sw_if_index = ~0;
1077 u32 flags = GBP_ENDPOINT_FLAG_NONE;
1081 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1083 ip46_address_reset (&ip);
1085 if (unformat (input, "%U", unformat_vnet_sw_interface,
1088 else if (unformat (input, "add"))
1090 else if (unformat (input, "del"))
1092 else if (unformat (input, "sclass %d", &sclass))
1094 else if (unformat (input, "handle %d", &handle))
1096 else if (unformat (input, "ip %U", unformat_ip4_address, &ip.ip4))
1098 else if (unformat (input, "ip %U", unformat_ip6_address, &ip.ip6))
1100 else if (unformat (input, "mac %U", unformat_mac_address, &mac))
1102 else if (unformat (input, "flags 0x%x", &flags))
1110 if (~0 == sw_if_index)
1111 return clib_error_return (0, "interface must be specified");
1112 if (SCLASS_INVALID == sclass)
1113 return clib_error_return (0, "SCLASS must be specified");
1116 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
1117 sw_if_index, ips, &mac,
1118 INDEX_INVALID, INDEX_INVALID,
1119 sclass, flags, NULL, NULL, &handle);
1122 return clib_error_return (0, "GBP Endpoint update returned %d", rv);
1124 vlib_cli_output (vm, "handle %d\n", handle);
1128 if (INDEX_INVALID == handle)
1129 return clib_error_return (0, "handle must be specified");
1131 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
1140 * Configure a GBP Endpoint
1143 * @cliexstart{gbp endpoint del <handle> | [add] <interface> sclass <SCLASS> ip <IP> mac <MAC> [flags <flags>]}
1147 VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
1148 .path = "gbp endpoint",
1149 .short_help = "gbp endpoint del <handle> | [add] <interface> sclass <SCLASS> ip <IP> mac <MAC> [flags <flags>]",
1150 .function = gbp_endpoint_cli,
1155 format_gbp_endpoint_src (u8 * s, va_list * args)
1157 gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
1161 #define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
1162 foreach_gbp_endpoint_src
1166 return (format (s, "unknown"));
1170 format_gbp_endpoint_fwd (u8 * s, va_list * args)
1172 gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
1174 s = format (s, "fwd:");
1175 s = format (s, "\n itf:[%U]", format_gbp_itf_hdl, gef->gef_itf);
1176 if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
1178 s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
1185 format_gbp_endpoint_key (u8 * s, va_list * args)
1187 gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
1188 const fib_prefix_t *pfx;
1190 s = format (s, "ips:[");
1192 vec_foreach (pfx, gek->gek_ips)
1194 s = format (s, "%U, ", format_fib_prefix, pfx);
1196 s = format (s, "]");
1198 s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
1204 format_gbp_endpoint_loc (u8 * s, va_list * args)
1206 gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
1208 s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
1209 s = format (s, "\n EPG:%d [%U]", gel->gel_epg,
1210 format_gbp_itf_hdl, gel->gel_itf);
1212 if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
1214 s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
1216 if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
1218 s = format (s, " tun:[");
1219 s = format (s, "parent:%U", format_vnet_sw_if_index_name,
1220 vnet_get_main (), gel->tun.gel_parent_sw_if_index);
1221 s = format (s, " {%U,%U}]",
1222 format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
1223 format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
1230 format_gbp_endpoint (u8 * s, va_list * args)
1232 index_t gei = va_arg (*args, index_t);
1233 gbp_endpoint_loc_t *gel;
1236 ge = gbp_endpoint_get (gei);
1238 s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
1239 s = format (s, " last-time:[%f]", ge->ge_last_time);
1241 vec_foreach (gel, ge->ge_locs)
1243 s = format (s, "\n %U", format_gbp_endpoint_loc, gel);
1245 s = format (s, "\n %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
1251 gbp_endpoint_show_one (index_t gei, void *ctx)
1256 vlib_cli_output (vm, " %U", format_gbp_endpoint, gei);
1258 return (WALK_CONTINUE);
1262 gbp_endpoint_walk_ip_itf (clib_bihash_kv_24_8_t * kvp, void *arg)
1270 gbp_endpoint_extract_key_ip_itf (kvp, &ip, &sw_if_index);
1272 vlib_cli_output (vm, " {%U, %U} -> %d",
1273 format_ip46_address, &ip, IP46_TYPE_ANY,
1274 format_vnet_sw_if_index_name, vnet_get_main (),
1275 sw_if_index, kvp->value);
1276 return (BIHASH_WALK_CONTINUE);
1280 gbp_endpoint_walk_mac_itf (clib_bihash_kv_16_8_t * kvp, void *arg)
1288 gbp_endpoint_extract_key_mac_itf (kvp, &mac, &sw_if_index);
1290 vlib_cli_output (vm, " {%U, %U} -> %d",
1291 format_mac_address_t, &mac,
1292 format_vnet_sw_if_index_name, vnet_get_main (),
1293 sw_if_index, kvp->value);
1294 return (BIHASH_WALK_CONTINUE);
1297 static clib_error_t *
1298 gbp_endpoint_show (vlib_main_t * vm,
1299 unformat_input_t * input, vlib_cli_command_t * cmd)
1301 u32 show_dbs, handle;
1303 handle = INDEX_INVALID;
1306 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1308 if (unformat (input, "%d", &handle))
1310 else if (unformat (input, "db"))
1316 if (INDEX_INVALID != handle)
1318 vlib_cli_output (vm, "%U", format_gbp_endpoint, handle);
1322 vlib_cli_output (vm, "\nDatabases:");
1323 clib_bihash_foreach_key_value_pair_24_8 (&gbp_ep_db.ged_by_ip_rd,
1324 gbp_endpoint_walk_ip_itf, vm);
1325 clib_bihash_foreach_key_value_pair_16_8
1326 (&gbp_ep_db.ged_by_mac_bd, gbp_endpoint_walk_mac_itf, vm);
1330 vlib_cli_output (vm, "Endpoints:");
1331 gbp_endpoint_walk (gbp_endpoint_show_one, vm);
1338 * Show Group Based Policy Endpoints and derived information
1341 * @cliexstart{show gbp endpoint}
1345 VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
1346 .path = "show gbp endpoint",
1347 .short_help = "show gbp endpoint\n",
1348 .function = gbp_endpoint_show,
1353 gbp_endpoint_check (index_t gei, f64 start_time)
1355 gbp_endpoint_group_t *gg;
1356 gbp_endpoint_loc_t *gel;
1359 ge = gbp_endpoint_get (gei);
1360 gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
1364 gg = gbp_endpoint_group_get (gel->gel_epg);
1366 if ((start_time - ge->ge_last_time) >
1367 gg->gg_retention.remote_ep_timeout)
1369 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
1375 gbp_endpoint_scan_l2 (vlib_main_t * vm)
1377 clib_bihash_16_8_t *gte_table = &gbp_ep_db.ged_by_mac_bd;
1378 f64 last_start, start_time, delta_t;
1381 if (!gte_table->instantiated)
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;
1436 if (!gte_table->instantiated)
1440 last_start = start_time = vlib_time_now (vm);
1442 for (i = 0; i < gte_table->nbuckets; i++)
1444 clib_bihash_bucket_24_8_t *b;
1445 clib_bihash_value_24_8_t *v;
1447 /* allow no more than 20us without a pause */
1448 delta_t = vlib_time_now (vm) - last_start;
1449 if (delta_t > 20e-6)
1451 /* suspend for 100 us */
1452 vlib_process_suspend (vm, 100e-6);
1453 last_start = vlib_time_now (vm);
1456 b = >e_table->buckets[i];
1459 v = clib_bihash_get_value_24_8 (gte_table, b->offset);
1461 for (j = 0; j < (1 << b->log2_pages); j++)
1463 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1465 if (clib_bihash_is_free_24_8 (&v->kvp[k]))
1468 gbp_endpoint_check (v->kvp[k].value, start_time);
1471 * Note: we may have just freed the bucket's backing
1472 * storage, so check right here...
1485 gbp_endpoint_scan (vlib_main_t * vm)
1487 gbp_endpoint_scan_l2 (vm);
1488 gbp_endpoint_scan_l3 (vm);
1492 gbp_endpoint_get_node (fib_node_index_t index)
1496 ge = gbp_endpoint_get (index);
1498 return (&ge->ge_node);
1501 static gbp_endpoint_t *
1502 gbp_endpoint_from_fib_node (fib_node_t * node)
1504 ASSERT (gbp_endpoint_fib_type == node->fn_type);
1505 return ((gbp_endpoint_t *) node);
1509 gbp_endpoint_last_lock_gone (fib_node_t * node)
1511 const gbp_bridge_domain_t *gbd;
1512 const gbp_route_domain_t *grd;
1513 const fib_prefix_t *pfx;
1516 ge = gbp_endpoint_from_fib_node (node);
1518 ASSERT (0 == vec_len (ge->ge_locs));
1520 gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
1523 * we have removed the last source. this EP is toast
1525 if (INDEX_INVALID != ge->ge_key.gek_gbd)
1527 gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
1529 vec_foreach (pfx, ge->ge_key.gek_ips)
1531 grd = gbp_route_domain_get (ge->ge_key.gek_grd);
1532 gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
1534 pool_put (gbp_endpoint_pool, ge);
1537 static fib_node_back_walk_rc_t
1538 gbp_endpoint_back_walk_notify (fib_node_t * node,
1539 fib_node_back_walk_ctx_t * ctx)
1543 return (FIB_NODE_BACK_WALK_CONTINUE);
1547 * The FIB path's graph node virtual function table
1549 static const fib_node_vft_t gbp_endpoint_vft = {
1550 .fnv_get = gbp_endpoint_get_node,
1551 .fnv_last_lock = gbp_endpoint_last_lock_gone,
1552 .fnv_back_walk = gbp_endpoint_back_walk_notify,
1553 // .fnv_mem_show = fib_path_memory_show,
1556 static clib_error_t *
1557 gbp_endpoint_init (vlib_main_t * vm)
1559 #define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
1560 #define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
1562 clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
1563 "GBP Endpoints - IP/RD",
1564 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1566 clib_bihash_init_16_8 (&gbp_ep_db.ged_by_mac_bd,
1567 "GBP Endpoints - MAC/BD",
1568 GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
1570 gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
1571 gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
1572 gbp_fib_source_hi = fib_source_allocate ("gbp-endpoint-hi",
1573 FIB_SOURCE_PRIORITY_HI,
1574 FIB_SOURCE_BH_SIMPLE);
1575 gbp_fib_source_low = fib_source_allocate ("gbp-endpoint-low",
1576 FIB_SOURCE_PRIORITY_LOW,
1577 FIB_SOURCE_BH_SIMPLE);
1582 VLIB_INIT_FUNCTION (gbp_endpoint_init);
1585 * fd.io coding-style-patch-verification: ON
1588 * eval: (c-set-style "gnu")