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:
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 * config.c: feature configuration
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
42 static vnet_config_feature_t *
43 duplicate_feature_vector (vnet_config_feature_t * feature_vector)
45 vnet_config_feature_t *result, *f;
47 result = vec_dup (feature_vector);
48 vec_foreach (f, result) f->feature_config = vec_dup (f->feature_config);
54 free_feature_vector (vnet_config_feature_t * feature_vector)
56 vnet_config_feature_t *f;
58 vec_foreach (f, feature_vector) vnet_config_feature_free (f);
59 vec_free (feature_vector);
63 add_next (vlib_main_t * vm,
64 vnet_config_main_t * cm, u32 last_node_index, u32 this_node_index)
68 if (last_node_index != ~0)
69 return vlib_node_add_next (vm, last_node_index, this_node_index);
71 for (i = 0; i < vec_len (cm->start_node_indices); i++)
75 vlib_node_add_next (vm, cm->start_node_indices[i], this_node_index);
78 /* Start nodes to first must agree on next indices. */
85 static vnet_config_t *
86 find_config_with_features (vlib_main_t * vm,
87 vnet_config_main_t * cm,
88 vnet_config_feature_t * feature_vector)
90 u32 last_node_index = ~0;
91 vnet_config_feature_t *f;
96 config_string = cm->config_string_temp;
97 cm->config_string_temp = 0;
99 _vec_len (config_string) = 0;
101 vec_foreach (f, feature_vector)
103 /* Connect node graph. */
104 f->next_index = add_next (vm, cm, last_node_index, f->node_index);
105 last_node_index = f->node_index;
107 /* Store next index in config string. */
108 vec_add1 (config_string, f->next_index);
110 /* Store feature config. */
111 vec_add (config_string, f->feature_config, vec_len (f->feature_config));
114 /* Terminate config string with next for end node. */
115 if (last_node_index == ~0 || last_node_index != cm->end_node_index)
117 u32 next_index = add_next (vm, cm, last_node_index, cm->end_node_index);
118 vec_add1 (config_string, next_index);
121 /* See if config string is unique. */
122 p = hash_get_mem (cm->config_string_hash, config_string);
125 /* Not unique. Share existing config. */
126 cm->config_string_temp = config_string; /* we'll use it again later. */
127 free_feature_vector (feature_vector);
128 c = pool_elt_at_index (cm->config_pool, p[0]);
134 pool_get (cm->config_pool, c);
135 c->index = c - cm->config_pool;
136 c->features = feature_vector;
137 c->config_string_vector = config_string;
139 /* Allocate copy of config string in heap.
140 VLIB buffers will maintain pointers to heap as they read out
141 configuration data. */
142 c->config_string_heap_index
143 = heap_alloc (cm->config_string_heap, vec_len (config_string) + 1,
144 c->config_string_heap_handle);
146 /* First element in heap points back to pool index. */
148 vec_elt_at_index (cm->config_string_heap,
149 c->config_string_heap_index);
151 clib_memcpy (d + 1, config_string, vec_bytes (config_string));
152 hash_set_mem (cm->config_string_hash, config_string, c->index);
154 c->reference_count = 0; /* will be incremented by caller. */
161 vnet_config_init (vlib_main_t * vm,
162 vnet_config_main_t * cm,
163 char *start_node_names[],
164 int n_start_node_names,
165 char *feature_node_names[], int n_feature_node_names)
170 clib_memset (cm, 0, sizeof (cm[0]));
172 cm->config_string_hash =
174 STRUCT_SIZE_OF (vnet_config_t, config_string_vector[0]),
177 ASSERT (n_feature_node_names >= 1);
179 vec_resize (cm->start_node_indices, n_start_node_names);
180 for (i = 0; i < n_start_node_names; i++)
182 n = vlib_get_node_by_name (vm, (u8 *) start_node_names[i]);
183 /* Given node name must exist. */
185 cm->start_node_indices[i] = n->index;
188 vec_resize (cm->node_index_by_feature_index, n_feature_node_names);
189 for (i = 0; i < n_feature_node_names; i++)
191 if (!feature_node_names[i])
192 cm->node_index_by_feature_index[i] = ~0;
195 n = vlib_get_node_by_name (vm, (u8 *) feature_node_names[i]);
196 /* Given node may exist in plug-in library which is not present */
199 if (i + 1 == n_feature_node_names)
200 cm->end_node_index = n->index;
201 cm->node_index_by_feature_index[i] = n->index;
204 cm->node_index_by_feature_index[i] = ~0;
210 remove_reference (vnet_config_main_t * cm, vnet_config_t * c)
212 ASSERT (c->reference_count > 0);
213 c->reference_count -= 1;
214 if (c->reference_count == 0)
216 hash_unset (cm->config_string_hash, c->config_string_vector);
217 vnet_config_free (cm, c);
218 pool_put (cm->config_pool, c);
223 feature_cmp (void *a1, void *a2)
225 vnet_config_feature_t *f1 = a1;
226 vnet_config_feature_t *f2 = a2;
228 return (int) f1->feature_index - f2->feature_index;
232 vnet_get_config_heap (vnet_config_main_t * cm, u32 ci)
234 return heap_elt_at_index (cm->config_string_heap, ci);
238 vnet_config_modify_end_node (vlib_main_t * vm,
239 vnet_config_main_t * cm,
240 u32 config_string_heap_index, u32 end_node_index)
242 vnet_config_feature_t *new_features;
243 vnet_config_t *old, *new;
245 if (end_node_index == ~0) // feature node does not exist
248 if (config_string_heap_index == ~0)
255 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
256 old = pool_elt_at_index (cm->config_pool, p[-1]);
257 new_features = old->features;
259 new_features = duplicate_feature_vector (new_features);
262 if (vec_len (new_features))
264 /* is the last feature the cuurent end node */
265 u32 last = vec_len (new_features) - 1;
266 if (new_features[last].node_index == cm->end_node_index)
268 vec_free (new_features->feature_config);
269 _vec_len (new_features) = last;
274 remove_reference (cm, old);
276 cm->end_node_index = end_node_index;
278 new = find_config_with_features (vm, cm, new_features);
279 new->reference_count += 1;
282 * User gets pointer to config string first element
283 * (which defines the pool index
284 * this config string comes from).
286 vec_validate (cm->config_pool_index_by_user_index,
287 new->config_string_heap_index + 1);
288 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
289 = new - cm->config_pool;
290 return new->config_string_heap_index + 1;
294 vnet_config_add_feature (vlib_main_t * vm,
295 vnet_config_main_t * cm,
296 u32 config_string_heap_index,
298 void *feature_config, u32 n_feature_config_bytes)
300 vnet_config_t *old, *new;
301 vnet_config_feature_t *new_features, *f;
302 u32 n_feature_config_u32s;
303 u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index);
305 if (node_index == ~0) // feature node does not exist
308 if (config_string_heap_index == ~0)
315 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
316 old = pool_elt_at_index (cm->config_pool, p[-1]);
317 new_features = old->features;
319 new_features = duplicate_feature_vector (new_features);
322 vec_add2 (new_features, f, 1);
323 f->feature_index = feature_index;
324 f->node_index = node_index;
326 n_feature_config_u32s =
327 round_pow2 (n_feature_config_bytes,
328 sizeof (f->feature_config[0])) /
329 sizeof (f->feature_config[0]);
330 vec_add (f->feature_config, feature_config, n_feature_config_u32s);
332 /* Sort (prioritize) features. */
333 if (vec_len (new_features) > 1)
334 vec_sort_with_function (new_features, feature_cmp);
337 remove_reference (cm, old);
339 new = find_config_with_features (vm, cm, new_features);
340 new->reference_count += 1;
343 * User gets pointer to config string first element
344 * (which defines the pool index
345 * this config string comes from).
347 vec_validate (cm->config_pool_index_by_user_index,
348 new->config_string_heap_index + 1);
349 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
350 = new - cm->config_pool;
351 return new->config_string_heap_index + 1;
355 vnet_config_del_feature (vlib_main_t * vm,
356 vnet_config_main_t * cm,
357 u32 config_string_heap_index,
359 void *feature_config, u32 n_feature_config_bytes)
361 vnet_config_t *old, *new;
362 vnet_config_feature_t *new_features, *f;
363 u32 n_feature_config_u32s;
366 u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
368 old = pool_elt_at_index (cm->config_pool, p[-1]);
371 n_feature_config_u32s =
372 round_pow2 (n_feature_config_bytes,
373 sizeof (f->feature_config[0])) /
374 sizeof (f->feature_config[0]);
376 /* Find feature with same index and opaque data. */
377 vec_foreach (f, old->features)
379 if (f->feature_index == feature_index
380 && vec_len (f->feature_config) == n_feature_config_u32s
381 && (n_feature_config_u32s == 0
382 || !memcmp (f->feature_config, feature_config,
383 n_feature_config_bytes)))
387 /* Feature not found. */
388 if (f >= vec_end (old->features))
391 new_features = duplicate_feature_vector (old->features);
392 f = new_features + (f - old->features);
393 vnet_config_feature_free (f);
394 vec_delete (new_features, 1, f - new_features);
396 /* must remove old from config_pool now as it may be expanded and change
397 memory location if the following function find_config_with_features()
398 adds a new config because none of existing config's has matching features
399 and so can be reused */
400 remove_reference (cm, old);
401 new = find_config_with_features (vm, cm, new_features);
402 new->reference_count += 1;
404 vec_validate (cm->config_pool_index_by_user_index,
405 new->config_string_heap_index + 1);
406 cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
407 = new - cm->config_pool;
408 return new->config_string_heap_index + 1;
412 * fd.io coding-style-patch-verification: ON
415 * eval: (c-set-style "gnu")