Use thread local storage for thread index
[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/fifo.h>
49
50 /** \brief Get vlib node by index.
51  @warning This function will ASSERT if @c i is out of range.
52  @param vm vlib_main_t pointer, varies by thread
53  @param i node index.
54  @return pointer to the requested vlib_node_t.
55 */
56
57 always_inline vlib_node_t *
58 vlib_get_node (vlib_main_t * vm, u32 i)
59 {
60   return vec_elt (vm->node_main.nodes, i);
61 }
62
63 /** \brief Get vlib node by graph arc (next) index.
64  @param vm vlib_main_t pointer, varies by thread
65  @param node_index index of original node
66  @param next_index graph arc index
67  @return pointer to the vlib_node_t at the end of the indicated arc
68 */
69
70 always_inline vlib_node_t *
71 vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index)
72 {
73   vlib_node_main_t *nm = &vm->node_main;
74   vlib_node_t *n;
75
76   n = vec_elt (nm->nodes, node_index);
77   ASSERT (next_index < vec_len (n->next_nodes));
78   return vlib_get_node (vm, n->next_nodes[next_index]);
79 }
80
81 /** \brief Get node runtime by node index.
82  @param vm vlib_main_t pointer, varies by thread
83  @param node_index index of node
84  @return pointer to the indicated vlib_node_runtime_t
85 */
86
87 always_inline vlib_node_runtime_t *
88 vlib_node_get_runtime (vlib_main_t * vm, u32 node_index)
89 {
90   vlib_node_main_t *nm = &vm->node_main;
91   vlib_node_t *n = vec_elt (nm->nodes, node_index);
92   vlib_process_t *p;
93   if (n->type != VLIB_NODE_TYPE_PROCESS)
94     return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
95   else
96     {
97       p = vec_elt (nm->processes, n->runtime_index);
98       return &p->node_runtime;
99     }
100 }
101
102 /** \brief Get node runtime private data by node index.
103  @param vm vlib_main_t pointer, varies by thread
104  @param node_index index of the node
105  @return pointer to the indicated vlib_node_runtime_t private data
106 */
107
108 always_inline void *
109 vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index)
110 {
111   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
112   return r->runtime_data;
113 }
114
115 /** \brief Set node runtime private data.
116  @param vm vlib_main_t pointer, varies by thread
117  @param node_index index of the node
118  @param runtime_data arbitrary runtime private data
119  @param n_runtime_data_bytes size of runtime private data
120 */
121
122 always_inline void
123 vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index,
124                             void *runtime_data, u32 n_runtime_data_bytes)
125 {
126   vlib_node_t *n = vlib_get_node (vm, node_index);
127   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
128
129   n->runtime_data_bytes = n_runtime_data_bytes;
130   vec_free (n->runtime_data);
131   vec_add (n->runtime_data, runtime_data, n_runtime_data_bytes);
132
133   ASSERT (vec_len (n->runtime_data) <= sizeof (vlib_node_runtime_t) -
134           STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data));
135
136   if (vec_len (n->runtime_data) > 0)
137     clib_memcpy (r->runtime_data, n->runtime_data, vec_len (n->runtime_data));
138 }
139
140 /** \brief Set node dispatch state.
141  @param vm vlib_main_t pointer, varies by thread
142  @param node_index index of the node
143  @param new_state new state for node, see vlib_node_state_t
144 */
145 always_inline void
146 vlib_node_set_state (vlib_main_t * vm, u32 node_index,
147                      vlib_node_state_t new_state)
148 {
149   vlib_node_main_t *nm = &vm->node_main;
150   vlib_node_t *n;
151   vlib_node_runtime_t *r;
152
153   n = vec_elt (nm->nodes, node_index);
154   if (n->type == VLIB_NODE_TYPE_PROCESS)
155     {
156       vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
157       r = &p->node_runtime;
158
159       /* When disabling make sure flags are cleared. */
160       p->flags &= ~(VLIB_PROCESS_RESUME_PENDING
161                     | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
162                     | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT);
163     }
164   else
165     r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
166
167   ASSERT (new_state < VLIB_N_NODE_STATE);
168
169   if (n->type == VLIB_NODE_TYPE_INPUT)
170     {
171       ASSERT (nm->input_node_counts_by_state[n->state] > 0);
172       nm->input_node_counts_by_state[n->state] -= 1;
173       nm->input_node_counts_by_state[new_state] += 1;
174     }
175
176   n->state = new_state;
177   r->state = new_state;
178 }
179
180 always_inline void
181 vlib_node_set_interrupt_pending (vlib_main_t * vm, u32 node_index)
182 {
183   vlib_node_main_t *nm = &vm->node_main;
184   vlib_node_t *n = vec_elt (nm->nodes, node_index);
185   ASSERT (n->type == VLIB_NODE_TYPE_INPUT);
186   clib_spinlock_lock_if_init (&nm->pending_interrupt_lock);
187   vec_add1 (nm->pending_interrupt_node_runtime_indices, n->runtime_index);
188   clib_spinlock_unlock_if_init (&nm->pending_interrupt_lock);
189 }
190
191 always_inline vlib_process_t *
192 vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node)
193 {
194   vlib_node_main_t *nm = &vm->node_main;
195   ASSERT (node->type == VLIB_NODE_TYPE_PROCESS);
196   return vec_elt (nm->processes, node->runtime_index);
197 }
198
199 /* Fetches frame with given handle. */
200 always_inline vlib_frame_t *
201 vlib_get_frame_no_check (vlib_main_t * vm, uword frame_index)
202 {
203   vlib_frame_t *f;
204   u32 thread_index = frame_index & VLIB_CPU_MASK;
205   u32 offset = frame_index & VLIB_OFFSET_MASK;
206   vm = vlib_mains[thread_index];
207   f = vm->heap_base + offset;
208   return f;
209 }
210
211 always_inline u32
212 vlib_frame_index_no_check (vlib_main_t * vm, vlib_frame_t * f)
213 {
214   u32 i;
215
216   ASSERT (((uword) f & VLIB_CPU_MASK) == 0);
217
218   vm = vlib_mains[f->thread_index];
219
220   i = ((u8 *) f - (u8 *) vm->heap_base);
221   return i | f->thread_index;
222 }
223
224 always_inline vlib_frame_t *
225 vlib_get_frame (vlib_main_t * vm, uword frame_index)
226 {
227   vlib_frame_t *f = vlib_get_frame_no_check (vm, frame_index);
228   ASSERT (f->flags & VLIB_FRAME_IS_ALLOCATED);
229   return f;
230 }
231
232 always_inline u32
233 vlib_frame_index (vlib_main_t * vm, vlib_frame_t * f)
234 {
235   uword i = vlib_frame_index_no_check (vm, f);
236   ASSERT (vlib_get_frame (vm, i) == f);
237   return i;
238 }
239
240 /* Byte alignment for vector arguments. */
241 #define VLIB_FRAME_VECTOR_ALIGN (1 << 4)
242
243 always_inline u32
244 vlib_frame_vector_byte_offset (u32 scalar_size)
245 {
246   return round_pow2 (sizeof (vlib_frame_t) + scalar_size,
247                      VLIB_FRAME_VECTOR_ALIGN);
248 }
249
250 /** \brief Get pointer to frame vector data.
251  @param f vlib_frame_t pointer
252  @return pointer to first vector element in frame
253 */
254 always_inline void *
255 vlib_frame_vector_args (vlib_frame_t * f)
256 {
257   return (void *) f + vlib_frame_vector_byte_offset (f->scalar_size);
258 }
259
260 /** \brief Get pointer to frame scalar data.
261
262  @warning This is almost certainly not the function you wish to call.
263  See @ref vlib_frame_vector_args instead.
264
265  @param f vlib_frame_t pointer
266
267  @return arbitrary node scalar data
268
269  @sa vlib_frame_vector_args
270 */
271 always_inline void *
272 vlib_frame_args (vlib_frame_t * f)
273 {
274   return vlib_frame_vector_args (f) - f->scalar_size;
275 }
276
277 always_inline vlib_next_frame_t *
278 vlib_node_runtime_get_next_frame (vlib_main_t * vm,
279                                   vlib_node_runtime_t * n, u32 next_index)
280 {
281   vlib_node_main_t *nm = &vm->node_main;
282   vlib_next_frame_t *nf;
283
284   ASSERT (next_index < n->n_next_nodes);
285   nf = vec_elt_at_index (nm->next_frames, n->next_frame_index + next_index);
286
287   if (CLIB_DEBUG > 0)
288     {
289       vlib_node_t *node, *next;
290       node = vec_elt (nm->nodes, n->node_index);
291       next = vec_elt (nm->nodes, node->next_nodes[next_index]);
292       ASSERT (nf->node_runtime_index == next->runtime_index);
293     }
294
295   return nf;
296 }
297
298 /** \brief Get pointer to frame by (@c node_index, @c next_index).
299
300  @warning This is not a function that you should call directly.
301  See @ref vlib_get_next_frame instead.
302
303  @param vm vlib_main_t pointer, varies by thread
304  @param node_index index of the node
305  @param next_index graph arc index
306
307  @return pointer to the requested vlib_next_frame_t
308
309  @sa vlib_get_next_frame
310 */
311
312 always_inline vlib_next_frame_t *
313 vlib_node_get_next_frame (vlib_main_t * vm, u32 node_index, u32 next_index)
314 {
315   vlib_node_main_t *nm = &vm->node_main;
316   vlib_node_t *n;
317   vlib_node_runtime_t *r;
318
319   n = vec_elt (nm->nodes, node_index);
320   r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
321   return vlib_node_runtime_get_next_frame (vm, r, next_index);
322 }
323
324 vlib_frame_t *vlib_get_next_frame_internal (vlib_main_t * vm,
325                                             vlib_node_runtime_t * node,
326                                             u32 next_index,
327                                             u32 alloc_new_frame);
328
329 #define vlib_get_next_frame_macro(vm,node,next_index,vectors,n_vectors_left,alloc_new_frame) \
330 do {                                                                    \
331   vlib_frame_t * _f                                                     \
332     = vlib_get_next_frame_internal ((vm), (node), (next_index),         \
333                                     (alloc_new_frame));                 \
334   u32 _n = _f->n_vectors;                                               \
335   (vectors) = vlib_frame_vector_args (_f) + _n * sizeof ((vectors)[0]); \
336   (n_vectors_left) = VLIB_FRAME_SIZE - _n;                              \
337 } while (0)
338
339
340 /** \brief Get pointer to next frame vector data by
341     (@c vlib_node_runtime_t, @c next_index).
342  Standard single/dual loop boilerplate element.
343  @attention This is a MACRO, with SIDE EFFECTS.
344
345  @param vm vlib_main_t pointer, varies by thread
346  @param node current node vlib_node_runtime_t pointer
347  @param next_index requested graph arc index
348
349  @return @c vectors -- pointer to next available vector slot
350  @return @c n_vectors_left -- number of vector slots available
351 */
352 #define vlib_get_next_frame(vm,node,next_index,vectors,n_vectors_left)  \
353   vlib_get_next_frame_macro (vm, node, next_index,                      \
354                              vectors, n_vectors_left,                   \
355                              /* alloc new frame */ 0)
356
357 #define vlib_get_new_next_frame(vm,node,next_index,vectors,n_vectors_left) \
358   vlib_get_next_frame_macro (vm, node, next_index,                      \
359                              vectors, n_vectors_left,                   \
360                              /* alloc new frame */ 1)
361
362 /** \brief Release pointer to next frame vector data.
363  Standard single/dual loop boilerplate element.
364  @param vm vlib_main_t pointer, varies by thread
365  @param r current node vlib_node_runtime_t pointer
366  @param next_index graph arc index
367  @param n_packets_left number of slots still available in vector
368 */
369 void
370 vlib_put_next_frame (vlib_main_t * vm,
371                      vlib_node_runtime_t * r,
372                      u32 next_index, u32 n_packets_left);
373
374 /* Combination get plus put.  Returns vector argument just added. */
375 #define vlib_set_next_frame(vm,node,next_index,v)                       \
376 ({                                                                      \
377   uword _n_left;                                                        \
378   vlib_get_next_frame ((vm), (node), (next_index), (v), _n_left);       \
379   ASSERT (_n_left > 0);                                                 \
380   vlib_put_next_frame ((vm), (node), (next_index), _n_left - 1);        \
381   (v);                                                                  \
382 })
383
384 always_inline void
385 vlib_set_next_frame_buffer (vlib_main_t * vm,
386                             vlib_node_runtime_t * node,
387                             u32 next_index, u32 buffer_index)
388 {
389   u32 *p;
390   p = vlib_set_next_frame (vm, node, next_index, p);
391   p[0] = buffer_index;
392 }
393
394 vlib_frame_t *vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index);
395 void vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index,
396                              vlib_frame_t * f);
397
398 always_inline vlib_process_t *
399 vlib_get_current_process (vlib_main_t * vm)
400 {
401   vlib_node_main_t *nm = &vm->node_main;
402   return vec_elt (nm->processes, nm->current_process_index);
403 }
404
405 always_inline uword
406 vlib_in_process_context (vlib_main_t * vm)
407 {
408   return vm->node_main.current_process_index != ~0;
409 }
410
411 always_inline uword
412 vlib_current_process (vlib_main_t * vm)
413 {
414   return vlib_get_current_process (vm)->node_runtime.node_index;
415 }
416
417 /** Returns TRUE if a process suspend time is less than 1us
418     @param dt - remaining poll time in seconds
419     @returns 1 if dt < 1e-6, 0 otherwise
420 */
421 always_inline uword
422 vlib_process_suspend_time_is_zero (f64 dt)
423 {
424   return dt < 1e-6;
425 }
426
427 /** Suspend a vlib cooperative multi-tasking thread for a period of time
428     @param vm - vlib_main_t *
429     @param dt - suspend interval in seconds
430     @returns VLIB_PROCESS_RESUME_LONGJMP_RESUME, routinely ignored
431 */
432
433 always_inline uword
434 vlib_process_suspend (vlib_main_t * vm, f64 dt)
435 {
436   uword r;
437   vlib_node_main_t *nm = &vm->node_main;
438   vlib_process_t *p = vec_elt (nm->processes, nm->current_process_index);
439   u64 dt_cpu = dt * vm->clib_time.clocks_per_second;
440
441   if (vlib_process_suspend_time_is_zero (dt))
442     return VLIB_PROCESS_RESUME_LONGJMP_RESUME;
443
444   p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK;
445   r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
446   if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
447     {
448       p->resume_cpu_time = clib_cpu_time_now () + dt_cpu;
449       clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
450     }
451
452   return r;
453 }
454
455 always_inline void
456 vlib_process_free_event_type (vlib_process_t * p, uword t,
457                               uword is_one_time_event)
458 {
459   ASSERT (!pool_is_free_index (p->event_type_pool, t));
460   pool_put_index (p->event_type_pool, t);
461   if (is_one_time_event)
462     p->one_time_event_type_bitmap =
463       clib_bitmap_andnoti (p->one_time_event_type_bitmap, t);
464 }
465
466 always_inline void
467 vlib_process_maybe_free_event_type (vlib_process_t * p, uword t)
468 {
469   ASSERT (!pool_is_free_index (p->event_type_pool, t));
470   if (clib_bitmap_get (p->one_time_event_type_bitmap, t))
471     vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
472 }
473
474 always_inline void *
475 vlib_process_get_event_data (vlib_main_t * vm,
476                              uword * return_event_type_opaque)
477 {
478   vlib_node_main_t *nm = &vm->node_main;
479   vlib_process_t *p;
480   vlib_process_event_type_t *et;
481   uword t;
482   void *event_data_vector;
483
484   p = vec_elt (nm->processes, nm->current_process_index);
485
486   /* Find first type with events ready.
487      Return invalid type when there's nothing there. */
488   t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
489   if (t == ~0)
490     return 0;
491
492   p->non_empty_event_type_bitmap =
493     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
494
495   ASSERT (_vec_len (p->pending_event_data_by_type_index[t]) > 0);
496   event_data_vector = p->pending_event_data_by_type_index[t];
497   p->pending_event_data_by_type_index[t] = 0;
498
499   et = pool_elt_at_index (p->event_type_pool, t);
500
501   /* Return user's opaque value and possibly index. */
502   *return_event_type_opaque = et->opaque;
503
504   vlib_process_maybe_free_event_type (p, t);
505
506   return event_data_vector;
507 }
508
509 /* Return event data vector for later reuse.  We reuse event data to avoid
510    repeatedly allocating event vectors in cases where we care about speed. */
511 always_inline void
512 vlib_process_put_event_data (vlib_main_t * vm, void *event_data)
513 {
514   vlib_node_main_t *nm = &vm->node_main;
515   vec_add1 (nm->recycled_event_data_vectors, event_data);
516 }
517
518 /** Return the first event type which has occurred and a vector of per-event
519     data of that type, or a timeout indication
520
521     @param vm - vlib_main_t pointer
522     @param data_vector - pointer to a (uword *) vector to receive event data
523     @returns either an event type and a vector of per-event instance data,
524     or ~0 to indicate a timeout.
525 */
526
527 always_inline uword
528 vlib_process_get_events (vlib_main_t * vm, uword ** data_vector)
529 {
530   vlib_node_main_t *nm = &vm->node_main;
531   vlib_process_t *p;
532   vlib_process_event_type_t *et;
533   uword r, t, l;
534
535   p = vec_elt (nm->processes, nm->current_process_index);
536
537   /* Find first type with events ready.
538      Return invalid type when there's nothing there. */
539   t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
540   if (t == ~0)
541     return t;
542
543   p->non_empty_event_type_bitmap =
544     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
545
546   l = _vec_len (p->pending_event_data_by_type_index[t]);
547   if (data_vector)
548     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
549   _vec_len (p->pending_event_data_by_type_index[t]) = 0;
550
551   et = pool_elt_at_index (p->event_type_pool, t);
552
553   /* Return user's opaque value. */
554   r = et->opaque;
555
556   vlib_process_maybe_free_event_type (p, t);
557
558   return r;
559 }
560
561 always_inline uword
562 vlib_process_get_events_helper (vlib_process_t * p, uword t,
563                                 uword ** data_vector)
564 {
565   uword l;
566
567   p->non_empty_event_type_bitmap =
568     clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
569
570   l = _vec_len (p->pending_event_data_by_type_index[t]);
571   if (data_vector)
572     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
573   _vec_len (p->pending_event_data_by_type_index[t]) = 0;
574
575   vlib_process_maybe_free_event_type (p, t);
576
577   return l;
578 }
579
580 /* As above but query as specified type of event.  Returns number of
581    events found. */
582 always_inline uword
583 vlib_process_get_events_with_type (vlib_main_t * vm, uword ** data_vector,
584                                    uword with_type_opaque)
585 {
586   vlib_node_main_t *nm = &vm->node_main;
587   vlib_process_t *p;
588   uword t, *h;
589
590   p = vec_elt (nm->processes, nm->current_process_index);
591   h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
592   if (!h)
593     /* This can happen when an event has not yet been
594        signaled with given opaque type. */
595     return 0;
596
597   t = h[0];
598   if (!clib_bitmap_get (p->non_empty_event_type_bitmap, t))
599     return 0;
600
601   return vlib_process_get_events_helper (p, t, data_vector);
602 }
603
604 always_inline uword *
605 vlib_process_wait_for_event (vlib_main_t * vm)
606 {
607   vlib_node_main_t *nm = &vm->node_main;
608   vlib_process_t *p;
609   uword r;
610
611   p = vec_elt (nm->processes, nm->current_process_index);
612   if (clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
613     {
614       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
615       r =
616         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
617       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
618         clib_longjmp (&p->return_longjmp,
619                       VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
620     }
621
622   return p->non_empty_event_type_bitmap;
623 }
624
625 always_inline uword
626 vlib_process_wait_for_one_time_event (vlib_main_t * vm,
627                                       uword ** data_vector,
628                                       uword with_type_index)
629 {
630   vlib_node_main_t *nm = &vm->node_main;
631   vlib_process_t *p;
632   uword r;
633
634   p = vec_elt (nm->processes, nm->current_process_index);
635   ASSERT (!pool_is_free_index (p->event_type_pool, with_type_index));
636   while (!clib_bitmap_get (p->non_empty_event_type_bitmap, with_type_index))
637     {
638       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
639       r =
640         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
641       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
642         clib_longjmp (&p->return_longjmp,
643                       VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
644     }
645
646   return vlib_process_get_events_helper (p, with_type_index, data_vector);
647 }
648
649 always_inline uword
650 vlib_process_wait_for_event_with_type (vlib_main_t * vm,
651                                        uword ** data_vector,
652                                        uword with_type_opaque)
653 {
654   vlib_node_main_t *nm = &vm->node_main;
655   vlib_process_t *p;
656   uword r, *h;
657
658   p = vec_elt (nm->processes, nm->current_process_index);
659   h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
660   while (!h || !clib_bitmap_get (p->non_empty_event_type_bitmap, h[0]))
661     {
662       p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
663       r =
664         clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
665       if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
666         clib_longjmp (&p->return_longjmp,
667                       VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
668
669       /* See if unknown event type has been signaled now. */
670       if (!h)
671         h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
672     }
673
674   return vlib_process_get_events_helper (p, h[0], data_vector);
675 }
676
677 /** Suspend a cooperative multi-tasking thread
678     Waits for an event, or for the indicated number of seconds to elapse
679     @param vm - vlib_main_t pointer
680     @param dt - timeout, in seconds.
681     @returns the remaining time interval
682 */
683
684 always_inline f64
685 vlib_process_wait_for_event_or_clock (vlib_main_t * vm, f64 dt)
686 {
687   vlib_node_main_t *nm = &vm->node_main;
688   vlib_process_t *p;
689   f64 wakeup_time;
690   uword r;
691
692   p = vec_elt (nm->processes, nm->current_process_index);
693
694   if (vlib_process_suspend_time_is_zero (dt)
695       || !clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
696     return dt;
697
698   wakeup_time = vlib_time_now (vm) + dt;
699
700   /* Suspend waiting for both clock and event to occur. */
701   p->flags |= (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
702                | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK);
703
704   r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
705   if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
706     {
707       p->resume_cpu_time = (clib_cpu_time_now ()
708                             + (dt * vm->clib_time.clocks_per_second));
709       clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
710     }
711
712   /* Return amount of time still left to sleep.
713      If <= 0 then we've been waken up by the clock (and not an event). */
714   return wakeup_time - vlib_time_now (vm);
715 }
716
717 always_inline vlib_process_event_type_t *
718 vlib_process_new_event_type (vlib_process_t * p, uword with_type_opaque)
719 {
720   vlib_process_event_type_t *et;
721   pool_get (p->event_type_pool, et);
722   et->opaque = with_type_opaque;
723   return et;
724 }
725
726 always_inline uword
727 vlib_process_create_one_time_event (vlib_main_t * vm, uword node_index,
728                                     uword with_type_opaque)
729 {
730   vlib_node_main_t *nm = &vm->node_main;
731   vlib_node_t *n = vlib_get_node (vm, node_index);
732   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
733   vlib_process_event_type_t *et;
734   uword t;
735
736   et = vlib_process_new_event_type (p, with_type_opaque);
737   t = et - p->event_type_pool;
738   p->one_time_event_type_bitmap =
739     clib_bitmap_ori (p->one_time_event_type_bitmap, t);
740   return t;
741 }
742
743 always_inline void
744 vlib_process_delete_one_time_event (vlib_main_t * vm, uword node_index,
745                                     uword t)
746 {
747   vlib_node_main_t *nm = &vm->node_main;
748   vlib_node_t *n = vlib_get_node (vm, node_index);
749   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
750
751   ASSERT (clib_bitmap_get (p->one_time_event_type_bitmap, t));
752   vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
753 }
754
755 always_inline void *
756 vlib_process_signal_event_helper (vlib_node_main_t * nm,
757                                   vlib_node_t * n,
758                                   vlib_process_t * p,
759                                   uword t,
760                                   uword n_data_elts, uword n_data_elt_bytes)
761 {
762   uword p_flags, add_to_pending, delete_from_wheel;
763   void *data_to_be_written_by_caller;
764
765   ASSERT (!pool_is_free_index (p->event_type_pool, t));
766
767   vec_validate (p->pending_event_data_by_type_index, t);
768
769   /* Resize data vector and return caller's data to be written. */
770   {
771     void *data_vec = p->pending_event_data_by_type_index[t];
772     uword l;
773
774     if (!data_vec && vec_len (nm->recycled_event_data_vectors))
775       {
776         data_vec = vec_pop (nm->recycled_event_data_vectors);
777         _vec_len (data_vec) = 0;
778       }
779
780     l = vec_len (data_vec);
781
782     data_vec = _vec_resize (data_vec,
783                             /* length_increment */ n_data_elts,
784                             /* total size after increment */
785                             (l + n_data_elts) * n_data_elt_bytes,
786                             /* header_bytes */ 0, /* data_align */ 0);
787
788     p->pending_event_data_by_type_index[t] = data_vec;
789     data_to_be_written_by_caller = data_vec + l * n_data_elt_bytes;
790   }
791
792   p->non_empty_event_type_bitmap =
793     clib_bitmap_ori (p->non_empty_event_type_bitmap, t);
794
795   p_flags = p->flags;
796
797   /* Event was already signalled? */
798   add_to_pending = (p_flags & VLIB_PROCESS_RESUME_PENDING) == 0;
799
800   /* Process will resume when suspend time elapses? */
801   delete_from_wheel = 0;
802   if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
803     {
804       /* Waiting for both event and clock? */
805       if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)
806         delete_from_wheel = 1;
807       else
808         /* Waiting only for clock.  Event will be queue and may be
809            handled when timer expires. */
810         add_to_pending = 0;
811     }
812
813   /* Never add current process to pending vector since current process is
814      already running. */
815   add_to_pending &= nm->current_process_index != n->runtime_index;
816
817   if (add_to_pending)
818     {
819       u32 x = vlib_timing_wheel_data_set_suspended_process (n->runtime_index);
820       p->flags = p_flags | VLIB_PROCESS_RESUME_PENDING;
821       vec_add1 (nm->data_from_advancing_timing_wheel, x);
822       if (delete_from_wheel)
823         timing_wheel_delete (&nm->timing_wheel, x);
824     }
825
826   return data_to_be_written_by_caller;
827 }
828
829 always_inline void *
830 vlib_process_signal_event_data (vlib_main_t * vm,
831                                 uword node_index,
832                                 uword type_opaque,
833                                 uword n_data_elts, uword n_data_elt_bytes)
834 {
835   vlib_node_main_t *nm = &vm->node_main;
836   vlib_node_t *n = vlib_get_node (vm, node_index);
837   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
838   uword *h, t;
839
840   h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
841   if (!h)
842     {
843       vlib_process_event_type_t *et =
844         vlib_process_new_event_type (p, type_opaque);
845       t = et - p->event_type_pool;
846       hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
847     }
848   else
849     t = h[0];
850
851   return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
852                                            n_data_elt_bytes);
853 }
854
855 always_inline void *
856 vlib_process_signal_event_at_time (vlib_main_t * vm,
857                                    f64 dt,
858                                    uword node_index,
859                                    uword type_opaque,
860                                    uword n_data_elts, uword n_data_elt_bytes)
861 {
862   vlib_node_main_t *nm = &vm->node_main;
863   vlib_node_t *n = vlib_get_node (vm, node_index);
864   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
865   uword *h, t;
866
867   h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
868   if (!h)
869     {
870       vlib_process_event_type_t *et =
871         vlib_process_new_event_type (p, type_opaque);
872       t = et - p->event_type_pool;
873       hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
874     }
875   else
876     t = h[0];
877
878   if (vlib_process_suspend_time_is_zero (dt))
879     return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
880                                              n_data_elt_bytes);
881   else
882     {
883       vlib_signal_timed_event_data_t *te;
884       u64 dt_cpu = dt * vm->clib_time.clocks_per_second;
885
886       pool_get_aligned (nm->signal_timed_event_data_pool, te, sizeof (te[0]));
887
888       te->n_data_elts = n_data_elts;
889       te->n_data_elt_bytes = n_data_elt_bytes;
890       te->n_data_bytes = n_data_elts * n_data_elt_bytes;
891
892       /* Assert that structure fields are big enough. */
893       ASSERT (te->n_data_elts == n_data_elts);
894       ASSERT (te->n_data_elt_bytes == n_data_elt_bytes);
895       ASSERT (te->n_data_bytes == n_data_elts * n_data_elt_bytes);
896
897       te->process_node_index = n->runtime_index;
898       te->event_type_index = t;
899
900       timing_wheel_insert (&nm->timing_wheel, clib_cpu_time_now () + dt_cpu,
901                            vlib_timing_wheel_data_set_timed_event (te -
902                                                                    nm->
903                                                                    signal_timed_event_data_pool));
904
905       /* Inline data big enough to hold event? */
906       if (te->n_data_bytes < sizeof (te->inline_event_data))
907         return te->inline_event_data;
908       else
909         {
910           te->event_data_as_vector = 0;
911           vec_resize (te->event_data_as_vector, te->n_data_bytes);
912           return te->event_data_as_vector;
913         }
914     }
915 }
916
917 always_inline void *
918 vlib_process_signal_one_time_event_data (vlib_main_t * vm,
919                                          uword node_index,
920                                          uword type_index,
921                                          uword n_data_elts,
922                                          uword n_data_elt_bytes)
923 {
924   vlib_node_main_t *nm = &vm->node_main;
925   vlib_node_t *n = vlib_get_node (vm, node_index);
926   vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
927   return vlib_process_signal_event_helper (nm, n, p, type_index, n_data_elts,
928                                            n_data_elt_bytes);
929 }
930
931 always_inline void
932 vlib_process_signal_event (vlib_main_t * vm,
933                            uword node_index, uword type_opaque, uword data)
934 {
935   uword *d = vlib_process_signal_event_data (vm, node_index, type_opaque,
936                                              1 /* elts */ , sizeof (uword));
937   d[0] = data;
938 }
939
940 always_inline void
941 vlib_process_signal_event_pointer (vlib_main_t * vm,
942                                    uword node_index,
943                                    uword type_opaque, void *data)
944 {
945   void **d = vlib_process_signal_event_data (vm, node_index, type_opaque,
946                                              1 /* elts */ , sizeof (data));
947   d[0] = data;
948 }
949
950 always_inline void
951 vlib_process_signal_one_time_event (vlib_main_t * vm,
952                                     uword node_index,
953                                     uword type_index, uword data)
954 {
955   uword *d =
956     vlib_process_signal_one_time_event_data (vm, node_index, type_index,
957                                              1 /* elts */ , sizeof (uword));
958   d[0] = data;
959 }
960
961 always_inline void
962 vlib_signal_one_time_waiting_process (vlib_main_t * vm,
963                                       vlib_one_time_waiting_process_t * p)
964 {
965   vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event,
966                                       /* data */ ~0);
967   memset (p, ~0, sizeof (p[0]));
968 }
969
970 always_inline void
971 vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm,
972                                              vlib_one_time_waiting_process_t
973                                              ** wps)
974 {
975   vlib_one_time_waiting_process_t *wp;
976   vec_foreach (wp, *wps) vlib_signal_one_time_waiting_process (vm, wp);
977   vec_free (*wps);
978 }
979
980 always_inline void
981 vlib_current_process_wait_for_one_time_event (vlib_main_t * vm,
982                                               vlib_one_time_waiting_process_t
983                                               * p)
984 {
985   p->node_index = vlib_current_process (vm);
986   p->one_time_event = vlib_process_create_one_time_event (vm, p->node_index,    /* type opaque */
987                                                           ~0);
988   vlib_process_wait_for_one_time_event (vm,
989                                         /* don't care about data */ 0,
990                                         p->one_time_event);
991 }
992
993 always_inline void
994 vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm,
995                                                      vlib_one_time_waiting_process_t
996                                                      ** wps)
997 {
998   vlib_one_time_waiting_process_t *wp;
999   vec_add2 (*wps, wp, 1);
1000   vlib_current_process_wait_for_one_time_event (vm, wp);
1001 }
1002
1003 always_inline u32
1004 vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm,
1005                                                  vlib_node_runtime_t * node,
1006                                                  uword n_vectors)
1007 {
1008   u32 i, d, vi0, vi1;
1009   u32 i0, i1;
1010
1011   ASSERT (is_pow2 (ARRAY_LEN (node->main_loop_vector_stats)));
1012   i = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
1013        & (ARRAY_LEN (node->main_loop_vector_stats) - 1));
1014   i0 = i ^ 0;
1015   i1 = i ^ 1;
1016   d = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
1017        -
1018        (node->main_loop_count_last_dispatch >>
1019         VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE));
1020   vi0 = node->main_loop_vector_stats[i0];
1021   vi1 = node->main_loop_vector_stats[i1];
1022   vi0 = d == 0 ? vi0 : 0;
1023   vi1 = d <= 1 ? vi1 : 0;
1024   vi0 += n_vectors;
1025   node->main_loop_vector_stats[i0] = vi0;
1026   node->main_loop_vector_stats[i1] = vi1;
1027   node->main_loop_count_last_dispatch = vm->main_loop_count;
1028   /* Return previous counter. */
1029   return node->main_loop_vector_stats[i1];
1030 }
1031
1032 always_inline f64
1033 vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index)
1034 {
1035   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
1036   u32 v;
1037
1038   v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt,  /* n_vectors */
1039                                                        0);
1040   return (f64) v / (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE);
1041 }
1042
1043 always_inline u32
1044 vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index)
1045 {
1046   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
1047   u32 v;
1048
1049   v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt,  /* n_vectors */
1050                                                        0);
1051   return v >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE;
1052 }
1053
1054 void
1055 vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f);
1056
1057 /* Add next node to given node in given slot. */
1058 uword
1059 vlib_node_add_next_with_slot (vlib_main_t * vm,
1060                               uword node, uword next_node, uword slot);
1061
1062 /* As above but adds to end of node's next vector. */
1063 always_inline uword
1064 vlib_node_add_next (vlib_main_t * vm, uword node, uword next_node)
1065 {
1066   return vlib_node_add_next_with_slot (vm, node, next_node, ~0);
1067 }
1068
1069 /* Add next node to given node in given slot. */
1070 uword
1071 vlib_node_add_named_next_with_slot (vlib_main_t * vm,
1072                                     uword node, char *next_name, uword slot);
1073
1074 /* As above but adds to end of node's next vector. */
1075 always_inline uword
1076 vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name)
1077 {
1078   return vlib_node_add_named_next_with_slot (vm, node, name, ~0);
1079 }
1080
1081 /* Query node given name. */
1082 vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name);
1083
1084 /* Rename a node. */
1085 void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...);
1086
1087 /* Register new packet processing node.  Nodes can be registered
1088    dynamically via this call or statically via the VLIB_REGISTER_NODE
1089    macro. */
1090 u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r);
1091
1092 /* Register all static nodes registered via VLIB_REGISTER_NODE. */
1093 void vlib_register_all_static_nodes (vlib_main_t * vm);
1094
1095 /* Start a process. */
1096 void vlib_start_process (vlib_main_t * vm, uword process_index);
1097
1098 /* Sync up runtime and main node stats. */
1099 void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
1100
1101 /* Node graph initialization function. */
1102 clib_error_t *vlib_node_main_init (vlib_main_t * vm);
1103
1104 format_function_t format_vlib_node_graph;
1105 format_function_t format_vlib_node_name;
1106 format_function_t format_vlib_next_node_name;
1107 format_function_t format_vlib_node_and_next;
1108 format_function_t format_vlib_cpu_time;
1109 format_function_t format_vlib_time;
1110 /* Parse node name -> node index. */
1111 unformat_function_t unformat_vlib_node;
1112
1113 always_inline void
1114 vlib_node_increment_counter (vlib_main_t * vm, u32 node_index,
1115                              u32 counter_index, u64 increment)
1116 {
1117   vlib_node_t *n = vlib_get_node (vm, node_index);
1118   vlib_error_main_t *em = &vm->error_main;
1119   u32 node_counter_base_index = n->error_heap_index;
1120   em->counters[node_counter_base_index + counter_index] += increment;
1121 }
1122
1123 #endif /* included_vlib_node_funcs_h */
1124
1125 /*
1126  * fd.io coding-style-patch-verification: ON
1127  *
1128  * Local Variables:
1129  * eval: (c-set-style "gnu")
1130  * End:
1131  */