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_learn.h>
18 #include <plugins/gbp/gbp_bridge_domain.h>
19 #include <plugins/gbp/gbp_route_domain.h>
21 #include <vnet/vxlan-gbp/vxlan_gbp.h>
22 #include <vlibmemory/api.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vlib/punt.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_
32 gbp_itf_hdl_t vxr_itf;
35 gbp_vxlan_tunnel_layer_t vxr_layer;
46 static vlib_log_class_t gt_logger;
49 * Pool of template tunnels
51 static gbp_vxlan_tunnel_t *gbp_vxlan_tunnel_pool;
54 * Pool of child tunnels
56 static vxlan_tunnel_ref_t *vxlan_tunnel_ref_pool;
59 * DB of template interfaces by SW interface index
61 static index_t *gbp_vxlan_tunnel_db;
64 * DB of child interfaces by SW interface index
66 static index_t *vxlan_tunnel_ref_db;
69 * handle registered with the ;unt infra
71 static vlib_punt_hdl_t punt_hdl;
73 static char *gbp_vxlan_tunnel_layer_strings[] = {
74 #define _(n,s) [GBP_VXLAN_TUN_##n] = s,
75 forecah_gbp_vxlan_tunnel_layer
79 #define GBP_VXLAN_TUN_DBG(...) \
80 vlib_log_debug (gt_logger, __VA_ARGS__);
84 gbp_vxlan_tunnel_get (index_t gti)
86 return (pool_elt_at_index (gbp_vxlan_tunnel_pool, gti));
89 static vxlan_tunnel_ref_t *
90 vxlan_tunnel_ref_get (index_t vxri)
92 return (pool_elt_at_index (vxlan_tunnel_ref_pool, vxri));
96 format_vxlan_tunnel_ref (u8 * s, va_list * args)
98 index_t vxri = va_arg (*args, u32);
99 vxlan_tunnel_ref_t *vxr;
101 vxr = vxlan_tunnel_ref_get (vxri);
103 s = format (s, "[%U]", format_gbp_itf_hdl, vxr->vxr_itf);
109 gdb_vxlan_dep_del (u32 sw_if_index)
111 vxlan_tunnel_ref_t *vxr;
112 gbp_vxlan_tunnel_t *gt;
116 vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
117 vxri = vxr - vxlan_tunnel_ref_pool;
118 gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
120 GBP_VXLAN_TUN_DBG ("del-dep:%U", format_vxlan_tunnel_ref, vxri);
122 vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = INDEX_INVALID;
123 pos = vec_search (gt->gt_tuns, vxri);
126 vec_del1 (gt->gt_tuns, pos);
128 vnet_vxlan_gbp_tunnel_del (vxr->vxr_sw_if_index);
130 pool_put (vxlan_tunnel_ref_pool, vxr);
134 gdb_vxlan_dep_add (gbp_vxlan_tunnel_t * gt,
135 const ip46_address_t * src, const ip46_address_t * dst)
137 vnet_vxlan_gbp_tunnel_add_del_args_t args = {
139 .is_ip6 = !ip46_address_is_ip4 (src),
144 .mode = (GBP_VXLAN_TUN_L2 == gt->gt_layer ?
145 VXLAN_GBP_TUNNEL_MODE_L2 : VXLAN_GBP_TUNNEL_MODE_L3),
147 vxlan_tunnel_ref_t *vxr;
153 rv = vnet_vxlan_gbp_tunnel_add_del (&args, &sw_if_index);
155 if (VNET_API_ERROR_TUNNEL_EXIST == rv)
157 vxri = vxlan_tunnel_ref_db[sw_if_index];
159 vxr = vxlan_tunnel_ref_get (vxri);
160 gbp_itf_lock (vxr->vxr_itf);
164 ASSERT (~0 != sw_if_index);
165 GBP_VXLAN_TUN_DBG ("add-dep:%U %U %U %d", format_vnet_sw_if_index_name,
166 vnet_get_main (), sw_if_index,
167 format_ip46_address, src, IP46_TYPE_ANY,
168 format_ip46_address, dst, IP46_TYPE_ANY, gt->gt_vni);
170 pool_get_zero (vxlan_tunnel_ref_pool, vxr);
172 vxri = (vxr - vxlan_tunnel_ref_pool);
173 vxr->vxr_parent = gt - gbp_vxlan_tunnel_pool;
174 vxr->vxr_sw_if_index = sw_if_index;
175 vxr->vxr_layer = gt->gt_layer;
178 * store the child both on the parent's list and the global DB
180 vec_add1 (gt->gt_tuns, vxri);
182 vec_validate_init_empty (vxlan_tunnel_ref_db,
183 vxr->vxr_sw_if_index, INDEX_INVALID);
184 vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = vxri;
186 if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
188 l2output_feat_masks_t ofeat;
189 l2input_feat_masks_t ifeat;
190 gbp_bridge_domain_t *gbd;
192 gbd = gbp_bridge_domain_get (gt->gt_gbd);
193 vxr->vxr_itf = gbp_itf_l2_add_and_lock_w_free
194 (vxr->vxr_sw_if_index, gt->gt_gbd, gdb_vxlan_dep_del);
196 ofeat = L2OUTPUT_FEAT_GBP_POLICY_MAC;
197 ifeat = L2INPUT_FEAT_NONE;
199 if (!(gbd->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
200 ifeat |= L2INPUT_FEAT_GBP_LEARN;
202 gbp_itf_l2_set_output_feature (vxr->vxr_itf, ofeat);
203 gbp_itf_l2_set_input_feature (vxr->vxr_itf, ifeat);
207 vxr->vxr_itf = gbp_itf_l3_add_and_lock_w_free
208 (vxr->vxr_sw_if_index, gt->gt_grd, gdb_vxlan_dep_del);
210 gbp_itf_l3_set_input_feature (vxr->vxr_itf, GBP_ITF_L3_FEAT_LEARN);
215 return (GBP_ITF_HDL_INVALID);
218 return (vxr->vxr_itf);
222 vxlan_gbp_tunnel_get_parent (u32 sw_if_index)
224 ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
225 (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
227 gbp_vxlan_tunnel_t *gt;
228 vxlan_tunnel_ref_t *vxr;
230 vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
231 gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
233 return (gt->gt_sw_if_index);
237 vxlan_gbp_tunnel_lock_itf (u32 sw_if_index)
239 ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
240 (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
242 vxlan_tunnel_ref_t *vxr;
244 vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
246 gbp_itf_lock (vxr->vxr_itf);
248 return (vxr->vxr_itf);
252 gbp_vxlan_tunnel_type_t
253 gbp_vxlan_tunnel_get_type (u32 sw_if_index)
255 if (sw_if_index < vec_len (vxlan_tunnel_ref_db) &&
256 INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index])
258 return (VXLAN_GBP_TUNNEL);
260 else if (sw_if_index < vec_len (gbp_vxlan_tunnel_db) &&
261 INDEX_INVALID != gbp_vxlan_tunnel_db[sw_if_index])
263 return (GBP_VXLAN_TEMPLATE_TUNNEL);
267 return (GBP_VXLAN_TEMPLATE_TUNNEL);
271 gbp_vxlan_tunnel_clone_and_lock (u32 sw_if_index,
272 const ip46_address_t * src,
273 const ip46_address_t * dst)
275 gbp_vxlan_tunnel_t *gt;
278 gti = gbp_vxlan_tunnel_db[sw_if_index];
280 if (INDEX_INVALID == gti)
281 return (GBP_ITF_HDL_INVALID);
283 gt = pool_elt_at_index (gbp_vxlan_tunnel_pool, gti);
285 return (gdb_vxlan_dep_add (gt, src, dst));
289 vxlan_gbp_tunnel_unlock (u32 sw_if_index)
291 /* vxlan_tunnel_ref_t *vxr; */
294 /* vxri = vxlan_tunnel_ref_db[sw_if_index]; */
296 /* ASSERT (vxri != INDEX_INVALID); */
298 /* vxr = vxlan_tunnel_ref_get (vxri); */
300 /* gdb_vxlan_dep_del (vxri); */
304 gbp_vxlan_walk (gbp_vxlan_cb_t cb, void *ctx)
306 gbp_vxlan_tunnel_t *gt;
309 pool_foreach (gt, gbp_vxlan_tunnel_pool)
311 if (WALK_CONTINUE != cb(gt, ctx))
318 gbp_vxlan_tunnel_show_one (gbp_vxlan_tunnel_t * gt, void *ctx)
320 vlib_cli_output (ctx, "%U", format_gbp_vxlan_tunnel,
321 gt - gbp_vxlan_tunnel_pool);
323 return (WALK_CONTINUE);
327 format_gbp_vxlan_tunnel_name (u8 * s, va_list * args)
329 u32 dev_instance = va_arg (*args, u32);
331 return format (s, "gbp-vxlan-%d", dev_instance);
335 format_gbp_vxlan_tunnel_layer (u8 * s, va_list * args)
337 gbp_vxlan_tunnel_layer_t gl = va_arg (*args, gbp_vxlan_tunnel_layer_t);
338 s = format (s, "%s", gbp_vxlan_tunnel_layer_strings[gl]);
344 format_gbp_vxlan_tunnel (u8 * s, va_list * args)
346 u32 dev_instance = va_arg (*args, u32);
347 CLIB_UNUSED (int verbose) = va_arg (*args, int);
348 gbp_vxlan_tunnel_t *gt = gbp_vxlan_tunnel_get (dev_instance);
351 s = format (s, " [%d] gbp-vxlan-tunnel: hw:%d sw:%d vni:%d %U",
352 dev_instance, gt->gt_hw_if_index,
353 gt->gt_sw_if_index, gt->gt_vni,
354 format_gbp_vxlan_tunnel_layer, gt->gt_layer);
355 if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
356 s = format (s, " BD:%d gbd-index:%d", gt->gt_bd_rd_id, gt->gt_gbd);
358 s = format (s, " RD:%d grd-index:%d", gt->gt_bd_rd_id, gt->gt_grd);
360 s = format (s, " dependents:");
361 vec_foreach (vxri, gt->gt_tuns)
363 s = format (s, "\n %U, ", format_vxlan_tunnel_ref, *vxri);
369 typedef struct gbp_vxlan_tx_trace_t_
372 } gbp_vxlan_tx_trace_t;
375 format_gbp_vxlan_tx_trace (u8 * s, va_list * args)
377 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
378 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
379 gbp_vxlan_tx_trace_t *t = va_arg (*args, gbp_vxlan_tx_trace_t *);
381 s = format (s, "GBP-VXLAN: vni:%d", t->vni);
387 gbp_vxlan_interface_admin_up_down (vnet_main_t * vnm,
388 u32 hw_if_index, u32 flags)
390 vnet_hw_interface_t *hi;
393 hi = vnet_get_hw_interface (vnm, hw_if_index);
395 if (NULL == gbp_vxlan_tunnel_db ||
396 hi->sw_if_index >= vec_len (gbp_vxlan_tunnel_db))
399 ti = gbp_vxlan_tunnel_db[hi->sw_if_index];
402 /* not one of ours */
405 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
406 vnet_hw_interface_set_flags (vnm, hw_if_index,
407 VNET_HW_INTERFACE_FLAG_LINK_UP);
409 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
415 gbp_vxlan_interface_tx (vlib_main_t * vm,
416 vlib_node_runtime_t * node, vlib_frame_t * frame)
418 clib_warning ("you shouldn't be here, leaking buffers...");
419 return frame->n_vectors;
423 VNET_DEVICE_CLASS (gbp_vxlan_device_class) = {
424 .name = "GBP VXLAN tunnel-template",
425 .format_device_name = format_gbp_vxlan_tunnel_name,
426 .format_device = format_gbp_vxlan_tunnel,
427 .format_tx_trace = format_gbp_vxlan_tx_trace,
428 .admin_up_down_function = gbp_vxlan_interface_admin_up_down,
429 .tx_function = gbp_vxlan_interface_tx,
432 VNET_HW_INTERFACE_CLASS (gbp_vxlan_hw_interface_class) = {
434 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
439 gbp_vxlan_tunnel_add (u32 vni, gbp_vxlan_tunnel_layer_t layer,
441 const ip4_address_t * src, u32 * sw_if_indexp)
443 gbp_vxlan_tunnel_t *gt;
449 p = hash_get (gv_db, vni);
451 GBP_VXLAN_TUN_DBG ("add: %d %d %d", vni, layer, bd_rd_id);
455 vnet_sw_interface_t *si;
456 vnet_hw_interface_t *hi;
460 gbi = grdi = INDEX_INVALID;
462 if (layer == GBP_VXLAN_TUN_L2)
464 gbi = gbp_bridge_domain_find_and_lock (bd_rd_id);
466 if (INDEX_INVALID == gbi)
468 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
473 grdi = gbp_route_domain_find_and_lock (bd_rd_id);
475 if (INDEX_INVALID == grdi)
477 return (VNET_API_ERROR_NO_SUCH_FIB);
481 vnm = vnet_get_main ();
482 pool_get (gbp_vxlan_tunnel_pool, gt);
483 gti = gt - gbp_vxlan_tunnel_pool;
486 gt->gt_layer = layer;
487 gt->gt_bd_rd_id = bd_rd_id;
488 gt->gt_src.ip4.as_u32 = src->as_u32;
489 gt->gt_hw_if_index = vnet_register_interface (vnm,
490 gbp_vxlan_device_class.index,
492 gbp_vxlan_hw_interface_class.index,
495 hi = vnet_get_hw_interface (vnm, gt->gt_hw_if_index);
497 gt->gt_sw_if_index = hi->sw_if_index;
499 /* don't flood packets in a BD to these interfaces */
500 si = vnet_get_sw_interface (vnm, gt->gt_sw_if_index);
501 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
503 if (layer == GBP_VXLAN_TUN_L2)
505 gbp_bridge_domain_t *gb;
507 gb = gbp_bridge_domain_get (gbi);
511 /* set it up as a GBP interface */
512 gt->gt_itf = gbp_itf_l2_add_and_lock (gt->gt_sw_if_index,
514 gbp_itf_l2_set_input_feature (gt->gt_itf, L2INPUT_FEAT_GBP_LEARN);
519 gt->gt_itf = gbp_itf_l3_add_and_lock (gt->gt_sw_if_index,
521 gbp_itf_l3_set_input_feature (gt->gt_itf, GBP_ITF_L3_FEAT_LEARN);
525 * save the tunnel by VNI and by sw_if_index
527 hash_set (gv_db, vni, gti);
529 vec_validate_init_empty (gbp_vxlan_tunnel_db,
530 gt->gt_sw_if_index, INDEX_INVALID);
531 gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = gti;
534 *sw_if_indexp = gt->gt_sw_if_index;
536 vxlan_gbp_register_udp_ports ();
541 rv = VNET_API_ERROR_IF_ALREADY_EXISTS;
544 GBP_VXLAN_TUN_DBG ("add: %U", format_gbp_vxlan_tunnel, gti);
550 gbp_vxlan_tunnel_del (u32 vni)
552 gbp_vxlan_tunnel_t *gt;
555 p = hash_get (gv_db, vni);
561 vnm = vnet_get_main ();
562 gt = gbp_vxlan_tunnel_get (p[0]);
564 vxlan_gbp_unregister_udp_ports ();
566 GBP_VXLAN_TUN_DBG ("del: %U", format_gbp_vxlan_tunnel,
567 gt - gbp_vxlan_tunnel_pool);
569 gbp_endpoint_flush (GBP_ENDPOINT_SRC_DP, gt->gt_sw_if_index);
570 ASSERT (0 == vec_len (gt->gt_tuns));
571 vec_free (gt->gt_tuns);
573 gbp_itf_unlock (>->gt_itf);
575 if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
577 gbp_bridge_domain_unlock (gt->gt_gbd);
581 gbp_route_domain_unlock (gt->gt_grd);
584 vnet_sw_interface_set_flags (vnm, gt->gt_sw_if_index, 0);
585 vnet_delete_hw_interface (vnm, gt->gt_hw_if_index);
587 hash_unset (gv_db, vni);
588 gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = INDEX_INVALID;
590 pool_put (gbp_vxlan_tunnel_pool, gt);
593 return VNET_API_ERROR_NO_SUCH_ENTRY;
598 static clib_error_t *
599 gbp_vxlan_show (vlib_main_t * vm,
600 unformat_input_t * input, vlib_cli_command_t * cmd)
603 vlib_cli_output (vm, "GBP-VXLAN Interfaces:");
605 gbp_vxlan_walk (gbp_vxlan_tunnel_show_one, vm);
611 * Show Group Based Policy VXLAN tunnels
614 * @cliexstart{show gbp vxlan}
618 VLIB_CLI_COMMAND (gbp_vxlan_show_node, static) = {
619 .path = "show gbp vxlan",
620 .short_help = "show gbp vxlan\n",
621 .function = gbp_vxlan_show,
625 static clib_error_t *
626 gbp_vxlan_init (vlib_main_t * vm)
628 vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
630 gt_logger = vlib_log_register_class ("gbp", "tun");
632 punt_hdl = vlib_punt_client_register ("gbp-vxlan");
634 vlib_punt_register (punt_hdl,
635 vxm->punt_no_such_tunnel[FIB_PROTOCOL_IP4],
642 VLIB_INIT_FUNCTION (gbp_vxlan_init) =
644 .runs_after = VLIB_INITS("punt_init", "vxlan_gbp_init"),
649 * fd.io coding-style-patch-verification: ON
652 * eval: (c-set-style "gnu")