blob: 81224ebf0e236dd463240bfee0a2deb827a6c32b [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*/
Damjan Marion13adc3d2018-04-09 20:59:53 +020046 struct _vnet_feature_registration *next, *next_in_arc;
Damjan Marion22311502016-10-28 20:30:15 +020047 /** 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} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200112static void __vnet_rm_feature_arc_registration_##x (void) \
113 __attribute__((__destructor__)) ; \
114static void __vnet_rm_feature_arc_registration_##x (void) \
115{ \
116 vnet_feature_main_t * fm = &feature_main; \
117 vnet_feature_arc_registration_t *r = &vnet_feat_arc_##x; \
118 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_arc, r, next); \
119} \
Damjan Marion22311502016-10-28 20:30:15 +0200120__VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
121
122#define VNET_FEATURE_INIT(x,...) \
123 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
124static void __vnet_add_feature_registration_##x (void) \
125 __attribute__((__constructor__)) ; \
126static void __vnet_add_feature_registration_##x (void) \
127{ \
128 vnet_feature_main_t * fm = &feature_main; \
129 vnet_feat_##x.next = fm->next_feature; \
130 fm->next_feature = & vnet_feat_##x; \
131} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200132static void __vnet_rm_feature_registration_##x (void) \
133 __attribute__((__destructor__)) ; \
134static void __vnet_rm_feature_registration_##x (void) \
135{ \
136 vnet_feature_main_t * fm = &feature_main; \
137 vnet_feature_registration_t *r = &vnet_feat_##x; \
138 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_feature, r, next); \
139} \
Damjan Marion22311502016-10-28 20:30:15 +0200140__VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
141
142void
Damjan Marion87cd1192016-11-04 11:00:27 +0100143vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
Damjan Marion22311502016-10-28 20:30:15 +0200144 u32 sw_if_index, int is_add);
145
Damjan Marion87cd1192016-11-04 11:00:27 +0100146u32 vnet_get_feature_index (u8 arc, const char *s);
147u8 vnet_get_feature_arc_index (const char *s);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100148vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
149 const char *node_name);
150
Damjan Marion22311502016-10-28 20:30:15 +0200151
Damjan Marion96b41f72016-11-10 18:01:42 +0100152int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100153vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
154 u32 sw_if_index, int enable_disable,
155 void *feature_config,
156 u32 n_feature_config_bytes);
157
158int
Damjan Marion22311502016-10-28 20:30:15 +0200159vnet_feature_enable_disable (const char *arc_name, const char *node_name,
160 u32 sw_if_index, int enable_disable,
161 void *feature_config,
162 u32 n_feature_config_bytes);
163
Damjan Marion8b3191e2016-11-09 19:54:20 +0100164static inline vnet_feature_config_main_t *
165vnet_get_feature_arc_config_main (u8 arc_index)
166{
167 vnet_feature_main_t *fm = &feature_main;
168
169 if (arc_index == (u8) ~ 0)
170 return 0;
171
172 return &fm->feature_config_mains[arc_index];
173}
Damjan Marion22311502016-10-28 20:30:15 +0200174
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100175static_always_inline vnet_feature_config_main_t *
176vnet_feature_get_config_main (u16 arc)
177{
178 vnet_feature_main_t *fm = &feature_main;
179 return &fm->feature_config_mains[arc];
180}
181
Damjan Marion22311502016-10-28 20:30:15 +0200182static_always_inline int
Damjan Marion87cd1192016-11-04 11:00:27 +0100183vnet_have_features (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200184{
185 vnet_feature_main_t *fm = &feature_main;
186 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
187}
188
189static_always_inline u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100190vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200191{
192 vnet_feature_main_t *fm = &feature_main;
193 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
194 return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
195}
196
Damjan Marion87cd1192016-11-04 11:00:27 +0100197static_always_inline void *
198vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
199 vlib_buffer_t * b, u32 n_data_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200200{
201 vnet_feature_main_t *fm = &feature_main;
Damjan Marion87cd1192016-11-04 11:00:27 +0100202 vnet_feature_config_main_t *cm;
203 cm = &fm->feature_config_mains[arc];
Damjan Marion22311502016-10-28 20:30:15 +0200204
205 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
206 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200207 vnet_buffer (b)->feature_arc_index = arc;
Damjan Marion87cd1192016-11-04 11:00:27 +0100208 b->current_config_index =
Damjan Marion22311502016-10-28 20:30:15 +0200209 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
Damjan Marion87cd1192016-11-04 11:00:27 +0100210 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
211 next, n_data_bytes);
Damjan Marion22311502016-10-28 20:30:15 +0200212 }
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100213 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200214}
215
216static_always_inline void
Damjan Marion87cd1192016-11-04 11:00:27 +0100217vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
218 vlib_buffer_t * b0)
219{
220 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
221}
222
223static_always_inline void *
224vnet_feature_next_with_data (u32 sw_if_index, u32 * next0,
225 vlib_buffer_t * b0, u32 n_data_bytes)
226{
227 vnet_feature_main_t *fm = &feature_main;
Damjan Marionaa682a32018-04-26 22:45:40 +0200228 u8 arc = vnet_buffer (b0)->feature_arc_index;
Damjan Marion87cd1192016-11-04 11:00:27 +0100229 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
230
231 return vnet_get_config_data (&cm->config_main,
232 &b0->current_config_index, next0,
233 n_data_bytes);
234}
235
236static_always_inline void
237vnet_feature_next (u32 sw_if_index, u32 * next0, vlib_buffer_t * b0)
238{
239 vnet_feature_next_with_data (sw_if_index, next0, b0, 0);
240}
241
Damjan Mariondc091332018-02-25 22:52:07 +0100242static_always_inline int
243vnet_device_input_have_features (u32 sw_if_index)
244{
245 vnet_feature_main_t *fm = &feature_main;
246 return vnet_have_features (fm->device_input_feature_arc_index, sw_if_index);
247}
248
Damjan Marion87cd1192016-11-04 11:00:27 +0100249static_always_inline void
250vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
Damjan Marion35af9e52017-03-06 12:02:50 +0100251 vlib_buffer_t * b0)
Damjan Marion22311502016-10-28 20:30:15 +0200252{
253 vnet_feature_main_t *fm = &feature_main;
254 vnet_feature_config_main_t *cm;
Damjan Marion87cd1192016-11-04 11:00:27 +0100255 u8 feature_arc_index = fm->device_input_feature_arc_index;
256 cm = &fm->feature_config_mains[feature_arc_index];
Damjan Marion22311502016-10-28 20:30:15 +0200257
258 if (PREDICT_FALSE
259 (clib_bitmap_get
Damjan Marion87cd1192016-11-04 11:00:27 +0100260 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
Damjan Marion22311502016-10-28 20:30:15 +0200261 {
262 /*
263 * Save next0 so that the last feature in the chain
264 * can skip ethernet-input if indicated...
265 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100266 u16 adv;
267
Damjan Marion22311502016-10-28 20:30:15 +0200268 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100269 adv = device_input_next_node_advance[*next0];
270 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
271 vlib_buffer_advance (b0, -adv);
Damjan Marion22311502016-10-28 20:30:15 +0200272
Damjan Marionaa682a32018-04-26 22:45:40 +0200273 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
Damjan Marion22311502016-10-28 20:30:15 +0200274 b0->current_config_index =
275 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
276 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
277 next0, /* # bytes of config data */ 0);
278 }
279}
280
Damjan Marion87cd1192016-11-04 11:00:27 +0100281static_always_inline void
282vnet_feature_start_device_input_x2 (u32 sw_if_index,
283 u32 * next0,
284 u32 * next1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100285 vlib_buffer_t * b0, vlib_buffer_t * b1)
Damjan Marion87cd1192016-11-04 11:00:27 +0100286{
287 vnet_feature_main_t *fm = &feature_main;
288 vnet_feature_config_main_t *cm;
289 u8 feature_arc_index = fm->device_input_feature_arc_index;
290 cm = &fm->feature_config_mains[feature_arc_index];
291
292 if (PREDICT_FALSE
293 (clib_bitmap_get
294 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
295 {
296 /*
297 * Save next0 so that the last feature in the chain
298 * can skip ethernet-input if indicated...
299 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100300 u16 adv;
301
Damjan Marion87cd1192016-11-04 11:00:27 +0100302 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100303 adv = device_input_next_node_advance[*next0];
304 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
305 vlib_buffer_advance (b0, -adv);
306
Damjan Marion87cd1192016-11-04 11:00:27 +0100307 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
Damjan Marion35af9e52017-03-06 12:02:50 +0100308 adv = device_input_next_node_advance[*next1];
309 vnet_buffer (b1)->device_input_feat.buffer_advance = adv;
310 vlib_buffer_advance (b1, -adv);
Damjan Marion87cd1192016-11-04 11:00:27 +0100311
Damjan Marionaa682a32018-04-26 22:45:40 +0200312 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
313 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
Damjan Marion87cd1192016-11-04 11:00:27 +0100314 b0->current_config_index =
315 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
316 b1->current_config_index = b0->current_config_index;
317 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
318 next0, /* # bytes of config data */ 0);
319 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
320 next1, /* # bytes of config data */ 0);
321 }
322}
323
Damjan Marion7dc41462016-11-15 19:47:58 +0100324static_always_inline void
325vnet_feature_start_device_input_x4 (u32 sw_if_index,
326 u32 * next0,
327 u32 * next1,
328 u32 * next2,
329 u32 * next3,
330 vlib_buffer_t * b0,
331 vlib_buffer_t * b1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100332 vlib_buffer_t * b2, vlib_buffer_t * b3)
Damjan Marion7dc41462016-11-15 19:47:58 +0100333{
334 vnet_feature_main_t *fm = &feature_main;
335 vnet_feature_config_main_t *cm;
336 u8 feature_arc_index = fm->device_input_feature_arc_index;
337 cm = &fm->feature_config_mains[feature_arc_index];
338
339 if (PREDICT_FALSE
340 (clib_bitmap_get
341 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
342 {
343 /*
344 * Save next0 so that the last feature in the chain
345 * can skip ethernet-input if indicated...
346 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100347 u16 adv;
348
Damjan Marion7dc41462016-11-15 19:47:58 +0100349 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
Damjan Marion35af9e52017-03-06 12:02:50 +0100350 adv = device_input_next_node_advance[*next0];
351 vnet_buffer (b0)->device_input_feat.buffer_advance = adv;
352 vlib_buffer_advance (b0, -adv);
353
Damjan Marion7dc41462016-11-15 19:47:58 +0100354 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
Damjan Marion35af9e52017-03-06 12:02:50 +0100355 adv = device_input_next_node_advance[*next1];
356 vnet_buffer (b1)->device_input_feat.buffer_advance = adv;
357 vlib_buffer_advance (b1, -adv);
358
Damjan Marion7dc41462016-11-15 19:47:58 +0100359 vnet_buffer (b2)->device_input_feat.saved_next_index = *next2;
Damjan Marion35af9e52017-03-06 12:02:50 +0100360 adv = device_input_next_node_advance[*next2];
361 vnet_buffer (b2)->device_input_feat.buffer_advance = adv;
362 vlib_buffer_advance (b2, -adv);
363
Damjan Marion7dc41462016-11-15 19:47:58 +0100364 vnet_buffer (b3)->device_input_feat.saved_next_index = *next3;
Damjan Marion35af9e52017-03-06 12:02:50 +0100365 adv = device_input_next_node_advance[*next3];
366 vnet_buffer (b3)->device_input_feat.buffer_advance = adv;
367 vlib_buffer_advance (b3, -adv);
Damjan Marion7dc41462016-11-15 19:47:58 +0100368
Damjan Marionaa682a32018-04-26 22:45:40 +0200369 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
370 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
371 vnet_buffer (b2)->feature_arc_index = feature_arc_index;
372 vnet_buffer (b3)->feature_arc_index = feature_arc_index;
Damjan Marion7dc41462016-11-15 19:47:58 +0100373
374 b0->current_config_index =
375 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
376 b1->current_config_index = b0->current_config_index;
377 b2->current_config_index = b0->current_config_index;
378 b3->current_config_index = b0->current_config_index;
379
380 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
381 next0, /* # bytes of config data */ 0);
382 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
383 next1, /* # bytes of config data */ 0);
384 vnet_get_config_data (&cm->config_main, &b2->current_config_index,
385 next2, /* # bytes of config data */ 0);
386 vnet_get_config_data (&cm->config_main, &b3->current_config_index,
387 next3, /* # bytes of config data */ 0);
388 }
389}
Damjan Marion87cd1192016-11-04 11:00:27 +0100390
Damjan Marion22311502016-10-28 20:30:15 +0200391#define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
392
393clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
394 vnet_config_main_t * vcm,
395 char **feature_start_nodes,
396 int num_feature_start_nodes,
397 vnet_feature_registration_t *
398 first_reg, char ***feature_nodes);
399
Dave Barach525c9d02018-05-26 10:48:55 -0400400void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index,
401 int verbose);
Damjan Marion22311502016-10-28 20:30:15 +0200402
403#endif /* included_feature_h */
404
405/*
406 * fd.io coding-style-patch-verification: ON
407 *
408 * Local Variables:
409 * eval: (c-set-style "gnu")
410 * End:
411 */