e1afdfb94dc753792521adb35ebc008739141e12
[vpp.git] / src / plugins / gbp / gbp_itf.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <plugins/gbp/gbp_itf.h>
17
18 /**
19  * Attributes and configurations attached to interfaces by GBP
20  */
21 typedef struct gbp_itf_t_
22 {
23   /**
24    * Number of references to this interface
25    */
26   u32 gi_locks;
27
28   u32 gi_sw_if_index;
29   u32 gi_bd_index;
30
31   /**
32    * L2/L3 Features configured by each user
33    */
34   u32 *gi_l2_input_fbs;
35   u32 gi_l2_input_fb;
36   u32 *gi_l2_output_fbs;
37   u32 gi_l2_output_fb;
38 } gbp_itf_t;
39
40 static gbp_itf_t *gbp_itfs;
41
42 static gbp_itf_t *
43 gbp_itf_get (index_t gii)
44 {
45   vec_validate (gbp_itfs, gii);
46
47   return (&gbp_itfs[gii]);
48 }
49
50 static index_t
51 gbp_itf_get_itf (u32 sw_if_index)
52 {
53   return (sw_if_index);
54 }
55
56 index_t
57 gbp_itf_add_and_lock (u32 sw_if_index, u32 bd_index)
58 {
59   gbp_itf_t *gi;
60
61   gi = gbp_itf_get (gbp_itf_get_itf (sw_if_index));
62
63   if (0 == gi->gi_locks)
64     {
65       gi->gi_sw_if_index = sw_if_index;
66       gi->gi_bd_index = bd_index;
67
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);
72
73     }
74
75   gi->gi_locks++;
76
77   return (sw_if_index);
78 }
79
80 void
81 gbp_itf_unlock (index_t gii)
82 {
83   gbp_itf_t *gi;
84
85   gi = gbp_itf_get (gii);
86   ASSERT (gi->gi_locks > 0);
87   gi->gi_locks--;
88
89   if (0 == gi->gi_locks)
90     {
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);
96
97       memset (gi, 0, sizeof (*gi));
98     }
99 }
100
101 void
102 gbp_itf_set_l2_input_feature (index_t gii,
103                               index_t useri, l2input_feat_masks_t feats)
104 {
105   u32 diff_fb, new_fb, *fb, feat;
106   gbp_itf_t *gi;
107
108   gi = gbp_itf_get (gii);
109
110   if (gi->gi_bd_index == ~0)
111     return;
112
113   vec_validate (gi->gi_l2_input_fbs, useri);
114   gi->gi_l2_input_fbs[useri] = feats;
115
116   new_fb = 0;
117   vec_foreach (fb, gi->gi_l2_input_fbs)
118   {
119     new_fb |= *fb;
120   }
121
122   /* add new features */
123   diff_fb = (gi->gi_l2_input_fb ^ new_fb) & new_fb;
124
125   /* *INDENT-OFF* */
126   foreach_set_bit (feat, diff_fb,
127   ({
128     l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
129   }));
130   /* *INDENT-ON* */
131
132   /* remove unneeded features */
133   diff_fb = (gi->gi_l2_input_fb ^ new_fb) & gi->gi_l2_input_fb;
134
135   /* *INDENT-OFF* */
136   foreach_set_bit (feat, diff_fb,
137   ({
138     l2input_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
139   }));
140   /* *INDENT-ON* */
141
142   gi->gi_l2_input_fb = new_fb;
143 }
144
145 void
146 gbp_itf_set_l2_output_feature (index_t gii,
147                                index_t useri, l2output_feat_masks_t feats)
148 {
149   u32 diff_fb, new_fb, *fb, feat;
150   gbp_itf_t *gi;
151
152   gi = gbp_itf_get (gii);
153
154   if (gi->gi_bd_index == ~0)
155     return;
156
157   vec_validate (gi->gi_l2_output_fbs, useri);
158   gi->gi_l2_output_fbs[useri] = feats;
159
160   new_fb = 0;
161   vec_foreach (fb, gi->gi_l2_output_fbs)
162   {
163     new_fb |= *fb;
164   }
165
166   /* add new features */
167   diff_fb = (gi->gi_l2_output_fb ^ new_fb) & new_fb;
168
169   /* *INDENT-OFF* */
170   foreach_set_bit (feat, diff_fb,
171   ({
172     l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 1);
173   }));
174   /* *INDENT-ON* */
175
176   /* remove unneeded features */
177   diff_fb = (gi->gi_l2_output_fb ^ new_fb) & gi->gi_l2_output_fb;
178
179   /* *INDENT-OFF* */
180   foreach_set_bit (feat, diff_fb,
181   ({
182     l2output_intf_bitmap_enable (gi->gi_sw_if_index, (1 << feat), 0);
183   }));
184   /* *INDENT-ON* */
185
186   gi->gi_l2_output_fb = new_fb;
187 }
188
189 u8 *
190 format_gbp_itf (u8 * s, va_list * args)
191 {
192   index_t gii = va_arg (*args, index_t);
193   gbp_itf_t *gi;
194
195   gi = gbp_itf_get (gii);
196
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, format_l2_input_features, 0,
200               gi->gi_l2_input_fb, format_l2_output_features, 0,
201               gi->gi_l2_output_fb);
202
203   return (s);
204 }
205
206 /*
207  * fd.io coding-style-patch-verification: ON
208  *
209  * Local Variables:
210  * eval: (c-set-style "gnu")
211  * End:
212  */