b026cdf1c8f54467e1e05996f11b21c94a0b96a0
[vpp.git] / vnet / vnet / feature / feature.h
1 /*
2  * Copyright (c) 2016 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 #ifndef included_features_h
17 #define included_features_h
18
19 #include <vnet/vnet.h>
20 #include <vnet/api_errno.h>
21
22 /** feature registration object */
23 typedef struct _vnet_feature_arc_registration
24 {
25   /** next registration in list of all registrations*/
26   struct _vnet_feature_arc_registration *next;
27   /** Feature Arc name */
28   char *arc_name;
29   /** Start nodes */
30   char **start_nodes;
31   int n_start_nodes;
32   /* Feature arc index, assigned by init function */
33   u8 feature_arc_index;
34   u8 *arc_index_ptr;
35 } vnet_feature_arc_registration_t;
36
37 /** feature registration object */
38 typedef struct _vnet_feature_registration
39 {
40   /** next registration in list of all registrations*/
41   struct _vnet_feature_registration *next;
42   /** Feature arc name */
43   char *arc_name;
44   /** Graph node name */
45   char *node_name;
46   /** Pointer to this feature index, filled in by vnet_feature_arc_init */
47   u32 *feature_index_ptr;
48   u32 feature_index;
49   /** Constraints of the form "this feature runs before X" */
50   char **runs_before;
51   /** Constraints of the form "this feature runs after Y" */
52   char **runs_after;
53 } vnet_feature_registration_t;
54
55 typedef struct vnet_feature_config_main_t_
56 {
57   vnet_config_main_t config_main;
58   u32 *config_index_by_sw_if_index;
59 } vnet_feature_config_main_t;
60
61 typedef struct
62 {
63   /** feature arc configuration list */
64   vnet_feature_arc_registration_t *next_arc;
65   uword **arc_index_by_name;
66
67   /** feature path configuration lists */
68   vnet_feature_registration_t *next_feature;
69   vnet_feature_registration_t **next_feature_by_arc;
70   uword **next_feature_by_name;
71
72   /** feature config main objects */
73   vnet_feature_config_main_t *feature_config_mains;
74
75   /** Save partial order results for show command */
76   char ***feature_nodes;
77
78   /** bitmap of interfaces which have driver rx features configured */
79   uword **sw_if_index_has_features;
80
81   /** feature reference counts by interface */
82   i16 **feature_count_by_sw_if_index;
83
84   /** Feature arc index for device-input */
85   u8 device_input_feature_arc_index;
86
87   /** convenience */
88   vlib_main_t *vlib_main;
89   vnet_main_t *vnet_main;
90 } vnet_feature_main_t;
91
92 extern vnet_feature_main_t feature_main;
93
94 #define VNET_FEATURE_ARC_INIT(x,...)                            \
95   __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
96 static void __vnet_add_feature_arc_registration_##x (void)      \
97   __attribute__((__constructor__)) ;                            \
98 static void __vnet_add_feature_arc_registration_##x (void)      \
99 {                                                               \
100   vnet_feature_main_t * fm = &feature_main;                     \
101   vnet_feat_arc_##x.next = fm->next_arc;                        \
102   fm->next_arc = & vnet_feat_arc_##x;                           \
103 }                                                               \
104 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
105
106 #define VNET_FEATURE_INIT(x,...)                                \
107   __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x;        \
108 static void __vnet_add_feature_registration_##x (void)          \
109   __attribute__((__constructor__)) ;                            \
110 static void __vnet_add_feature_registration_##x (void)          \
111 {                                                               \
112   vnet_feature_main_t * fm = &feature_main;                     \
113   vnet_feat_##x.next = fm->next_feature;                        \
114   fm->next_feature = & vnet_feat_##x;                           \
115 }                                                               \
116 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
117
118 void
119 vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
120                                   u32 sw_if_index, int is_add);
121
122 u32 vnet_get_feature_index (u8 arc, const char *s);
123 u8 vnet_get_feature_arc_index (const char *s);
124
125 int
126 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
127                                         u32 sw_if_index, int enable_disable,
128                                         void *feature_config,
129                                         u32 n_feature_config_bytes);
130
131 int
132 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
133                              u32 sw_if_index, int enable_disable,
134                              void *feature_config,
135                              u32 n_feature_config_bytes);
136
137 static inline vnet_feature_config_main_t *
138 vnet_get_feature_arc_config_main (u8 arc_index)
139 {
140   vnet_feature_main_t *fm = &feature_main;
141
142   if (arc_index == (u8) ~ 0)
143     return 0;
144
145   return &fm->feature_config_mains[arc_index];
146 }
147
148 static_always_inline int
149 vnet_have_features (u8 arc, u32 sw_if_index)
150 {
151   vnet_feature_main_t *fm = &feature_main;
152   return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
153 }
154
155 static_always_inline u32
156 vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
157 {
158   vnet_feature_main_t *fm = &feature_main;
159   vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
160   return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
161 }
162
163 static_always_inline void *
164 vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
165                                   vlib_buffer_t * b, u32 n_data_bytes)
166 {
167   vnet_feature_main_t *fm = &feature_main;
168   vnet_feature_config_main_t *cm;
169   cm = &fm->feature_config_mains[arc];
170
171   if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
172     {
173       b->feature_arc_index = arc;
174       b->current_config_index =
175         vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
176       return vnet_get_config_data (&cm->config_main, &b->current_config_index,
177                                    next, n_data_bytes);
178     }
179   return 0;
180 }
181
182 static_always_inline void
183 vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
184                         vlib_buffer_t * b0)
185 {
186   vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
187 }
188
189 static_always_inline void *
190 vnet_feature_next_with_data (u32 sw_if_index, u32 * next0,
191                              vlib_buffer_t * b0, u32 n_data_bytes)
192 {
193   vnet_feature_main_t *fm = &feature_main;
194   u8 arc = b0->feature_arc_index;
195   vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
196
197   return vnet_get_config_data (&cm->config_main,
198                                &b0->current_config_index, next0,
199                                n_data_bytes);
200 }
201
202 static_always_inline void
203 vnet_feature_next (u32 sw_if_index, u32 * next0, vlib_buffer_t * b0)
204 {
205   vnet_feature_next_with_data (sw_if_index, next0, b0, 0);
206 }
207
208 static_always_inline void
209 vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
210                                     vlib_buffer_t * b0, u16 buffer_advanced0)
211 {
212   vnet_feature_main_t *fm = &feature_main;
213   vnet_feature_config_main_t *cm;
214   u8 feature_arc_index = fm->device_input_feature_arc_index;
215   cm = &fm->feature_config_mains[feature_arc_index];
216
217   if (PREDICT_FALSE
218       (clib_bitmap_get
219        (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
220     {
221       /*
222        * Save next0 so that the last feature in the chain
223        * can skip ethernet-input if indicated...
224        */
225       vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
226       vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0;
227       vlib_buffer_advance (b0, -buffer_advanced0);
228
229       b0->feature_arc_index = feature_arc_index;
230       b0->current_config_index =
231         vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
232       vnet_get_config_data (&cm->config_main, &b0->current_config_index,
233                             next0, /* # bytes of config data */ 0);
234     }
235 }
236
237 static_always_inline void
238 vnet_feature_start_device_input_x2 (u32 sw_if_index,
239                                     u32 * next0,
240                                     u32 * next1,
241                                     vlib_buffer_t * b0,
242                                     vlib_buffer_t * b1,
243                                     u16 buffer_advanced0,
244                                     u16 buffer_advanced1)
245 {
246   vnet_feature_main_t *fm = &feature_main;
247   vnet_feature_config_main_t *cm;
248   u8 feature_arc_index = fm->device_input_feature_arc_index;
249   cm = &fm->feature_config_mains[feature_arc_index];
250
251   if (PREDICT_FALSE
252       (clib_bitmap_get
253        (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
254     {
255       /*
256        * Save next0 so that the last feature in the chain
257        * can skip ethernet-input if indicated...
258        */
259       vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
260       vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
261       vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0;
262       vnet_buffer (b1)->device_input_feat.buffer_advance = buffer_advanced1;
263       vlib_buffer_advance (b0, -buffer_advanced0);
264       vlib_buffer_advance (b1, -buffer_advanced1);
265
266       b0->feature_arc_index = feature_arc_index;
267       b1->feature_arc_index = feature_arc_index;
268       b0->current_config_index =
269         vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
270       b1->current_config_index = b0->current_config_index;
271       vnet_get_config_data (&cm->config_main, &b0->current_config_index,
272                             next0, /* # bytes of config data */ 0);
273       vnet_get_config_data (&cm->config_main, &b1->current_config_index,
274                             next1, /* # bytes of config data */ 0);
275     }
276 }
277
278
279 #define VNET_FEATURES(...)  (char*[]) { __VA_ARGS__, 0}
280
281 clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
282                                      vnet_config_main_t * vcm,
283                                      char **feature_start_nodes,
284                                      int num_feature_start_nodes,
285                                      vnet_feature_registration_t *
286                                      first_reg, char ***feature_nodes);
287
288 void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index);
289
290 #endif /* included_feature_h */
291
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */