blob: b27aaf1780498dca075a12e503984030fa682fec [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 Marion22311502016-10-28 20:30:15 +020021
22/** feature registration object */
23typedef 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;
Damjan Marion21da6ce2016-11-28 18:21:59 +010032 /** End node */
33 char *end_node;
Damjan Marion22311502016-10-28 20:30:15 +020034 /* Feature arc index, assigned by init function */
Damjan Marion87cd1192016-11-04 11:00:27 +010035 u8 feature_arc_index;
Damjan Marion8b3191e2016-11-09 19:54:20 +010036 u8 *arc_index_ptr;
Damjan Marion22311502016-10-28 20:30:15 +020037} vnet_feature_arc_registration_t;
38
Pavel Kotucek7490a752016-11-15 09:19:11 +010039/* Enable feature callback. */
40typedef clib_error_t *(vnet_feature_enable_disable_function_t)
41 (u32 sw_if_index, int enable_disable);
42
Damjan Marion22311502016-10-28 20:30:15 +020043/** feature registration object */
44typedef struct _vnet_feature_registration
45{
46 /** next registration in list of all registrations*/
47 struct _vnet_feature_registration *next;
48 /** Feature arc name */
49 char *arc_name;
50 /** Graph node name */
51 char *node_name;
52 /** Pointer to this feature index, filled in by vnet_feature_arc_init */
Damjan Marion8b3191e2016-11-09 19:54:20 +010053 u32 *feature_index_ptr;
54 u32 feature_index;
Damjan Marion22311502016-10-28 20:30:15 +020055 /** Constraints of the form "this feature runs before X" */
56 char **runs_before;
57 /** Constraints of the form "this feature runs after Y" */
58 char **runs_after;
Pavel Kotucek7490a752016-11-15 09:19:11 +010059
60 /** Function to enable/disable feature **/
61 vnet_feature_enable_disable_function_t *enable_disable_cb;
Damjan Marion22311502016-10-28 20:30:15 +020062} vnet_feature_registration_t;
63
64typedef struct vnet_feature_config_main_t_
65{
66 vnet_config_main_t config_main;
67 u32 *config_index_by_sw_if_index;
Damjan Marion21da6ce2016-11-28 18:21:59 +010068 u32 end_feature_index;
Damjan Marion22311502016-10-28 20:30:15 +020069} vnet_feature_config_main_t;
70
71typedef struct
72{
73 /** feature arc configuration list */
74 vnet_feature_arc_registration_t *next_arc;
75 uword **arc_index_by_name;
76
77 /** feature path configuration lists */
78 vnet_feature_registration_t *next_feature;
79 vnet_feature_registration_t **next_feature_by_arc;
80 uword **next_feature_by_name;
81
82 /** feature config main objects */
83 vnet_feature_config_main_t *feature_config_mains;
84
85 /** Save partial order results for show command */
86 char ***feature_nodes;
87
88 /** bitmap of interfaces which have driver rx features configured */
89 uword **sw_if_index_has_features;
90
91 /** feature reference counts by interface */
92 i16 **feature_count_by_sw_if_index;
93
Damjan Marion87cd1192016-11-04 11:00:27 +010094 /** Feature arc index for device-input */
95 u8 device_input_feature_arc_index;
96
Damjan Marion22311502016-10-28 20:30:15 +020097 /** convenience */
98 vlib_main_t *vlib_main;
99 vnet_main_t *vnet_main;
100} vnet_feature_main_t;
101
102extern vnet_feature_main_t feature_main;
103
104#define VNET_FEATURE_ARC_INIT(x,...) \
105 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
106static void __vnet_add_feature_arc_registration_##x (void) \
107 __attribute__((__constructor__)) ; \
108static void __vnet_add_feature_arc_registration_##x (void) \
109{ \
110 vnet_feature_main_t * fm = &feature_main; \
111 vnet_feat_arc_##x.next = fm->next_arc; \
112 fm->next_arc = & vnet_feat_arc_##x; \
113} \
114__VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
115
116#define VNET_FEATURE_INIT(x,...) \
117 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
118static void __vnet_add_feature_registration_##x (void) \
119 __attribute__((__constructor__)) ; \
120static void __vnet_add_feature_registration_##x (void) \
121{ \
122 vnet_feature_main_t * fm = &feature_main; \
123 vnet_feat_##x.next = fm->next_feature; \
124 fm->next_feature = & vnet_feat_##x; \
125} \
126__VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
127
128void
Damjan Marion87cd1192016-11-04 11:00:27 +0100129vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
Damjan Marion22311502016-10-28 20:30:15 +0200130 u32 sw_if_index, int is_add);
131
Damjan Marion87cd1192016-11-04 11:00:27 +0100132u32 vnet_get_feature_index (u8 arc, const char *s);
133u8 vnet_get_feature_arc_index (const char *s);
Pavel Kotucek7490a752016-11-15 09:19:11 +0100134vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
135 const char *node_name);
136
Damjan Marion22311502016-10-28 20:30:15 +0200137
Damjan Marion96b41f72016-11-10 18:01:42 +0100138int
Damjan Marion8b3191e2016-11-09 19:54:20 +0100139vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
140 u32 sw_if_index, int enable_disable,
141 void *feature_config,
142 u32 n_feature_config_bytes);
143
144int
Damjan Marion22311502016-10-28 20:30:15 +0200145vnet_feature_enable_disable (const char *arc_name, const char *node_name,
146 u32 sw_if_index, int enable_disable,
147 void *feature_config,
148 u32 n_feature_config_bytes);
149
Damjan Marion8b3191e2016-11-09 19:54:20 +0100150static inline vnet_feature_config_main_t *
151vnet_get_feature_arc_config_main (u8 arc_index)
152{
153 vnet_feature_main_t *fm = &feature_main;
154
155 if (arc_index == (u8) ~ 0)
156 return 0;
157
158 return &fm->feature_config_mains[arc_index];
159}
Damjan Marion22311502016-10-28 20:30:15 +0200160
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100161static_always_inline vnet_feature_config_main_t *
162vnet_feature_get_config_main (u16 arc)
163{
164 vnet_feature_main_t *fm = &feature_main;
165 return &fm->feature_config_mains[arc];
166}
167
Damjan Marion22311502016-10-28 20:30:15 +0200168static_always_inline int
Damjan Marion87cd1192016-11-04 11:00:27 +0100169vnet_have_features (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200170{
171 vnet_feature_main_t *fm = &feature_main;
172 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
173}
174
175static_always_inline u32
Damjan Marion87cd1192016-11-04 11:00:27 +0100176vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
Damjan Marion22311502016-10-28 20:30:15 +0200177{
178 vnet_feature_main_t *fm = &feature_main;
179 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
180 return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
181}
182
Damjan Marion87cd1192016-11-04 11:00:27 +0100183static_always_inline void *
184vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
185 vlib_buffer_t * b, u32 n_data_bytes)
Damjan Marion22311502016-10-28 20:30:15 +0200186{
187 vnet_feature_main_t *fm = &feature_main;
Damjan Marion87cd1192016-11-04 11:00:27 +0100188 vnet_feature_config_main_t *cm;
189 cm = &fm->feature_config_mains[arc];
Damjan Marion22311502016-10-28 20:30:15 +0200190
191 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
192 {
Damjan Marion87cd1192016-11-04 11:00:27 +0100193 b->feature_arc_index = arc;
194 b->current_config_index =
Damjan Marion22311502016-10-28 20:30:15 +0200195 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
Damjan Marion87cd1192016-11-04 11:00:27 +0100196 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
197 next, n_data_bytes);
Damjan Marion22311502016-10-28 20:30:15 +0200198 }
Damjan Marion05bb1dd2016-11-08 21:28:22 +0100199 return 0;
Damjan Marion22311502016-10-28 20:30:15 +0200200}
201
202static_always_inline void
Damjan Marion87cd1192016-11-04 11:00:27 +0100203vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
204 vlib_buffer_t * b0)
205{
206 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
207}
208
209static_always_inline void *
210vnet_feature_next_with_data (u32 sw_if_index, u32 * next0,
211 vlib_buffer_t * b0, u32 n_data_bytes)
212{
213 vnet_feature_main_t *fm = &feature_main;
214 u8 arc = b0->feature_arc_index;
215 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
216
217 return vnet_get_config_data (&cm->config_main,
218 &b0->current_config_index, next0,
219 n_data_bytes);
220}
221
222static_always_inline void
223vnet_feature_next (u32 sw_if_index, u32 * next0, vlib_buffer_t * b0)
224{
225 vnet_feature_next_with_data (sw_if_index, next0, b0, 0);
226}
227
228static_always_inline void
229vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
230 vlib_buffer_t * b0, u16 buffer_advanced0)
Damjan Marion22311502016-10-28 20:30:15 +0200231{
232 vnet_feature_main_t *fm = &feature_main;
233 vnet_feature_config_main_t *cm;
Damjan Marion87cd1192016-11-04 11:00:27 +0100234 u8 feature_arc_index = fm->device_input_feature_arc_index;
235 cm = &fm->feature_config_mains[feature_arc_index];
Damjan Marion22311502016-10-28 20:30:15 +0200236
237 if (PREDICT_FALSE
238 (clib_bitmap_get
Damjan Marion87cd1192016-11-04 11:00:27 +0100239 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
Damjan Marion22311502016-10-28 20:30:15 +0200240 {
241 /*
242 * Save next0 so that the last feature in the chain
243 * can skip ethernet-input if indicated...
244 */
245 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
246 vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0;
247 vlib_buffer_advance (b0, -buffer_advanced0);
248
Damjan Marion87cd1192016-11-04 11:00:27 +0100249 b0->feature_arc_index = feature_arc_index;
Damjan Marion22311502016-10-28 20:30:15 +0200250 b0->current_config_index =
251 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
252 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
253 next0, /* # bytes of config data */ 0);
254 }
255}
256
Damjan Marion87cd1192016-11-04 11:00:27 +0100257static_always_inline void
258vnet_feature_start_device_input_x2 (u32 sw_if_index,
259 u32 * next0,
260 u32 * next1,
261 vlib_buffer_t * b0,
262 vlib_buffer_t * b1,
263 u16 buffer_advanced0,
264 u16 buffer_advanced1)
265{
266 vnet_feature_main_t *fm = &feature_main;
267 vnet_feature_config_main_t *cm;
268 u8 feature_arc_index = fm->device_input_feature_arc_index;
269 cm = &fm->feature_config_mains[feature_arc_index];
270
271 if (PREDICT_FALSE
272 (clib_bitmap_get
273 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
274 {
275 /*
276 * Save next0 so that the last feature in the chain
277 * can skip ethernet-input if indicated...
278 */
279 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
280 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
281 vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0;
282 vnet_buffer (b1)->device_input_feat.buffer_advance = buffer_advanced1;
283 vlib_buffer_advance (b0, -buffer_advanced0);
284 vlib_buffer_advance (b1, -buffer_advanced1);
285
286 b0->feature_arc_index = feature_arc_index;
287 b1->feature_arc_index = feature_arc_index;
288 b0->current_config_index =
289 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
290 b1->current_config_index = b0->current_config_index;
291 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
292 next0, /* # bytes of config data */ 0);
293 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
294 next1, /* # bytes of config data */ 0);
295 }
296}
297
Damjan Marion7dc41462016-11-15 19:47:58 +0100298static_always_inline void
299vnet_feature_start_device_input_x4 (u32 sw_if_index,
300 u32 * next0,
301 u32 * next1,
302 u32 * next2,
303 u32 * next3,
304 vlib_buffer_t * b0,
305 vlib_buffer_t * b1,
306 vlib_buffer_t * b2,
307 vlib_buffer_t * b3,
308 u16 buffer_advanced0,
309 u16 buffer_advanced1,
310 u16 buffer_advanced2,
311 u16 buffer_advanced3)
312{
313 vnet_feature_main_t *fm = &feature_main;
314 vnet_feature_config_main_t *cm;
315 u8 feature_arc_index = fm->device_input_feature_arc_index;
316 cm = &fm->feature_config_mains[feature_arc_index];
317
318 if (PREDICT_FALSE
319 (clib_bitmap_get
320 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
321 {
322 /*
323 * Save next0 so that the last feature in the chain
324 * can skip ethernet-input if indicated...
325 */
326 vnet_buffer (b0)->device_input_feat.saved_next_index = *next0;
327 vnet_buffer (b1)->device_input_feat.saved_next_index = *next1;
328 vnet_buffer (b2)->device_input_feat.saved_next_index = *next2;
329 vnet_buffer (b3)->device_input_feat.saved_next_index = *next3;
330
331 vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0;
332 vnet_buffer (b1)->device_input_feat.buffer_advance = buffer_advanced1;
333 vnet_buffer (b2)->device_input_feat.buffer_advance = buffer_advanced2;
334 vnet_buffer (b3)->device_input_feat.buffer_advance = buffer_advanced3;
335
336 vlib_buffer_advance (b0, -buffer_advanced0);
337 vlib_buffer_advance (b1, -buffer_advanced1);
338 vlib_buffer_advance (b2, -buffer_advanced2);
339 vlib_buffer_advance (b3, -buffer_advanced3);
340
341 b0->feature_arc_index = feature_arc_index;
342 b1->feature_arc_index = feature_arc_index;
343 b2->feature_arc_index = feature_arc_index;
344 b3->feature_arc_index = feature_arc_index;
345
346 b0->current_config_index =
347 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
348 b1->current_config_index = b0->current_config_index;
349 b2->current_config_index = b0->current_config_index;
350 b3->current_config_index = b0->current_config_index;
351
352 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
353 next0, /* # bytes of config data */ 0);
354 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
355 next1, /* # bytes of config data */ 0);
356 vnet_get_config_data (&cm->config_main, &b2->current_config_index,
357 next2, /* # bytes of config data */ 0);
358 vnet_get_config_data (&cm->config_main, &b3->current_config_index,
359 next3, /* # bytes of config data */ 0);
360 }
361}
Damjan Marion87cd1192016-11-04 11:00:27 +0100362
Damjan Marion22311502016-10-28 20:30:15 +0200363#define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
364
365clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
366 vnet_config_main_t * vcm,
367 char **feature_start_nodes,
368 int num_feature_start_nodes,
369 vnet_feature_registration_t *
370 first_reg, char ***feature_nodes);
371
372void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index);
373
374#endif /* included_feature_h */
375
376/*
377 * fd.io coding-style-patch-verification: ON
378 *
379 * Local Variables:
380 * eval: (c-set-style "gnu")
381 * End:
382 */