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