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