blob: 59375746af5607e8c0ade85ac1e45d6721886b49 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
42static vnet_config_feature_t *
43duplicate_feature_vector (vnet_config_feature_t * feature_vector)
44{
Dave Barachba868bb2016-08-08 09:51:21 -040045 vnet_config_feature_t *result, *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47 result = vec_dup (feature_vector);
Dave Barachba868bb2016-08-08 09:51:21 -040048 vec_foreach (f, result) f->feature_config = vec_dup (f->feature_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
50 return result;
51}
52
53static void
54free_feature_vector (vnet_config_feature_t * feature_vector)
55{
Dave Barachba868bb2016-08-08 09:51:21 -040056 vnet_config_feature_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Dave Barachba868bb2016-08-08 09:51:21 -040058 vec_foreach (f, feature_vector) vnet_config_feature_free (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 vec_free (feature_vector);
60}
61
62static u32
63add_next (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -040064 vnet_config_main_t * cm, u32 last_node_index, u32 this_node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
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 Barachba868bb2016-08-08 09:51:21 -040074 tmp =
75 vlib_node_add_next (vm, cm->start_node_indices[i], this_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 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
85static vnet_config_t *
86find_config_with_features (vlib_main_t * vm,
87 vnet_config_main_t * cm,
Neale Ranns5d0136f2020-05-12 08:51:02 +000088 vnet_config_feature_t * feature_vector,
89 u32 end_node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
91 u32 last_node_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -040092 vnet_config_feature_t *f;
93 u32 *config_string;
94 uword *p;
95 vnet_config_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 config_string = cm->config_string_temp;
98 cm->config_string_temp = 0;
99 if (config_string)
100 _vec_len (config_string) = 0;
101
102 vec_foreach (f, feature_vector)
Dave Barachba868bb2016-08-08 09:51:21 -0400103 {
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 Warnickecb9cada2015-12-08 15:45:58 -0700107
Dave Barachba868bb2016-08-08 09:51:21 -0400108 /* Store next index in config string. */
109 vec_add1 (config_string, f->next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Dave Barachba868bb2016-08-08 09:51:21 -0400111 /* Store feature config. */
112 vec_add (config_string, f->feature_config, vec_len (f->feature_config));
113 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 /* Terminate config string with next for end node. */
Neale Ranns5d0136f2020-05-12 08:51:02 +0000116 if (last_node_index == ~0 || last_node_index != end_node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 {
Neale Ranns5d0136f2020-05-12 08:51:02 +0000118 u32 next_index = add_next (vm, cm, last_node_index, end_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 vec_add1 (config_string, next_index);
120 }
121
122 /* See if config string is unique. */
123 p = hash_get_mem (cm->config_string_hash, config_string);
124 if (p)
125 {
126 /* Not unique. Share existing config. */
Dave Barachba868bb2016-08-08 09:51:21 -0400127 cm->config_string_temp = config_string; /* we'll use it again later. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 free_feature_vector (feature_vector);
129 c = pool_elt_at_index (cm->config_pool, p[0]);
130 }
131 else
132 {
Dave Barachba868bb2016-08-08 09:51:21 -0400133 u32 *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 pool_get (cm->config_pool, c);
136 c->index = c - cm->config_pool;
137 c->features = feature_vector;
138 c->config_string_vector = config_string;
139
140 /* Allocate copy of config string in heap.
Dave Barachba868bb2016-08-08 09:51:21 -0400141 VLIB buffers will maintain pointers to heap as they read out
142 configuration data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 c->config_string_heap_index
144 = heap_alloc (cm->config_string_heap, vec_len (config_string) + 1,
145 c->config_string_heap_handle);
146
147 /* First element in heap points back to pool index. */
Dave Barachba868bb2016-08-08 09:51:21 -0400148 d =
149 vec_elt_at_index (cm->config_string_heap,
150 c->config_string_heap_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 d[0] = c->index;
Damjan Marionf1213b82016-03-13 02:22:06 +0100152 clib_memcpy (d + 1, config_string, vec_bytes (config_string));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 hash_set_mem (cm->config_string_hash, config_string, c->index);
154
Dave Barachba868bb2016-08-08 09:51:21 -0400155 c->reference_count = 0; /* will be incremented by caller. */
Neale Ranns5d0136f2020-05-12 08:51:02 +0000156
157 vec_validate_init_empty (cm->end_node_indices_by_user_index,
158 c->config_string_heap_index + 1,
159 cm->default_end_node_index);
160 cm->end_node_indices_by_user_index[c->config_string_heap_index + 1]
161 = end_node_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 }
163
164 return c;
165}
166
Dave Barachba868bb2016-08-08 09:51:21 -0400167void
168vnet_config_init (vlib_main_t * vm,
169 vnet_config_main_t * cm,
170 char *start_node_names[],
171 int n_start_node_names,
172 char *feature_node_names[], int n_feature_node_names)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
Dave Barachba868bb2016-08-08 09:51:21 -0400174 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 u32 i;
176
Dave Barachb7b92992018-10-17 10:38:51 -0400177 clib_memset (cm, 0, sizeof (cm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Dave Barachba868bb2016-08-08 09:51:21 -0400179 cm->config_string_hash =
180 hash_create_vec (0,
181 STRUCT_SIZE_OF (vnet_config_t, config_string_vector[0]),
182 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 ASSERT (n_feature_node_names >= 1);
185
186 vec_resize (cm->start_node_indices, n_start_node_names);
187 for (i = 0; i < n_start_node_names; i++)
188 {
189 n = vlib_get_node_by_name (vm, (u8 *) start_node_names[i]);
190 /* Given node name must exist. */
191 ASSERT (n != 0);
192 cm->start_node_indices[i] = n->index;
193 }
194
195 vec_resize (cm->node_index_by_feature_index, n_feature_node_names);
196 for (i = 0; i < n_feature_node_names; i++)
197 {
Dave Barachba868bb2016-08-08 09:51:21 -0400198 if (!feature_node_names[i])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 cm->node_index_by_feature_index[i] = ~0;
200 else
201 {
202 n = vlib_get_node_by_name (vm, (u8 *) feature_node_names[i]);
203 /* Given node may exist in plug-in library which is not present */
204 if (n)
205 {
206 if (i + 1 == n_feature_node_names)
Neale Ranns5d0136f2020-05-12 08:51:02 +0000207 cm->default_end_node_index = n->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 cm->node_index_by_feature_index[i] = n->index;
Dave Barachba868bb2016-08-08 09:51:21 -0400209 }
210 else
211 cm->node_index_by_feature_index[i] = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 }
213 }
214}
215
216static void
217remove_reference (vnet_config_main_t * cm, vnet_config_t * c)
218{
219 ASSERT (c->reference_count > 0);
220 c->reference_count -= 1;
221 if (c->reference_count == 0)
222 {
223 hash_unset (cm->config_string_hash, c->config_string_vector);
224 vnet_config_free (cm, c);
225 pool_put (cm->config_pool, c);
226 }
227}
228
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500229static int
Dave Barachba868bb2016-08-08 09:51:21 -0400230feature_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500231{
Dave Barachba868bb2016-08-08 09:51:21 -0400232 vnet_config_feature_t *f1 = a1;
233 vnet_config_feature_t *f2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500234
235 return (int) f1->feature_index - f2->feature_index;
236}
237
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238always_inline u32 *
239vnet_get_config_heap (vnet_config_main_t * cm, u32 ci)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
Dave Barachba868bb2016-08-08 09:51:21 -0400241 return heap_elt_at_index (cm->config_string_heap, ci);
242}
243
244u32
Neale Ranns4ec36c52020-03-31 09:21:29 -0400245vnet_config_modify_end_node (vlib_main_t * vm,
246 vnet_config_main_t * cm,
247 u32 config_string_heap_index, u32 end_node_index)
248{
249 vnet_config_feature_t *new_features;
250 vnet_config_t *old, *new;
251
252 if (end_node_index == ~0) // feature node does not exist
253 return ~0;
254
255 if (config_string_heap_index == ~0)
256 {
257 old = 0;
258 new_features = 0;
259 }
260 else
261 {
262 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
263 old = pool_elt_at_index (cm->config_pool, p[-1]);
264 new_features = old->features;
265 if (new_features)
266 new_features = duplicate_feature_vector (new_features);
267 }
268
269 if (vec_len (new_features))
270 {
271 /* is the last feature the cuurent end node */
272 u32 last = vec_len (new_features) - 1;
Neale Ranns5d0136f2020-05-12 08:51:02 +0000273 if (new_features[last].node_index == cm->default_end_node_index)
Neale Ranns4ec36c52020-03-31 09:21:29 -0400274 {
275 vec_free (new_features->feature_config);
276 _vec_len (new_features) = last;
277 }
278 }
279
280 if (old)
281 remove_reference (cm, old);
282
Neale Ranns5d0136f2020-05-12 08:51:02 +0000283 new = find_config_with_features (vm, cm, new_features, end_node_index);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400284 new->reference_count += 1;
285
286 /*
287 * User gets pointer to config string first element
288 * (which defines the pool index
289 * this config string comes from).
290 */
291 vec_validate (cm->config_pool_index_by_user_index,
292 new->config_string_heap_index + 1);
293 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
294 = new - cm->config_pool;
295 return new->config_string_heap_index + 1;
296}
297
298u32
Dave Barachba868bb2016-08-08 09:51:21 -0400299vnet_config_add_feature (vlib_main_t * vm,
300 vnet_config_main_t * cm,
301 u32 config_string_heap_index,
302 u32 feature_index,
303 void *feature_config, u32 n_feature_config_bytes)
304{
305 vnet_config_t *old, *new;
306 vnet_config_feature_t *new_features, *f;
Neale Ranns5d0136f2020-05-12 08:51:02 +0000307 u32 n_feature_config_u32s, end_node_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index);
309
Dave Barachba868bb2016-08-08 09:51:21 -0400310 if (node_index == ~0) // feature node does not exist
Matthew Smithc3267ed2018-05-15 15:51:30 -0500311 return ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 if (config_string_heap_index == ~0)
314 {
315 old = 0;
316 new_features = 0;
Neale Ranns5d0136f2020-05-12 08:51:02 +0000317 end_node_index = cm->default_end_node_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 }
319 else
320 {
Dave Barachba868bb2016-08-08 09:51:21 -0400321 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 old = pool_elt_at_index (cm->config_pool, p[-1]);
323 new_features = old->features;
Neale Ranns5d0136f2020-05-12 08:51:02 +0000324 end_node_index =
325 cm->end_node_indices_by_user_index[config_string_heap_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 if (new_features)
327 new_features = duplicate_feature_vector (new_features);
328 }
329
330 vec_add2 (new_features, f, 1);
331 f->feature_index = feature_index;
332 f->node_index = node_index;
333
Dave Barachba868bb2016-08-08 09:51:21 -0400334 n_feature_config_u32s =
335 round_pow2 (n_feature_config_bytes,
336 sizeof (f->feature_config[0])) /
337 sizeof (f->feature_config[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 vec_add (f->feature_config, feature_config, n_feature_config_u32s);
Dave Barachba868bb2016-08-08 09:51:21 -0400339
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 /* Sort (prioritize) features. */
341 if (vec_len (new_features) > 1)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500342 vec_sort_with_function (new_features, feature_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344 if (old)
345 remove_reference (cm, old);
346
Neale Ranns5d0136f2020-05-12 08:51:02 +0000347 new = find_config_with_features (vm, cm, new_features, end_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 new->reference_count += 1;
349
Dave Barachba868bb2016-08-08 09:51:21 -0400350 /*
351 * User gets pointer to config string first element
Dave Barachde393bb2016-06-23 11:27:51 -0400352 * (which defines the pool index
Dave Barachba868bb2016-08-08 09:51:21 -0400353 * this config string comes from).
Dave Barachde393bb2016-06-23 11:27:51 -0400354 */
355 vec_validate (cm->config_pool_index_by_user_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400356 new->config_string_heap_index + 1);
357 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
358 = new - cm->config_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 return new->config_string_heap_index + 1;
360}
361
Dave Barachba868bb2016-08-08 09:51:21 -0400362u32
363vnet_config_del_feature (vlib_main_t * vm,
364 vnet_config_main_t * cm,
365 u32 config_string_heap_index,
366 u32 feature_index,
367 void *feature_config, u32 n_feature_config_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368{
Dave Barachba868bb2016-08-08 09:51:21 -0400369 vnet_config_t *old, *new;
370 vnet_config_feature_t *new_features, *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 u32 n_feature_config_u32s;
372
373 {
Dave Barachba868bb2016-08-08 09:51:21 -0400374 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
376 old = pool_elt_at_index (cm->config_pool, p[-1]);
377 }
378
Dave Barachba868bb2016-08-08 09:51:21 -0400379 n_feature_config_u32s =
380 round_pow2 (n_feature_config_bytes,
381 sizeof (f->feature_config[0])) /
382 sizeof (f->feature_config[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
384 /* Find feature with same index and opaque data. */
385 vec_foreach (f, old->features)
Dave Barachba868bb2016-08-08 09:51:21 -0400386 {
387 if (f->feature_index == feature_index
388 && vec_len (f->feature_config) == n_feature_config_u32s
389 && (n_feature_config_u32s == 0
390 || !memcmp (f->feature_config, feature_config,
391 n_feature_config_bytes)))
392 break;
393 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395 /* Feature not found. */
396 if (f >= vec_end (old->features))
Matthew Smithc3267ed2018-05-15 15:51:30 -0500397 return ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399 new_features = duplicate_feature_vector (old->features);
400 f = new_features + (f - old->features);
401 vnet_config_feature_free (f);
402 vec_delete (new_features, 1, f - new_features);
403
404 /* must remove old from config_pool now as it may be expanded and change
Dave Barachba868bb2016-08-08 09:51:21 -0400405 memory location if the following function find_config_with_features()
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 adds a new config because none of existing config's has matching features
407 and so can be reused */
408 remove_reference (cm, old);
Neale Ranns5d0136f2020-05-12 08:51:02 +0000409 new = find_config_with_features (vm, cm, new_features,
410 cm->end_node_indices_by_user_index
411 [config_string_heap_index]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 new->reference_count += 1;
413
Dave Barachde393bb2016-06-23 11:27:51 -0400414 vec_validate (cm->config_pool_index_by_user_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400415 new->config_string_heap_index + 1);
416 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
417 = new - cm->config_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 return new->config_string_heap_index + 1;
419}
Dave Barachba868bb2016-08-08 09:51:21 -0400420
421/*
422 * fd.io coding-style-patch-verification: ON
423 *
424 * Local Variables:
425 * eval: (c-set-style "gnu")
426 * End:
427 */