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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_features_h
17 #define included_features_h
19 #include <vnet/vnet.h>
20 #include <vnet/api_errno.h>
21 #include <vnet/devices/devices.h>
23 /** feature registration object */
24 typedef struct _vnet_feature_arc_registration
26 /** next registration in list of all registrations*/
27 struct _vnet_feature_arc_registration *next;
28 /** Feature Arc name */
33 /* Feature arc index, assigned by init function */
36 } vnet_feature_arc_registration_t;
38 /* Enable feature callback. */
39 typedef clib_error_t *(vnet_feature_enable_disable_function_t)
40 (u32 sw_if_index, int enable_disable);
42 /** feature registration object */
43 typedef struct _vnet_feature_registration
45 /** next registration in list of all registrations*/
46 struct _vnet_feature_registration *next, *next_in_arc;
47 /** Feature arc name */
49 /** Graph node name */
51 /** Pointer to this feature index, filled in by vnet_feature_arc_init */
52 u32 *feature_index_ptr;
54 /** Constraints of the form "this feature runs before X" */
56 /** Constraints of the form "this feature runs after Y" */
59 /** Function to enable/disable feature **/
60 vnet_feature_enable_disable_function_t *enable_disable_cb;
61 } vnet_feature_registration_t;
63 typedef struct vnet_feature_config_main_t_
65 vnet_config_main_t config_main;
66 u32 *config_index_by_sw_if_index;
67 } vnet_feature_config_main_t;
71 /** feature arc configuration list */
72 vnet_feature_arc_registration_t *next_arc;
73 uword **arc_index_by_name;
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;
80 /** feature config main objects */
81 vnet_feature_config_main_t *feature_config_mains;
83 /** Save partial order results for show command */
84 char ***feature_nodes;
86 /** bitmap of interfaces which have driver rx features configured */
87 uword **sw_if_index_has_features;
89 /** feature reference counts by interface */
90 i16 **feature_count_by_sw_if_index;
92 /** Feature arc index for device-input */
93 u8 device_input_feature_arc_index;
96 vlib_main_t *vlib_main;
97 vnet_main_t *vnet_main;
98 } vnet_feature_main_t;
100 extern vnet_feature_main_t feature_main;
102 #define VNET_FEATURE_ARC_INIT(x,...) \
103 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
104 static void __vnet_add_feature_arc_registration_##x (void) \
105 __attribute__((__constructor__)) ; \
106 static void __vnet_add_feature_arc_registration_##x (void) \
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; \
112 static void __vnet_rm_feature_arc_registration_##x (void) \
113 __attribute__((__destructor__)) ; \
114 static void __vnet_rm_feature_arc_registration_##x (void) \
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); \
120 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
122 #define VNET_FEATURE_INIT(x,...) \
123 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
124 static void __vnet_add_feature_registration_##x (void) \
125 __attribute__((__constructor__)) ; \
126 static void __vnet_add_feature_registration_##x (void) \
128 vnet_feature_main_t * fm = &feature_main; \
129 vnet_feat_##x.next = fm->next_feature; \
130 fm->next_feature = & vnet_feat_##x; \
132 static void __vnet_rm_feature_registration_##x (void) \
133 __attribute__((__destructor__)) ; \
134 static void __vnet_rm_feature_registration_##x (void) \
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); \
140 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
143 vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
144 u32 sw_if_index, int is_add);
146 u32 vnet_get_feature_index (u8 arc, const char *s);
147 u8 vnet_get_feature_arc_index (const char *s);
148 vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
149 const char *node_name);
153 vnet_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);
159 vnet_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);
164 static inline vnet_feature_config_main_t *
165 vnet_get_feature_arc_config_main (u8 arc_index)
167 vnet_feature_main_t *fm = &feature_main;
169 if (arc_index == (u8) ~ 0)
172 return &fm->feature_config_mains[arc_index];
175 static_always_inline vnet_feature_config_main_t *
176 vnet_feature_get_config_main (u16 arc)
178 vnet_feature_main_t *fm = &feature_main;
179 return &fm->feature_config_mains[arc];
182 static_always_inline int
183 vnet_have_features (u8 arc, u32 sw_if_index)
185 vnet_feature_main_t *fm = &feature_main;
186 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
189 static_always_inline u32
190 vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
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);
197 static_always_inline void *
198 vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
199 vlib_buffer_t * b, u32 n_data_bytes)
201 vnet_feature_main_t *fm = &feature_main;
202 vnet_feature_config_main_t *cm;
203 cm = &fm->feature_config_mains[arc];
205 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
207 vnet_buffer (b)->feature_arc_index = arc;
208 b->current_config_index =
209 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
210 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
216 static_always_inline void
217 vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
220 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
223 static_always_inline void *
224 vnet_feature_next_with_data (u32 * next0, vlib_buffer_t * b0,
227 vnet_feature_main_t *fm = &feature_main;
228 u8 arc = vnet_buffer (b0)->feature_arc_index;
229 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
231 return vnet_get_config_data (&cm->config_main,
232 &b0->current_config_index, next0,
236 static_always_inline void
237 vnet_feature_next (u32 * next0, vlib_buffer_t * b0)
239 vnet_feature_next_with_data (next0, b0, 0);
242 static_always_inline int
243 vnet_device_input_have_features (u32 sw_if_index)
245 vnet_feature_main_t *fm = &feature_main;
246 return vnet_have_features (fm->device_input_feature_arc_index, sw_if_index);
249 static_always_inline void
250 vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0,
253 vnet_feature_main_t *fm = &feature_main;
254 vnet_feature_config_main_t *cm;
255 u8 feature_arc_index = fm->device_input_feature_arc_index;
256 cm = &fm->feature_config_mains[feature_arc_index];
260 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
263 * Save next0 so that the last feature in the chain
264 * can skip ethernet-input if indicated...
268 adv = device_input_next_node_advance[*next0];
269 vlib_buffer_advance (b0, -adv);
271 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
272 b0->current_config_index =
273 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
274 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
275 next0, /* # bytes of config data */ 0);
279 static_always_inline void
280 vnet_feature_start_device_input_x2 (u32 sw_if_index,
283 vlib_buffer_t * b0, vlib_buffer_t * b1)
285 vnet_feature_main_t *fm = &feature_main;
286 vnet_feature_config_main_t *cm;
287 u8 feature_arc_index = fm->device_input_feature_arc_index;
288 cm = &fm->feature_config_mains[feature_arc_index];
292 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
295 * Save next0 so that the last feature in the chain
296 * can skip ethernet-input if indicated...
300 adv = device_input_next_node_advance[*next0];
301 vlib_buffer_advance (b0, -adv);
303 adv = device_input_next_node_advance[*next1];
304 vlib_buffer_advance (b1, -adv);
306 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
307 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
308 b0->current_config_index =
309 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
310 b1->current_config_index = b0->current_config_index;
311 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
312 next0, /* # bytes of config data */ 0);
313 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
314 next1, /* # bytes of config data */ 0);
318 static_always_inline void
319 vnet_feature_start_device_input_x4 (u32 sw_if_index,
326 vlib_buffer_t * b2, vlib_buffer_t * b3)
328 vnet_feature_main_t *fm = &feature_main;
329 vnet_feature_config_main_t *cm;
330 u8 feature_arc_index = fm->device_input_feature_arc_index;
331 cm = &fm->feature_config_mains[feature_arc_index];
335 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
338 * Save next0 so that the last feature in the chain
339 * can skip ethernet-input if indicated...
343 adv = device_input_next_node_advance[*next0];
344 vlib_buffer_advance (b0, -adv);
346 adv = device_input_next_node_advance[*next1];
347 vlib_buffer_advance (b1, -adv);
349 adv = device_input_next_node_advance[*next2];
350 vlib_buffer_advance (b2, -adv);
352 adv = device_input_next_node_advance[*next3];
353 vlib_buffer_advance (b3, -adv);
355 vnet_buffer (b0)->feature_arc_index = feature_arc_index;
356 vnet_buffer (b1)->feature_arc_index = feature_arc_index;
357 vnet_buffer (b2)->feature_arc_index = feature_arc_index;
358 vnet_buffer (b3)->feature_arc_index = feature_arc_index;
360 b0->current_config_index =
361 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
362 b1->current_config_index = b0->current_config_index;
363 b2->current_config_index = b0->current_config_index;
364 b3->current_config_index = b0->current_config_index;
366 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
367 next0, /* # bytes of config data */ 0);
368 vnet_get_config_data (&cm->config_main, &b1->current_config_index,
369 next1, /* # bytes of config data */ 0);
370 vnet_get_config_data (&cm->config_main, &b2->current_config_index,
371 next2, /* # bytes of config data */ 0);
372 vnet_get_config_data (&cm->config_main, &b3->current_config_index,
373 next3, /* # bytes of config data */ 0);
377 #define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
379 clib_error_t *vnet_feature_arc_init (vlib_main_t * vm,
380 vnet_config_main_t * vcm,
381 char **feature_start_nodes,
382 int num_feature_start_nodes,
383 vnet_feature_registration_t *
384 first_reg, char ***feature_nodes);
386 void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index,
389 #endif /* included_feature_h */
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")