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 /** End of the arc (optional, for consistency-checking) */
35 /* Feature arc index, assigned by init function */
38 } vnet_feature_arc_registration_t;
40 /* Enable feature callback. */
41 typedef clib_error_t *(vnet_feature_enable_disable_function_t)
42 (u32 sw_if_index, int enable_disable);
44 /** feature registration object */
45 typedef struct _vnet_feature_registration
47 /** next registration in list of all registrations*/
48 struct _vnet_feature_registration *next, *next_in_arc;
49 /** Feature arc name */
51 /** Graph node name */
53 /** Pointer to this feature index, filled in by vnet_feature_arc_init */
54 u32 *feature_index_ptr;
56 /** Constraints of the form "this feature runs before X" */
58 /** Constraints of the form "this feature runs after Y" */
61 /** Function to enable/disable feature **/
62 vnet_feature_enable_disable_function_t *enable_disable_cb;
63 } vnet_feature_registration_t;
65 /** constraint registration object */
66 typedef struct _vnet_feature_constraint_registration
68 /** next constraint set in list of all registrations*/
69 struct _vnet_feature_constraint_registration *next, *next_in_arc;
70 /** Feature arc name */
73 /** Feature arc index, assigned by init function */
76 /** Node names, to run in the specified order */
78 } vnet_feature_constraint_registration_t;
80 typedef struct vnet_feature_config_main_t_
82 vnet_config_main_t config_main;
83 u32 *config_index_by_sw_if_index;
84 } vnet_feature_config_main_t;
88 /** feature arc configuration list */
89 vnet_feature_arc_registration_t *next_arc;
90 uword **arc_index_by_name;
92 /** feature path configuration lists */
93 vnet_feature_registration_t *next_feature;
94 vnet_feature_registration_t **next_feature_by_arc;
95 vnet_feature_constraint_registration_t *next_constraint;
96 vnet_feature_constraint_registration_t **next_constraint_by_arc;
97 uword **next_feature_by_name;
99 /** feature config main objects */
100 vnet_feature_config_main_t *feature_config_mains;
102 /** Save partial order results for show command */
103 char ***feature_nodes;
105 /** bitmap of interfaces which have driver rx features configured */
106 uword **sw_if_index_has_features;
108 /** feature reference counts by interface */
109 i16 **feature_count_by_sw_if_index;
111 /** Feature arc index for device-input */
112 u8 device_input_feature_arc_index;
115 vlib_main_t *vlib_main;
116 vnet_main_t *vnet_main;
117 } vnet_feature_main_t;
119 extern vnet_feature_main_t feature_main;
121 #ifndef CLIB_MARCH_VARIANT
122 #define VNET_FEATURE_ARC_INIT(x,...) \
123 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
124 static void __vnet_add_feature_arc_registration_##x (void) \
125 __attribute__((__constructor__)) ; \
126 static void __vnet_add_feature_arc_registration_##x (void) \
128 vnet_feature_main_t * fm = &feature_main; \
129 vnet_feat_arc_##x.next = fm->next_arc; \
130 fm->next_arc = & vnet_feat_arc_##x; \
132 static void __vnet_rm_feature_arc_registration_##x (void) \
133 __attribute__((__destructor__)) ; \
134 static void __vnet_rm_feature_arc_registration_##x (void) \
136 vnet_feature_main_t * fm = &feature_main; \
137 vnet_feature_arc_registration_t *r = &vnet_feat_arc_##x; \
138 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_arc, r, next); \
140 __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x
142 #define VNET_FEATURE_INIT(x,...) \
143 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \
144 static void __vnet_add_feature_registration_##x (void) \
145 __attribute__((__constructor__)) ; \
146 static void __vnet_add_feature_registration_##x (void) \
148 vnet_feature_main_t * fm = &feature_main; \
149 vnet_feat_##x.next = fm->next_feature; \
150 fm->next_feature = & vnet_feat_##x; \
152 static void __vnet_rm_feature_registration_##x (void) \
153 __attribute__((__destructor__)) ; \
154 static void __vnet_rm_feature_registration_##x (void) \
156 vnet_feature_main_t * fm = &feature_main; \
157 vnet_feature_registration_t *r = &vnet_feat_##x; \
158 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_feature, r, next); \
160 __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x
162 #define VNET_FEATURE_ARC_ORDER(x,...) \
163 __VA_ARGS__ vnet_feature_constraint_registration_t \
164 vnet_feature_constraint_##x; \
165 static void __vnet_add_constraint_registration_##x (void) \
166 __attribute__((__constructor__)) ; \
167 static void __vnet_add_constraint_registration_##x (void) \
169 vnet_feature_main_t * fm = &feature_main; \
170 vnet_feature_constraint_##x.next = fm->next_constraint; \
171 fm->next_constraint = & vnet_feature_constraint_##x; \
173 static void __vnet_rm_constraint_registration_##x (void) \
174 __attribute__((__destructor__)) ; \
175 static void __vnet_rm_constraint_registration_##x (void) \
177 vnet_feature_main_t * fm = &feature_main; \
178 vnet_feature_constraint_registration_t *r = &vnet_feature_constraint_##x; \
179 VLIB_REMOVE_FROM_LINKED_LIST (fm->next_constraint, r, next); \
181 __VA_ARGS__ vnet_feature_constraint_registration_t vnet_feature_constraint_##x
184 #define VNET_FEATURE_ARC_INIT(x,...) \
185 extern vnet_feature_arc_registration_t __clib_unused vnet_feat_arc_##x; \
186 static vnet_feature_arc_registration_t __clib_unused __clib_unused_vnet_feat_arc_##x
187 #define VNET_FEATURE_INIT(x,...) \
188 extern vnet_feature_registration_t __clib_unused vnet_feat_##x; \
189 static vnet_feature_registration_t __clib_unused __clib_unused_vnet_feat_##x
191 #define VNET_FEATURE_ARC_ORDER(x,...) \
192 extern vnet_feature_constraint_registration_t \
193 __clib_unused vnet_feature_constraint_##x; \
194 static vnet_feature_constraint_registration_t __clib_unused \
195 __clib_unused_vnet_feature_constraint_##x
201 vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
202 u32 sw_if_index, int is_add);
204 u32 vnet_get_feature_index (u8 arc, const char *s);
205 u8 vnet_get_feature_arc_index (const char *s);
206 vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
207 const char *node_name);
211 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
212 u32 sw_if_index, int enable_disable,
213 void *feature_config,
214 u32 n_feature_config_bytes);
217 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
218 u32 sw_if_index, int enable_disable,
219 void *feature_config,
220 u32 n_feature_config_bytes);
223 vnet_feature_modify_end_node (u8 arc_index, u32 sw_if_index, u32 node_index);
225 u32 vnet_feature_get_end_node (u8 arc_index, u32 sw_if_index);
227 u32 vnet_feature_reset_end_node (u8 arc_index, u32 sw_if_index);
229 static_always_inline u32
230 vnet_get_feature_count (u8 arc, u32 sw_if_index)
232 vnet_feature_main_t *fm = &feature_main;
233 return (fm->feature_count_by_sw_if_index[arc][sw_if_index]);
236 static inline vnet_feature_config_main_t *
237 vnet_get_feature_arc_config_main (u8 arc_index)
239 vnet_feature_main_t *fm = &feature_main;
241 if (arc_index == (u8) ~ 0)
244 return &fm->feature_config_mains[arc_index];
247 static_always_inline vnet_feature_config_main_t *
248 vnet_feature_get_config_main (u16 arc)
250 vnet_feature_main_t *fm = &feature_main;
251 return &fm->feature_config_mains[arc];
254 static_always_inline int
255 vnet_have_features (u8 arc, u32 sw_if_index)
257 vnet_feature_main_t *fm = &feature_main;
258 return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
261 static_always_inline u32
262 vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
264 vnet_feature_main_t *fm = &feature_main;
265 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
266 return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
269 static_always_inline void *
270 vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
271 vlib_buffer_t * b, u32 n_data_bytes)
273 vnet_feature_main_t *fm = &feature_main;
274 vnet_feature_config_main_t *cm;
275 cm = &fm->feature_config_mains[arc];
277 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
279 vnet_buffer (b)->feature_arc_index = arc;
280 b->current_config_index =
281 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
282 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
288 static_always_inline void *
289 vnet_feature_arc_start_w_cfg_index (u8 arc,
292 vlib_buffer_t * b, u32 cfg_index)
294 vnet_feature_main_t *fm = &feature_main;
295 vnet_feature_config_main_t *cm;
296 cm = &fm->feature_config_mains[arc];
298 vnet_buffer (b)->feature_arc_index = arc;
299 b->current_config_index = cfg_index;
301 return vnet_get_config_data (&cm->config_main, &b->current_config_index,
305 static_always_inline void
306 vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
309 vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
312 static_always_inline void *
313 vnet_feature_next_with_data (u32 * next0, vlib_buffer_t * b0,
316 vnet_feature_main_t *fm = &feature_main;
317 u8 arc = vnet_buffer (b0)->feature_arc_index;
318 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
320 return vnet_get_config_data (&cm->config_main,
321 &b0->current_config_index, next0,
325 static_always_inline void
326 vnet_feature_next (u32 * next0, vlib_buffer_t * b0)
328 vnet_feature_next_with_data (next0, b0, 0);
331 static_always_inline void
332 vnet_feature_next_u16 (u16 * next0, vlib_buffer_t * b0)
335 vnet_feature_next_with_data (&next32, b0, 0);
339 static_always_inline int
340 vnet_device_input_have_features (u32 sw_if_index)
342 vnet_feature_main_t *fm = &feature_main;
343 return vnet_have_features (fm->device_input_feature_arc_index, sw_if_index);
346 static_always_inline void
347 vnet_feature_start_device_input (u32 sw_if_index, u32 *next0,
350 vnet_feature_main_t *fm = &feature_main;
351 vnet_feature_config_main_t *cm;
352 u8 feature_arc_index = fm->device_input_feature_arc_index;
353 cm = &fm->feature_config_mains[feature_arc_index];
357 (fm->sw_if_index_has_features[feature_arc_index], sw_if_index)))
359 vnet_buffer (b0)->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 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
363 next0, /* # bytes of config data */ 0);
367 #define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0}
369 clib_error_t *vnet_feature_arc_init
371 vnet_config_main_t * vcm,
372 char **feature_start_nodes,
373 int num_feature_start_nodes,
375 vnet_feature_registration_t * first_reg,
376 vnet_feature_constraint_registration_t * first_const_set,
377 char ***in_feature_nodes);
379 void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index,
382 typedef void (*vnet_feature_update_cb_t) (u32 sw_if_index,
384 u8 is_enable, void *cb);
386 extern void vnet_feature_register (vnet_feature_update_cb_t cb, void *data);
389 vnet_feature_is_enabled (const char *arc_name, const char *feature_node_name,
392 #endif /* included_feature_h */
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")