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 #include <vnet/ip/ip.h>
22 #define foreach_gbp_itf_mode \
26 typedef enum gbp_ift_mode_t_
28 #define _(s,v) GBP_ITF_MODE_##s,
34 * Attributes and configurations attached to interfaces by GBP
36 typedef struct gbp_itf_t_
39 * Number of references to this interface
44 * The interface this wrapper is managing
49 * The mode of the interface
51 gbp_itf_mode_t gi_mode;
54 * Users of this interface - this is encoded in the user's handle
59 * L2/L3 Features configured by each user
67 * function to call when the interface is deleted.
69 gbp_itf_free_fn_t gi_free_fn;
81 static gbp_itf_t *gbp_itf_pool;
82 static uword *gbp_itf_db;
84 static const char *gbp_itf_feat_bit_pos_to_arc[] = {
85 #define _(s,v,a) [GBP_ITF_L3_FEAT_POS_##s] = a,
86 foreach_gdb_l3_feature
90 static const char *gbp_itf_feat_bit_pos_to_feat[] = {
91 #define _(s,v,a) [GBP_ITF_L3_FEAT_POS_##s] = v,
92 foreach_gdb_l3_feature
97 format_gbp_itf_l3_feat (u8 * s, va_list * args)
99 gbp_itf_l3_feat_t flags = va_arg (*args, gbp_itf_l3_feat_t);
102 if (flags & GBP_ITF_L3_FEAT_##a) \
103 s = format (s, "%s ", b);
104 foreach_gdb_l3_feature
110 gbp_itf_hdl_reset (gbp_itf_hdl_t * gh)
112 *gh = GBP_ITF_HDL_INVALID;
116 gbp_itf_hdl_is_valid (gbp_itf_hdl_t gh)
118 return (gh.gh_which != GBP_ITF_HDL_INVALID.gh_which);
122 gbp_itf_get (index_t gii)
124 if (pool_is_free_index (gbp_itf_pool, gii))
127 return (pool_elt_at_index (gbp_itf_pool, gii));
131 gbp_itf_find (u32 sw_if_index)
135 p = hash_get (gbp_itf_db, sw_if_index);
138 return (gbp_itf_get (p[0]));
144 gbp_itf_find_hdl (gbp_itf_hdl_t gh)
146 return (gbp_itf_find (gh.gh_which));
150 gbp_itf_get_sw_if_index (gbp_itf_hdl_t hdl)
152 return (hdl.gh_which);
156 gbp_itf_mk_hdl (gbp_itf_t * gi)
161 pool_get (gi->gi_users, useri);
164 gh.gh_who = useri - gi->gi_users;
165 gh.gh_which = gi->gi_sw_if_index;
171 gbp_itf_l2_add_and_lock_i (u32 sw_if_index, index_t gbi, gbp_itf_free_fn_t ff)
175 gi = gbp_itf_find (sw_if_index);
179 pool_get_zero (gbp_itf_pool, gi);
181 gi->gi_sw_if_index = sw_if_index;
183 gi->gi_mode = GBP_ITF_MODE_L2;
186 gbp_bridge_domain_itf_add (gi->gi_gbi, gi->gi_sw_if_index,
187 L2_BD_PORT_TYPE_NORMAL);
189 hash_set (gbp_itf_db, gi->gi_sw_if_index, gi - gbp_itf_pool);
194 return (gbp_itf_mk_hdl (gi));
198 gbp_itf_l2_add_and_lock (u32 sw_if_index, index_t gbi)
200 return (gbp_itf_l2_add_and_lock_i (sw_if_index, gbi, NULL));
204 gbp_itf_l2_add_and_lock_w_free (u32 sw_if_index,
205 index_t gbi, gbp_itf_free_fn_t ff)
207 return (gbp_itf_l2_add_and_lock_i (sw_if_index, gbi, ff));
211 gbp_itf_l3_add_and_lock_i (u32 sw_if_index, index_t gri, gbp_itf_free_fn_t ff)
215 gi = gbp_itf_find (sw_if_index);
219 const gbp_route_domain_t *grd;
220 fib_protocol_t fproto;
222 pool_get_zero (gbp_itf_pool, gi);
224 gi->gi_sw_if_index = sw_if_index;
225 gi->gi_mode = GBP_ITF_MODE_L3;
229 grd = gbp_route_domain_get (gi->gi_gri);
231 ip4_sw_interface_enable_disable (gi->gi_sw_if_index, 1);
232 ip6_sw_interface_enable_disable (gi->gi_sw_if_index, 1);
234 FOR_EACH_FIB_IP_PROTOCOL (fproto)
235 ip_table_bind (fproto, gi->gi_sw_if_index,
236 grd->grd_table_id[fproto], 1);
238 hash_set (gbp_itf_db, gi->gi_sw_if_index, gi - gbp_itf_pool);
243 return (gbp_itf_mk_hdl (gi));
247 gbp_itf_l3_add_and_lock (u32 sw_if_index, index_t gri)
249 return (gbp_itf_l3_add_and_lock_i (sw_if_index, gri, NULL));
253 gbp_itf_l3_add_and_lock_w_free (u32 sw_if_index,
254 index_t gri, gbp_itf_free_fn_t ff)
256 return (gbp_itf_l3_add_and_lock_i (sw_if_index, gri, ff));
260 gbp_itf_lock (gbp_itf_hdl_t gh)
264 if (!gbp_itf_hdl_is_valid (gh))
267 gi = gbp_itf_find_hdl (gh);
273 gbp_itf_clone_and_lock (gbp_itf_hdl_t gh)
277 if (!gbp_itf_hdl_is_valid (gh))
278 return (GBP_ITF_HDL_INVALID);
280 gi = gbp_itf_find_hdl (gh);
284 return (gbp_itf_mk_hdl (gi));
288 gbp_itf_unlock (gbp_itf_hdl_t * gh)
292 if (!gbp_itf_hdl_is_valid (*gh))
295 gi = gbp_itf_find_hdl (*gh);
296 ASSERT (gi->gi_locks > 0);
299 if (0 == gi->gi_locks)
301 if (GBP_ITF_MODE_L2 == gi->gi_mode)
303 gbp_itf_l2_set_input_feature (*gh, L2INPUT_FEAT_NONE);
304 gbp_itf_l2_set_output_feature (*gh, L2OUTPUT_FEAT_NONE);
305 gbp_bridge_domain_itf_del (gi->gi_gbi,
307 L2_BD_PORT_TYPE_NORMAL);
311 fib_protocol_t fproto;
313 gbp_itf_l3_set_input_feature (*gh, GBP_ITF_L3_FEAT_NONE);
314 FOR_EACH_FIB_IP_PROTOCOL (fproto)
315 ip_table_bind (fproto, gi->gi_sw_if_index, 0, 0);
317 ip4_sw_interface_enable_disable (gi->gi_sw_if_index, 0);
318 ip6_sw_interface_enable_disable (gi->gi_sw_if_index, 0);
321 hash_unset (gbp_itf_db, gi->gi_sw_if_index);
324 gi->gi_free_fn (gi->gi_sw_if_index);
326 pool_free (gi->gi_users);
327 vec_free (gi->gi_input_fbs);
328 vec_free (gi->gi_output_fbs);
330 memset (gi, 0, sizeof (*gi));
333 gbp_itf_hdl_reset (gh);
337 gbp_itf_l3_set_input_feature (gbp_itf_hdl_t gh, gbp_itf_l3_feat_t feats)
339 u32 diff_fb, new_fb, *fb, feat;
342 gi = gbp_itf_find_hdl (gh);
344 if (NULL == gi || GBP_ITF_MODE_L3 != gi->gi_mode)
347 vec_validate (gi->gi_input_fbs, gh.gh_who);
348 gi->gi_input_fbs[gh.gh_who] = feats;
351 vec_foreach (fb, gi->gi_input_fbs)
356 /* add new features */
357 diff_fb = (gi->gi_input_fb ^ new_fb) & new_fb;
360 foreach_set_bit (feat, diff_fb,
362 vnet_feature_enable_disable (gbp_itf_feat_bit_pos_to_arc[feat],
363 gbp_itf_feat_bit_pos_to_feat[feat],
364 gi->gi_sw_if_index, 1, 0, 0);
368 /* remove unneeded features */
369 diff_fb = (gi->gi_input_fb ^ new_fb) & gi->gi_input_fb;
372 foreach_set_bit (feat, diff_fb,
374 vnet_feature_enable_disable (gbp_itf_feat_bit_pos_to_arc[feat],
375 gbp_itf_feat_bit_pos_to_feat[feat],
376 gi->gi_sw_if_index, 0, 0, 0);
380 gi->gi_input_fb = new_fb;
384 gbp_itf_l2_set_input_feature (gbp_itf_hdl_t gh, l2input_feat_masks_t feats)
386 u32 diff_fb, new_fb, *fb, feat;
389 gi = gbp_itf_find_hdl (gh);
391 if (NULL == gi || GBP_ITF_MODE_L2 != gi->gi_mode)
397 vec_validate (gi->gi_input_fbs, gh.gh_who);
398 gi->gi_input_fbs[gh.gh_who] = feats;
401 vec_foreach (fb, gi->gi_input_fbs)
406 /* add new features */
407 diff_fb = (gi->gi_input_fb ^ new_fb) & new_fb;
410 foreach_set_bit (feat, diff_fb,
412 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
416 /* remove unneeded features */
417 diff_fb = (gi->gi_input_fb ^ new_fb) & gi->gi_input_fb;
420 foreach_set_bit (feat, diff_fb,
422 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
426 gi->gi_input_fb = new_fb;
430 gbp_itf_l2_set_output_feature (gbp_itf_hdl_t gh, l2output_feat_masks_t feats)
432 u32 diff_fb, new_fb, *fb, feat;
435 gi = gbp_itf_find_hdl (gh);
437 if (NULL == gi || GBP_ITF_MODE_L2 != gi->gi_mode)
443 vec_validate (gi->gi_output_fbs, gh.gh_who);
444 gi->gi_output_fbs[gh.gh_who] = feats;
447 vec_foreach (fb, gi->gi_output_fbs)
452 /* add new features */
453 diff_fb = (gi->gi_output_fb ^ new_fb) & new_fb;
456 foreach_set_bit (feat, diff_fb,
458 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
462 /* remove unneeded features */
463 diff_fb = (gi->gi_output_fb ^ new_fb) & gi->gi_output_fb;
466 foreach_set_bit (feat, diff_fb,
468 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
472 gi->gi_output_fb = new_fb;
476 format_gbp_itf_mode (u8 * s, va_list * args)
478 gbp_itf_mode_t mode = va_arg (*args, gbp_itf_mode_t);
483 case GBP_ITF_MODE_##a: \
484 return format(s, "%s", v);
492 format_gbp_itf (u8 * s, va_list * args)
494 index_t gii = va_arg (*args, index_t);
497 if (INDEX_INVALID == gii)
498 return (format (s, "unset"));
500 gi = gbp_itf_get (gii);
502 s = format (s, "%U locks:%d mode:%U ",
503 format_vnet_sw_if_index_name, vnet_get_main (),
504 gi->gi_sw_if_index, gi->gi_locks,
505 format_gbp_itf_mode, gi->gi_mode);
507 if (GBP_ITF_MODE_L2 == gi->gi_mode)
508 s = format (s, "gbp-bd:%d input-feats:[%U] output-feats:[%U]",
510 format_l2_input_features, gi->gi_input_fb, 0,
511 format_l2_output_features, gi->gi_output_fb, 0);
513 s = format (s, "gbp-rd:%d input-feats:[%U] output-feats:[%U]",
515 format_gbp_itf_l3_feat, gi->gi_input_fb,
516 format_gbp_itf_l3_feat, gi->gi_output_fb);
522 format_gbp_itf_hdl (u8 * s, va_list * args)
524 gbp_itf_hdl_t gh = va_arg (*args, gbp_itf_hdl_t);
527 gi = gbp_itf_find_hdl (gh);
530 return format (s, "INVALID");
532 return (format (s, "%U", format_gbp_itf, gi - gbp_itf_pool));
535 static clib_error_t *
536 gbp_itf_show (vlib_main_t * vm,
537 unformat_input_t * input, vlib_cli_command_t * cmd)
541 vlib_cli_output (vm, "Interfaces:");
544 pool_foreach_index (gii, gbp_itf_pool)
546 vlib_cli_output (vm, " [%d] %U", gii, format_gbp_itf, gii);
554 * Show Group Based Interfaces
557 * @cliexstart{show gbp contract}
561 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
562 .path = "show gbp interface",
563 .short_help = "show gbp interface\n",
564 .function = gbp_itf_show,
570 * fd.io coding-style-patch-verification: ON
573 * eval: (c-set-style "gnu")