FD.io VPP  v20.09-rc2-28-g3c5414029
Vector Packet Processing
config.c
Go to the documentation of this file.
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 *
44 {
45  vnet_config_feature_t *result, *f;
46 
47  result = vec_dup (feature_vector);
48  vec_foreach (f, result) f->feature_config = vec_dup (f->feature_config);
49 
50  return result;
51 }
52 
53 static void
55 {
57 
58  vec_foreach (f, feature_vector) vnet_config_feature_free (f);
59  vec_free (feature_vector);
60 }
61 
62 static u32
64  vnet_config_main_t * cm, u32 last_node_index, u32 this_node_index)
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;
74  tmp =
75  vlib_node_add_next (vm, cm->start_node_indices[i], this_node_index);
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 *
88  vnet_config_feature_t * feature_vector,
89  u32 end_node_index)
90 {
91  u32 last_node_index = ~0;
93  u32 *config_string;
94  uword *p;
96 
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)
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;
107 
108  /* Store next index in config string. */
109  vec_add1 (config_string, f->next_index);
110 
111  /* Store feature config. */
112  vec_add (config_string, f->feature_config, vec_len (f->feature_config));
113  }
114 
115  /* Terminate config string with next for end node. */
116  if (last_node_index == ~0 || last_node_index != end_node_index)
117  {
118  u32 next_index = add_next (vm, cm, last_node_index, end_node_index);
119  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. */
127  cm->config_string_temp = config_string; /* we'll use it again later. */
128  free_feature_vector (feature_vector);
129  c = pool_elt_at_index (cm->config_pool, p[0]);
130  }
131  else
132  {
133  u32 *d;
134 
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.
141  VLIB buffers will maintain pointers to heap as they read out
142  configuration data. */
144  = heap_alloc (cm->config_string_heap, vec_len (config_string) + 1,
146 
147  /* First element in heap points back to pool index. */
148  d =
151  d[0] = c->index;
152  clib_memcpy (d + 1, config_string, vec_bytes (config_string));
153  hash_set_mem (cm->config_string_hash, config_string, c->index);
154 
155  c->reference_count = 0; /* will be incremented by caller. */
156 
161  = end_node_index;
162  }
163 
164  return c;
165 }
166 
167 void
170  char *start_node_names[],
171  int n_start_node_names,
172  char *feature_node_names[], int n_feature_node_names)
173 {
174  vlib_node_t *n;
175  u32 i;
176 
177  clib_memset (cm, 0, sizeof (cm[0]));
178 
179  cm->config_string_hash =
180  hash_create_vec (0,
181  STRUCT_SIZE_OF (vnet_config_t, config_string_vector[0]),
182  sizeof (uword));
183 
184  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  {
198  if (!feature_node_names[i])
199  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)
207  cm->default_end_node_index = n->index;
209  }
210  else
211  cm->node_index_by_feature_index[i] = ~0;
212  }
213  }
214 }
215 
216 static void
218 {
219  ASSERT (c->reference_count > 0);
220  c->reference_count -= 1;
221  if (c->reference_count == 0)
222  {
224  vnet_config_free (cm, c);
225  pool_put (cm->config_pool, c);
226  }
227 }
228 
229 static int
230 feature_cmp (void *a1, void *a2)
231 {
232  vnet_config_feature_t *f1 = a1;
233  vnet_config_feature_t *f2 = a2;
234 
235  return (int) f1->feature_index - f2->feature_index;
236 }
237 
240 {
241  return heap_elt_at_index (cm->config_string_heap, ci);
242 }
243 
244 u32
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;
273  if (new_features[last].node_index == cm->default_end_node_index)
274  {
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 
283  new = find_config_with_features (vm, cm, new_features, end_node_index);
284  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  */
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 
298 u32
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;
307  u32 n_feature_config_u32s, end_node_index;
308  u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index);
309 
310  if (node_index == ~0) // feature node does not exist
311  return ~0;
312 
313  if (config_string_heap_index == ~0)
314  {
315  old = 0;
316  new_features = 0;
317  end_node_index = cm->default_end_node_index;
318  }
319  else
320  {
321  u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
322  old = pool_elt_at_index (cm->config_pool, p[-1]);
323  new_features = old->features;
324  end_node_index =
325  cm->end_node_indices_by_user_index[config_string_heap_index];
326  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 
334  if (n_feature_config_bytes)
335  {
336  n_feature_config_u32s =
337  round_pow2 (n_feature_config_bytes,
338  sizeof (f->feature_config[0])) /
339  sizeof (f->feature_config[0]);
340  vec_validate (f->feature_config, n_feature_config_u32s - 1);
341  clib_memcpy_fast (f->feature_config, feature_config,
342  n_feature_config_bytes);
343  }
344 
345  /* Sort (prioritize) features. */
346  if (vec_len (new_features) > 1)
347  vec_sort_with_function (new_features, feature_cmp);
348 
349  if (old)
350  remove_reference (cm, old);
351 
352  new = find_config_with_features (vm, cm, new_features, end_node_index);
353  new->reference_count += 1;
354 
355  /*
356  * User gets pointer to config string first element
357  * (which defines the pool index
358  * this config string comes from).
359  */
361  new->config_string_heap_index + 1);
362  cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
363  = new - cm->config_pool;
364  return new->config_string_heap_index + 1;
365 }
366 
367 u32
370  u32 config_string_heap_index,
371  u32 feature_index,
372  void *feature_config, u32 n_feature_config_bytes)
373 {
374  vnet_config_t *old, *new;
375  vnet_config_feature_t *new_features, *f;
376  u32 n_feature_config_u32s;
377 
378  {
379  u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
380 
381  old = pool_elt_at_index (cm->config_pool, p[-1]);
382  }
383 
384  n_feature_config_u32s =
385  round_pow2 (n_feature_config_bytes,
386  sizeof (f->feature_config[0])) /
387  sizeof (f->feature_config[0]);
388 
389  /* Find feature with same index and opaque data. */
390  vec_foreach (f, old->features)
391  {
392  if (f->feature_index == feature_index
393  && vec_len (f->feature_config) == n_feature_config_u32s
394  && (n_feature_config_u32s == 0
395  || !memcmp (f->feature_config, feature_config,
396  n_feature_config_bytes)))
397  break;
398  }
399 
400  /* Feature not found. */
401  if (f >= vec_end (old->features))
402  return ~0;
403 
404  new_features = duplicate_feature_vector (old->features);
405  f = new_features + (f - old->features);
407  vec_delete (new_features, 1, f - new_features);
408 
409  /* must remove old from config_pool now as it may be expanded and change
410  memory location if the following function find_config_with_features()
411  adds a new config because none of existing config's has matching features
412  and so can be reused */
413  remove_reference (cm, old);
414  new = find_config_with_features (vm, cm, new_features,
416  [config_string_heap_index]);
417  new->reference_count += 1;
418 
420  new->config_string_heap_index + 1);
421  cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
422  = new - cm->config_pool;
423  return new->config_string_heap_index + 1;
424 }
425 
426 /*
427  * fd.io coding-style-patch-verification: ON
428  *
429  * Local Variables:
430  * eval: (c-set-style "gnu")
431  * End:
432  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
vnet_config_feature_t * features
Definition: config.h:71
void vnet_config_init(vlib_main_t *vm, vnet_config_main_t *cm, char *start_node_names[], int n_start_node_names, char *feature_node_names[], int n_feature_node_names)
Definition: config.c:168
#define hash_unset(h, key)
Definition: hash.h:261
static vnet_config_t * find_config_with_features(vlib_main_t *vm, vnet_config_main_t *cm, vnet_config_feature_t *feature_vector, u32 end_node_index)
Definition: config.c:86
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:279
u32 vnet_config_del_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:368
static void remove_reference(vnet_config_main_t *cm, vnet_config_t *c)
Definition: config.c:217
u32 default_end_node_index
Definition: config.h:98
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
#define heap_elt_at_index(v, index)
Definition: heap.h:296
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u32 config_string_heap_handle
Definition: config.h:77
vlib_main_t * vm
Definition: in2out_ed.c:1582
u32 * config_string_vector
Definition: config.h:74
#define vec_bytes(v)
Number of data bytes in vector.
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1173
unsigned char u8
Definition: types.h:56
u32 * node_index_by_feature_index
Definition: config.h:102
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:668
u32 vnet_config_modify_end_node(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 end_node_index)
Definition: config.c:245
vl_api_cnat_endpoint_t new
Definition: cnat.api:97
static int feature_cmp(void *a1, void *a2)
Definition: config.c:230
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static void vnet_config_feature_free(vnet_config_feature_t *f)
Definition: config.h:63
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:281
unsigned int u32
Definition: types.h:88
#define vec_end(v)
End (last data address) of vector.
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
u32 * start_node_indices
Definition: config.h:98
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
#define always_inline
Definition: ipsec.h:28
static void free_feature_vector(vnet_config_feature_t *feature_vector)
Definition: config.c:54
u32 * config_pool_index_by_user_index
Definition: config.h:105
svmdb_client_t * c
static void vnet_config_free(vnet_config_main_t *cm, vnet_config_t *c)
Definition: config.h:113
static u32 * vnet_get_config_heap(vnet_config_main_t *cm, u32 ci)
Definition: config.c:239
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
u32 * config_string_heap
Definition: config.h:95
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:264
static vnet_config_feature_t * duplicate_feature_vector(vnet_config_feature_t *feature_vector)
Definition: config.c:43
#define ASSERT(truth)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:854
u32 vnet_config_add_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:299
u32 index
Definition: config.h:80
#define heap_alloc(v, size, handle)
Definition: heap.h:337
static u32 add_next(vlib_main_t *vm, vnet_config_main_t *cm, u32 last_node_index, u32 this_node_index)
Definition: config.c:63
#define vec_elt(v, i)
Get vector value at index i.
u32 * config_string_temp
Definition: config.h:109
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:668
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1055
#define hash_get_mem(h, key)
Definition: hash.h:269
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:72
vnet_config_t * config_pool
Definition: config.h:89
#define vec_foreach(var, vec)
Vector iterator.
u32 * end_node_indices_by_user_index
Definition: config.h:98
u32 reference_count
Definition: config.h:83
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
uword * config_string_hash
Definition: config.h:92
u32 config_string_heap_index
Definition: config.h:77
u32 * feature_config
Definition: config.h:59