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> |
| 20 | |
| 21 | /** feature registration object */ |
| 22 | typedef struct _vnet_feature_arc_registration |
| 23 | { |
| 24 | /** next registration in list of all registrations*/ |
| 25 | struct _vnet_feature_arc_registration *next; |
| 26 | /** Feature Arc name */ |
| 27 | char *arc_name; |
| 28 | /** Start nodes */ |
| 29 | char **start_nodes; |
| 30 | int n_start_nodes; |
| 31 | /* Feature arc index, assigned by init function */ |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 32 | u8 feature_arc_index; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 33 | } vnet_feature_arc_registration_t; |
| 34 | |
| 35 | /** feature registration object */ |
| 36 | typedef struct _vnet_feature_registration |
| 37 | { |
| 38 | /** next registration in list of all registrations*/ |
| 39 | struct _vnet_feature_registration *next; |
| 40 | /** Feature arc name */ |
| 41 | char *arc_name; |
| 42 | /** Graph node name */ |
| 43 | char *node_name; |
| 44 | /** Pointer to this feature index, filled in by vnet_feature_arc_init */ |
| 45 | u32 *feature_index; |
| 46 | u32 feature_index_u32; |
| 47 | /** Constraints of the form "this feature runs before X" */ |
| 48 | char **runs_before; |
| 49 | /** Constraints of the form "this feature runs after Y" */ |
| 50 | char **runs_after; |
| 51 | } vnet_feature_registration_t; |
| 52 | |
| 53 | typedef struct vnet_feature_config_main_t_ |
| 54 | { |
| 55 | vnet_config_main_t config_main; |
| 56 | u32 *config_index_by_sw_if_index; |
| 57 | } vnet_feature_config_main_t; |
| 58 | |
| 59 | typedef struct |
| 60 | { |
| 61 | /** feature arc configuration list */ |
| 62 | vnet_feature_arc_registration_t *next_arc; |
| 63 | uword **arc_index_by_name; |
| 64 | |
| 65 | /** feature path configuration lists */ |
| 66 | vnet_feature_registration_t *next_feature; |
| 67 | vnet_feature_registration_t **next_feature_by_arc; |
| 68 | uword **next_feature_by_name; |
| 69 | |
| 70 | /** feature config main objects */ |
| 71 | vnet_feature_config_main_t *feature_config_mains; |
| 72 | |
| 73 | /** Save partial order results for show command */ |
| 74 | char ***feature_nodes; |
| 75 | |
| 76 | /** bitmap of interfaces which have driver rx features configured */ |
| 77 | uword **sw_if_index_has_features; |
| 78 | |
| 79 | /** feature reference counts by interface */ |
| 80 | i16 **feature_count_by_sw_if_index; |
| 81 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 82 | /** Feature arc index for device-input */ |
| 83 | u8 device_input_feature_arc_index; |
| 84 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 85 | /** convenience */ |
| 86 | vlib_main_t *vlib_main; |
| 87 | vnet_main_t *vnet_main; |
| 88 | } vnet_feature_main_t; |
| 89 | |
| 90 | extern vnet_feature_main_t feature_main; |
| 91 | |
| 92 | #define VNET_FEATURE_ARC_INIT(x,...) \ |
| 93 | __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\ |
| 94 | static void __vnet_add_feature_arc_registration_##x (void) \ |
| 95 | __attribute__((__constructor__)) ; \ |
| 96 | static void __vnet_add_feature_arc_registration_##x (void) \ |
| 97 | { \ |
| 98 | vnet_feature_main_t * fm = &feature_main; \ |
| 99 | vnet_feat_arc_##x.next = fm->next_arc; \ |
| 100 | fm->next_arc = & vnet_feat_arc_##x; \ |
| 101 | } \ |
| 102 | __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x |
| 103 | |
| 104 | #define VNET_FEATURE_INIT(x,...) \ |
| 105 | __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x; \ |
| 106 | static void __vnet_add_feature_registration_##x (void) \ |
| 107 | __attribute__((__constructor__)) ; \ |
| 108 | static void __vnet_add_feature_registration_##x (void) \ |
| 109 | { \ |
| 110 | vnet_feature_main_t * fm = &feature_main; \ |
| 111 | vnet_feat_##x.next = fm->next_feature; \ |
| 112 | fm->next_feature = & vnet_feat_##x; \ |
| 113 | } \ |
| 114 | __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x |
| 115 | |
| 116 | void |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 117 | vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc, |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 118 | u32 sw_if_index, int is_add); |
| 119 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 120 | u32 vnet_get_feature_index (u8 arc, const char *s); |
| 121 | u8 vnet_get_feature_arc_index (const char *s); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 122 | |
| 123 | void |
| 124 | vnet_feature_enable_disable (const char *arc_name, const char *node_name, |
| 125 | u32 sw_if_index, int enable_disable, |
| 126 | void *feature_config, |
| 127 | u32 n_feature_config_bytes); |
| 128 | |
| 129 | |
| 130 | static_always_inline int |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 131 | vnet_have_features (u8 arc, u32 sw_if_index) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 132 | { |
| 133 | vnet_feature_main_t *fm = &feature_main; |
| 134 | return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index); |
| 135 | } |
| 136 | |
| 137 | static_always_inline u32 |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 138 | vnet_get_feature_config_index (u8 arc, u32 sw_if_index) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 139 | { |
| 140 | vnet_feature_main_t *fm = &feature_main; |
| 141 | vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc]; |
| 142 | return vec_elt (cm->config_index_by_sw_if_index, sw_if_index); |
| 143 | } |
| 144 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 145 | static_always_inline void * |
| 146 | vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next, |
| 147 | vlib_buffer_t * b, u32 n_data_bytes) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 148 | { |
| 149 | vnet_feature_main_t *fm = &feature_main; |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 150 | vnet_feature_config_main_t *cm; |
| 151 | cm = &fm->feature_config_mains[arc]; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 152 | |
| 153 | if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index))) |
| 154 | { |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 155 | b->feature_arc_index = arc; |
| 156 | b->current_config_index = |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 157 | vec_elt (cm->config_index_by_sw_if_index, sw_if_index); |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 158 | return vnet_get_config_data (&cm->config_main, &b->current_config_index, |
| 159 | next, n_data_bytes); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | static_always_inline void |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 164 | vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0, |
| 165 | vlib_buffer_t * b0) |
| 166 | { |
| 167 | vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0); |
| 168 | } |
| 169 | |
| 170 | static_always_inline void * |
| 171 | vnet_feature_next_with_data (u32 sw_if_index, u32 * next0, |
| 172 | vlib_buffer_t * b0, u32 n_data_bytes) |
| 173 | { |
| 174 | vnet_feature_main_t *fm = &feature_main; |
| 175 | u8 arc = b0->feature_arc_index; |
| 176 | vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc]; |
| 177 | |
| 178 | return vnet_get_config_data (&cm->config_main, |
| 179 | &b0->current_config_index, next0, |
| 180 | n_data_bytes); |
| 181 | } |
| 182 | |
| 183 | static_always_inline void |
| 184 | vnet_feature_next (u32 sw_if_index, u32 * next0, vlib_buffer_t * b0) |
| 185 | { |
| 186 | vnet_feature_next_with_data (sw_if_index, next0, b0, 0); |
| 187 | } |
| 188 | |
| 189 | static_always_inline void |
| 190 | vnet_feature_start_device_input_x1 (u32 sw_if_index, u32 * next0, |
| 191 | vlib_buffer_t * b0, u16 buffer_advanced0) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 192 | { |
| 193 | vnet_feature_main_t *fm = &feature_main; |
| 194 | vnet_feature_config_main_t *cm; |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 195 | u8 feature_arc_index = fm->device_input_feature_arc_index; |
| 196 | cm = &fm->feature_config_mains[feature_arc_index]; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 197 | |
| 198 | if (PREDICT_FALSE |
| 199 | (clib_bitmap_get |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 200 | (fm->sw_if_index_has_features[feature_arc_index], sw_if_index))) |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 201 | { |
| 202 | /* |
| 203 | * Save next0 so that the last feature in the chain |
| 204 | * can skip ethernet-input if indicated... |
| 205 | */ |
| 206 | vnet_buffer (b0)->device_input_feat.saved_next_index = *next0; |
| 207 | vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0; |
| 208 | vlib_buffer_advance (b0, -buffer_advanced0); |
| 209 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 210 | b0->feature_arc_index = feature_arc_index; |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 211 | b0->current_config_index = |
| 212 | vec_elt (cm->config_index_by_sw_if_index, sw_if_index); |
| 213 | vnet_get_config_data (&cm->config_main, &b0->current_config_index, |
| 214 | next0, /* # bytes of config data */ 0); |
| 215 | } |
| 216 | } |
| 217 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame^] | 218 | static_always_inline void |
| 219 | vnet_feature_start_device_input_x2 (u32 sw_if_index, |
| 220 | u32 * next0, |
| 221 | u32 * next1, |
| 222 | vlib_buffer_t * b0, |
| 223 | vlib_buffer_t * b1, |
| 224 | u16 buffer_advanced0, |
| 225 | u16 buffer_advanced1) |
| 226 | { |
| 227 | vnet_feature_main_t *fm = &feature_main; |
| 228 | vnet_feature_config_main_t *cm; |
| 229 | u8 feature_arc_index = fm->device_input_feature_arc_index; |
| 230 | cm = &fm->feature_config_mains[feature_arc_index]; |
| 231 | |
| 232 | if (PREDICT_FALSE |
| 233 | (clib_bitmap_get |
| 234 | (fm->sw_if_index_has_features[feature_arc_index], sw_if_index))) |
| 235 | { |
| 236 | /* |
| 237 | * Save next0 so that the last feature in the chain |
| 238 | * can skip ethernet-input if indicated... |
| 239 | */ |
| 240 | vnet_buffer (b0)->device_input_feat.saved_next_index = *next0; |
| 241 | vnet_buffer (b1)->device_input_feat.saved_next_index = *next1; |
| 242 | vnet_buffer (b0)->device_input_feat.buffer_advance = buffer_advanced0; |
| 243 | vnet_buffer (b1)->device_input_feat.buffer_advance = buffer_advanced1; |
| 244 | vlib_buffer_advance (b0, -buffer_advanced0); |
| 245 | vlib_buffer_advance (b1, -buffer_advanced1); |
| 246 | |
| 247 | b0->feature_arc_index = feature_arc_index; |
| 248 | b1->feature_arc_index = feature_arc_index; |
| 249 | b0->current_config_index = |
| 250 | vec_elt (cm->config_index_by_sw_if_index, sw_if_index); |
| 251 | b1->current_config_index = b0->current_config_index; |
| 252 | vnet_get_config_data (&cm->config_main, &b0->current_config_index, |
| 253 | next0, /* # bytes of config data */ 0); |
| 254 | vnet_get_config_data (&cm->config_main, &b1->current_config_index, |
| 255 | next1, /* # bytes of config data */ 0); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 260 | #define ORDER_CONSTRAINTS (char*[]) |
| 261 | #define VNET_FEATURES(...) (char*[]) { __VA_ARGS__, 0} |
| 262 | |
| 263 | clib_error_t *vnet_feature_arc_init (vlib_main_t * vm, |
| 264 | vnet_config_main_t * vcm, |
| 265 | char **feature_start_nodes, |
| 266 | int num_feature_start_nodes, |
| 267 | vnet_feature_registration_t * |
| 268 | first_reg, char ***feature_nodes); |
| 269 | |
| 270 | void vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index); |
| 271 | |
| 272 | #endif /* included_feature_h */ |
| 273 | |
| 274 | /* |
| 275 | * fd.io coding-style-patch-verification: ON |
| 276 | * |
| 277 | * Local Variables: |
| 278 | * eval: (c-set-style "gnu") |
| 279 | * End: |
| 280 | */ |