66f079c0547439ef20048752e4955efe160d9b78
[vpp.git] / src / vlib / node_funcs.h
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_funcs.h: processing nodes global functions/inlines
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 /** \file
41     vlib node functions
42 */
43
44
45 #ifndef included_vlib_node_funcs_h
46 #define included_vlib_node_funcs_h
47
48 #include <vppinfra/clib.h>
49 #include <vppinfra/fifo.h>
50 #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
51 #include <vppinfra/interrupt.h>
52
53 #ifdef CLIB_SANITIZE_ADDR
54 #include <sanitizer/asan_interface.h>
55 #endif
56
57 static_always_inline void
58 vlib_process_start_switch_stack (vlib_main_t * vm, vlib_process_t * p)
59 {
60 #ifdef CLIB_SANITIZE_ADDR
61   void *stack = p ? (void *) p->stack : vlib_thread_stacks[vm->thread_index];
62   u32 stack_bytes =
63     p ? (1ULL < p->log2_n_stack_bytes) : VLIB_THREAD_STACK_SIZE;
64   __sanitizer_start_switch_fiber (&vm->asan_stack_save, stack, stack_bytes);
65 #endif
66 }
67
68 static_always_inline void
69 vlib_process_finish_switch_stack (vlib_main_t * vm)
70 {
71 #ifdef CLIB_SANITIZE_ADDR
72   const void *bottom_old;
73   size_t size_old;
74
75   __sanitizer_finish_switch_fiber (&vm->asan_stack_save, &bottom_old,
76                                    &size_old);
77 #endif
78 }
79
80 /** \brief Get vlib node by index.
81  @warning This function will ASSERT if @c i is out of range.
82  @param vm vlib_main_t pointer, varies by thread
83  @param i node index.
84  @return pointer to the requested vlib_node_t.
85 */
86
87 always_inline vlib_node_t *
88 vlib_get_node (vlib_main_t * vm, u32 i)
89 {
90   return vec_elt (vm->node_main.nodes, i);
91 }
92
93 /** \brief Get vlib node by graph arc (next) index.
94  @param vm vlib_main_t pointer, varies by thread
95  @param node_index index of original node
96  @param next_index graph arc index
97  @return pointer to the vlib_node_t at the end of the indicated arc
98 */
99
100 always_inline vlib_node_t *
101 vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index)
102 {
103   vlib_node_main_t *nm = &vm->node_main;
104   vlib_node_t *n;
105
106   n = vec_elt (nm->nodes, node_index);
107   ASSERT (next_index < vec_len (n->next_nodes));
108   return vlib_get_node (vm, n->next_nodes[next_index]);
109 }
110
111 /** \brief Get node runtime by node index.
112  @param vm vlib_main_t pointer, varies by thread
113  @param node_index index of node
114  @return pointer to the indicated vlib_node_runtime_t
115 */
116
117 always_inline vlib_node_runtime_t *
118 vlib_node_get_runtime (vlib_main_t * vm, u32 node_index)
119 {
120   vlib_node_main_t *nm = &vm->node_main;
121   vlib_node_t *n = vec_elt (nm->nodes, node_index);
122   vlib_process_t *p;
123   if (n->type != VLIB_NODE_TYPE_PROCESS)
124     return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
125   else
126     {
127       p = vec_elt (nm->processes, n->runtime_index);
128       return &p->node_runtime;
129     }
130 }
131
132 /** \brief Get node runtime private data by node index.
133  @param vm vlib_main_t pointer, varies by thread
134  @param node_index index of the node
135  @return pointer to the indicated vlib_node_runtime_t private data
136 */
137
138 always_inline void *
139 vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index)
140 {
141   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
142   return r->runtime_data;
143 }
144
145 /** \brief Set node runtime private data.
146  @param vm vlib_main_t pointer, varies by thread
147  @param node_index index of the node
148  @param runtime_data arbitrary runtime private data
149  @param n_runtime_data_bytes size of runtime private data
150 */
151
152 always_inline void
153 vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index,
154                             void *runtime_data, u32 n_runtime_data_bytes)
155 {
156   vlib_node_t *n = vlib_get_node (vm, node_index);
157   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
158
159   n->runtime_data_bytes = n_runtime_data_bytes;
160   vec_free (n->runtime_data);
161   vec_add (n->runtime_data, runtime_data, n_runtime_data_bytes);
162
163   ASSERT (vec_len (n->runtime_data) <= sizeof (vlib_node_runtime_t) -
164           STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data));
165
166   if (vec_len (n->runtime_data) > 0)
167     clib_memcpy_fast (r->runtime_data, n->runtime_data,
168                       vec_len (n->runtime_data));
169 }
170
171 /** \brief Set node dispatch state.
172  @param vm vlib_main_t pointer, varies by thread
173  @param node_index index of the node
174  @param new_state new state for node, see vlib_node_state_t
175 */
176 always_inline void
177 vlib_node_set_state (vlib_main_t * vm, u32 node_index,
178                      vlib_node_state_t new_state)
179 {
180   vlib_node_main_t *nm = &vm->node_main;
181   vlib_node_t *n;
182   vlib_node_runtime_t *r;
183
184   n = vec_elt (nm->nodes, node_index);
185   if (n->type == VLIB_NODE_TYPE_PROCESS)
186     {
187       vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
188       r = &p->node_runtime;
189
190       /* When disabling make sure flags are cleared. */
191       p->flags &= ~(VLIB_PROCESS_RESUME_PENDING
192                     | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
193                     | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT);
194     }
195   else
196     r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
197
198   ASSERT (new_state < VLIB_N_NODE_STATE);
199
200   if (n->type == VLIB_NODE_TYPE_INPUT)
201     {
202       ASSERT (nm->input_node_counts_by_state[n->state] > 0);
203       nm->input_node_counts_by_state[n->state] -= 1;
204       nm->input_node_counts_by_state[new_state] += 1;
205     }
206
207   if (PREDICT_FALSE (r->state == VLIB_NODE_STATE_DISABLED))
208     vlib_node_runtime_perf_counter (vm, r, 0, 0, 0,
209                                     VLIB_NODE_RUNTIME_PERF_RESET);
210
211   n->state = new_state;
212   r->state = new_state;
213 }
214
215 /** \brief Get node dispatch state.
216  @param vm vlib_main_t pointer, varies by thread
217  @param node_index index of the node
218  @return state for node, see vlib_node_state_t
219 */
220 always_inline vlib_node_state_t
221 vlib_node_get_state (vlib_main_t * vm, u32 node_index)
222 {
223   vlib_node_main_t *nm = &vm->node_main;
224   vlib_node_t *n;
225   n = vec_elt (nm->nodes, node_index);
226   return n->state;
227 }
228
229 always_inline void
230 vlib_node_set_flag (vlib_main_t *vm, u32 node_index, u16 flag, u8 enable)
231 {
232   vlib_node_runtime_t *r;
233   vlib_node_t *n;
234
235   n = vlib_get_node (vm, node_index);
236   r = vlib_node_get_runtime (vm, node_index);
237
238   if (enable)
239     {
240       n->flags |= flag;
241       r->flags |= flag;
242     }
243   else
244     {
245       n->flags &= ~flag;
246       r->flags &= ~flag;
247     }
248 }
249
250 always_inline void
251 vlib_node_set_interrupt_pending (vlib_main_t *vm, u32 node_index)
252 {
253   vlib_node_main_t *nm = &vm->node_main;
254   vlib_node_t *n = vec_elt (nm->nodes, node_index);
255   void *interrupts;
256
257   if (n->type == VLIB_NODE_TYPE_INPUT)
258     interrupts = nm->input_node_interrupts;
259   else if (n->type == VLIB_NODE_TYPE_PRE_INPUT)
260     interrupts = nm->pre_input_node_interrupts;
261   else
262     ASSERT (0);
263
264   if (vm != vlib_get_main ())
265     clib_interrupt_set_atomic (interrupts, n->runtime_index);
266   else
267     clib_interrupt_set (interrupts, n->runtime_index);
268
269   __atomic_store_n (&nm->pending_interrupts, 1, __ATOMIC_RELEASE);
270 }
271
272 always_inline vlib_process_t *
273 vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node)
274 {
275   vlib_node_main_t *nm = &vm->node_main;
276   ASSERT (node->type == VLIB_NODE_TYPE_PROCESS);
277   return vec_elt (nm->processes, node->runtime_index);
278 }
279
280 always_inline vlib_frame_t *
281 vlib_get_frame (vlib_main_t * vm, vlib_frame_t * f)
282 {
283   ASSERT (f != NULL);
284   ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
285   return f;
286 }
287
288 always_inline void
289 vlib_frame_no_append (vlib_frame_t * f)
290 {
291   f->frame_flags |= VLIB_FRAME_NO_APPEND;
292 }
293
294 /** \brief Get pointer to frame vector data.
295  @param f vlib_frame_t pointer
296  @return pointer to first vector element in frame
297 */
298 always_inline void *
299 vlib_frame_vector_args (vlib_frame_t * f)
300 {
301   ASSERT (f->vector_offset);
302   return (void *) f + f->vector_offset;
303 }
304
305 /** \brief Get pointer to frame vector aux data.
306  @param f vlib_frame_t pointer
307  @return pointer to first vector aux data element in frame
308 */
309 always_inline void *
310 vlib_frame_aux_args (vlib_frame_t *f)
311 {
312   ASSERT (f->aux_offset);
313   return (void *) f + f->aux_offset;
314 }
315
316 /** \brief Get pointer to frame scalar data.
317
318  @param f vlib_frame_t pointer
319
320  @return arbitrary node scalar data
321
322  @sa vlib_frame_vector_args
323 */
324 always_inline void *
325 vlib_frame_scalar_args (vlib_frame_t * f)
326 {
327   ASSERT (f->scalar_offset);
328   return (void *) f + f->scalar_offset;
329 }
330
331 always_inline vlib_next_frame_t *
332 vlib_node_runtime_get_next_frame (vlib_main_t * vm,
333                                   vlib_node_runtime_t * n, u32 next_index)
334 {
335   vlib_node_main_t *nm = &vm->node_main;
336   vlib_next_frame_t *nf;
337
338   ASSERT (next_index < n->n_next_nodes);
339   nf = vec_elt_at_index (nm->next_frames, n->next_frame_index + next_index);
340
341   if (CLIB_DEBUG > 0)
342     {
343       vlib_node_t *node, *next;
344       node = vec_elt (nm->nodes, n->node_index);
345       next = vec_elt (nm->nodes, node->next_nodes[next_index]);
346       ASSERT (nf->node_runtime_index == next->runtime_index);
347     }
348
349   return nf;
350 }
351
352 /** \brief Get pointer to frame by (@c node_index, @c next_index).
353
354  @warning This is not a function that you should call directly.
355  See @ref vlib_get_next_frame instead.
356
357  @param vm vlib_main_t pointer, varies by thread
358  @param node_index index of the node
359  @param next_index graph arc index
360
361  @return pointer to the requested vlib_next_frame_t
362
363  @sa vlib_get_next_frame
364 */
365
366 always_inline vlib_next_frame_t *
367 vlib_node_get_next_frame (vlib_main_t * vm, u32 node_index, u32 next_index)
368 {
369   vlib_node_main_t *nm = &vm->node_main;
370   vlib_node_t *n;
371   vlib_node_runtime_t *r;
372
373   n = vec_elt (nm->nodes, node_index);
374   r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
375   return vlib_node_runtime_get_next_frame (vm, r, next_index);
376 }
377
378 vlib_frame_t *vlib_get_next_frame_internal (vlib_main_t * vm,
379                                             vlib_node_runtime_t * node,
380                                             u32 next_index,
381                                             u32 alloc_new_frame);
382
383 #define vlib_get_next_frame_macro(vm, node, next_index, vectors,              \
384                                   n_vectors_left, alloc_new_frame)            \
385   do                                                                          \
386     {                                                                         \
387       vlib_frame_t *_f = vlib_get_next_frame_internal (                       \
388         (vm), (node), (next_index), (alloc_new_frame));                       \
389       u32 _n = _f->n_vectors;                                                 \
390       (vectors) = vlib_frame_vector_args (_f) + _n * sizeof ((vectors)[0]);   \
391       (n_vectors_left) = VLIB_FRAME_SIZE - _n;                                \
392     }                                                                         \
393   while (0)
394
395 #define vlib_get_next_frame_macro_with_aux(vm, node, next_index, vectors,     \
396                                            n_vectors_left, alloc_new_frame,   \
397                                            aux_data, maybe_no_aux)            \
398   do                                                                          \
399     {                                                                         \
400       vlib_frame_t *_f = vlib_get_next_frame_internal (                       \
401         (vm), (node), (next_index), (alloc_new_frame));                       \
402       u32 _n = _f->n_vectors;                                                 \
403       (vectors) = vlib_frame_vector_args (_f) + _n * sizeof ((vectors)[0]);   \
404       if ((maybe_no_aux) && (_f)->aux_offset == 0)                            \
405         (aux_data) = NULL;                                                    \
406       else                                                                    \
407         (aux_data) = vlib_frame_aux_args (_f) + _n * sizeof ((aux_data)[0]);  \
408       (n_vectors_left) = VLIB_FRAME_SIZE - _n;                                \
409     }                                                                         \
410   while (0)
411
412 /** \brief Get pointer to next frame vector data by
413     (@c vlib_node_runtime_t, @c next_index).
414  Standard single/dual loop boilerplate element.
415  @attention This is a MACRO, with SIDE EFFECTS.
416
417  @param vm vlib_main_t pointer, varies by thread
418  @param node current node vlib_node_runtime_t pointer
419  @param next_index requested graph arc index
420
421  @return @c vectors -- pointer to next available vector slot
422  @return @c n_vectors_left -- number of vector slots available
423 */
424 #define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)    \
425   vlib_get_next_frame_macro (vm, node, next_index, vectors, n_vectors_left,   \
426                              /* alloc new frame */ 0)
427
428 #define vlib_get_new_next_frame(vm, node, next_index, vectors,                \
429                                 n_vectors_left)                               \
430   vlib_get_next_frame_macro (vm, node, next_index, vectors, n_vectors_left,   \
431                              /* alloc new frame */ 1)
432
433 /** \brief Get pointer to next frame vector data and next frame aux data by
434     (@c vlib_node_runtime_t, @c next_index).
435  Standard single/dual loop boilerplate element.
436  @attention This is a MACRO, with SIDE EFFECTS.
437  @attention This MACRO is unsafe in case the next node does not support
438  aux_data
439
440  @param vm vlib_main_t pointer, varies by thread
441  @param node current node vlib_node_runtime_t pointer
442  @param next_index requested graph arc index
443
444  @return @c vectors -- pointer to next available vector slot
445  @return @c aux_data -- pointer to next available aux data slot
446  @return @c n_vectors_left -- number of vector slots available
447 */
448 #define vlib_get_next_frame_with_aux(vm, node, next_index, vectors, aux_data, \
449                                      n_vectors_left)                          \
450   vlib_get_next_frame_macro_with_aux (                                        \
451     vm, node, next_index, vectors, n_vectors_left, /* alloc new frame */ 0,   \
452     aux_data, /* maybe_no_aux */ 0)
453
454 #define vlib_get_new_next_frame_with_aux(vm, node, next_index, vectors,       \
455                                          aux_data, n_vectors_left)            \
456   vlib_get_next_frame_macro_with_aux (                                        \
457     vm, node, next_index, vectors, n_vectors_left, /* alloc new frame */ 1,   \
458     aux_data, /* maybe_no_aux */ 0)
459
460 /** \brief Get pointer to next frame vector data and next frame aux data by
461     (@c vlib_node_runtime_t, @c next_index).
462  Standard single/dual loop boilerplate element.
463  @attention This is a MACRO, with SIDE EFFECTS.
464  @attention This MACRO is safe in case the next node does not support aux_data.
465  In that case aux_data is set to NULL.
466
467  @param vm vlib_main_t pointer, varies by thread
468  @param node current node vlib_node_runtime_t pointer
469  @param next_index requested graph arc index
470
471  @return @c vectors -- pointer to next available vector slot
472  @return @c aux_data -- pointer to next available aux data slot
473  @return @c n_vectors_left -- number of vector slots available
474 */
475 #define vlib_get_next_frame_with_aux_safe(vm, node, next_index, vectors,      \
476                                           aux_data, n_vectors_left)           \
477   vlib_get_next_frame_macro_with_aux (                                        \
478     vm, node, next_index, vectors, n_vectors_left, /* alloc new frame */ 0,   \
479     aux_data, /* maybe_no_aux */ 1)
480
481 #define vlib_get_new_next_frame_with_aux_safe(vm, node, next_index, vectors,  \
482                                               aux_data, n_vectors_left)       \
483   vlib_get_next_frame_macro_with_aux (                                        \
484     vm, node, next_index, vectors, n_vectors_left, /* alloc new frame */ 1,   \
485     aux_data, /* maybe_no_aux */ 1)
486
487 /** \brief Release pointer to next frame vector data.
488  Standard single/dual loop boilerplate element.
489  @param vm vlib_main_t pointer, varies by thread
490  @param r current node vlib_node_runtime_t pointer
491  @param next_index graph arc index
492  @param n_packets_left number of slots still available in vector
493 */
494 void
495 vlib_put_next_frame (vlib_main_t * vm,
496                      vlib_node_runtime_t * r,
497                      u32 next_index, u32 n_packets_left);
498
499 /* Combination get plus put.  Returns vector argument just added. */
500 #define vlib_set_next_frame(vm,node,next_index,v)                       \
501 ({                                                                      \
502   uword _n_left;                                                        \
503   vlib_get_next_frame ((vm), (node), (next_index), (v), _n_left);       \
504   ASSERT (_n_left > 0);                                                 \
505   vlib_put_next_frame ((vm), (node), (next_index), _n_left - 1);        \
506   (v);                                                                  \
507 })
508
509 #define vlib_set_next_frame_with_aux_safe(vm, node, next_index, v, aux)       \
510   ({                                                                          \
511     uword _n_left;                                                            \
512     vlib_get_next_frame_with_aux_safe ((vm), (node), (next_index), (v),       \
513                                        (aux), _n_left);                       \
514     ASSERT (_n_left > 0);                                                     \
515     vlib_put_next_frame ((vm), (node), (next_index), _n_left - 1);            \
516     (v);                                                                      \
517   })
518
519 always_inline void
520 vlib_set_next_frame_buffer (vlib_main_t * vm,
521                             vlib_node_runtime_t * node,
522                             u32 next_index, u32 buffer_index)
523 {
524   u32 *p;
525   p = vlib_set_next_frame (vm, node, next_index, p);
526   p[0] = buffer_index;
527 }
528
529 always_inline void
530 vlib_set_next_frame_buffer_with_aux_safe (vlib_main_t *vm,
531                                           vlib_node_runtime_t *node,
532                                           u32 next_index, u32 buffer_index,
533                                           u32 aux)
534 {
535   u32 *p;
536   u32 *a;
537   p = vlib_set_next_frame_with_aux_safe (vm, node, next_index, p, a);
538   p[0] = buffer_index;
539   if (a)
540     a[0] = aux;
541 }
542
543 vlib_frame_t *vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index);
544 void vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index,
545                              vlib_frame_t * f);
546
547 always_inline uword
548 vlib_in_process_context (vlib_main_t * vm)
549 {
550   return vm->node_main.current_process_index != ~0;
551 }
552
553 always_inline vlib_process_t *
554 vlib_get_current_process (vlib_main_t * vm)
555 {
556   vlib_node_main_t *nm = &vm->node_main;
557   if (vlib_in_process_context (vm))
558     return vec_elt (nm->processes, nm->current_process_index);
559   return 0;
560 }
561
562 always_inline uword
563 vlib_current_process (vlib_main_t * vm)
564 {
565   return vlib_get_current_process (vm)->node_runtime.node_index;
566 }
567
568 always_inline u32
569 vlib_get_current_process_node_index (vlib_main_t * vm)
570 {
571   vlib_process_t *process = vlib_get_current_process (vm);
572   return process->node_runtime.node_index;
573 }
574
575 /** Returns TRUE if a process suspend time is less than 10us
576     @param dt - remaining poll time in seconds
577     @returns 1 if dt < 10e-6, 0 otherwise
578 */
579 always_inline uword
580 vlib_process_suspend_time_is_zero (f64 dt)
581 {
582   return dt < 10e-6;
583 }
584
585 /** Suspend a vlib cooperative multi-tasking thread for a period of time
586     @param vm - vlib_main_t *
587     @param dt - suspend interval in seconds
588     @returns VLIB_PROCESS_RESUME_LONGJMP_RESUME, routinely ignored
589 */
590
591 always_inline uword
592 vlib_process_suspend (vlib_main_t * vm, f64 dt)
593 {
594   uword r;
595   vlib_node_main_t *nm = &vm->node_main;
596   vlib_process_t *p = vec_elt (nm->processes, nm->current_process_index);
597
598   if (vlib_process_suspend_time_is_zero (dt))
599     return VLIB_PROCESS_RESUME_LONGJMP_RESUME;
600
601   p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK;
602   r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
603   if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
604     {
605       /* expiration time in 10us ticks */
606       p->resume_clock_interval = dt * 1e5;
607       vlib_process_start_switch_stack (vm, 0);
608       clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
609     }
610   else
611     vlib_process_finish_switch_stack (vm);
612
613   return r;
614 }
615
616 always_inline void
617 vlib_process_free_event_type (vlib_process_t * p, uword t,
618                               uword is_one_time_event)
619 {
620   ASSERT (!pool_is_free_index (p->event_type_pool, t));
621   pool_put_index (p->event_type_pool, t);
622   if (is_one_time_event)
623     p->one_time_event_type_bitmap =
624       clib_bitmap_andnoti (p->one_time_event_type_bitmap, t);
625 }
626
627 always_inline void
628 vlib_process_maybe_free_event_type (vlib_process_t * p, uword t)
629 {
630   ASSERT (!pool_is_free_index (p->event_type_pool, t));
631   if (clib_bitmap_get (p->one_time_event_type_bitmap, t))
632     vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
633 }
634
635 always_inline void *
636 vlib_process_get_event_data (vlib_main_t * vm,
637                              uword * return_event_type_opaque)
638 {
639   vlib_node_main_t *nm = &vm->node_main;
640   vlib_process_t *p;
641   vlib_process_event_type_t *et;
642   uword t;
643   void *event_data_vector;
644
645   p = vec_elt (nm->processes, nm->current_process_index);
646
647   /* Find first type with events ready.
648      Return invalid type when there's nothing there. */
649   t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
650   if (t == ~0)
651     return 0;
652
653   p->non_empty_event_type_bitmap =
654     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
655
656   ASSERT (_vec_len (p->pending_event_data_by_type_index[t]) > 0);
657   event_data_vector = p->pending_event_data_by_type_index[t];
658   p->pending_event_data_by_type_index[t] = 0;
659
660   et = pool_elt_at_index (p->event_type_pool, t);
661
662   /* Return user's opaque value and possibly index. */
663   *return_event_type_opaque = et->opaque;
664
665   vlib_process_maybe_free_event_type (p, t);
666
667   return event_data_vector;
668 }
669
670 /* Return event data vector for later reuse.  We reuse event data to avoid
671    repeatedly allocating event vectors in cases where we care about speed. */
672 always_inline void
673 vlib_process_put_event_data (vlib_main_t * vm, void *event_data)
674 {
675   vlib_node_main_t *nm = &vm->node_main;
676   vec_add1 (nm->recycled_event_data_vectors, event_data);
677 }
678
679 /** Return the first event type which has occurred and a vector of per-event
680     data of that type, or a timeout indication
681
682     @param vm - vlib_main_t pointer
683     @param data_vector - pointer to a (uword *) vector to receive event data
684     @returns either an event type and a vector of per-event instance data,
685     or ~0 to indicate a timeout.
686 */
687
688 always_inline uword
689 vlib_process_get_events (vlib_main_t * vm, uword ** data_vector)
690 {
691   vlib_node_main_t *nm = &vm->node_main;
692   vlib_process_t *p;
693   vlib_process_event_type_t *et;
694   uword r, t, l;
695
696   p = vec_elt (nm->processes, nm->current_process_index);
697
698   /* Find first type with events ready.
699      Return invalid type when there's nothing there. */
700   t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
701   if (t == ~0)
702     return t;
703
704   p->non_empty_event_type_bitmap =
705     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
706
707   l = _vec_len (p->pending_event_data_by_type_index[t]);
708   if (data_vector)
709     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
710   vec_set_len (p->pending_event_data_by_type_index[t], 0);
711
712   et = pool_elt_at_index (p->event_type_pool, t);
713
714   /* Return user's opaque value. */
715   r = et->opaque;
716
717   vlib_process_maybe_free_event_type (p, t);
718
719   return r;
720 }
721
722 always_inline uword
723 vlib_process_get_events_helper (vlib_process_t * p, uword t,
724                                 uword ** data_vector)
725 {
726   uword l;
727
728   p->non_empty_event_type_bitmap =
729     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
730
731   l = _vec_len (p->pending_event_data_by_type_index[t]);
732   if (data_vector)
733     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
734   vec_set_len (p->pending_event_data_by_type_index[t], 0);
735
736   vlib_process_maybe_free_event_type (p, t);
737
738   return l;
739 }
740
741 /* As above but query as specified type of event.  Returns number of
742    events found. */
743 always_inline uword
744 vlib_process_get_events_with_type (vlib_main_t * vm, uword ** data_vector,
745                                    uword with_type_opaque)
746 {
747   vlib_node_main_t *nm = &vm->node_main;
748   vlib_process_t *p;
749   uword t, *h;
750
751   p = vec_elt (nm->processes, nm->current_process_index);
752   h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
753   if (!h)
754     /* This can happen when an event has not yet been
755        signaled with given opaque type. */
756     return 0;
757
758   t = h[0];
759   if (!clib_bitmap_get (p->non_empty_event_type_bitmap, t))
760     return 0;
761
762   return vlib_process_get_events_helper (p, t, data_vector);
763 }
764
765 always_inline uword *
766 vlib_process_wait_for_event (vlib_main_t * vm)
767 {
768   vlib_node_main_t *nm = &vm->node_main;
769   vlib_process_t *p;
770   uword r;
771
772   p = vec_elt (nm->processes, nm->current_process_index);
773   if (clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
774     {
775       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
776       r =
777         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
778       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
779         {
780           vlib_process_start_switch_stack (vm, 0);
781           clib_longjmp (&p->return_longjmp,
782                         VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
783         }
784       else
785         vlib_process_finish_switch_stack (vm);
786     }
787
788   return p->non_empty_event_type_bitmap;
789 }
790
791 always_inline uword
792 vlib_process_wait_for_one_time_event (vlib_main_t * vm,
793                                       uword ** data_vector,
794                                       uword with_type_index)
795 {
796   vlib_node_main_t *nm = &vm->node_main;
797   vlib_process_t *p;
798   uword r;
799
800   p = vec_elt (nm->processes, nm->current_process_index);
801   ASSERT (!pool_is_free_index (p->event_type_pool, with_type_index));
802   while (!clib_bitmap_get (p->non_empty_event_type_bitmap, with_type_index))
803     {
804       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
805       r =
806         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
807       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
808         {
809           vlib_process_start_switch_stack (vm, 0);
810           clib_longjmp (&p->return_longjmp,
811                         VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
812         }
813       else
814         vlib_process_finish_switch_stack (vm);
815     }
816
817   return vlib_process_get_events_helper (p, with_type_index, data_vector);
818 }
819
820 always_inline uword
821 vlib_process_wait_for_event_with_type (vlib_main_t * vm,
822                                        uword ** data_vector,
823                                        uword with_type_opaque)
824 {
825   vlib_node_main_t *nm = &vm->node_main;
826   vlib_process_t *p;
827   uword r, *h;
828
829   p = vec_elt (nm->processes, nm->current_process_index);
830   h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
831   while (!h || !clib_bitmap_get (p->non_empty_event_type_bitmap, h[0]))
832     {
833       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
834       r =
835         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
836       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
837         {
838           vlib_process_start_switch_stack (vm, 0);
839           clib_longjmp (&p->return_longjmp,
840                         VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
841         }
842       else
843         vlib_process_finish_switch_stack (vm);
844
845       /* See if unknown event type has been signaled now. */
846       if (!h)
847         h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
848     }
849
850   return vlib_process_get_events_helper (p, h[0], data_vector);
851 }
852
853 /** Suspend a cooperative multi-tasking thread
854     Waits for an event, or for the indicated number of seconds to elapse
855     @param vm - vlib_main_t pointer
856     @param dt - timeout, in seconds.
857     @returns the remaining time interval
858 */
859
860 always_inline f64
861 vlib_process_wait_for_event_or_clock (vlib_main_t * vm, f64 dt)
862 {
863   vlib_node_main_t *nm = &vm->node_main;
864   vlib_process_t *p;
865   f64 wakeup_time;
866   uword r;
867
868   p = vec_elt (nm->processes, nm->current_process_index);
869
870   if (vlib_process_suspend_time_is_zero (dt)
871       || !clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
872     return dt;
873
874   wakeup_time = vlib_time_now (vm) + dt;
875
876   /* Suspend waiting for both clock and event to occur. */
877   p->flags |= (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
878                | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK);
879
880   r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
881   if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
882     {
883       p->resume_clock_interval = dt * 1e5;
884       vlib_process_start_switch_stack (vm, 0);
885       clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
886     }
887   else
888     vlib_process_finish_switch_stack (vm);
889
890   /* Return amount of time still left to sleep.
891      If <= 0 then we've been waken up by the clock (and not an event). */
892   return wakeup_time - vlib_time_now (vm);
893 }
894
895 always_inline vlib_process_event_type_t *
896 vlib_process_new_event_type (vlib_process_t * p, uword with_type_opaque)
897 {
898   vlib_process_event_type_t *et;
899   pool_get (p->event_type_pool, et);
900   et->opaque = with_type_opaque;
901   return et;
902 }
903
904 always_inline uword
905 vlib_process_create_one_time_event (vlib_main_t * vm, uword node_index,
906                                     uword with_type_opaque)
907 {
908   vlib_node_main_t *nm = &vm->node_main;
909   vlib_node_t *n = vlib_get_node (vm, node_index);
910   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
911   vlib_process_event_type_t *et;
912   uword t;
913
914   et = vlib_process_new_event_type (p, with_type_opaque);
915   t = et - p->event_type_pool;
916   p->one_time_event_type_bitmap =
917     clib_bitmap_ori (p->one_time_event_type_bitmap, t);
918   return t;
919 }
920
921 always_inline void
922 vlib_process_delete_one_time_event (vlib_main_t * vm, uword node_index,
923                                     uword t)
924 {
925   vlib_node_main_t *nm = &vm->node_main;
926   vlib_node_t *n = vlib_get_node (vm, node_index);
927   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
928
929   ASSERT (clib_bitmap_get (p->one_time_event_type_bitmap, t));
930   vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
931 }
932
933 always_inline void *
934 vlib_process_signal_event_helper (vlib_node_main_t * nm,
935                                   vlib_node_t * n,
936                                   vlib_process_t * p,
937                                   uword t,
938                                   uword n_data_elts, uword n_data_elt_bytes)
939 {
940   uword p_flags, add_to_pending, delete_from_wheel;
941   u8 *data_to_be_written_by_caller;
942   vec_attr_t va = { .elt_sz = n_data_elt_bytes };
943
944   ASSERT (n->type == VLIB_NODE_TYPE_PROCESS);
945
946   ASSERT (!pool_is_free_index (p->event_type_pool, t));
947
948   vec_validate (p->pending_event_data_by_type_index, t);
949
950   /* Resize data vector and return caller's data to be written. */
951   {
952     u8 *data_vec = p->pending_event_data_by_type_index[t];
953     uword l;
954
955     if (!data_vec && vec_len (nm->recycled_event_data_vectors))
956       {
957         data_vec = vec_pop (nm->recycled_event_data_vectors);
958         vec_reset_length (data_vec);
959       }
960
961     l = vec_len (data_vec);
962
963     data_vec = _vec_realloc_internal (data_vec, l + n_data_elts, &va);
964
965     p->pending_event_data_by_type_index[t] = data_vec;
966     data_to_be_written_by_caller = data_vec + l * n_data_elt_bytes;
967   }
968
969   p->non_empty_event_type_bitmap =
970     clib_bitmap_ori (p->non_empty_event_type_bitmap, t);
971
972   p_flags = p->flags;
973
974   /* Event was already signalled? */
975   add_to_pending = (p_flags & VLIB_PROCESS_RESUME_PENDING) == 0;
976
977   /* Process will resume when suspend time elapses? */
978   delete_from_wheel = 0;
979   if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
980     {
981       /* Waiting for both event and clock? */
982       if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)
983         {
984           if (!TW (tw_timer_handle_is_free)
985               ((TWT (tw_timer_wheel) *) nm->timing_wheel,
986                p->stop_timer_handle))
987             delete_from_wheel = 1;
988           else
989             /* timer just popped so process should already be on the list */
990             add_to_pending = 0;
991         }
992       else
993         /* Waiting only for clock.  Event will be queue and may be
994            handled when timer expires. */
995         add_to_pending = 0;
996     }
997
998   /* Never add current process to pending vector since current process is
999      already running. */
1000   add_to_pending &= nm->current_process_index != n->runtime_index;
1001
1002   if (add_to_pending)
1003     {
1004       u32 x = vlib_timing_wheel_data_set_suspended_process (n->runtime_index);
1005       p->flags = p_flags | VLIB_PROCESS_RESUME_PENDING;
1006       vec_add1 (nm->data_from_advancing_timing_wheel, x);
1007       if (delete_from_wheel)
1008         {
1009           TW (tw_timer_stop)
1010           ((TWT (tw_timer_wheel) *) nm->timing_wheel, p->stop_timer_handle);
1011           p->stop_timer_handle = ~0;
1012         }
1013     }
1014
1015   return data_to_be_written_by_caller;
1016 }
1017
1018 always_inline void *
1019 vlib_process_signal_event_data (vlib_main_t * vm,
1020                                 uword node_index,
1021                                 uword type_opaque,
1022                                 uword n_data_elts, uword n_data_elt_bytes)
1023 {
1024   vlib_node_main_t *nm = &vm->node_main;
1025   vlib_node_t *n = vlib_get_node (vm, node_index);
1026   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
1027   uword *h, t;
1028
1029   /* Must be in main thread */
1030   ASSERT (vlib_get_thread_index () == 0);
1031
1032   h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
1033   if (!h)
1034     {
1035       vlib_process_event_type_t *et =
1036         vlib_process_new_event_type (p, type_opaque);
1037       t = et - p->event_type_pool;
1038       hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
1039     }
1040   else
1041     t = h[0];
1042
1043   return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
1044                                            n_data_elt_bytes);
1045 }
1046
1047 always_inline void *
1048 vlib_process_signal_event_at_time (vlib_main_t * vm,
1049                                    f64 dt,
1050                                    uword node_index,
1051                                    uword type_opaque,
1052                                    uword n_data_elts, uword n_data_elt_bytes)
1053 {
1054   vlib_node_main_t *nm = &vm->node_main;
1055   vlib_node_t *n = vlib_get_node (vm, node_index);
1056   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
1057   uword *h, t;
1058
1059   h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
1060   if (!h)
1061     {
1062       vlib_process_event_type_t *et =
1063         vlib_process_new_event_type (p, type_opaque);
1064       t = et - p->event_type_pool;
1065       hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
1066     }
1067   else
1068     t = h[0];
1069
1070   if (vlib_process_suspend_time_is_zero (dt))
1071     return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
1072                                              n_data_elt_bytes);
1073   else
1074     {
1075       vlib_signal_timed_event_data_t *te;
1076
1077       pool_get_aligned (nm->signal_timed_event_data_pool, te, sizeof (te[0]));
1078
1079       te->n_data_elts = n_data_elts;
1080       te->n_data_elt_bytes = n_data_elt_bytes;
1081       te->n_data_bytes = n_data_elts * n_data_elt_bytes;
1082
1083       /* Assert that structure fields are big enough. */
1084       ASSERT (te->n_data_elts == n_data_elts);
1085       ASSERT (te->n_data_elt_bytes == n_data_elt_bytes);
1086       ASSERT (te->n_data_bytes == n_data_elts * n_data_elt_bytes);
1087
1088       te->process_node_index = n->runtime_index;
1089       te->event_type_index = t;
1090
1091       p->stop_timer_handle =
1092         TW (tw_timer_start) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
1093                              vlib_timing_wheel_data_set_timed_event
1094                              (te - nm->signal_timed_event_data_pool),
1095                              0 /* timer_id */ ,
1096                              (vlib_time_now (vm) + dt) * 1e5);
1097
1098       /* Inline data big enough to hold event? */
1099       if (te->n_data_bytes < sizeof (te->inline_event_data))
1100         return te->inline_event_data;
1101       else
1102         {
1103           te->event_data_as_vector = 0;
1104           vec_resize (te->event_data_as_vector, te->n_data_bytes);
1105           return te->event_data_as_vector;
1106         }
1107     }
1108 }
1109
1110 always_inline void *
1111 vlib_process_signal_one_time_event_data (vlib_main_t * vm,
1112                                          uword node_index,
1113                                          uword type_index,
1114                                          uword n_data_elts,
1115                                          uword n_data_elt_bytes)
1116 {
1117   vlib_node_main_t *nm = &vm->node_main;
1118   vlib_node_t *n = vlib_get_node (vm, node_index);
1119   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
1120   return vlib_process_signal_event_helper (nm, n, p, type_index, n_data_elts,
1121                                            n_data_elt_bytes);
1122 }
1123
1124 always_inline void
1125 vlib_process_signal_event (vlib_main_t * vm,
1126                            uword node_index, uword type_opaque, uword data)
1127 {
1128   uword *d = vlib_process_signal_event_data (vm, node_index, type_opaque,
1129                                              1 /* elts */ , sizeof (uword));
1130   d[0] = data;
1131 }
1132
1133 always_inline void
1134 vlib_process_signal_event_pointer (vlib_main_t * vm,
1135                                    uword node_index,
1136                                    uword type_opaque, void *data)
1137 {
1138   void **d = vlib_process_signal_event_data (vm, node_index, type_opaque,
1139                                              1 /* elts */ , sizeof (data));
1140   d[0] = data;
1141 }
1142
1143 /**
1144  * Signal event to process from any thread.
1145  *
1146  * When in doubt, use this.
1147  */
1148 always_inline void
1149 vlib_process_signal_event_mt (vlib_main_t * vm,
1150                               uword node_index, uword type_opaque, uword data)
1151 {
1152   if (vlib_get_thread_index () != 0)
1153     {
1154       vlib_process_signal_event_mt_args_t args = {
1155         .node_index = node_index,
1156         .type_opaque = type_opaque,
1157         .data = data,
1158       };
1159       vlib_rpc_call_main_thread (vlib_process_signal_event_mt_helper,
1160                                  (u8 *) & args, sizeof (args));
1161     }
1162   else
1163     vlib_process_signal_event (vm, node_index, type_opaque, data);
1164 }
1165
1166 always_inline void
1167 vlib_process_signal_one_time_event (vlib_main_t * vm,
1168                                     uword node_index,
1169                                     uword type_index, uword data)
1170 {
1171   uword *d =
1172     vlib_process_signal_one_time_event_data (vm, node_index, type_index,
1173                                              1 /* elts */ , sizeof (uword));
1174   d[0] = data;
1175 }
1176
1177 always_inline void
1178 vlib_signal_one_time_waiting_process (vlib_main_t * vm,
1179                                       vlib_one_time_waiting_process_t * p)
1180 {
1181   vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event,
1182                                       /* data */ ~0);
1183   clib_memset (p, ~0, sizeof (p[0]));
1184 }
1185
1186 always_inline void
1187 vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm,
1188                                              vlib_one_time_waiting_process_t
1189                                              ** wps)
1190 {
1191   vlib_one_time_waiting_process_t *wp;
1192   vec_foreach (wp, *wps) vlib_signal_one_time_waiting_process (vm, wp);
1193   vec_free (*wps);
1194 }
1195
1196 always_inline void
1197 vlib_current_process_wait_for_one_time_event (vlib_main_t * vm,
1198                                               vlib_one_time_waiting_process_t
1199                                               * p)
1200 {
1201   p->node_index = vlib_current_process (vm);
1202   p->one_time_event = vlib_process_create_one_time_event (vm, p->node_index,    /* type opaque */
1203                                                           ~0);
1204   vlib_process_wait_for_one_time_event (vm,
1205                                         /* don't care about data */ 0,
1206                                         p->one_time_event);
1207 }
1208
1209 always_inline void
1210 vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm,
1211                                                      vlib_one_time_waiting_process_t
1212                                                      ** wps)
1213 {
1214   vlib_one_time_waiting_process_t *wp;
1215   vec_add2 (*wps, wp, 1);
1216   vlib_current_process_wait_for_one_time_event (vm, wp);
1217 }
1218
1219 always_inline u32
1220 vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm,
1221                                                  vlib_node_runtime_t * node,
1222                                                  uword n_vectors)
1223 {
1224   u32 i, d, vi0, vi1;
1225   u32 i0, i1;
1226
1227   ASSERT (is_pow2 (ARRAY_LEN (node->main_loop_vector_stats)));
1228   i = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
1229        & (ARRAY_LEN (node->main_loop_vector_stats) - 1));
1230   i0 = i ^ 0;
1231   i1 = i ^ 1;
1232   d = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
1233        -
1234        (node->main_loop_count_last_dispatch >>
1235         VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE));
1236   vi0 = node->main_loop_vector_stats[i0];
1237   vi1 = node->main_loop_vector_stats[i1];
1238   vi0 = d == 0 ? vi0 : 0;
1239   vi1 = d <= 1 ? vi1 : 0;
1240   vi0 += n_vectors;
1241   node->main_loop_vector_stats[i0] = vi0;
1242   node->main_loop_vector_stats[i1] = vi1;
1243   node->main_loop_count_last_dispatch = vm->main_loop_count;
1244   /* Return previous counter. */
1245   return node->main_loop_vector_stats[i1];
1246 }
1247
1248 always_inline f64
1249 vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index)
1250 {
1251   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
1252   u32 v;
1253
1254   v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt,  /* n_vectors */
1255                                                        0);
1256   return (f64) v / (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE);
1257 }
1258
1259 always_inline u32
1260 vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index)
1261 {
1262   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
1263   u32 v;
1264
1265   v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt,  /* n_vectors */
1266                                                        0);
1267   return v >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE;
1268 }
1269
1270 void vlib_frame_free (vlib_main_t *vm, vlib_frame_t *f);
1271
1272 /* Return the edge index if present, ~0 otherwise */
1273 uword vlib_node_get_next (vlib_main_t * vm, uword node, uword next_node);
1274
1275 /* Add next node to given node in given slot. */
1276 uword
1277 vlib_node_add_next_with_slot (vlib_main_t * vm,
1278                               uword node, uword next_node, uword slot);
1279
1280 /* As above but adds to end of node's next vector. */
1281 always_inline uword
1282 vlib_node_add_next (vlib_main_t * vm, uword node, uword next_node)
1283 {
1284   return vlib_node_add_next_with_slot (vm, node, next_node, ~0);
1285 }
1286
1287 /* Add next node to given node in given slot. */
1288 uword
1289 vlib_node_add_named_next_with_slot (vlib_main_t * vm,
1290                                     uword node, char *next_name, uword slot);
1291
1292 /* As above but adds to end of node's next vector. */
1293 always_inline uword
1294 vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name)
1295 {
1296   return vlib_node_add_named_next_with_slot (vm, node, name, ~0);
1297 }
1298
1299 /**
1300  * Get list of nodes
1301  */
1302 void
1303 vlib_node_get_nodes (vlib_main_t * vm, u32 max_threads, int include_stats,
1304                      int barrier_sync, vlib_node_t **** node_dupsp,
1305                      vlib_main_t *** stat_vmsp);
1306
1307 /* Query node given name. */
1308 vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name);
1309
1310 /* Rename a node. */
1311 void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...);
1312
1313 /* Register new packet processing node.  Nodes can be registered
1314    dynamically via this call or statically via the VLIB_REGISTER_NODE
1315    macro. */
1316 u32 vlib_register_node (vlib_main_t *vm, vlib_node_registration_t *r,
1317                         char *fmt, ...);
1318
1319 /* Register all node function variants */
1320 void vlib_register_all_node_march_variants (vlib_main_t *vm);
1321
1322 /* Register all static nodes registered via VLIB_REGISTER_NODE. */
1323 void vlib_register_all_static_nodes (vlib_main_t * vm);
1324
1325 /* Start a process. */
1326 void vlib_start_process (vlib_main_t * vm, uword process_index);
1327
1328 /* Sync up runtime and main node stats. */
1329 void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
1330 void vlib_node_runtime_sync_stats (vlib_main_t *vm, vlib_node_runtime_t *r,
1331                                    uword n_calls, uword n_vectors,
1332                                    uword n_clocks);
1333 void vlib_node_runtime_sync_stats_node (vlib_node_t *n, vlib_node_runtime_t *r,
1334                                         uword n_calls, uword n_vectors,
1335                                         uword n_clocks);
1336
1337 /* Node graph initialization function. */
1338 clib_error_t *vlib_node_main_init (vlib_main_t * vm);
1339
1340 format_function_t format_vlib_node_graph;
1341 format_function_t format_vlib_node_name;
1342 format_function_t format_vlib_next_node_name;
1343 format_function_t format_vlib_node_and_next;
1344 format_function_t format_vlib_cpu_time;
1345 format_function_t format_vlib_time;
1346 /* Parse node name -> node index. */
1347 unformat_function_t unformat_vlib_node;
1348
1349 always_inline void
1350 vlib_node_increment_counter (vlib_main_t * vm, u32 node_index,
1351                              u32 counter_index, u64 increment)
1352 {
1353   vlib_node_t *n = vlib_get_node (vm, node_index);
1354   vlib_error_main_t *em = &vm->error_main;
1355   u32 node_counter_base_index = n->error_heap_index;
1356   em->counters[node_counter_base_index + counter_index] += increment;
1357 }
1358
1359 /** @brief Create a vlib process
1360  *  @param vm &vlib_global_main
1361  *  @param f the process node function
1362  *  @param log2_n_stack_bytes size of the process stack, defaults to 16K
1363  *  @return newly-create node index
1364  *  @warning call only on the main thread. Barrier sync required
1365  */
1366 u32 vlib_process_create (vlib_main_t * vm, char *name,
1367                          vlib_node_function_t * f, u32 log2_n_stack_bytes);
1368
1369 always_inline int
1370 vlib_node_set_dispatch_wrapper (vlib_main_t *vm, vlib_node_function_t *fn)
1371 {
1372   if (fn && vm->dispatch_wrapper_fn)
1373     return 1;
1374   vm->dispatch_wrapper_fn = fn;
1375   return 0;
1376 }
1377
1378 int vlib_node_set_march_variant (vlib_main_t *vm, u32 node_index,
1379                                  clib_march_variant_type_t march_variant);
1380
1381 vlib_node_function_t *
1382 vlib_node_get_preferred_node_fn_variant (vlib_main_t *vm,
1383                                          vlib_node_fn_registration_t *regs);
1384
1385 /*
1386  * vlib_frame_bitmap functions
1387  */
1388
1389 #define VLIB_FRAME_BITMAP_N_UWORDS                                            \
1390   (((VLIB_FRAME_SIZE + uword_bits - 1) & ~(uword_bits - 1)) / uword_bits)
1391
1392 typedef uword vlib_frame_bitmap_t[VLIB_FRAME_BITMAP_N_UWORDS];
1393
1394 static_always_inline void
1395 vlib_frame_bitmap_init (uword *bmp, u32 n_first_bits_set)
1396 {
1397   u32 n_left = VLIB_FRAME_BITMAP_N_UWORDS;
1398   while (n_first_bits_set >= (sizeof (uword) * 8) && n_left)
1399     {
1400       bmp++[0] = ~0;
1401       n_first_bits_set -= sizeof (uword) * 8;
1402       n_left--;
1403     }
1404
1405   if (n_first_bits_set && n_left)
1406     {
1407       bmp++[0] = pow2_mask (n_first_bits_set);
1408       n_left--;
1409     }
1410
1411   while (n_left--)
1412     bmp++[0] = 0;
1413 }
1414
1415 static_always_inline void
1416 vlib_frame_bitmap_set_bit_at_index (uword *bmp, uword bit_index)
1417 {
1418   uword_bitmap_set_bits_at_index (bmp, bit_index, 1);
1419 }
1420
1421 static_always_inline void
1422 _vlib_frame_bitmap_clear_bit_at_index (uword *bmp, uword bit_index)
1423 {
1424   uword_bitmap_clear_bits_at_index (bmp, bit_index, 1);
1425 }
1426
1427 static_always_inline void
1428 vlib_frame_bitmap_set_bits_at_index (uword *bmp, uword bit_index, uword n_bits)
1429 {
1430   uword_bitmap_set_bits_at_index (bmp, bit_index, n_bits);
1431 }
1432
1433 static_always_inline void
1434 vlib_frame_bitmap_clear_bits_at_index (uword *bmp, uword bit_index,
1435                                        uword n_bits)
1436 {
1437   uword_bitmap_clear_bits_at_index (bmp, bit_index, n_bits);
1438 }
1439
1440 static_always_inline void
1441 vlib_frame_bitmap_clear (uword *bmp)
1442 {
1443   u32 n_left = VLIB_FRAME_BITMAP_N_UWORDS;
1444   while (n_left--)
1445     bmp++[0] = 0;
1446 }
1447
1448 static_always_inline void
1449 vlib_frame_bitmap_xor (uword *bmp, uword *bmp2)
1450 {
1451   u32 n_left = VLIB_FRAME_BITMAP_N_UWORDS;
1452   while (n_left--)
1453     bmp++[0] ^= bmp2++[0];
1454 }
1455
1456 static_always_inline void
1457 vlib_frame_bitmap_or (uword *bmp, uword *bmp2)
1458 {
1459   u32 n_left = VLIB_FRAME_BITMAP_N_UWORDS;
1460   while (n_left--)
1461     bmp++[0] |= bmp2++[0];
1462 }
1463
1464 static_always_inline void
1465 vlib_frame_bitmap_and (uword *bmp, uword *bmp2)
1466 {
1467   u32 n_left = VLIB_FRAME_BITMAP_N_UWORDS;
1468   while (n_left--)
1469     bmp++[0] &= bmp2++[0];
1470 }
1471
1472 static_always_inline uword
1473 vlib_frame_bitmap_count_set_bits (uword *bmp)
1474 {
1475   return uword_bitmap_count_set_bits (bmp, VLIB_FRAME_BITMAP_N_UWORDS);
1476 }
1477
1478 static_always_inline uword
1479 vlib_frame_bitmap_is_bit_set (uword *bmp, uword bit_index)
1480 {
1481   return uword_bitmap_is_bit_set (bmp, bit_index);
1482 }
1483
1484 static_always_inline uword
1485 vlib_frame_bitmap_find_first_set (uword *bmp)
1486 {
1487   uword rv = uword_bitmap_find_first_set (bmp);
1488   ASSERT (rv < VLIB_FRAME_BITMAP_N_UWORDS * uword_bits);
1489   return rv;
1490 }
1491
1492 #define foreach_vlib_frame_bitmap_set_bit_index(i, v)                         \
1493   for (uword _off = 0; _off < ARRAY_LEN (v); _off++)                          \
1494     for (uword _tmp =                                                         \
1495            (v[_off]) + 0 * (uword) (i = _off * uword_bits +                   \
1496                                         get_lowest_set_bit_index (v[_off]));  \
1497          _tmp; i = _off * uword_bits + get_lowest_set_bit_index (             \
1498                                          _tmp = clear_lowest_set_bit (_tmp)))
1499
1500 #endif /* included_vlib_node_funcs_h */
1501
1502 /*
1503  * fd.io coding-style-patch-verification: ON
1504  *
1505  * Local Variables:
1506  * eval: (c-set-style "gnu")
1507  * End:
1508  */