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>
20 * Attributes and configurations attached to interfaces by GBP
22 typedef struct gbp_itf_t_
25 * Number of references to this interface
33 * L2/L3 Features configured by each user
37 u32 *gi_l2_output_fbs;
41 static gbp_itf_t *gbp_itfs;
44 gbp_itf_get (index_t gii)
46 vec_validate (gbp_itfs, gii);
48 return (&gbp_itfs[gii]);
52 gbp_itf_get_itf (u32 sw_if_index)
58 gbp_itf_add_and_lock (u32 sw_if_index, u32 bd_index)
62 gi = gbp_itf_get (gbp_itf_get_itf (sw_if_index));
64 if (0 == gi->gi_locks)
66 gi->gi_sw_if_index = sw_if_index;
67 gi->gi_bd_index = bd_index;
69 if (~0 != gi->gi_bd_index)
70 gbp_bridge_domain_itf_add (sw_if_index, bd_index,
71 L2_BD_PORT_TYPE_NORMAL);
80 gbp_itf_unlock (index_t gii)
84 gi = gbp_itf_get (gii);
85 ASSERT (gi->gi_locks > 0);
88 if (0 == gi->gi_locks)
90 if (~0 != gi->gi_bd_index)
91 gbp_bridge_domain_itf_del (gi->gi_sw_if_index, gi->gi_bd_index,
92 L2_BD_PORT_TYPE_NORMAL);
93 vec_free (gi->gi_l2_input_fbs);
94 vec_free (gi->gi_l2_output_fbs);
96 memset (gi, 0, sizeof (*gi));
101 gbp_itf_set_l2_input_feature (index_t gii,
102 index_t useri, l2input_feat_masks_t feats)
104 u32 diff_fb, new_fb, *fb, feat;
107 gi = gbp_itf_get (gii);
109 if (gi->gi_bd_index == ~0)
112 vec_validate (gi->gi_l2_input_fbs, useri);
113 gi->gi_l2_input_fbs[useri] = feats;
116 vec_foreach (fb, gi->gi_l2_input_fbs)
121 /* add new features */
122 diff_fb = (gi->gi_l2_input_fb ^ new_fb) & new_fb;
125 foreach_set_bit (feat, diff_fb,
127 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
131 /* remove unneeded features */
132 diff_fb = (gi->gi_l2_input_fb ^ new_fb) & gi->gi_l2_input_fb;
135 foreach_set_bit (feat, diff_fb,
137 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
141 gi->gi_l2_input_fb = new_fb;
145 gbp_itf_set_l2_output_feature (index_t gii,
146 index_t useri, l2output_feat_masks_t feats)
148 u32 diff_fb, new_fb, *fb, feat;
151 gi = gbp_itf_get (gii);
153 if (gi->gi_bd_index == ~0)
156 vec_validate (gi->gi_l2_output_fbs, useri);
157 gi->gi_l2_output_fbs[useri] = feats;
160 vec_foreach (fb, gi->gi_l2_output_fbs)
165 /* add new features */
166 diff_fb = (gi->gi_l2_output_fb ^ new_fb) & new_fb;
169 foreach_set_bit (feat, diff_fb,
171 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
175 /* remove unneeded features */
176 diff_fb = (gi->gi_l2_output_fb ^ new_fb) & gi->gi_l2_output_fb;
179 foreach_set_bit (feat, diff_fb,
181 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
185 gi->gi_l2_output_fb = new_fb;
189 format_gbp_itf (u8 * s, va_list * args)
191 index_t gii = va_arg (*args, index_t);
194 gi = gbp_itf_get (gii);
196 s = format (s, "%U locks:%d bd-index:%d input-feats:%U output-feats:%U",
197 format_vnet_sw_if_index_name, vnet_get_main (),
198 gi->gi_sw_if_index, gi->gi_locks,
200 format_l2_input_features, gi->gi_l2_input_fb, 0,
201 format_l2_output_features, gi->gi_l2_output_fb, 0);
206 static clib_error_t *
207 gbp_itf_show (vlib_main_t * vm,
208 unformat_input_t * input, vlib_cli_command_t * cmd)
212 vlib_cli_output (vm, "Interfaces:");
214 vec_foreach_index (gii, gbp_itfs)
216 vlib_cli_output (vm, " [%d] %U", gii, format_gbp_itf, gii);
223 * Show Group Based Interfaces
226 * @cliexstart{show gbp contract}
230 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
231 .path = "show gbp interface",
232 .short_help = "show gbp interface\n",
233 .function = gbp_itf_show,
239 * fd.io coding-style-patch-verification: ON
242 * eval: (c-set-style "gnu")