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>
19 * Attributes and configurations attached to interfaces by GBP
21 typedef struct gbp_itf_t_
24 * Number of references to this interface
32 * L2/L3 Features configured by each user
36 u32 *gi_l2_output_fbs;
40 static gbp_itf_t *gbp_itfs;
43 gbp_itf_get (index_t gii)
45 vec_validate (gbp_itfs, gii);
47 return (&gbp_itfs[gii]);
51 gbp_itf_get_itf (u32 sw_if_index)
57 gbp_itf_add_and_lock (u32 sw_if_index, u32 bd_index)
61 gi = gbp_itf_get (gbp_itf_get_itf (sw_if_index));
63 if (0 == gi->gi_locks)
65 gi->gi_sw_if_index = sw_if_index;
66 gi->gi_bd_index = bd_index;
68 if (~0 != gi->gi_bd_index)
69 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
70 MODE_L2_BRIDGE, sw_if_index, bd_index,
71 L2_BD_PORT_TYPE_NORMAL, 0, 0);
81 gbp_itf_unlock (index_t gii)
85 gi = gbp_itf_get (gii);
86 ASSERT (gi->gi_locks > 0);
89 if (0 == gi->gi_locks)
91 if (~0 != gi->gi_bd_index)
92 set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L3,
93 gi->gi_sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL, 0, 0);
94 vec_free (gi->gi_l2_input_fbs);
95 vec_free (gi->gi_l2_output_fbs);
97 memset (gi, 0, sizeof (*gi));
102 gbp_itf_set_l2_input_feature (index_t gii,
103 index_t useri, l2input_feat_masks_t feats)
105 u32 diff_fb, new_fb, *fb, feat;
108 gi = gbp_itf_get (gii);
110 if (gi->gi_bd_index == ~0)
113 vec_validate (gi->gi_l2_input_fbs, useri);
114 gi->gi_l2_input_fbs[useri] = feats;
117 vec_foreach (fb, gi->gi_l2_input_fbs)
122 /* add new features */
123 diff_fb = (gi->gi_l2_input_fb ^ new_fb) & new_fb;
126 foreach_set_bit (feat, diff_fb,
128 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
132 /* remove unneeded features */
133 diff_fb = (gi->gi_l2_input_fb ^ new_fb) & gi->gi_l2_input_fb;
136 foreach_set_bit (feat, diff_fb,
138 l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
142 gi->gi_l2_input_fb = new_fb;
146 gbp_itf_set_l2_output_feature (index_t gii,
147 index_t useri, l2output_feat_masks_t feats)
149 u32 diff_fb, new_fb, *fb, feat;
152 gi = gbp_itf_get (gii);
154 if (gi->gi_bd_index == ~0)
157 vec_validate (gi->gi_l2_output_fbs, useri);
158 gi->gi_l2_output_fbs[useri] = feats;
161 vec_foreach (fb, gi->gi_l2_output_fbs)
166 /* add new features */
167 diff_fb = (gi->gi_l2_output_fb ^ new_fb) & new_fb;
170 foreach_set_bit (feat, diff_fb,
172 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
176 /* remove unneeded features */
177 diff_fb = (gi->gi_l2_output_fb ^ new_fb) & gi->gi_l2_output_fb;
180 foreach_set_bit (feat, diff_fb,
182 l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
186 gi->gi_l2_output_fb = new_fb;
190 format_gbp_itf (u8 * s, va_list * args)
192 index_t gii = va_arg (*args, index_t);
195 gi = gbp_itf_get (gii);
197 s = format (s, "%U locks:%d input-feats:%U output-feats:%U",
198 format_vnet_sw_if_index_name, vnet_get_main (),
199 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);
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")