Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 1 | /* |
| 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 Marion | 96b41f7 | 2016-11-10 18:01:42 +0100 | [diff] [blame] | 20 | #include <vnet/api_errno.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 21 | |
| 22 | /** feature registration object */ |
| 23 | typedef 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 Marion | 21da6ce | 2016-11-28 18:21:59 +0100 | [diff] [blame] | 32 | /** End node */ |
| 33 | char *end_node; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 34 | /* Feature arc index, assigned by init function */ |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 35 | u8 feature_arc_index; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 36 | u8 *arc_index_ptr; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 37 | } vnet_feature_arc_registration_t; |
| 38 | |
Pavel Kotucek | 7490a75 | 2016-11-15 09:19:11 +0100 | [diff] [blame] | 39 | /* Enable feature callback. */ |
| 40 | typedef clib_error_t *(vnet_feature_enable_disable_function_t) |
| 41 | (u32 sw_if_index, int enable_disable); |
| 42 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 43 | /** feature registration object */ |
| 44 | typedef 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 Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 53 | u32 *feature_index_ptr; |
| 54 | u32 feature_index; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 55 | /** 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 Kotucek | 7490a75 | 2016-11-15 09:19:11 +0100 | [diff] [blame] | 59 | |
| 60 | /** Function to enable/disable feature **/ |
| 61 | vnet_feature_enable_disable_function_t *enable_disable_cb; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 62 | } vnet_feature_registration_t; |
| 63 | |
| 64 | typedef struct vnet_feature_config_main_t_ |
| 65 | { |
| 66 | vnet_config_main_t config_main; |
| 67 | u32 *config_index_by_sw_if_index; |
Damjan Marion | 21da6ce | 2016-11-28 18:21:59 +0100 | [diff] [blame] | 68 | u32 end_feature_index; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 69 | } vnet_feature_config_main_t; |
| 70 | |
| 71 | typedef 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 Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 94 | /** Feature arc index for device-input */ |
| 95 | u8 device_input_feature_arc_index; |
| 96 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 97 | /** convenience */ |
| 98 | vlib_main_t *vlib_main; |
| 99 | vnet_main_t *vnet_main; |
| 100 | } vnet_feature_main_t; |
| 101 | |
| 102 | extern 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;\ |
| 106 | static void __vnet_add_feature_arc_registration_##x (void) \ |
| 107 | __attribute__((__constructor__)) ; \ |
| 108 | static 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; \ |
| 118 | static void __vnet_add_feature_registration_##x (void) \ |
| 119 | __attribute__((__constructor__)) ; \ |
| 120 | static 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 | |
| 128 | void |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 129 | vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc, |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 130 | u32 sw_if_index, int is_add); |
| 131 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 132 | u32 vnet_get_feature_index (u8 arc, const char *s); |
| 133 | u8 vnet_get_feature_arc_index (const char *s); |
Pavel Kotucek | 7490a75 | 2016-11-15 09:19:11 +0100 | [diff] [blame] | 134 | vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name, |
| 135 | const char *node_name); |
| 136 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 137 | |
Damjan Marion | 96b41f7 | 2016-11-10 18:01:42 +0100 | [diff] [blame] | 138 | int |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 139 | vnet_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 | |
| 144 | int |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 145 | vnet_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 Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 150 | static inline vnet_feature_config_main_t * |
| 151 | vnet_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 Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 160 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 161 | static_always_inline vnet_feature_config_main_t * |
| 162 | vnet_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 Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 168 | static_always_inline int |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 169 | vnet_have_features (u8 arc, u32 sw_if_index) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 170 | { |
| 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 | |
| 175 | static_always_inline u32 |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 176 | vnet_get_feature_config_index (u8 arc, u32 sw_if_index) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 177 | { |
| 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 Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 183 | static_always_inline void * |
| 184 | vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next, |
| 185 | vlib_buffer_t * b, u32 n_data_bytes) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 186 | { |
| 187 | vnet_feature_main_t *fm = &feature_main; |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 188 | vnet_feature_config_main_t *cm; |
| 189 | cm = &fm->feature_config_mains[arc]; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 190 | |
| 191 | if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index))) |
| 192 | { |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 193 | b->feature_arc_index = arc; |
| 194 | b->current_config_index = |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 195 | vec_elt (cm->config_index_by_sw_if_index, sw_if_index); |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 196 | return vnet_get_config_data (&cm->config_main, &b->current_config_index, |
| 197 | next, n_data_bytes); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 198 | } |
Damjan Marion | 05bb1dd | 2016-11-08 21:28:22 +0100 | [diff] [blame] | 199 | return 0; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static_always_inline void |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 203 | vnet_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 | |
| 209 | static_always_inline void * |
| 210 | vnet_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 | |
| 222 | static_always_inline void |
| 223 | vnet_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 | |
| 228 | static_always_inline void |
| 229 | vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0, |
| 230 | vlib_buffer_t * b0, u16 buffer_advanced0) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 231 | { |
| 232 | vnet_feature_main_t *fm = &feature_main; |
| 233 | vnet_feature_config_main_t *cm; |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 234 | u8 feature_arc_index = fm->device_input_feature_arc_index; |
| 235 | cm = &fm->feature_config_mains[feature_arc_index]; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 236 | |
| 237 | if (PREDICT_FALSE |
| 238 | (clib_bitmap_get |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 239 | (fm->sw_if_index_has_features[feature_arc_index], sw_if_index))) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 240 | { |
| 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 Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 249 | b0->feature_arc_index = feature_arc_index; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 250 | 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 Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 257 | static_always_inline void |
| 258 | vnet_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 Marion | 7dc4146 | 2016-11-15 19:47:58 +0100 | [diff] [blame] | 298 | static_always_inline void |
| 299 | vnet_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 Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 362 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 363 | #define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0} |
| 364 | |
| 365 | clib_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 | |
| 372 | void 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 | */ |