Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | * config.c: feature configuration |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/vnet.h> |
| 41 | |
| 42 | static vnet_config_feature_t * |
| 43 | duplicate_feature_vector (vnet_config_feature_t * feature_vector) |
| 44 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 45 | vnet_config_feature_t *result, *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | result = vec_dup (feature_vector); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 48 | vec_foreach (f, result) f->feature_config = vec_dup (f->feature_config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | free_feature_vector (vnet_config_feature_t * feature_vector) |
| 55 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 56 | vnet_config_feature_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 58 | vec_foreach (f, feature_vector) vnet_config_feature_free (f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | vec_free (feature_vector); |
| 60 | } |
| 61 | |
| 62 | static u32 |
| 63 | add_next (vlib_main_t * vm, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 64 | vnet_config_main_t * cm, u32 last_node_index, u32 this_node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
| 66 | u32 i, ni = ~0; |
| 67 | |
| 68 | if (last_node_index != ~0) |
| 69 | return vlib_node_add_next (vm, last_node_index, this_node_index); |
| 70 | |
| 71 | for (i = 0; i < vec_len (cm->start_node_indices); i++) |
| 72 | { |
| 73 | u32 tmp; |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 74 | tmp = |
| 75 | vlib_node_add_next (vm, cm->start_node_indices[i], this_node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | if (ni == ~0) |
| 77 | ni = tmp; |
| 78 | /* Start nodes to first must agree on next indices. */ |
| 79 | ASSERT (ni == tmp); |
| 80 | } |
| 81 | |
| 82 | return ni; |
| 83 | } |
| 84 | |
| 85 | static vnet_config_t * |
| 86 | find_config_with_features (vlib_main_t * vm, |
| 87 | vnet_config_main_t * cm, |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 88 | vnet_config_feature_t * feature_vector, |
| 89 | u32 end_node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
| 91 | u32 last_node_index = ~0; |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 92 | vnet_config_feature_t *f; |
| 93 | u32 *config_string; |
| 94 | uword *p; |
| 95 | vnet_config_t *c; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
| 97 | config_string = cm->config_string_temp; |
| 98 | cm->config_string_temp = 0; |
| 99 | if (config_string) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 100 | vec_set_len (config_string, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
| 102 | vec_foreach (f, feature_vector) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 103 | { |
| 104 | /* Connect node graph. */ |
| 105 | f->next_index = add_next (vm, cm, last_node_index, f->node_index); |
| 106 | last_node_index = f->node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 108 | /* Store next index in config string. */ |
| 109 | vec_add1 (config_string, f->next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 111 | /* Store feature config. */ |
| 112 | vec_add (config_string, f->feature_config, vec_len (f->feature_config)); |
| 113 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
| 115 | /* Terminate config string with next for end node. */ |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 116 | if (last_node_index == ~0 || last_node_index != end_node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 118 | u32 next_index = add_next (vm, cm, last_node_index, end_node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | vec_add1 (config_string, next_index); |
| 120 | } |
| 121 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 122 | /* Add the end node index to the config string so that it is part of |
| 123 | * the key used to detect string sharing. If this is not included then |
| 124 | * a modification of the end node would affect all the user of a shared |
| 125 | * string. */ |
| 126 | vec_add1 (config_string, end_node_index); |
| 127 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | /* See if config string is unique. */ |
| 129 | p = hash_get_mem (cm->config_string_hash, config_string); |
| 130 | if (p) |
| 131 | { |
| 132 | /* Not unique. Share existing config. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 133 | cm->config_string_temp = config_string; /* we'll use it again later. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | free_feature_vector (feature_vector); |
| 135 | c = pool_elt_at_index (cm->config_pool, p[0]); |
| 136 | } |
| 137 | else |
| 138 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 139 | u32 *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
| 141 | pool_get (cm->config_pool, c); |
| 142 | c->index = c - cm->config_pool; |
| 143 | c->features = feature_vector; |
| 144 | c->config_string_vector = config_string; |
| 145 | |
| 146 | /* Allocate copy of config string in heap. |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 147 | VLIB buffers will maintain pointers to heap as they read out |
| 148 | configuration data. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | c->config_string_heap_index |
| 150 | = heap_alloc (cm->config_string_heap, vec_len (config_string) + 1, |
| 151 | c->config_string_heap_handle); |
| 152 | |
| 153 | /* First element in heap points back to pool index. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 154 | d = |
| 155 | vec_elt_at_index (cm->config_string_heap, |
| 156 | c->config_string_heap_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | d[0] = c->index; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 158 | clib_memcpy (d + 1, config_string, vec_bytes (config_string)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | hash_set_mem (cm->config_string_hash, config_string, c->index); |
| 160 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 161 | c->reference_count = 0; /* will be incremented by caller. */ |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 162 | |
| 163 | vec_validate_init_empty (cm->end_node_indices_by_user_index, |
| 164 | c->config_string_heap_index + 1, |
| 165 | cm->default_end_node_index); |
| 166 | cm->end_node_indices_by_user_index[c->config_string_heap_index + 1] |
| 167 | = end_node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return c; |
| 171 | } |
| 172 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 173 | void |
| 174 | vnet_config_init (vlib_main_t * vm, |
| 175 | vnet_config_main_t * cm, |
| 176 | char *start_node_names[], |
| 177 | int n_start_node_names, |
| 178 | char *feature_node_names[], int n_feature_node_names) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 180 | vlib_node_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | u32 i; |
| 182 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 183 | clib_memset (cm, 0, sizeof (cm[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 185 | cm->config_string_hash = |
| 186 | hash_create_vec (0, |
| 187 | STRUCT_SIZE_OF (vnet_config_t, config_string_vector[0]), |
| 188 | sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | ASSERT (n_feature_node_names >= 1); |
| 191 | |
| 192 | vec_resize (cm->start_node_indices, n_start_node_names); |
| 193 | for (i = 0; i < n_start_node_names; i++) |
| 194 | { |
| 195 | n = vlib_get_node_by_name (vm, (u8 *) start_node_names[i]); |
| 196 | /* Given node name must exist. */ |
| 197 | ASSERT (n != 0); |
| 198 | cm->start_node_indices[i] = n->index; |
| 199 | } |
| 200 | |
| 201 | vec_resize (cm->node_index_by_feature_index, n_feature_node_names); |
| 202 | for (i = 0; i < n_feature_node_names; i++) |
| 203 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 204 | if (!feature_node_names[i]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | cm->node_index_by_feature_index[i] = ~0; |
| 206 | else |
| 207 | { |
| 208 | n = vlib_get_node_by_name (vm, (u8 *) feature_node_names[i]); |
| 209 | /* Given node may exist in plug-in library which is not present */ |
| 210 | if (n) |
| 211 | { |
| 212 | if (i + 1 == n_feature_node_names) |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 213 | cm->default_end_node_index = n->index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | cm->node_index_by_feature_index[i] = n->index; |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | cm->node_index_by_feature_index[i] = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | remove_reference (vnet_config_main_t * cm, vnet_config_t * c) |
| 224 | { |
| 225 | ASSERT (c->reference_count > 0); |
| 226 | c->reference_count -= 1; |
| 227 | if (c->reference_count == 0) |
| 228 | { |
| 229 | hash_unset (cm->config_string_hash, c->config_string_vector); |
| 230 | vnet_config_free (cm, c); |
| 231 | pool_put (cm->config_pool, c); |
| 232 | } |
| 233 | } |
| 234 | |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 235 | static int |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 236 | feature_cmp (void *a1, void *a2) |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 237 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 238 | vnet_config_feature_t *f1 = a1; |
| 239 | vnet_config_feature_t *f2 = a2; |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 240 | |
| 241 | return (int) f1->feature_index - f2->feature_index; |
| 242 | } |
| 243 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | always_inline u32 * |
| 245 | vnet_get_config_heap (vnet_config_main_t * cm, u32 ci) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 247 | return heap_elt_at_index (cm->config_string_heap, ci); |
| 248 | } |
| 249 | |
BenoƮt Ganne | 6178bda | 2020-11-04 10:02:03 +0100 | [diff] [blame] | 250 | void |
| 251 | vnet_config_del (vnet_config_main_t * cm, u32 config_id) |
| 252 | { |
| 253 | u32 *p = vnet_get_config_heap (cm, config_id); |
| 254 | vnet_config_t *old = pool_elt_at_index (cm->config_pool, p[-1]); |
| 255 | remove_reference (cm, old); |
| 256 | } |
| 257 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 258 | u32 |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 259 | vnet_config_reset_end_node (vlib_main_t *vm, vnet_config_main_t *cm, u32 ci) |
| 260 | { |
| 261 | cm->end_node_indices_by_user_index[ci] = cm->default_end_node_index; |
| 262 | |
| 263 | return ( |
| 264 | vnet_config_modify_end_node (vm, cm, ci, cm->default_end_node_index)); |
| 265 | } |
| 266 | |
| 267 | u32 |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 268 | vnet_config_modify_end_node (vlib_main_t * vm, |
| 269 | vnet_config_main_t * cm, |
| 270 | u32 config_string_heap_index, u32 end_node_index) |
| 271 | { |
| 272 | vnet_config_feature_t *new_features; |
| 273 | vnet_config_t *old, *new; |
| 274 | |
| 275 | if (end_node_index == ~0) // feature node does not exist |
| 276 | return ~0; |
| 277 | |
| 278 | if (config_string_heap_index == ~0) |
| 279 | { |
| 280 | old = 0; |
| 281 | new_features = 0; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | u32 *p = vnet_get_config_heap (cm, config_string_heap_index); |
| 286 | old = pool_elt_at_index (cm->config_pool, p[-1]); |
| 287 | new_features = old->features; |
| 288 | if (new_features) |
| 289 | new_features = duplicate_feature_vector (new_features); |
| 290 | } |
| 291 | |
| 292 | if (vec_len (new_features)) |
| 293 | { |
| 294 | /* is the last feature the cuurent end node */ |
| 295 | u32 last = vec_len (new_features) - 1; |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 296 | if (new_features[last].node_index == cm->default_end_node_index) |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 297 | { |
| 298 | vec_free (new_features->feature_config); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 299 | vec_set_len (new_features, last); |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | if (old) |
| 304 | remove_reference (cm, old); |
| 305 | |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 306 | new = find_config_with_features (vm, cm, new_features, end_node_index); |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 307 | new->reference_count += 1; |
| 308 | |
| 309 | /* |
| 310 | * User gets pointer to config string first element |
| 311 | * (which defines the pool index |
| 312 | * this config string comes from). |
| 313 | */ |
| 314 | vec_validate (cm->config_pool_index_by_user_index, |
| 315 | new->config_string_heap_index + 1); |
| 316 | cm->config_pool_index_by_user_index[new->config_string_heap_index + 1] |
| 317 | = new - cm->config_pool; |
| 318 | return new->config_string_heap_index + 1; |
| 319 | } |
| 320 | |
| 321 | u32 |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 322 | vnet_config_get_end_node (vlib_main_t *vm, vnet_config_main_t *cm, |
| 323 | u32 config_string_heap_index) |
| 324 | { |
| 325 | if (config_string_heap_index >= vec_len (cm->end_node_indices_by_user_index)) |
| 326 | return cm->default_end_node_index; |
| 327 | if (~0 == cm->end_node_indices_by_user_index[config_string_heap_index]) |
| 328 | return cm->default_end_node_index; |
| 329 | |
| 330 | return (cm->end_node_indices_by_user_index[config_string_heap_index]); |
| 331 | } |
| 332 | |
| 333 | u32 |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 334 | vnet_config_add_feature (vlib_main_t * vm, |
| 335 | vnet_config_main_t * cm, |
| 336 | u32 config_string_heap_index, |
| 337 | u32 feature_index, |
| 338 | void *feature_config, u32 n_feature_config_bytes) |
| 339 | { |
| 340 | vnet_config_t *old, *new; |
| 341 | vnet_config_feature_t *new_features, *f; |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 342 | u32 n_feature_config_u32s, end_node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index); |
| 344 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 345 | if (node_index == ~0) // feature node does not exist |
Matthew Smith | c3267ed | 2018-05-15 15:51:30 -0500 | [diff] [blame] | 346 | return ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | |
| 348 | if (config_string_heap_index == ~0) |
| 349 | { |
| 350 | old = 0; |
| 351 | new_features = 0; |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 352 | end_node_index = cm->default_end_node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | } |
| 354 | else |
| 355 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 356 | u32 *p = vnet_get_config_heap (cm, config_string_heap_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | old = pool_elt_at_index (cm->config_pool, p[-1]); |
| 358 | new_features = old->features; |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 359 | end_node_index = |
| 360 | cm->end_node_indices_by_user_index[config_string_heap_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | if (new_features) |
| 362 | new_features = duplicate_feature_vector (new_features); |
| 363 | } |
| 364 | |
| 365 | vec_add2 (new_features, f, 1); |
| 366 | f->feature_index = feature_index; |
| 367 | f->node_index = node_index; |
| 368 | |
BenoƮt Ganne | c79a14f | 2020-07-17 11:54:28 +0200 | [diff] [blame] | 369 | if (n_feature_config_bytes) |
| 370 | { |
| 371 | n_feature_config_u32s = |
| 372 | round_pow2 (n_feature_config_bytes, |
| 373 | sizeof (f->feature_config[0])) / |
| 374 | sizeof (f->feature_config[0]); |
| 375 | vec_validate (f->feature_config, n_feature_config_u32s - 1); |
| 376 | clib_memcpy_fast (f->feature_config, feature_config, |
| 377 | n_feature_config_bytes); |
| 378 | } |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 379 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | /* Sort (prioritize) features. */ |
| 381 | if (vec_len (new_features) > 1) |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 382 | vec_sort_with_function (new_features, feature_cmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
| 384 | if (old) |
| 385 | remove_reference (cm, old); |
| 386 | |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 387 | new = find_config_with_features (vm, cm, new_features, end_node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | new->reference_count += 1; |
| 389 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 390 | /* |
| 391 | * User gets pointer to config string first element |
Dave Barach | de393bb | 2016-06-23 11:27:51 -0400 | [diff] [blame] | 392 | * (which defines the pool index |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 393 | * this config string comes from). |
Dave Barach | de393bb | 2016-06-23 11:27:51 -0400 | [diff] [blame] | 394 | */ |
| 395 | vec_validate (cm->config_pool_index_by_user_index, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 396 | new->config_string_heap_index + 1); |
| 397 | cm->config_pool_index_by_user_index[new->config_string_heap_index + 1] |
| 398 | = new - cm->config_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | return new->config_string_heap_index + 1; |
| 400 | } |
| 401 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 402 | u32 |
| 403 | vnet_config_del_feature (vlib_main_t * vm, |
| 404 | vnet_config_main_t * cm, |
| 405 | u32 config_string_heap_index, |
| 406 | u32 feature_index, |
| 407 | void *feature_config, u32 n_feature_config_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 409 | vnet_config_t *old, *new; |
| 410 | vnet_config_feature_t *new_features, *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | u32 n_feature_config_u32s; |
| 412 | |
| 413 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 414 | u32 *p = vnet_get_config_heap (cm, config_string_heap_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | |
| 416 | old = pool_elt_at_index (cm->config_pool, p[-1]); |
| 417 | } |
| 418 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 419 | n_feature_config_u32s = |
| 420 | round_pow2 (n_feature_config_bytes, |
| 421 | sizeof (f->feature_config[0])) / |
| 422 | sizeof (f->feature_config[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | |
| 424 | /* Find feature with same index and opaque data. */ |
| 425 | vec_foreach (f, old->features) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 426 | { |
| 427 | if (f->feature_index == feature_index |
| 428 | && vec_len (f->feature_config) == n_feature_config_u32s |
| 429 | && (n_feature_config_u32s == 0 |
| 430 | || !memcmp (f->feature_config, feature_config, |
| 431 | n_feature_config_bytes))) |
| 432 | break; |
| 433 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | |
| 435 | /* Feature not found. */ |
| 436 | if (f >= vec_end (old->features)) |
Matthew Smith | c3267ed | 2018-05-15 15:51:30 -0500 | [diff] [blame] | 437 | return ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
| 439 | new_features = duplicate_feature_vector (old->features); |
| 440 | f = new_features + (f - old->features); |
| 441 | vnet_config_feature_free (f); |
| 442 | vec_delete (new_features, 1, f - new_features); |
| 443 | |
| 444 | /* must remove old from config_pool now as it may be expanded and change |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 445 | memory location if the following function find_config_with_features() |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | adds a new config because none of existing config's has matching features |
| 447 | and so can be reused */ |
| 448 | remove_reference (cm, old); |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 449 | new = find_config_with_features (vm, cm, new_features, |
| 450 | cm->end_node_indices_by_user_index |
| 451 | [config_string_heap_index]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | new->reference_count += 1; |
| 453 | |
Dave Barach | de393bb | 2016-06-23 11:27:51 -0400 | [diff] [blame] | 454 | vec_validate (cm->config_pool_index_by_user_index, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 455 | new->config_string_heap_index + 1); |
| 456 | cm->config_pool_index_by_user_index[new->config_string_heap_index + 1] |
| 457 | = new - cm->config_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | return new->config_string_heap_index + 1; |
| 459 | } |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * fd.io coding-style-patch-verification: ON |
| 463 | * |
| 464 | * Local Variables: |
| 465 | * eval: (c-set-style "gnu") |
| 466 | * End: |
| 467 | */ |