blob: eb9b7b062436f0eefcb76d619f694dfcc4709972 [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
Damjan Marion6e363512018-08-10 22:39:11 +0200102#ifndef CLIB_MARCH_VARIANT
Damjan Marion22311502016-10-28 20:30:15 +0200103#define VNET_FEATURE_ARC_INIT(x,...) \
104 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
105static void __vnet_add_feature_arc_registration_##x (void) \
106 __attribute__((__constructor__)) ; \
107static void __vnet_add_feature_arc_registration_##x (void) \
108{ \
109 vnet_feature_main_t * fm = &feature_main; \
110 vnet_feat_arc_##x.next = fm->next_arc; \
111 fm->next_arc = & vnet_feat_arc_##x; \
112} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200113static void __vnet_rm_feature_arc_registration_##x (void) \
114 __attribute__((__destructor__)) ; \
115static void __vnet_rm_feature_arc_registration_##x (void) \
116{ \
117 vnet_feature_main_t * fm = &feature_main; \
118 vnet_feature_arc_registration_t *r = &vnet_feat_arc_##x; \
119 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_arc, r, next); \
120} \
Damjan Marion22311502016-10-28 20:30:15 +0200121__VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
122
123#define VNET_FEATURE_INIT(x,...) \
124 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
125static void __vnet_add_feature_registration_##x (void) \
126 __attribute__((__constructor__)) ; \
127static void __vnet_add_feature_registration_##x (void) \
128{ \
129 vnet_feature_main_t * fm = &feature_main; \
130 vnet_feat_##x.next = fm->next_feature; \
131 fm->next_feature = & vnet_feat_##x; \
132} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200133static void __vnet_rm_feature_registration_##x (void) \
134 __attribute__((__destructor__)) ; \
135static void __vnet_rm_feature_registration_##x (void) \
136{ \
137 vnet_feature_main_t * fm = &feature_main; \
138 vnet_feature_registration_t *r = &vnet_feat_##x; \
139 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_feature, r, next); \
140} \
Damjan Marion22311502016-10-28 20:30:15 +0200141__VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
Damjan Marion6e363512018-08-10 22:39:11 +0200142#else
143#define VNET_FEATURE_ARC_INIT(x,...) \
144extern vnet_feature_arc_registration_t __clib_unused vnet_feat_arc_##x; \
145static vnet_feature_arc_registration_t __clib_unused __clib_unused_vnet_feat_arc_##x
146#define VNET_FEATURE_INIT(x,...) \
147extern vnet_feature_registration_t __clib_unused vnet_feat_##x; \
148static vnet_feature_registration_t __clib_unused __clib_unused_vnet_feat_##x
149#endif
Damjan Marion22311502016-10-28 20:30:15 +0200150
151void
Damjan Marion87cd1192016-11-04 11:00:27 +0100152vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
Damjan Marion22311502016-10-28 20:30:15 +0200153 u32 sw_if_index, int is_add);
154
Damjan Marion87cd1192016-11-04 11:00:27 +0100155u32 vnet_get_feature_index (u8 arc, const char *s);
156u8 vnet_get_feature_arc_index (const char *s);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100157vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
158 const char *node_name);
159
Damjan Marion22311502016-10-28 20:30:15 +0200160
Damjan Marion96b41f72016-11-10 18:01:42 +0100161int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100162vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
163 u32 sw_if_index, int enable_disable,
164 void *feature_config,
165 u32 n_feature_config_bytes);
166
167int
Damjan Marion22311502016-10-28 20:30:15 +0200168vnet_feature_enable_disable (const char *arc_name, const char *node_name,
169 u32 sw_if_index, int enable_disable,
170 void *feature_config,
171 u32 n_feature_config_bytes);
172
Damjan Marion8b3191e2016-11-09 19:54:20 +0100173static inline vnet_feature_config_main_t *
174vnet_get_feature_arc_config_main (u8 arc_index)
175{
176 vnet_feature_main_t *fm = &feature_main;
177
178 if (arc_index == (u8) ~ 0)
179 return 0;
180
181 return &fm->feature_config_mains[arc_index];
182}
Damjan Marion22311502016-10-28 20:30:15 +0200183
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100184static_always_inline vnet_feature_config_main_t *
185vnet_feature_get_config_main (u16 arc)
186{
187 vnet_feature_main_t *fm = &feature_main;
188 return &fm->feature_config_mains[arc];
189}
190
Damjan Marion22311502016-10-28 20:30:15 +0200191static_always_inline int
Damjan Marion87cd1192016-11-04 11:00:27 +0100192vnet_have_features (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200193{
194 vnet_feature_main_t *fm = &feature_main;
195 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
196}
197
198static_always_inline u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100199vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200200{
201 vnet_feature_main_t *fm = &feature_main;
202 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
203 return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
204}
205
Damjan Marion87cd1192016-11-04 11:00:27 +0100206static_always_inline void *
207vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
208 vlib_buffer_t * b, u32 n_data_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200209{
210 vnet_feature_main_t *fm = &feature_main;
Damjan Marion87cd1192016-11-04 11:00:27 +0100211 vnet_feature_config_main_t *cm;
212 cm = &fm->feature_config_mains[arc];
Damjan Marion22311502016-10-28 20:30:15 +0200213
214 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
215 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200216 vnet_buffer (b)->feature_arc_index = arc;
Damjan Marion87cd1192016-11-04 11:00:27 +0100217 b->current_config_index =
Damjan Marion22311502016-10-28 20:30:15 +0200218 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
Damjan Marion87cd1192016-11-04 11:00:27 +0100219 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
220 next, n_data_bytes);
Damjan Marion22311502016-10-28 20:30:15 +0200221 }
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100222 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200223}
224
225static_always_inline void
Damjan Marion87cd1192016-11-04 11:00:27 +0100226vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
227 vlib_buffer_t * b0)
228{
229 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
230}
231
232static_always_inline void *
Damjan Marion7d98a122018-07-19 20:42:08 +0200233vnet_feature_next_with_data (u32 * next0, vlib_buffer_t * b0,
234 u32 n_data_bytes)
Damjan Marion87cd1192016-11-04 11:00:27 +0100235{
236 vnet_feature_main_t *fm = &feature_main;
Damjan Marionaa682a32018-04-26 22:45:40 +0200237 u8 arc = vnet_buffer (b0)->feature_arc_index;
Damjan Marion87cd1192016-11-04 11:00:27 +0100238 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
239
240 return vnet_get_config_data (&cm->config_main,
241 &b0->current_config_index, next0,
242 n_data_bytes);
243}
244
245static_always_inline void
Damjan Marion7d98a122018-07-19 20:42:08 +0200246vnet_feature_next (u32 * next0, vlib_buffer_t * b0)
Damjan Marion87cd1192016-11-04 11:00:27 +0100247{
Damjan Marion7d98a122018-07-19 20:42:08 +0200248 vnet_feature_next_with_data (next0, b0, 0);
Damjan Marion87cd1192016-11-04 11:00:27 +0100249}
250
Damjan Mariondc091332018-02-25 22:52:07 +0100251static_always_inline int
252vnet_device_input_have_features (u32 sw_if_index)
253{
254 vnet_feature_main_t *fm = &feature_main;
255 return vnet_have_features (fm->device_input_feature_arc_index, sw_if_index);
256}
257
Damjan Marion87cd1192016-11-04 11:00:27 +0100258static_always_inline void
259vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
Damjan Marion35af9e52017-03-06 12:02:50 +0100260 vlib_buffer_t * b0)
Damjan Marion22311502016-10-28 20:30:15 +0200261{
262 vnet_feature_main_t *fm = &feature_main;
263 vnet_feature_config_main_t *cm;
Damjan Marion87cd1192016-11-04 11:00:27 +0100264 u8 feature_arc_index = fm->device_input_feature_arc_index;
265 cm = &fm->feature_config_mains[feature_arc_index];
Damjan Marion22311502016-10-28 20:30:15 +0200266
267 if (PREDICT_FALSE
268 (clib_bitmap_get
Damjan Marion87cd1192016-11-04 11:00:27 +0100269 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
Damjan Marion22311502016-10-28 20:30:15 +0200270 {
271 /*
272 * Save next0 so that the last feature in the chain
273 * can skip ethernet-input if indicated...
274 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100275 u16 adv;
276
Damjan Marion35af9e52017-03-06 12:02:50 +0100277 adv = device_input_next_node_advance[*next0];
Damjan Marion35af9e52017-03-06 12:02:50 +0100278 vlib_buffer_advance (b0, -adv);
Damjan Marion22311502016-10-28 20:30:15 +0200279
Damjan Marionaa682a32018-04-26 22:45:40 +0200280 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
Damjan Marion22311502016-10-28 20:30:15 +0200281 b0->current_config_index =
282 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
283 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
284 next0, /* # bytes of config data */ 0);
285 }
286}
287
Damjan Marion87cd1192016-11-04 11:00:27 +0100288static_always_inline void
289vnet_feature_start_device_input_x2 (u32 sw_if_index,
290 u32 * next0,
291 u32 * next1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100292 vlib_buffer_t * b0, vlib_buffer_t * b1)
Damjan Marion87cd1192016-11-04 11:00:27 +0100293{
294 vnet_feature_main_t *fm = &feature_main;
295 vnet_feature_config_main_t *cm;
296 u8 feature_arc_index = fm->device_input_feature_arc_index;
297 cm = &fm->feature_config_mains[feature_arc_index];
298
299 if (PREDICT_FALSE
300 (clib_bitmap_get
301 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
302 {
303 /*
304 * Save next0 so that the last feature in the chain
305 * can skip ethernet-input if indicated...
306 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100307 u16 adv;
308
Damjan Marion35af9e52017-03-06 12:02:50 +0100309 adv = device_input_next_node_advance[*next0];
Damjan Marion35af9e52017-03-06 12:02:50 +0100310 vlib_buffer_advance (b0, -adv);
311
Damjan Marion35af9e52017-03-06 12:02:50 +0100312 adv = device_input_next_node_advance[*next1];
Damjan Marion35af9e52017-03-06 12:02:50 +0100313 vlib_buffer_advance (b1, -adv);
Damjan Marion87cd1192016-11-04 11:00:27 +0100314
Damjan Marionaa682a32018-04-26 22:45:40 +0200315 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
316 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
Damjan Marion87cd1192016-11-04 11:00:27 +0100317 b0->current_config_index =
318 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
319 b1->current_config_index = b0->current_config_index;
320 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
321 next0, /* # bytes of config data */ 0);
322 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
323 next1, /* # bytes of config data */ 0);
324 }
325}
326
Damjan Marion7dc41462016-11-15 19:47:58 +0100327static_always_inline void
328vnet_feature_start_device_input_x4 (u32 sw_if_index,
329 u32 * next0,
330 u32 * next1,
331 u32 * next2,
332 u32 * next3,
333 vlib_buffer_t * b0,
334 vlib_buffer_t * b1,
Damjan Marion35af9e52017-03-06 12:02:50 +0100335 vlib_buffer_t * b2, vlib_buffer_t * b3)
Damjan Marion7dc41462016-11-15 19:47:58 +0100336{
337 vnet_feature_main_t *fm = &feature_main;
338 vnet_feature_config_main_t *cm;
339 u8 feature_arc_index = fm->device_input_feature_arc_index;
340 cm = &fm->feature_config_mains[feature_arc_index];
341
342 if (PREDICT_FALSE
343 (clib_bitmap_get
344 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
345 {
346 /*
347 * Save next0 so that the last feature in the chain
348 * can skip ethernet-input if indicated...
349 */
Damjan Marion35af9e52017-03-06 12:02:50 +0100350 u16 adv;
351
Damjan Marion35af9e52017-03-06 12:02:50 +0100352 adv = device_input_next_node_advance[*next0];
Damjan Marion35af9e52017-03-06 12:02:50 +0100353 vlib_buffer_advance (b0, -adv);
354
Damjan Marion35af9e52017-03-06 12:02:50 +0100355 adv = device_input_next_node_advance[*next1];
Damjan Marion35af9e52017-03-06 12:02:50 +0100356 vlib_buffer_advance (b1, -adv);
357
Damjan Marion35af9e52017-03-06 12:02:50 +0100358 adv = device_input_next_node_advance[*next2];
Damjan Marion35af9e52017-03-06 12:02:50 +0100359 vlib_buffer_advance (b2, -adv);
360
Damjan Marion35af9e52017-03-06 12:02:50 +0100361 adv = device_input_next_node_advance[*next3];
Damjan Marion35af9e52017-03-06 12:02:50 +0100362 vlib_buffer_advance (b3, -adv);
Damjan Marion7dc41462016-11-15 19:47:58 +0100363
Damjan Marionaa682a32018-04-26 22:45:40 +0200364 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
365 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
366 vnet_buffer (b2)->feature_arc_index = feature_arc_index;
367 vnet_buffer (b3)->feature_arc_index = feature_arc_index;
Damjan Marion7dc41462016-11-15 19:47:58 +0100368
369 b0->current_config_index =
370 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
371 b1->current_config_index = b0->current_config_index;
372 b2->current_config_index = b0->current_config_index;
373 b3->current_config_index = b0->current_config_index;
374
375 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
376 next0, /* # bytes of config data */ 0);
377 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
378 next1, /* # bytes of config data */ 0);
379 vnet_get_config_data (&cm->config_main, &b2->current_config_index,
380 next2, /* # bytes of config data */ 0);
381 vnet_get_config_data (&cm->config_main, &b3->current_config_index,
382 next3, /* # bytes of config data */ 0);
383 }
384}
Damjan Marion87cd1192016-11-04 11:00:27 +0100385
Damjan Marion22311502016-10-28 20:30:15 +0200386#define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
387
388clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
389 vnet_config_main_t * vcm,
390 char **feature_start_nodes,
391 int num_feature_start_nodes,
392 vnet_feature_registration_t *
393 first_reg, char ***feature_nodes);
394
Dave Barach525c9d02018-05-26 10:48:55 -0400395void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index,
396 int verbose);
Damjan Marion22311502016-10-28 20:30:15 +0200397
398#endif /* included_feature_h */
399
400/*
401 * fd.io coding-style-patch-verification: ON
402 *
403 * Local Variables:
404 * eval: (c-set-style "gnu")
405 * End:
406 */