blob: 7ec43ea853b55b7953e1eba5aabf4b7809dc48c4 [file] [log] [blame]
Damjan Marion22311502016-10-28 20:30:15 +02001/*
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>
Damjan Marion96b41f72016-11-10 18:01:42 +010020#include <vnet/api_errno.h>
Damjan Marion35af9e52017-03-06 12:02:50 +010021#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020022
23/** feature registration object */
24typedef struct _vnet_feature_arc_registration
25{
26 /** next registration in list of all registrations*/
27 struct _vnet_feature_arc_registration *next;
28 /** Feature Arc name */
29 char *arc_name;
30 /** Start nodes */
31 char **start_nodes;
32 int n_start_nodes;
33 /* Feature arc index, assigned by init function */
Damjan Marion87cd1192016-11-04 11:00:27 +010034 u8 feature_arc_index;
Damjan Marion8b3191e2016-11-09 19:54:20 +010035 u8 *arc_index_ptr;
Damjan Marion22311502016-10-28 20:30:15 +020036} vnet_feature_arc_registration_t;
37
Pavel Kotucek7490a752016-11-15 09:19:11 +010038/* Enable feature callback. */
39typedef clib_error_t *(vnet_feature_enable_disable_function_t)
40 (u32 sw_if_index, int enable_disable);
41
Damjan Marion22311502016-10-28 20:30:15 +020042/** feature registration object */
43typedef struct _vnet_feature_registration
44{
45 /** next registration in list of all registrations*/
46 struct _vnet_feature_registration *next;
47 /** Feature arc name */
48 char *arc_name;
49 /** Graph node name */
50 char *node_name;
51 /** Pointer to this feature index, filled in by vnet_feature_arc_init */
Damjan Marion8b3191e2016-11-09 19:54:20 +010052 u32 *feature_index_ptr;
53 u32 feature_index;
Damjan Marion22311502016-10-28 20:30:15 +020054 /** Constraints of the form "this feature runs before X" */
55 char **runs_before;
56 /** Constraints of the form "this feature runs after Y" */
57 char **runs_after;
Pavel Kotucek7490a752016-11-15 09:19:11 +010058
59 /** Function to enable/disable feature **/
60 vnet_feature_enable_disable_function_t *enable_disable_cb;
Damjan Marion22311502016-10-28 20:30:15 +020061} vnet_feature_registration_t;
62
63typedef struct vnet_feature_config_main_t_
64{
65 vnet_config_main_t config_main;
66 u32 *config_index_by_sw_if_index;
67} vnet_feature_config_main_t;
68
69typedef struct
70{
71 /** feature arc configuration list */
72 vnet_feature_arc_registration_t *next_arc;
73 uword **arc_index_by_name;
74
75 /** feature path configuration lists */
76 vnet_feature_registration_t *next_feature;
77 vnet_feature_registration_t **next_feature_by_arc;
78 uword **next_feature_by_name;
79
80 /** feature config main objects */
81 vnet_feature_config_main_t *feature_config_mains;
82
83 /** Save partial order results for show command */
84 char ***feature_nodes;
85
86 /** bitmap of interfaces which have driver rx features configured */
87 uword **sw_if_index_has_features;
88
89 /** feature reference counts by interface */
90 i16 **feature_count_by_sw_if_index;
91
Damjan Marion87cd1192016-11-04 11:00:27 +010092 /** Feature arc index for device-input */
93 u8 device_input_feature_arc_index;
94
Damjan Marion22311502016-10-28 20:30:15 +020095 /** convenience */
96 vlib_main_t *vlib_main;
97 vnet_main_t *vnet_main;
98} vnet_feature_main_t;
99
100extern vnet_feature_main_t feature_main;
101
102#define VNET_FEATURE_ARC_INIT(x,...) \
103 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
104static void __vnet_add_feature_arc_registration_##x (void) \
105 __attribute__((__constructor__)) ; \
106static void __vnet_add_feature_arc_registration_##x (void) \
107{ \
108 vnet_feature_main_t * fm = &feature_main; \
109 vnet_feat_arc_##x.next = fm->next_arc; \
110 fm->next_arc = & vnet_feat_arc_##x; \
111} \
112__VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
113
114#define VNET_FEATURE_INIT(x,...) \
115 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
116static void __vnet_add_feature_registration_##x (void) \
117 __attribute__((__constructor__)) ; \
118static void __vnet_add_feature_registration_##x (void) \
119{ \
120 vnet_feature_main_t * fm = &feature_main; \
121 vnet_feat_##x.next = fm->next_feature; \
122 fm->next_feature = & vnet_feat_##x; \
123} \
124__VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
125
126void
Damjan Marion87cd1192016-11-04 11:00:27 +0100127vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
Damjan Marion22311502016-10-28 20:30:15 +0200128 u32 sw_if_index, int is_add);
129
Damjan Marion87cd1192016-11-04 11:00:27 +0100130u32 vnet_get_feature_index (u8 arc, const char *s);
131u8 vnet_get_feature_arc_index (const char *s);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100132vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
133 const char *node_name);
134
Damjan Marion22311502016-10-28 20:30:15 +0200135
Damjan Marion96b41f72016-11-10 18:01:42 +0100136int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100137vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
138 u32 sw_if_index, int enable_disable,
139 void *feature_config,
140 u32 n_feature_config_bytes);
141
142int
Damjan Marion22311502016-10-28 20:30:15 +0200143vnet_feature_enable_disable (const char *arc_name, const char *node_name,
144 u32 sw_if_index, int enable_disable,
145 void *feature_config,
146 u32 n_feature_config_bytes);
147
Damjan Marion8b3191e2016-11-09 19:54:20 +0100148static inline vnet_feature_config_main_t *
149vnet_get_feature_arc_config_main (u8 arc_index)
150{
151 vnet_feature_main_t *fm = &feature_main;
152
153 if (arc_index == (u8) ~ 0)
154 return 0;
155
156 return &fm->feature_config_mains[arc_index];
157}
Damjan Marion22311502016-10-28 20:30:15 +0200158
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100159static_always_inline vnet_feature_config_main_t *
160vnet_feature_get_config_main (u16 arc)
161{
162 vnet_feature_main_t *fm = &feature_main;
163 return &fm->feature_config_mains[arc];
164}
165
Damjan Marion22311502016-10-28 20:30:15 +0200166static_always_inline int
Damjan Marion87cd1192016-11-04 11:00:27 +0100167vnet_have_features (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200168{
169 vnet_feature_main_t *fm = &feature_main;
170 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
171}
172
173static_always_inline u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100174vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200175{
176 vnet_feature_main_t *fm = &feature_main;
177 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
178 return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
179}
180
Damjan Marion87cd1192016-11-04 11:00:27 +0100181static_always_inline void *
182vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
183 vlib_buffer_t * b, u32 n_data_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200184{
185 vnet_feature_main_t *fm = &feature_main;
Damjan Marion87cd1192016-11-04 11:00:27 +0100186 vnet_feature_config_main_t *cm;
187 cm = &fm->feature_config_mains[arc];
Damjan Marion22311502016-10-28 20:30:15 +0200188
189 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
190 {
Damjan Marion87cd1192016-11-04 11:00:27 +0100191 b->feature_arc_index = arc;
192 b->current_config_index =
Damjan Marion22311502016-10-28 20:30:15 +0200193 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
Damjan Marion87cd1192016-11-04 11:00:27 +0100194 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
195 next, n_data_bytes);
Damjan Marion22311502016-10-28 20:30:15 +0200196 }
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100197 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200198}
199
200static_always_inline void
Damjan Marion87cd1192016-11-04 11:00:27 +0100201vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
202 vlib_buffer_t * b0)
203{
204 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
205}
206
207static_always_inline void *
208vnet_feature_next_with_data (u32 sw_if_index, u32 * next0,
209 vlib_buffer_t * b0, u32 n_data_bytes)
210{
211 vnet_feature_main_t *fm = &feature_main;
212 u8 arc = b0->feature_arc_index;
213 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
214
215 return vnet_get_config_data (&cm->config_main,
216 &b0->current_config_index, next0,
217 n_data_bytes);
218}
219
220static_always_inline void
221vnet_feature_next (u32 sw_if_index, u32 * next0, vlib_buffer_t * b0)
222{
223 vnet_feature_next_with_data (sw_if_index, next0, b0, 0);
224}
225
226static_always_inline void
227vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
Damjan Marion35af9e52017-03-06 12:02:50 +0100228 vlib_buffer_t * b0)
Damjan Marion22311502016-10-28 20:30:15 +0200229{
230 vnet_feature_main_t *fm = &feature_main;
231 vnet_feature_config_main_t *cm;
Damjan Marion87cd1192016-11-04 11:00:27 +0100232 u8 feature_arc_index = fm->device_input_feature_arc_index;
233 cm = &fm->feature_config_mains[feature_arc_index];
Damjan Marion22311502016-10-28 20:30:15 +0200234
235 if (PREDICT_FALSE
236 (clib_bitmap_get
Damjan Marion87cd1192016-11-04 11:00:27 +0100237 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
Damjan Marion22311502016-10-28 20:30:15 +0200238 {
239 /*
240 * Save next0 so that the last feature in the chain
241 * can skip ethernet-input if indicated...
242 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100243 u16 adv;
244
Damjan Marion22311502016-10-28 20:30:15 +0200245 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100246 adv = device_input_next_node_advance[*next0];
247 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
248 vlib_buffer_advance (b0, -adv);
Damjan Marion22311502016-10-28 20:30:15 +0200249
Damjan Marion87cd1192016-11-04 11:00:27 +0100250 b0->feature_arc_index = feature_arc_index;
Damjan Marion22311502016-10-28 20:30:15 +0200251 b0->current_config_index =
252 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
253 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
254 next0, /* # bytes of config data */ 0);
255 }
256}
257
Damjan Marion87cd1192016-11-04 11:00:27 +0100258static_always_inline void
259vnet_feature_start_device_input_x2 (u32 sw_if_index,
260 u32 * next0,
261 u32 * next1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100262 vlib_buffer_t * b0, vlib_buffer_t * b1)
Damjan Marion87cd1192016-11-04 11:00:27 +0100263{
264 vnet_feature_main_t *fm = &feature_main;
265 vnet_feature_config_main_t *cm;
266 u8 feature_arc_index = fm->device_input_feature_arc_index;
267 cm = &fm->feature_config_mains[feature_arc_index];
268
269 if (PREDICT_FALSE
270 (clib_bitmap_get
271 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
272 {
273 /*
274 * Save next0 so that the last feature in the chain
275 * can skip ethernet-input if indicated...
276 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100277 u16 adv;
278
Damjan Marion87cd1192016-11-04 11:00:27 +0100279 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100280 adv = device_input_next_node_advance[*next0];
281 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
282 vlib_buffer_advance (b0, -adv);
283
Damjan Marion87cd1192016-11-04 11:00:27 +0100284 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
Damjan Marion35af9e52017-03-06 12:02:50 +0100285 adv = device_input_next_node_advance[*next1];
286 vnet_buffer (b1)->device_input_feat.buffer_advance = adv;
287 vlib_buffer_advance (b1, -adv);
Damjan Marion87cd1192016-11-04 11:00:27 +0100288
289 b0->feature_arc_index = feature_arc_index;
290 b1->feature_arc_index = feature_arc_index;
291 b0->current_config_index =
292 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
293 b1->current_config_index = b0->current_config_index;
294 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
295 next0, /* # bytes of config data */ 0);
296 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
297 next1, /* # bytes of config data */ 0);
298 }
299}
300
Damjan Marion7dc41462016-11-15 19:47:58 +0100301static_always_inline void
302vnet_feature_start_device_input_x4 (u32 sw_if_index,
303 u32 * next0,
304 u32 * next1,
305 u32 * next2,
306 u32 * next3,
307 vlib_buffer_t * b0,
308 vlib_buffer_t * b1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100309 vlib_buffer_t * b2, vlib_buffer_t * b3)
Damjan Marion7dc41462016-11-15 19:47:58 +0100310{
311 vnet_feature_main_t *fm = &feature_main;
312 vnet_feature_config_main_t *cm;
313 u8 feature_arc_index = fm->device_input_feature_arc_index;
314 cm = &fm->feature_config_mains[feature_arc_index];
315
316 if (PREDICT_FALSE
317 (clib_bitmap_get
318 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
319 {
320 /*
321 * Save next0 so that the last feature in the chain
322 * can skip ethernet-input if indicated...
323 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100324 u16 adv;
325
Damjan Marion7dc41462016-11-15 19:47:58 +0100326 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100327 adv = device_input_next_node_advance[*next0];
328 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
329 vlib_buffer_advance (b0, -adv);
330
Damjan Marion7dc41462016-11-15 19:47:58 +0100331 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
Damjan Marion35af9e52017-03-06 12:02:50 +0100332 adv = device_input_next_node_advance[*next1];
333 vnet_buffer (b1)->device_input_feat.buffer_advance = adv;
334 vlib_buffer_advance (b1, -adv);
335
Damjan Marion7dc41462016-11-15 19:47:58 +0100336 vnet_buffer (b2)->device_input_feat.saved_next_index = *next2;
Damjan Marion35af9e52017-03-06 12:02:50 +0100337 adv = device_input_next_node_advance[*next2];
338 vnet_buffer (b2)->device_input_feat.buffer_advance = adv;
339 vlib_buffer_advance (b2, -adv);
340
Damjan Marion7dc41462016-11-15 19:47:58 +0100341 vnet_buffer (b3)->device_input_feat.saved_next_index = *next3;
Damjan Marion35af9e52017-03-06 12:02:50 +0100342 adv = device_input_next_node_advance[*next3];
343 vnet_buffer (b3)->device_input_feat.buffer_advance = adv;
344 vlib_buffer_advance (b3, -adv);
Damjan Marion7dc41462016-11-15 19:47:58 +0100345
346 b0->feature_arc_index = feature_arc_index;
347 b1->feature_arc_index = feature_arc_index;
348 b2->feature_arc_index = feature_arc_index;
349 b3->feature_arc_index = feature_arc_index;
350
351 b0->current_config_index =
352 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
353 b1->current_config_index = b0->current_config_index;
354 b2->current_config_index = b0->current_config_index;
355 b3->current_config_index = b0->current_config_index;
356
357 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
358 next0, /* # bytes of config data */ 0);
359 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
360 next1, /* # bytes of config data */ 0);
361 vnet_get_config_data (&cm->config_main, &b2->current_config_index,
362 next2, /* # bytes of config data */ 0);
363 vnet_get_config_data (&cm->config_main, &b3->current_config_index,
364 next3, /* # bytes of config data */ 0);
365 }
366}
Damjan Marion87cd1192016-11-04 11:00:27 +0100367
Damjan Marion22311502016-10-28 20:30:15 +0200368#define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
369
370clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
371 vnet_config_main_t * vcm,
372 char **feature_start_nodes,
373 int num_feature_start_nodes,
374 vnet_feature_registration_t *
375 first_reg, char ***feature_nodes);
376
377void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index);
378
379#endif /* included_feature_h */
380
381/*
382 * fd.io coding-style-patch-verification: ON
383 *
384 * Local Variables:
385 * eval: (c-set-style "gnu")
386 * End:
387 */