Initial commit of vpp code.
[vpp.git] / vlib / vlib / node.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  * node.c: VLIB processing nodes
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 <vlib/vlib.h>
41 #include <vlib/threads.h>
42
43 /* Query node given name. */
44 vlib_node_t * vlib_get_node_by_name (vlib_main_t * vm, u8 * name)
45 {
46   vlib_node_main_t * nm = &vm->node_main;
47   uword * p;
48   u8 * key = name;
49   if (! clib_mem_is_heap_object (key))
50     key = format (0, "%s", key);
51   p = hash_get (nm->node_by_name, key);
52   if (key != name)
53     vec_free (key);
54   return p ? vec_elt (nm->nodes, p[0]) : 0;
55 }
56
57 static void node_set_elog_name (vlib_main_t * vm, uword node_index)
58 {
59   vlib_node_t * n = vlib_get_node (vm, node_index);
60   elog_event_type_t * t;
61
62   t = vec_elt_at_index (vm->node_call_elog_event_types, node_index);
63   vec_free (t->format);
64   t->format = (char *) format (0, "%v (%%d)", n->name);
65
66   t = vec_elt_at_index (vm->node_return_elog_event_types, node_index);
67   vec_free (t->format);
68   t->format = (char *) format (0, "%v () = %%d", n->name);
69
70   n->name_elog_string = elog_string (&vm->elog_main, "%v", n->name);
71 }
72
73 void vlib_node_rename (vlib_main_t * vm, u32 node_index, char * fmt, ...)
74 {
75   va_list va;
76   vlib_node_main_t * nm = &vm->node_main;
77   vlib_node_t * n = vlib_get_node (vm, node_index);
78
79   va_start (va, fmt);
80   hash_unset (nm->node_by_name, n->name);
81   vec_free (n->name);
82   n->name = va_format (0, fmt, &va);
83   va_end (va);
84   hash_set (nm->node_by_name, n->name, n->index);
85
86   node_set_elog_name (vm, node_index);
87 }
88
89 static void
90 vlib_node_runtime_update (vlib_main_t * vm,
91                           u32 node_index,
92                           u32 next_index)
93 {
94   vlib_node_main_t * nm = &vm->node_main;
95   vlib_node_runtime_t * r, * s;
96   vlib_node_t * node, * next_node;
97   vlib_next_frame_t * nf;
98   vlib_pending_frame_t * pf;
99   i32 i, j, n_insert;
100
101   ASSERT(os_get_cpu_number() == 0);
102
103   vlib_worker_thread_barrier_sync(vm);
104
105   node = vec_elt (nm->nodes, node_index);
106   r = vlib_node_get_runtime (vm, node_index);
107
108   n_insert = vec_len (node->next_nodes) - r->n_next_nodes;
109   if (n_insert > 0)
110     {
111       i = r->next_frame_index + r->n_next_nodes;
112       vec_insert (nm->next_frames, n_insert, i);
113
114       /* Initialize newly inserted next frames. */
115       for (j = 0; j < n_insert; j++)
116         vlib_next_frame_init (nm->next_frames + i + j);
117
118       /* Relocate other next frames at higher indices. */
119       for (j = 0; j < vec_len (nm->nodes); j++)
120         {
121           s = vlib_node_get_runtime (vm, j);
122           if (j != node_index
123               && s->next_frame_index >= i)
124             s->next_frame_index += n_insert;
125         }
126
127       /* Pending frames may need to be relocated also. */
128       vec_foreach (pf, nm->pending_frames)
129         {
130           if (pf->next_frame_index != VLIB_PENDING_FRAME_NO_NEXT_FRAME
131               && pf->next_frame_index >= i)
132             pf->next_frame_index += n_insert;
133         }
134       pool_foreach (pf, nm->suspended_process_frames, ({
135           if (pf->next_frame_index != ~0 && pf->next_frame_index >= i)
136             pf->next_frame_index += n_insert;
137       }));
138
139       r->n_next_nodes = vec_len (node->next_nodes);
140     }
141
142   /* Set frame's node runtime index. */
143   next_node = vlib_get_node (vm, node->next_nodes[next_index]);
144   nf = nm->next_frames + r->next_frame_index + next_index;
145   nf->node_runtime_index = next_node->runtime_index;
146
147   vlib_worker_thread_node_runtime_update();
148
149   vlib_worker_thread_barrier_release(vm);
150 }
151
152 /* Add next node to given node in given slot. */
153 uword
154 vlib_node_add_next_with_slot (vlib_main_t * vm,
155                               uword node_index,
156                               uword next_node_index,
157                               uword slot)
158 {
159   vlib_node_main_t * nm = &vm->node_main;
160   vlib_node_t * node, * next;
161   uword * p;
162
163   node = vec_elt (nm->nodes, node_index);
164   next = vec_elt (nm->nodes, next_node_index);
165
166   /* Fill in static next nodes if runtime has yet to be initialized. */
167   if (slot == ~0 && ! (nm->flags & VLIB_NODE_MAIN_RUNTIME_STARTED))
168     {
169       uword i;
170       for (i = 0; i < vec_len (node->next_node_names); i++)
171         {
172           char * a = node->next_node_names[i];
173           if (a)
174             vlib_node_add_named_next_with_slot (vm, node->index, a, i);
175         }
176     }
177
178   if ((p = hash_get (node->next_slot_by_node, next_node_index)))
179     {
180       /* Next already exists: slot must match. */
181       if (slot != ~0)
182         ASSERT (slot == p[0]);
183       return p[0];
184     }
185
186   if (slot == ~0)
187     slot = vec_len (node->next_nodes);
188
189   vec_validate_init_empty (node->next_nodes, slot, ~0);
190   vec_validate (node->n_vectors_by_next_node, slot);
191
192   node->next_nodes[slot] = next_node_index;
193   hash_set (node->next_slot_by_node, next_node_index, slot);
194
195   vlib_node_runtime_update (vm, node_index, slot);
196
197   next->prev_node_bitmap = clib_bitmap_ori (next->prev_node_bitmap,
198                                             node_index);
199
200   /* Siblings all get same node structure. */
201   {
202     uword sib_node_index, sib_slot;
203     vlib_node_t * sib_node;
204     clib_bitmap_foreach (sib_node_index, node->sibling_bitmap, ({
205       sib_node = vec_elt (nm->nodes, sib_node_index);
206       if (sib_node != node)
207         {
208           sib_slot = vlib_node_add_next_with_slot (vm, sib_node_index, next_node_index, slot);
209           ASSERT (sib_slot == slot);
210         }
211     }));
212   }
213
214   return slot;
215 }
216
217 /* Add named next node to given node in given slot. */
218 uword
219 vlib_node_add_named_next_with_slot (vlib_main_t * vm,
220                                     uword node,
221                                     char * name,
222                                     uword slot)
223 {
224   vlib_node_main_t * nm;
225   vlib_node_t * n, * n_next;
226
227   nm = &vm->node_main;
228   n = vlib_get_node (vm, node);
229
230   n_next = vlib_get_node_by_name (vm, (u8 *) name);
231   if (! n_next)
232     {
233       if (nm->flags & VLIB_NODE_MAIN_RUNTIME_STARTED)
234         return ~0;
235
236       if (slot == ~0)
237         slot = clib_max (vec_len (n->next_node_names),
238                          vec_len (n->next_nodes));
239       vec_validate (n->next_node_names, slot);
240       n->next_node_names[slot] = name;
241       return slot;
242     }
243
244   return vlib_node_add_next_with_slot (vm, node, n_next->index, slot);
245 }
246
247 static void node_elog_init (vlib_main_t * vm, uword ni)
248 {
249   elog_event_type_t t;
250
251   memset (&t, 0, sizeof (t));
252
253   /* 2 event types for this node: one when node function is called.
254      One when it returns. */
255   vec_validate (vm->node_call_elog_event_types, ni);
256   vm->node_call_elog_event_types[ni] = t;
257
258   vec_validate (vm->node_return_elog_event_types, ni);
259   vm->node_return_elog_event_types[ni] = t;
260
261   node_set_elog_name (vm, ni);
262 }
263
264 #ifdef CLIB_UNIX
265 #define STACK_ALIGN 4096
266 #else
267 #define STACK_ALIGN CLIB_CACHE_LINE_BYTES
268 #endif
269
270 static void register_node (vlib_main_t * vm,
271                            vlib_node_registration_t * r)
272 {
273   vlib_node_main_t * nm = &vm->node_main;
274   vlib_node_t * n;
275   int i;
276
277   if (CLIB_DEBUG > 0)
278     {
279       /* Default (0) type should match INTERNAL. */
280       vlib_node_t zero = {0};
281       ASSERT (VLIB_NODE_TYPE_INTERNAL == zero.type);
282     }
283
284   ASSERT (r->function != 0);
285
286   n = clib_mem_alloc_no_fail (sizeof (n[0]));
287   memset (n, 0, sizeof (n[0]));
288   n->index = vec_len (nm->nodes);
289
290   vec_add1 (nm->nodes, n);
291         
292   /* Name is always a vector so it can be formatted with %v. */
293   if (clib_mem_is_heap_object (vec_header (r->name, 0)))
294     n->name = vec_dup ((u8 *) r->name);
295   else
296     n->name = format (0, "%s", r->name);
297
298   if (! nm->node_by_name)
299     nm->node_by_name = hash_create_vec (/* size */ 32,
300                                         sizeof (n->name[0]),
301                                         sizeof (uword));
302
303   /* Node names must be unique. */
304   {
305     vlib_node_t * o = vlib_get_node_by_name (vm, n->name);
306     if (o)
307       clib_error ("more than one node named `%v'", n->name);
308   }
309
310   hash_set (nm->node_by_name, n->name, n->index);
311
312   r->index = n->index;          /* save index in registration */
313   n->function = r->function;
314
315   /* Node index of next sibling will be filled in by vlib_node_main_init. */
316   n->sibling_of = r->sibling_of;
317
318   if (r->type == VLIB_NODE_TYPE_INTERNAL)
319     ASSERT (r->vector_size > 0);
320
321 #define _(f) n->f = r->f
322
323   _ (type);
324   _ (flags);
325   _ (state);
326   _ (scalar_size);
327   _ (vector_size);
328   _ (format_buffer);
329   _ (unformat_buffer);
330   _ (format_trace);
331   _ (validate_frame);
332
333   /* Register error counters. */
334   vlib_register_errors (vm, n->index, r->n_errors, r->error_strings);
335   node_elog_init (vm, n->index);
336
337   _ (runtime_data_bytes);
338   if (r->runtime_data_bytes > 0)
339     {
340       vec_resize (n->runtime_data, r->runtime_data_bytes);
341       if (r->runtime_data)
342         memcpy (n->runtime_data, r->runtime_data, r->runtime_data_bytes);
343     }
344
345   vec_resize (n->next_node_names, r->n_next_nodes);
346   for (i = 0; i < r->n_next_nodes; i++)
347     n->next_node_names[i] = r->next_nodes[i];
348
349   vec_validate_init_empty (n->next_nodes, r->n_next_nodes - 1, ~0);
350   vec_validate (n->n_vectors_by_next_node, r->n_next_nodes - 1);
351
352   n->owner_node_index = n->owner_next_index = ~0;
353
354   /* Initialize node runtime. */
355   {
356     vlib_node_runtime_t * rt;
357     u32 i;
358
359     if (n->type == VLIB_NODE_TYPE_PROCESS)
360       {
361         vlib_process_t * p;
362         uword log2_n_stack_bytes;
363
364         log2_n_stack_bytes = clib_max (r->process_log2_n_stack_bytes, 15);
365
366         p = clib_mem_alloc_aligned_no_fail 
367             (sizeof (p[0]) + (1 << log2_n_stack_bytes),
368              STACK_ALIGN);
369
370         memset (p, 0, sizeof (p[0]));
371         p->log2_n_stack_bytes = log2_n_stack_bytes;
372
373         /* Process node's runtime index is really index into process
374            pointer vector. */
375         n->runtime_index = vec_len (nm->processes);
376
377         vec_add1 (nm->processes, p);
378
379         /* Paint first stack word with magic number so we can at least
380            detect process stack overruns. */
381         p->stack[0] = VLIB_PROCESS_STACK_MAGIC;
382
383         /* Node runtime is stored inside of process. */
384         rt = &p->node_runtime;
385
386 #ifdef CLIB_UNIX
387         /* 
388          * Disallow writes to the bottom page of the stack, to
389          * catch stack overflows.
390          */
391         if (mprotect (p->stack, 4096, PROT_READ) < 0)
392             clib_unix_warning ("process stack");
393 #endif
394
395       }
396     else
397       {
398         vec_add2_aligned (nm->nodes_by_type[n->type], rt, 1,
399                           /* align */ CLIB_CACHE_LINE_BYTES);
400         n->runtime_index = rt - nm->nodes_by_type[n->type];
401       }
402
403     if (n->type == VLIB_NODE_TYPE_INPUT)
404       nm->input_node_counts_by_state[n->state] += 1;
405
406     rt->function = n->function;
407     rt->flags = n->flags;
408     rt->state = n->state;
409     rt->node_index = n->index;
410
411     rt->n_next_nodes = r->n_next_nodes;
412     rt->next_frame_index = vec_len (nm->next_frames);
413
414     vec_resize (nm->next_frames, rt->n_next_nodes);
415     for (i = 0; i < rt->n_next_nodes; i++)
416       vlib_next_frame_init (nm->next_frames + rt->next_frame_index + i);
417
418     vec_resize (rt->errors, r->n_errors);
419     for (i = 0; i < vec_len (rt->errors); i++)
420       rt->errors[i] = vlib_error_set (n->index, i);
421
422     ASSERT (vec_len (n->runtime_data) <= sizeof (rt->runtime_data));
423     if (vec_len (n->runtime_data) > 0)
424       memcpy (rt->runtime_data, n->runtime_data, vec_len (n->runtime_data));
425
426     vec_free (n->runtime_data);
427   }
428 }
429
430 /* Register new packet processing node. */
431 u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r)
432 {
433   register_node (vm, r);
434   return r->index;
435 }
436
437 void vlib_register_all_static_nodes (vlib_main_t * vm)
438 {
439   vlib_node_registration_t * r;
440   
441   r = vm->node_main.node_registrations;
442   while (r) {
443     register_node (vm, r);
444     r = r->next_registration;
445   }
446 }
447
448 clib_error_t *
449 vlib_node_main_init (vlib_main_t * vm)
450 {
451   vlib_node_main_t * nm = &vm->node_main;
452   clib_error_t * error = 0;
453   vlib_node_t * n;
454   uword ni;
455
456   nm->flags |= VLIB_NODE_MAIN_RUNTIME_STARTED;
457
458   /* Resolve next names into next indices. */
459   for (ni = 0; ni < vec_len (nm->nodes); ni++)
460     {
461       uword i;
462
463       n = vec_elt (nm->nodes, ni);
464
465       for (i = 0; i < vec_len (n->next_node_names); i++)
466         {
467           char * a = n->next_node_names[i];
468
469           if (! a)
470             continue;
471
472           if (~0 == vlib_node_add_named_next_with_slot (vm, n->index, a, i))
473             {
474               error = clib_error_create
475                 ("node `%v' refers to unknown node `%s'", n->name, a);
476               goto done;
477             }
478         }
479
480       vec_free (n->next_node_names);
481     }
482
483   /* Set previous node pointers. */
484   for (ni = 0; ni < vec_len (nm->nodes); ni++)
485     {
486       vlib_node_t * n_next;
487       uword i;
488
489       n = vec_elt (nm->nodes, ni);
490
491       for (i = 0; i < vec_len (n->next_nodes); i++)
492         {
493           if (n->next_nodes[i] >= vec_len (nm->nodes))
494             continue;
495
496           n_next = vec_elt (nm->nodes, n->next_nodes[i]);
497           n_next->prev_node_bitmap =
498             clib_bitmap_ori (n_next->prev_node_bitmap, n->index);
499         }
500     }
501
502   {
503     vlib_next_frame_t * nf;
504     vlib_node_runtime_t * r;
505     vlib_node_t * next;
506     uword i;
507
508     vec_foreach (r, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
509       {
510         if (r->n_next_nodes == 0)
511           continue;
512
513         n = vlib_get_node (vm, r->node_index);
514         nf = vec_elt_at_index (nm->next_frames, r->next_frame_index);
515
516         for (i = 0; i < vec_len (n->next_nodes); i++)
517           {
518             next = vlib_get_node (vm, n->next_nodes[i]);
519
520             /* Validate node runtime indices are correctly initialized. */
521             ASSERT (nf[i].node_runtime_index == next->runtime_index);
522
523             nf[i].flags = 0;
524             if (next->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH)
525               nf[i].flags |= VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
526           }
527       }
528   }
529
530   /* Generate node sibling relationships. */
531   {
532     vlib_node_t * n, * sib;
533     uword si;
534
535     for (ni = 0; ni < vec_len (nm->nodes); ni++)
536       {
537         n = vec_elt (nm->nodes, ni);
538
539         if (! n->sibling_of)
540           continue;
541
542         sib = vlib_get_node_by_name (vm, (u8 *) n->sibling_of);
543         if (! sib)
544           clib_error ("sibling `%s' not found for node `%v'", n->sibling_of, n->name);
545
546         clib_bitmap_foreach (si, sib->sibling_bitmap, ({
547           vlib_node_t * m = vec_elt (nm->nodes, si);
548
549           /* Connect all of sibling's siblings to us. */
550           m->sibling_bitmap = clib_bitmap_ori (m->sibling_bitmap, n->index);
551
552           /* Connect us to all of sibling's siblings. */
553           n->sibling_bitmap = clib_bitmap_ori (n->sibling_bitmap, si);
554         }));
555
556         /* Connect sibling to us. */
557         sib->sibling_bitmap = clib_bitmap_ori (sib->sibling_bitmap, n->index);
558
559         /* Connect us to sibling. */
560         n->sibling_bitmap = clib_bitmap_ori (n->sibling_bitmap, sib->index);
561       }
562   }
563
564  done:
565   return error;
566 }