FD.io VPP  v16.06
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)
50 
51  return result;
52 }
53 
54 static void
56 {
58 
59  vec_foreach (f, feature_vector)
61  vec_free (feature_vector);
62 }
63 
64 static u32
66  vnet_config_main_t * cm,
67  u32 last_node_index,
68  u32 this_node_index)
69 {
70  u32 i, ni = ~0;
71 
72  if (last_node_index != ~0)
73  return vlib_node_add_next (vm, last_node_index, this_node_index);
74 
75  for (i = 0; i < vec_len (cm->start_node_indices); i++)
76  {
77  u32 tmp;
78  tmp = vlib_node_add_next (vm, cm->start_node_indices[i], this_node_index);
79  if (ni == ~0)
80  ni = tmp;
81  /* Start nodes to first must agree on next indices. */
82  ASSERT (ni == tmp);
83  }
84 
85  return ni;
86 }
87 
88 static vnet_config_t *
90  vnet_config_main_t * cm,
91  vnet_config_feature_t * feature_vector)
92 {
93  u32 last_node_index = ~0;
95  u32 * config_string;
96  uword * p;
97  vnet_config_t * c;
98 
99  config_string = cm->config_string_temp;
100  cm->config_string_temp = 0;
101  if (config_string)
102  _vec_len (config_string) = 0;
103 
104  vec_foreach (f, feature_vector)
105  {
106  /* Connect node graph. */
107  f->next_index = add_next (vm, cm, last_node_index, f->node_index);
108  last_node_index = f->node_index;
109 
110  /* Store next index in config string. */
111  vec_add1 (config_string, f->next_index);
112 
113  /* Store feature config. */
114  vec_add (config_string, f->feature_config, vec_len (f->feature_config));
115  }
116 
117  /* Terminate config string with next for end node. */
118  if (last_node_index == ~0 || last_node_index != cm->end_node_index)
119  {
120  u32 next_index = add_next (vm, cm, last_node_index, cm->end_node_index);
121  vec_add1 (config_string, next_index);
122  }
123 
124  /* See if config string is unique. */
125  p = hash_get_mem (cm->config_string_hash, config_string);
126  if (p)
127  {
128  /* Not unique. Share existing config. */
129  cm->config_string_temp = config_string; /* we'll use it again later. */
130  free_feature_vector (feature_vector);
131  c = pool_elt_at_index (cm->config_pool, p[0]);
132  }
133  else
134  {
135  u32 * d;
136 
137  pool_get (cm->config_pool, c);
138  c->index = c - cm->config_pool;
139  c->features = feature_vector;
140  c->config_string_vector = config_string;
141 
142  /* Allocate copy of config string in heap.
143  VLIB buffers will maintain pointers to heap as they read out
144  configuration data. */
146  = heap_alloc (cm->config_string_heap, vec_len (config_string) + 1,
148 
149  /* First element in heap points back to pool index. */
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  }
157 
158  return c;
159 }
160 
162  vnet_config_main_t * cm,
163  char * start_node_names[],
164  int n_start_node_names,
165  char * feature_node_names[],
166  int n_feature_node_names)
167 {
168  vlib_node_t * n;
169  u32 i;
170 
171  memset (cm, 0, sizeof (cm[0]));
172 
173  cm->config_string_hash = hash_create_vec (0, STRUCT_SIZE_OF (vnet_config_t, config_string_vector[0]), sizeof (uword));
174 
175  ASSERT (n_start_node_names >= 1);
176  ASSERT (n_feature_node_names >= 1);
177 
178  vec_resize (cm->start_node_indices, n_start_node_names);
179  for (i = 0; i < n_start_node_names; i++)
180  {
181  n = vlib_get_node_by_name (vm, (u8 *) start_node_names[i]);
182  /* Given node name must exist. */
183  ASSERT (n != 0);
184  cm->start_node_indices[i] = n->index;
185  }
186 
187  vec_resize (cm->node_index_by_feature_index, n_feature_node_names);
188  for (i = 0; i < n_feature_node_names; i++)
189  {
190  if (! feature_node_names[i])
191  cm->node_index_by_feature_index[i] = ~0;
192  else
193  {
194  n = vlib_get_node_by_name (vm, (u8 *) feature_node_names[i]);
195  /* Given node may exist in plug-in library which is not present */
196  if (n)
197  {
198  if (i + 1 == n_feature_node_names)
199  cm->end_node_index = n->index;
201  }
202  else cm->node_index_by_feature_index[i] = ~0;
203  }
204  }
205 }
206 
207 static void
209 {
210  ASSERT (c->reference_count > 0);
211  c->reference_count -= 1;
212  if (c->reference_count == 0)
213  {
215  vnet_config_free (cm, c);
216  pool_put (cm->config_pool, c);
217  }
218 }
219 
220 static int
221 feature_cmp (void * a1, void * a2)
222 {
223  vnet_config_feature_t * f1 = a1;
224  vnet_config_feature_t * f2 = a2;
225 
226  return (int) f1->feature_index - f2->feature_index;
227 }
228 
231 { return heap_elt_at_index (cm->config_string_heap, ci); }
232 
234  vnet_config_main_t * cm,
235  u32 config_string_heap_index,
236  u32 feature_index,
237  void * feature_config,
238  u32 n_feature_config_bytes)
239 {
240  vnet_config_t * old, * new;
241  vnet_config_feature_t * new_features, * f;
242  u32 n_feature_config_u32s;
243  u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index);
244 
245  if (node_index == ~0) // feature node does not exist
246  return config_string_heap_index; // return original config index
247 
248  if (config_string_heap_index == ~0)
249  {
250  old = 0;
251  new_features = 0;
252  }
253  else
254  {
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;
258  if (new_features)
259  new_features = duplicate_feature_vector (new_features);
260  }
261 
262  vec_add2 (new_features, f, 1);
263  f->feature_index = feature_index;
264  f->node_index = node_index;
265 
266  n_feature_config_u32s = round_pow2 (n_feature_config_bytes, sizeof (f->feature_config[0])) / sizeof (f->feature_config[0]);
267  vec_add (f->feature_config, feature_config, n_feature_config_u32s);
268 
269  /* Sort (prioritize) features. */
270  if (vec_len (new_features) > 1)
271  vec_sort_with_function (new_features, feature_cmp);
272 
273  if (old)
274  remove_reference (cm, old);
275 
276  new = find_config_with_features (vm, cm, new_features);
277  new->reference_count += 1;
278 
279  /* User gets pointer to config string first element (which defines the pool index
280  this config string comes from). */
281  return new->config_string_heap_index + 1;
282 }
283 
285  vnet_config_main_t * cm,
286  u32 config_string_heap_index,
287  u32 feature_index,
288  void * feature_config,
289  u32 n_feature_config_bytes)
290 {
291  vnet_config_t * old, * new;
292  vnet_config_feature_t * new_features, * f;
293  u32 n_feature_config_u32s;
294 
295  {
296  u32 * p = vnet_get_config_heap (cm, config_string_heap_index);
297 
298  old = pool_elt_at_index (cm->config_pool, p[-1]);
299  }
300 
301  n_feature_config_u32s = round_pow2 (n_feature_config_bytes, sizeof (f->feature_config[0])) / sizeof (f->feature_config[0]);
302 
303  /* Find feature with same index and opaque data. */
304  vec_foreach (f, old->features)
305  {
306  if (f->feature_index == feature_index
307  && vec_len (f->feature_config) == n_feature_config_u32s
308  && (n_feature_config_u32s == 0
309  || ! memcmp (f->feature_config, feature_config, n_feature_config_bytes)))
310  break;
311  }
312 
313  /* Feature not found. */
314  if (f >= vec_end (old->features))
315  return config_string_heap_index; // return original config index
316 
317  new_features = duplicate_feature_vector (old->features);
318  f = new_features + (f - old->features);
320  vec_delete (new_features, 1, f - new_features);
321 
322  /* must remove old from config_pool now as it may be expanded and change
323  memory location if the following function find_config_with_features()
324  adds a new config because none of existing config's has matching features
325  and so can be reused */
326  remove_reference (cm, old);
327  new = find_config_with_features (vm, cm, new_features);
328  new->reference_count += 1;
329 
330  return new->config_string_heap_index + 1;
331 }
always_inline uword round_pow2(uword x, uword pow2)
Definition: clib.h:255
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
vnet_config_feature_t * features
Definition: config.h:67
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:161
#define hash_unset(h, key)
Definition: hash.h:243
u32 index
Definition: node.h:203
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:284
static void remove_reference(vnet_config_main_t *cm, vnet_config_t *c)
Definition: config.c:208
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define heap_elt_at_index(v, index)
Definition: heap.h:267
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
#define hash_set_mem(h, key, value)
Definition: hash.h:257
u32 config_string_heap_handle
Definition: config.h:73
u32 * config_string_vector
Definition: config.h:70
#define vec_bytes(v)
Number of data bytes in vector.
#define pool_get(P, E)
Definition: pool.h:186
u32 * node_index_by_feature_index
Definition: config.h:96
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:557
#define always_inline
Definition: clib.h:84
static int feature_cmp(void *a1, void *a2)
Definition: config.c:221
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
always_inline void vnet_config_free(vnet_config_main_t *cm, vnet_config_t *c)
Definition: config.h:104
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:199
#define vec_end(v)
End (last data address) of vector.
#define pool_elt_at_index(p, i)
Definition: pool.h:346
u32 * start_node_indices
Definition: config.h:93
#define pool_put(P, E)
Definition: pool.h:200
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:332
always_inline u32 * vnet_get_config_heap(vnet_config_main_t *cm, u32 ci)
Definition: config.c:230
static void free_feature_vector(vnet_config_feature_t *feature_vector)
Definition: config.c:55
static vnet_config_t * find_config_with_features(vlib_main_t *vm, vnet_config_main_t *cm, vnet_config_feature_t *feature_vector)
Definition: config.c:89
u32 * config_string_heap
Definition: config.h:90
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define clib_memcpy(a, b, c)
Definition: string.h:63
static vnet_config_feature_t * duplicate_feature_vector(vnet_config_feature_t *feature_vector)
Definition: config.c:43
always_inline void vnet_config_feature_free(vnet_config_feature_t *f)
Definition: config.h:62
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:743
u32 end_node_index
Definition: config.h:93
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:233
u32 index
Definition: config.h:76
#define heap_alloc(v, size, handle)
Definition: heap.h:309
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
static u32 add_next(vlib_main_t *vm, vnet_config_main_t *cm, u32 last_node_index, u32 this_node_index)
Definition: config.c:65
u64 uword
Definition: types.h:112
#define vec_elt(v, i)
Get vector value at index i.
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:44
u32 * config_string_temp
Definition: config.h:100
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:601
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:898
#define hash_get_mem(h, key)
Definition: hash.h:251
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:64
vnet_config_t * config_pool
Definition: config.h:84
#define vec_foreach(var, vec)
Vector iterator.
u32 reference_count
Definition: config.h:79
uword * config_string_hash
Definition: config.h:87
u32 config_string_heap_index
Definition: config.h:73
u32 * feature_config
Definition: config.h:58