ip: use IPv6 flowlabel in flow hash computation
[vpp.git] / src / vnet / config.c
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 {
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
54 free_feature_vector (vnet_config_feature_t * feature_vector)
55 {
56   vnet_config_feature_t *f;
57
58   vec_foreach (f, feature_vector) vnet_config_feature_free (f);
59   vec_free (feature_vector);
60 }
61
62 static u32
63 add_next (vlib_main_t * vm,
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 *
86 find_config_with_features (vlib_main_t * vm,
87                            vnet_config_main_t * cm,
88                            vnet_config_feature_t * feature_vector,
89                            u32 end_node_index)
90 {
91   u32 last_node_index = ~0;
92   vnet_config_feature_t *f;
93   u32 *config_string;
94   uword *p;
95   vnet_config_t *c;
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. */
143       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. */
148       d =
149         vec_elt_at_index (cm->config_string_heap,
150                           c->config_string_heap_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       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;
162     }
163
164   return c;
165 }
166
167 void
168 vnet_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)
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;
208               cm->node_index_by_feature_index[i] = n->index;
209             }
210           else
211             cm->node_index_by_feature_index[i] = ~0;
212         }
213     }
214 }
215
216 static void
217 remove_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
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
238 always_inline u32 *
239 vnet_get_config_heap (vnet_config_main_t * cm, u32 ci)
240 {
241   return heap_elt_at_index (cm->config_string_heap, ci);
242 }
243
244 void
245 vnet_config_del (vnet_config_main_t * cm, u32 config_id)
246 {
247   u32 *p = vnet_get_config_heap (cm, config_id);
248   vnet_config_t *old = pool_elt_at_index (cm->config_pool, p[-1]);
249   remove_reference (cm, old);
250 }
251
252 u32
253 vnet_config_modify_end_node (vlib_main_t * vm,
254                              vnet_config_main_t * cm,
255                              u32 config_string_heap_index, u32 end_node_index)
256 {
257   vnet_config_feature_t *new_features;
258   vnet_config_t *old, *new;
259
260   if (end_node_index == ~0)     // feature node does not exist
261     return ~0;
262
263   if (config_string_heap_index == ~0)
264     {
265       old = 0;
266       new_features = 0;
267     }
268   else
269     {
270       u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
271       old = pool_elt_at_index (cm->config_pool, p[-1]);
272       new_features = old->features;
273       if (new_features)
274         new_features = duplicate_feature_vector (new_features);
275     }
276
277   if (vec_len (new_features))
278     {
279       /* is the last feature the cuurent end node */
280       u32 last = vec_len (new_features) - 1;
281       if (new_features[last].node_index == cm->default_end_node_index)
282         {
283           vec_free (new_features->feature_config);
284           _vec_len (new_features) = last;
285         }
286     }
287
288   if (old)
289     remove_reference (cm, old);
290
291   new = find_config_with_features (vm, cm, new_features, end_node_index);
292   new->reference_count += 1;
293
294   /*
295    * User gets pointer to config string first element
296    * (which defines the pool index
297    * this config string comes from).
298    */
299   vec_validate (cm->config_pool_index_by_user_index,
300                 new->config_string_heap_index + 1);
301   cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
302     = new - cm->config_pool;
303   return new->config_string_heap_index + 1;
304 }
305
306 u32
307 vnet_config_add_feature (vlib_main_t * vm,
308                          vnet_config_main_t * cm,
309                          u32 config_string_heap_index,
310                          u32 feature_index,
311                          void *feature_config, u32 n_feature_config_bytes)
312 {
313   vnet_config_t *old, *new;
314   vnet_config_feature_t *new_features, *f;
315   u32 n_feature_config_u32s, end_node_index;
316   u32 node_index = vec_elt (cm->node_index_by_feature_index, feature_index);
317
318   if (node_index == ~0)         // feature node does not exist
319     return ~0;
320
321   if (config_string_heap_index == ~0)
322     {
323       old = 0;
324       new_features = 0;
325       end_node_index = cm->default_end_node_index;
326     }
327   else
328     {
329       u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
330       old = pool_elt_at_index (cm->config_pool, p[-1]);
331       new_features = old->features;
332       end_node_index =
333         cm->end_node_indices_by_user_index[config_string_heap_index];
334       if (new_features)
335         new_features = duplicate_feature_vector (new_features);
336     }
337
338   vec_add2 (new_features, f, 1);
339   f->feature_index = feature_index;
340   f->node_index = node_index;
341
342   if (n_feature_config_bytes)
343     {
344       n_feature_config_u32s =
345         round_pow2 (n_feature_config_bytes,
346                     sizeof (f->feature_config[0])) /
347         sizeof (f->feature_config[0]);
348       vec_validate (f->feature_config, n_feature_config_u32s - 1);
349       clib_memcpy_fast (f->feature_config, feature_config,
350                         n_feature_config_bytes);
351     }
352
353   /* Sort (prioritize) features. */
354   if (vec_len (new_features) > 1)
355     vec_sort_with_function (new_features, feature_cmp);
356
357   if (old)
358     remove_reference (cm, old);
359
360   new = find_config_with_features (vm, cm, new_features, end_node_index);
361   new->reference_count += 1;
362
363   /*
364    * User gets pointer to config string first element
365    * (which defines the pool index
366    * this config string comes from).
367    */
368   vec_validate (cm->config_pool_index_by_user_index,
369                 new->config_string_heap_index + 1);
370   cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
371     = new - cm->config_pool;
372   return new->config_string_heap_index + 1;
373 }
374
375 u32
376 vnet_config_del_feature (vlib_main_t * vm,
377                          vnet_config_main_t * cm,
378                          u32 config_string_heap_index,
379                          u32 feature_index,
380                          void *feature_config, u32 n_feature_config_bytes)
381 {
382   vnet_config_t *old, *new;
383   vnet_config_feature_t *new_features, *f;
384   u32 n_feature_config_u32s;
385
386   {
387     u32 *p = vnet_get_config_heap (cm, config_string_heap_index);
388
389     old = pool_elt_at_index (cm->config_pool, p[-1]);
390   }
391
392   n_feature_config_u32s =
393     round_pow2 (n_feature_config_bytes,
394                 sizeof (f->feature_config[0])) /
395     sizeof (f->feature_config[0]);
396
397   /* Find feature with same index and opaque data. */
398   vec_foreach (f, old->features)
399   {
400     if (f->feature_index == feature_index
401         && vec_len (f->feature_config) == n_feature_config_u32s
402         && (n_feature_config_u32s == 0
403             || !memcmp (f->feature_config, feature_config,
404                         n_feature_config_bytes)))
405       break;
406   }
407
408   /* Feature not found. */
409   if (f >= vec_end (old->features))
410     return ~0;
411
412   new_features = duplicate_feature_vector (old->features);
413   f = new_features + (f - old->features);
414   vnet_config_feature_free (f);
415   vec_delete (new_features, 1, f - new_features);
416
417   /* must remove old from config_pool now as it may be expanded and change
418      memory location if the following function find_config_with_features()
419      adds a new config because none of existing config's has matching features
420      and so can be reused */
421   remove_reference (cm, old);
422   new = find_config_with_features (vm, cm, new_features,
423                                    cm->end_node_indices_by_user_index
424                                    [config_string_heap_index]);
425   new->reference_count += 1;
426
427   vec_validate (cm->config_pool_index_by_user_index,
428                 new->config_string_heap_index + 1);
429   cm->config_pool_index_by_user_index[new->config_string_heap_index + 1]
430     = new - cm->config_pool;
431   return new->config_string_heap_index + 1;
432 }
433
434 /*
435  * fd.io coding-style-patch-verification: ON
436  *
437  * Local Variables:
438  * eval: (c-set-style "gnu")
439  * End:
440  */