2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <plugins/gbp/gbp_vxlan.h>
17 #include <plugins/gbp/gbp_itf.h>
18 #include <plugins/gbp/gbp_learn.h>
19 #include <plugins/gbp/gbp_bridge_domain.h>
20 #include <plugins/gbp/gbp_route_domain.h>
22 #include <vnet/vxlan-gbp/vxlan_gbp.h>
23 #include <vlibmemory/api.h>
24 #include <vnet/fib/fib_table.h>
27 * A reference to a VXLAN-GBP tunnel created as a child/dependent tunnel
28 * of the tempplate GBP-VXLAN tunnel
30 typedef struct vxlan_tunnel_ref_t_
36 gbp_vxlan_tunnel_layer_t vxr_layer;
47 vlib_log_class_t gt_logger;
50 * Pool of template tunnels
52 gbp_vxlan_tunnel_t *gbp_vxlan_tunnel_pool;
55 * Pool of child tunnels
57 vxlan_tunnel_ref_t *vxlan_tunnel_ref_pool;
60 * DB of template interfaces by SW interface index
62 index_t *gbp_vxlan_tunnel_db;
65 * DB of child interfaces by SW interface index
67 index_t *vxlan_tunnel_ref_db;
70 static char *gbp_vxlan_tunnel_layer_strings[] = {
71 #define _(n,s) [GBP_VXLAN_TUN_##n] = s,
72 forecah_gbp_vxlan_tunnel_layer
76 #define GBP_VXLAN_TUN_DBG(...) \
77 vlib_log_debug (gt_logger, __VA_ARGS__);
82 gbp_vxlan_tunnel_get (index_t gti)
84 return (pool_elt_at_index (gbp_vxlan_tunnel_pool, gti));
87 static vxlan_tunnel_ref_t *
88 vxlan_tunnel_ref_get (index_t vxri)
90 return (pool_elt_at_index (vxlan_tunnel_ref_pool, vxri));
94 format_vxlan_tunnel_ref (u8 * s, va_list * args)
96 index_t vxri = va_arg (*args, u32);
97 vxlan_tunnel_ref_t *vxr;
99 vxr = vxlan_tunnel_ref_get (vxri);
101 s = format (s, "[%U locks:%d]", format_vnet_sw_if_index_name,
102 vnet_get_main (), vxr->vxr_sw_if_index, vxr->vxr_locks);
108 gdb_vxlan_dep_add (gbp_vxlan_tunnel_t * gt,
109 const ip46_address_t * src, const ip46_address_t * dst)
111 vnet_vxlan_gbp_tunnel_add_del_args_t args = {
113 .is_ip6 = !ip46_address_is_ip4 (src),
118 .mode = (GBP_VXLAN_TUN_L2 == gt->gt_layer ?
119 VXLAN_GBP_TUNNEL_MODE_L2 : VXLAN_GBP_TUNNEL_MODE_L3),
121 vxlan_tunnel_ref_t *vxr;
127 rv = vnet_vxlan_gbp_tunnel_add_del (&args, &sw_if_index);
129 if (VNET_API_ERROR_TUNNEL_EXIST == rv)
131 vxri = vxlan_tunnel_ref_db[sw_if_index];
133 vxr = vxlan_tunnel_ref_get (vxri);
138 ASSERT (~0 != sw_if_index);
139 GBP_VXLAN_TUN_DBG ("add-dep:%U %U %U %d", format_vnet_sw_if_index_name,
140 vnet_get_main (), sw_if_index,
141 format_ip46_address, src, IP46_TYPE_ANY,
142 format_ip46_address, dst, IP46_TYPE_ANY, gt->gt_vni);
144 pool_get_zero (vxlan_tunnel_ref_pool, vxr);
146 vxri = (vxr - vxlan_tunnel_ref_pool);
147 vxr->vxr_parent = gt - gbp_vxlan_tunnel_pool;
148 vxr->vxr_sw_if_index = sw_if_index;
150 vxr->vxr_layer = gt->gt_layer;
153 * store the child both on the parent's list and the global DB
155 vec_add1 (gt->gt_tuns, vxri);
157 vec_validate_init_empty (vxlan_tunnel_ref_db,
158 vxr->vxr_sw_if_index, INDEX_INVALID);
159 vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = vxri;
161 if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
163 l2output_feat_masks_t ofeat;
164 l2input_feat_masks_t ifeat;
165 gbp_bridge_domain_t *gbd;
167 gbd = gbp_bridge_domain_get (gt->gt_gbd);
168 vxr->vxr_itf = gbp_itf_add_and_lock (vxr->vxr_sw_if_index,
171 ofeat = L2OUTPUT_FEAT_GBP_POLICY_MAC;
172 ifeat = L2INPUT_FEAT_NONE;
174 if (!(gbd->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
175 ifeat |= L2INPUT_FEAT_GBP_LEARN;
177 gbp_itf_set_l2_output_feature (vxr->vxr_itf,
178 vxr->vxr_sw_if_index, ofeat);
179 gbp_itf_set_l2_input_feature (vxr->vxr_itf,
180 vxr->vxr_sw_if_index, ifeat);
184 const gbp_route_domain_t *grd;
185 fib_protocol_t fproto;
187 grd = gbp_route_domain_get (gt->gt_grd);
189 FOR_EACH_FIB_IP_PROTOCOL (fproto)
190 ip_table_bind (fproto, vxr->vxr_sw_if_index,
191 grd->grd_table_id[fproto], 1);
193 gbp_learn_enable (vxr->vxr_sw_if_index, GBP_LEARN_MODE_L3);
197 return (sw_if_index);
201 vxlan_gbp_tunnel_get_parent (u32 sw_if_index)
203 ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
204 (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
206 gbp_vxlan_tunnel_t *gt;
207 vxlan_tunnel_ref_t *vxr;
209 vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
210 gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
212 return (gt->gt_sw_if_index);
215 gbp_vxlan_tunnel_type_t
216 gbp_vxlan_tunnel_get_type (u32 sw_if_index)
218 if (sw_if_index < vec_len (vxlan_tunnel_ref_db) &&
219 INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index])
221 return (VXLAN_GBP_TUNNEL);
223 else if (sw_if_index < vec_len (gbp_vxlan_tunnel_db) &&
224 INDEX_INVALID != gbp_vxlan_tunnel_db[sw_if_index])
226 return (GBP_VXLAN_TEMPLATE_TUNNEL);
230 return (GBP_VXLAN_TEMPLATE_TUNNEL);
234 gbp_vxlan_tunnel_clone_and_lock (u32 sw_if_index,
235 const ip46_address_t * src,
236 const ip46_address_t * dst)
238 gbp_vxlan_tunnel_t *gt;
241 gti = gbp_vxlan_tunnel_db[sw_if_index];
243 if (INDEX_INVALID == gti)
246 gt = pool_elt_at_index (gbp_vxlan_tunnel_pool, gti);
248 return (gdb_vxlan_dep_add (gt, src, dst));
252 gdb_vxlan_dep_del (index_t vxri)
254 vxlan_tunnel_ref_t *vxr;
255 gbp_vxlan_tunnel_t *gt;
258 vxr = vxlan_tunnel_ref_get (vxri);
259 gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
261 GBP_VXLAN_TUN_DBG ("del-dep:%U", format_vxlan_tunnel_ref, vxri);
263 vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = INDEX_INVALID;
264 pos = vec_search (gt->gt_tuns, vxri);
267 vec_del1 (gt->gt_tuns, pos);
269 if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
271 gbp_itf_set_l2_output_feature (vxr->vxr_itf, vxr->vxr_sw_if_index,
273 gbp_itf_set_l2_input_feature (vxr->vxr_itf, vxr->vxr_sw_if_index,
275 gbp_itf_unlock (vxr->vxr_itf);
279 fib_protocol_t fproto;
281 FOR_EACH_FIB_IP_PROTOCOL (fproto)
282 ip_table_bind (fproto, vxr->vxr_sw_if_index, 0, 0);
283 gbp_learn_disable (vxr->vxr_sw_if_index, GBP_LEARN_MODE_L3);
286 vnet_vxlan_gbp_tunnel_del (vxr->vxr_sw_if_index);
288 pool_put (vxlan_tunnel_ref_pool, vxr);
292 vxlan_gbp_tunnel_unlock (u32 sw_if_index)
294 vxlan_tunnel_ref_t *vxr;
297 vxri = vxlan_tunnel_ref_db[sw_if_index];
299 ASSERT (vxri != INDEX_INVALID);
301 vxr = vxlan_tunnel_ref_get (vxri);
304 if (0 == vxr->vxr_locks)
306 gdb_vxlan_dep_del (vxri);
311 vxlan_gbp_tunnel_lock (u32 sw_if_index)
313 vxlan_tunnel_ref_t *vxr;
316 vxri = vxlan_tunnel_ref_db[sw_if_index];
318 ASSERT (vxri != INDEX_INVALID);
320 vxr = vxlan_tunnel_ref_get (vxri);
325 gbp_vxlan_walk (gbp_vxlan_cb_t cb, void *ctx)
327 gbp_vxlan_tunnel_t *gt;
330 pool_foreach (gt, gbp_vxlan_tunnel_pool,
332 if (WALK_CONTINUE != cb(gt, ctx))
339 gbp_vxlan_tunnel_show_one (gbp_vxlan_tunnel_t * gt, void *ctx)
341 vlib_cli_output (ctx, "%U", format_gbp_vxlan_tunnel,
342 gt - gbp_vxlan_tunnel_pool);
344 return (WALK_CONTINUE);
348 format_gbp_vxlan_tunnel_name (u8 * s, va_list * args)
350 u32 dev_instance = va_arg (*args, u32);
352 return format (s, "gbp-vxlan-%d", dev_instance);
356 format_gbp_vxlan_tunnel_layer (u8 * s, va_list * args)
358 gbp_vxlan_tunnel_layer_t gl = va_arg (*args, gbp_vxlan_tunnel_layer_t);
359 s = format (s, "%s", gbp_vxlan_tunnel_layer_strings[gl]);
365 format_gbp_vxlan_tunnel (u8 * s, va_list * args)
367 u32 dev_instance = va_arg (*args, u32);
368 CLIB_UNUSED (int verbose) = va_arg (*args, int);
369 gbp_vxlan_tunnel_t *gt = gbp_vxlan_tunnel_get (dev_instance);
372 s = format (s, "GBP VXLAN tunnel: hw:%d sw:%d vni:%d %U",
373 gt->gt_hw_if_index, gt->gt_sw_if_index, gt->gt_vni,
374 format_gbp_vxlan_tunnel_layer, gt->gt_layer);
375 if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
376 s = format (s, " BD:%d bd-index:%d", gt->gt_bd_rd_id, gt->gt_bd_index);
378 s = format (s, " RD:%d fib-index:[%d,%d]",
380 gt->gt_fib_index[FIB_PROTOCOL_IP4],
381 gt->gt_fib_index[FIB_PROTOCOL_IP6]);
383 s = format (s, " children:[");
384 vec_foreach (vxri, gt->gt_tuns)
386 s = format (s, "%U, ", format_vxlan_tunnel_ref, *vxri);
393 typedef struct gbp_vxlan_tx_trace_t_
396 } gbp_vxlan_tx_trace_t;
399 format_gbp_vxlan_tx_trace (u8 * s, va_list * args)
401 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
402 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
403 gbp_vxlan_tx_trace_t *t = va_arg (*args, gbp_vxlan_tx_trace_t *);
405 s = format (s, "GBP-VXLAN: vni:%d", t->vni);
411 gbp_vxlan_interface_admin_up_down (vnet_main_t * vnm,
412 u32 hw_if_index, u32 flags)
414 vnet_hw_interface_t *hi;
417 hi = vnet_get_hw_interface (vnm, hw_if_index);
419 if (NULL == gbp_vxlan_tunnel_db ||
420 hi->sw_if_index >= vec_len (gbp_vxlan_tunnel_db))
423 ti = gbp_vxlan_tunnel_db[hi->sw_if_index];
426 /* not one of ours */
429 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
430 vnet_hw_interface_set_flags (vnm, hw_if_index,
431 VNET_HW_INTERFACE_FLAG_LINK_UP);
433 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
439 gbp_vxlan_interface_tx (vlib_main_t * vm,
440 vlib_node_runtime_t * node, vlib_frame_t * frame)
442 clib_warning ("you shouldn't be here, leaking buffers...");
443 return frame->n_vectors;
447 VNET_DEVICE_CLASS (gbp_vxlan_device_class) = {
448 .name = "GBP VXLAN tunnel-template",
449 .format_device_name = format_gbp_vxlan_tunnel_name,
450 .format_device = format_gbp_vxlan_tunnel,
451 .format_tx_trace = format_gbp_vxlan_tx_trace,
452 .admin_up_down_function = gbp_vxlan_interface_admin_up_down,
453 .tx_function = gbp_vxlan_interface_tx,
456 VNET_HW_INTERFACE_CLASS (gbp_vxlan_hw_interface_class) = {
458 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
463 gbp_vxlan_tunnel_add (u32 vni, gbp_vxlan_tunnel_layer_t layer,
465 const ip4_address_t * src, u32 * sw_if_indexp)
467 gbp_vxlan_tunnel_t *gt;
473 p = hash_get (gv_db, vni);
475 GBP_VXLAN_TUN_DBG ("add: %d %d %d", vni, layer, bd_rd_id);
479 vnet_sw_interface_t *si;
480 vnet_hw_interface_t *hi;
484 gbi = grdi = INDEX_INVALID;
486 if (layer == GBP_VXLAN_TUN_L2)
488 gbi = gbp_bridge_domain_find_and_lock (bd_rd_id);
490 if (INDEX_INVALID == gbi)
492 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
497 grdi = gbp_route_domain_find_and_lock (bd_rd_id);
499 if (INDEX_INVALID == grdi)
501 return (VNET_API_ERROR_NO_SUCH_FIB);
505 vnm = vnet_get_main ();
506 pool_get (gbp_vxlan_tunnel_pool, gt);
507 gti = gt - gbp_vxlan_tunnel_pool;
510 gt->gt_layer = layer;
511 gt->gt_bd_rd_id = bd_rd_id;
512 gt->gt_src.ip4.as_u32 = src->as_u32;
513 gt->gt_hw_if_index = vnet_register_interface (vnm,
514 gbp_vxlan_device_class.index,
516 gbp_vxlan_hw_interface_class.index,
519 hi = vnet_get_hw_interface (vnm, gt->gt_hw_if_index);
521 gt->gt_sw_if_index = hi->sw_if_index;
523 /* don't flood packets in a BD to these interfaces */
524 si = vnet_get_sw_interface (vnm, gt->gt_sw_if_index);
525 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
527 if (layer == GBP_VXLAN_TUN_L2)
529 gbp_bridge_domain_t *gb;
531 gb = gbp_bridge_domain_get (gbi);
534 gt->gt_bd_index = gb->gb_bd_index;
536 /* set it up as a GBP interface */
537 gt->gt_itf = gbp_itf_add_and_lock (gt->gt_sw_if_index,
539 gbp_learn_enable (gt->gt_sw_if_index, GBP_LEARN_MODE_L2);
543 gbp_route_domain_t *grd;
544 fib_protocol_t fproto;
546 grd = gbp_route_domain_get (grdi);
549 grd->grd_vni_sw_if_index = gt->gt_sw_if_index;
551 gbp_learn_enable (gt->gt_sw_if_index, GBP_LEARN_MODE_L3);
553 ip4_sw_interface_enable_disable (gt->gt_sw_if_index, 1);
554 ip6_sw_interface_enable_disable (gt->gt_sw_if_index, 1);
556 FOR_EACH_FIB_IP_PROTOCOL (fproto)
558 gt->gt_fib_index[fproto] = grd->grd_fib_index[fproto];
560 ip_table_bind (fproto, gt->gt_sw_if_index,
561 grd->grd_table_id[fproto], 1);
566 * save the tunnel by VNI and by sw_if_index
568 hash_set (gv_db, vni, gti);
570 vec_validate_init_empty (gbp_vxlan_tunnel_db,
571 gt->gt_sw_if_index, INDEX_INVALID);
572 gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = gti;
575 *sw_if_indexp = gt->gt_sw_if_index;
577 vxlan_gbp_register_udp_ports ();
582 rv = VNET_API_ERROR_IF_ALREADY_EXISTS;
585 GBP_VXLAN_TUN_DBG ("add: %U", format_gbp_vxlan_tunnel, gti);
591 gbp_vxlan_tunnel_del (u32 vni)
593 gbp_vxlan_tunnel_t *gt;
596 p = hash_get (gv_db, vni);
602 vnm = vnet_get_main ();
603 gt = gbp_vxlan_tunnel_get (p[0]);
605 vxlan_gbp_unregister_udp_ports ();
607 GBP_VXLAN_TUN_DBG ("del: %U", format_gbp_vxlan_tunnel,
608 gt - gbp_vxlan_tunnel_pool);
610 gbp_endpoint_flush (GBP_ENDPOINT_SRC_DP, gt->gt_sw_if_index);
611 ASSERT (0 == vec_len (gt->gt_tuns));
612 vec_free (gt->gt_tuns);
614 if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
616 gbp_learn_disable (gt->gt_sw_if_index, GBP_LEARN_MODE_L2);
617 gbp_itf_unlock (gt->gt_itf);
618 gbp_bridge_domain_unlock (gt->gt_gbd);
622 fib_protocol_t fproto;
624 FOR_EACH_FIB_IP_PROTOCOL (fproto)
625 ip_table_bind (fproto, gt->gt_sw_if_index, 0, 0);
627 ip4_sw_interface_enable_disable (gt->gt_sw_if_index, 0);
628 ip6_sw_interface_enable_disable (gt->gt_sw_if_index, 0);
630 gbp_learn_disable (gt->gt_sw_if_index, GBP_LEARN_MODE_L3);
631 gbp_route_domain_unlock (gt->gt_grd);
634 vnet_sw_interface_set_flags (vnm, gt->gt_sw_if_index, 0);
635 vnet_delete_hw_interface (vnm, gt->gt_hw_if_index);
637 hash_unset (gv_db, vni);
638 gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = INDEX_INVALID;
640 pool_put (gbp_vxlan_tunnel_pool, gt);
643 return VNET_API_ERROR_NO_SUCH_ENTRY;
648 static clib_error_t *
649 gbp_vxlan_show (vlib_main_t * vm,
650 unformat_input_t * input, vlib_cli_command_t * cmd)
652 gbp_vxlan_walk (gbp_vxlan_tunnel_show_one, vm);
658 * Show Group Based Policy VXLAN tunnels
661 * @cliexstart{show gbp vxlan}
665 VLIB_CLI_COMMAND (gbp_vxlan_show_node, static) = {
666 .path = "show gbp vxlan",
667 .short_help = "show gbp vxlan\n",
668 .function = gbp_vxlan_show,
672 static clib_error_t *
673 gbp_vxlan_init (vlib_main_t * vm)
676 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "gbp-vxlan4");
679 * insert ourselves into the VXLAN-GBP arc to collect the no-tunnel
682 slot4 = vlib_node_add_next_with_slot (vm,
683 vxlan4_gbp_input_node.index,
685 VXLAN_GBP_INPUT_NEXT_NO_TUNNEL);
686 ASSERT (slot4 == VXLAN_GBP_INPUT_NEXT_NO_TUNNEL);
688 /* slot6 = vlib_node_add_next_with_slot (vm, */
689 /* vxlan6_gbp_input_node.index, */
690 /* gbp_vxlan6_input_node.index, */
691 /* VXLAN_GBP_INPUT_NEXT_NO_TUNNEL); */
692 /* ASSERT (slot6 == VXLAN_GBP_INPUT_NEXT_NO_TUNNEL); */
694 gt_logger = vlib_log_register_class ("gbp", "tun");
699 VLIB_INIT_FUNCTION (gbp_vxlan_init);
702 * fd.io coding-style-patch-verification: ON
705 * eval: (c-set-style "gnu")