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_itf.h>
17 #include <plugins/gbp/gbp_bridge_domain.h>
18 #include <plugins/gbp/gbp_route_domain.h>
20 #define foreach_gbp_itf_mode \
24 typedef enum gbp_ift_mode_t_
26 #define _(s,v) GBP_ITF_MODE_##s,
32 * Attributes and configurations attached to interfaces by GBP
34 typedef struct gbp_itf_t_
37 * Number of references to this interface
42 * The interface this wrapper is managing
47 * The mode of the interface
49 gbp_itf_mode_t gi_mode;
52 * Users of this interface - this is encoded in the user's handle
57 * L2/L3 Features configured by each user
65 * function to call when the interface is deleted.
67 gbp_itf_free_fn_t gi_free_fn;
79 static gbp_itf_t *gbp_itf_pool;
80 static uword *gbp_itf_db;
82 static const char *gbp_itf_feat_bit_pos_to_arc[] = {
83 #define _(s,v,a) [GBP_ITF_L3_FEAT_POS_##s] = a,
84 foreach_gdb_l3_feature
88 static const char *gbp_itf_feat_bit_pos_to_feat[] = {
89 #define _(s,v,a) [GBP_ITF_L3_FEAT_POS_##s] = v,
90 foreach_gdb_l3_feature
95 format_gbp_itf_l3_feat (u8 * s, va_list * args)
97 gbp_itf_l3_feat_t flags = va_arg (*args, gbp_itf_l3_feat_t);
100 if (flags & GBP_ITF_L3_FEAT_##a) \
101 s = format (s, "%s ", b);
102 foreach_gdb_l3_feature
108 gbp_itf_hdl_reset (gbp_itf_hdl_t * gh)
110 *gh = GBP_ITF_HDL_INVALID;
114 gbp_itf_hdl_is_valid (gbp_itf_hdl_t gh)
116 return (gh.gh_which != GBP_ITF_HDL_INVALID.gh_which);
120 gbp_itf_get (index_t gii)
122 if (pool_is_free_index (gbp_itf_pool, gii))
125 return (pool_elt_at_index (gbp_itf_pool, gii));
129 gbp_itf_find (u32 sw_if_index)
133 p = hash_get (gbp_itf_db, sw_if_index);
136 return (gbp_itf_get (p[0]));
142 gbp_itf_find_hdl (gbp_itf_hdl_t gh)
144 return (gbp_itf_find (gh.gh_which));
148 gbp_itf_get_sw_if_index (gbp_itf_hdl_t hdl)
150 return (hdl.gh_which);
154 gbp_itf_mk_hdl (gbp_itf_t * gi)
159 pool_get (gi->gi_users, useri);
162 gh.gh_who = useri - gi->gi_users;
163 gh.gh_which = gi->gi_sw_if_index;
169 gbp_itf_l2_add_and_lock_i (u32 sw_if_index, index_t gbi, gbp_itf_free_fn_t ff)
173 gi = gbp_itf_find (sw_if_index);
177 pool_get_zero (gbp_itf_pool, gi);
179 gi->gi_sw_if_index = sw_if_index;
181 gi->gi_mode = GBP_ITF_MODE_L2;
184 gbp_bridge_domain_itf_add (gi->gi_gbi, gi->gi_sw_if_index,
185 L2_BD_PORT_TYPE_NORMAL);
187 hash_set (gbp_itf_db, gi->gi_sw_if_index, gi - gbp_itf_pool);
192 return (gbp_itf_mk_hdl (gi));
196 gbp_itf_l2_add_and_lock (u32 sw_if_index, index_t gbi)
198 return (gbp_itf_l2_add_and_lock_i (sw_if_index, gbi, NULL));
202 gbp_itf_l2_add_and_lock_w_free (u32 sw_if_index,
203 index_t gbi, gbp_itf_free_fn_t ff)
205 return (gbp_itf_l2_add_and_lock_i (sw_if_index, gbi, ff));
209 gbp_itf_l3_add_and_lock_i (u32 sw_if_index, index_t gri, gbp_itf_free_fn_t ff)
213 gi = gbp_itf_find (sw_if_index);
217 const gbp_route_domain_t *grd;
218 fib_protocol_t fproto;
220 pool_get_zero (gbp_itf_pool, gi);
222 gi->gi_sw_if_index = sw_if_index;
223 gi->gi_mode = GBP_ITF_MODE_L3;
227 grd = gbp_route_domain_get (gi->gi_gri);
229 ip4_sw_interface_enable_disable (gi->gi_sw_if_index, 1);
230 ip6_sw_interface_enable_disable (gi->gi_sw_if_index, 1);
232 FOR_EACH_FIB_IP_PROTOCOL (fproto)
233 ip_table_bind (fproto, gi->gi_sw_if_index,
234 grd->grd_table_id[fproto], 1);
236 hash_set (gbp_itf_db, gi->gi_sw_if_index, gi - gbp_itf_pool);
241 return (gbp_itf_mk_hdl (gi));
245 gbp_itf_l3_add_and_lock (u32 sw_if_index, index_t gri)
247 return (gbp_itf_l3_add_and_lock_i (sw_if_index, gri, NULL));
251 gbp_itf_l3_add_and_lock_w_free (u32 sw_if_index,
252 index_t gri, gbp_itf_free_fn_t ff)
254 return (gbp_itf_l3_add_and_lock_i (sw_if_index, gri, ff));
258 gbp_itf_lock (gbp_itf_hdl_t gh)
262 if (!gbp_itf_hdl_is_valid (gh))
265 gi = gbp_itf_find_hdl (gh);
271 gbp_itf_clone_and_lock (gbp_itf_hdl_t gh)
275 if (!gbp_itf_hdl_is_valid (gh))
276 return (GBP_ITF_HDL_INVALID);
278 gi = gbp_itf_find_hdl (gh);
282 return (gbp_itf_mk_hdl (gi));
286 gbp_itf_unlock (gbp_itf_hdl_t * gh)
290 if (!gbp_itf_hdl_is_valid (*gh))
293 gi = gbp_itf_find_hdl (*gh);
294 ASSERT (gi->gi_locks > 0);
297 if (0 == gi->gi_locks)
299 if (GBP_ITF_MODE_L2 == gi->gi_mode)
301 gbp_itf_l2_set_input_feature (*gh, L2INPUT_FEAT_NONE);
302 gbp_itf_l2_set_output_feature (*gh, L2OUTPUT_FEAT_NONE);
303 gbp_bridge_domain_itf_del (gi->gi_gbi,
305 L2_BD_PORT_TYPE_NORMAL);
309 fib_protocol_t fproto;
311 gbp_itf_l3_set_input_feature (*gh, GBP_ITF_L3_FEAT_NONE);
312 FOR_EACH_FIB_IP_PROTOCOL (fproto)
313 ip_table_bind (fproto, gi->gi_sw_if_index, 0, 0);
315 ip4_sw_interface_enable_disable (gi->gi_sw_if_index, 0);
316 ip6_sw_interface_enable_disable (gi->gi_sw_if_index, 0);
319 hash_unset (gbp_itf_db, gi->gi_sw_if_index);
322 gi->gi_free_fn (gi->gi_sw_if_index);
324 pool_free (gi->gi_users);
325 vec_free (gi->gi_input_fbs);
326 vec_free (gi->gi_output_fbs);
328 memset (gi, 0, sizeof (*gi));
331 gbp_itf_hdl_reset (gh);
335 gbp_itf_l3_set_input_feature (gbp_itf_hdl_t gh, gbp_itf_l3_feat_t feats)
337 u32 diff_fb, new_fb, *fb, feat;
340 gi = gbp_itf_find_hdl (gh);
342 if (NULL == gi || GBP_ITF_MODE_L3 != gi->gi_mode)
345 vec_validate (gi->gi_input_fbs, gh.gh_who);
346 gi->gi_input_fbs[gh.gh_who] = feats;
349 vec_foreach (fb, gi->gi_input_fbs)
354 /* add new features */
355 diff_fb = (gi->gi_input_fb ^ new_fb) & new_fb;
358 foreach_set_bit (feat, diff_fb,
360 vnet_feature_enable_disable (gbp_itf_feat_bit_pos_to_arc[feat],
361 gbp_itf_feat_bit_pos_to_feat[feat],
362 gi->gi_sw_if_index, 1, 0, 0);
366 /* remove unneeded features */
367 diff_fb = (gi->gi_input_fb ^ new_fb) & gi->gi_input_fb;
370 foreach_set_bit (feat, diff_fb,
372 vnet_feature_enable_disable (gbp_itf_feat_bit_pos_to_arc[feat],
373 gbp_itf_feat_bit_pos_to_feat[feat],
374 gi->gi_sw_if_index, 0, 0, 0);
378 gi->gi_input_fb = new_fb;
382 gbp_itf_l2_set_input_feature (gbp_itf_hdl_t gh, l2input_feat_masks_t feats)
384 u32 diff_fb, new_fb, *fb, feat;
387 gi = gbp_itf_find_hdl (gh);
389 if (NULL == gi || GBP_ITF_MODE_L2 != gi->gi_mode)
395 vec_validate (gi->gi_input_fbs, gh.gh_who);
396 gi->gi_input_fbs[gh.gh_who] = feats;
399 vec_foreach (fb, gi->gi_input_fbs)
404 /* add new features */
405 diff_fb = (gi->gi_input_fb ^ new_fb) & new_fb;
408 foreach_set_bit (feat, diff_fb,
410 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
414 /* remove unneeded features */
415 diff_fb = (gi->gi_input_fb ^ new_fb) & gi->gi_input_fb;
418 foreach_set_bit (feat, diff_fb,
420 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
424 gi->gi_input_fb = new_fb;
428 gbp_itf_l2_set_output_feature (gbp_itf_hdl_t gh, l2output_feat_masks_t feats)
430 u32 diff_fb, new_fb, *fb, feat;
433 gi = gbp_itf_find_hdl (gh);
435 if (NULL == gi || GBP_ITF_MODE_L2 != gi->gi_mode)
441 vec_validate (gi->gi_output_fbs, gh.gh_who);
442 gi->gi_output_fbs[gh.gh_who] = feats;
445 vec_foreach (fb, gi->gi_output_fbs)
450 /* add new features */
451 diff_fb = (gi->gi_output_fb ^ new_fb) & new_fb;
454 foreach_set_bit (feat, diff_fb,
456 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
460 /* remove unneeded features */
461 diff_fb = (gi->gi_output_fb ^ new_fb) & gi->gi_output_fb;
464 foreach_set_bit (feat, diff_fb,
466 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
470 gi->gi_output_fb = new_fb;
474 format_gbp_itf_mode (u8 * s, va_list * args)
476 gbp_itf_mode_t mode = va_arg (*args, gbp_itf_mode_t);
481 case GBP_ITF_MODE_##a: \
482 return format(s, "%s", v);
490 format_gbp_itf (u8 * s, va_list * args)
492 index_t gii = va_arg (*args, index_t);
495 if (INDEX_INVALID == gii)
496 return (format (s, "unset"));
498 gi = gbp_itf_get (gii);
500 s = format (s, "%U locks:%d mode:%U ",
501 format_vnet_sw_if_index_name, vnet_get_main (),
502 gi->gi_sw_if_index, gi->gi_locks,
503 format_gbp_itf_mode, gi->gi_mode);
505 if (GBP_ITF_MODE_L2 == gi->gi_mode)
506 s = format (s, "gbp-bd:%d input-feats:[%U] output-feats:[%U]",
508 format_l2_input_features, gi->gi_input_fb, 0,
509 format_l2_output_features, gi->gi_output_fb, 0);
511 s = format (s, "gbp-rd:%d input-feats:[%U] output-feats:[%U]",
513 format_gbp_itf_l3_feat, gi->gi_input_fb,
514 format_gbp_itf_l3_feat, gi->gi_output_fb);
520 format_gbp_itf_hdl (u8 * s, va_list * args)
522 gbp_itf_hdl_t gh = va_arg (*args, gbp_itf_hdl_t);
525 gi = gbp_itf_find_hdl (gh);
528 return format (s, "INVALID");
530 return (format (s, "%U", format_gbp_itf, gi - gbp_itf_pool));
533 static clib_error_t *
534 gbp_itf_show (vlib_main_t * vm,
535 unformat_input_t * input, vlib_cli_command_t * cmd)
539 vlib_cli_output (vm, "Interfaces:");
542 pool_foreach_index (gii, gbp_itf_pool,
544 vlib_cli_output (vm, " [%d] %U", gii, format_gbp_itf, gii);
552 * Show Group Based Interfaces
555 * @cliexstart{show gbp contract}
559 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
560 .path = "show gbp interface",
561 .short_help = "show gbp interface\n",
562 .function = gbp_itf_show,
568 * fd.io coding-style-patch-verification: ON
571 * eval: (c-set-style "gnu")