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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * node_funcs.h: processing nodes global functions/inlines
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_vlib_node_funcs_h
41 #define included_vlib_node_funcs_h
43 #include <vppinfra/fifo.h>
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); }
49 always_inline vlib_node_t *
50 vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index)
52 vlib_node_main_t * nm = &vm->node_main;
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]);
60 always_inline vlib_node_runtime_t *
61 vlib_node_get_runtime (vlib_main_t * vm, u32 node_index)
63 vlib_node_main_t * nm = &vm->node_main;
64 vlib_node_t * n = vec_elt (nm->nodes, node_index);
66 if (n->type != VLIB_NODE_TYPE_PROCESS)
67 return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
70 p = vec_elt (nm->processes, n->runtime_index);
71 return &p->node_runtime;
76 vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index)
78 vlib_node_runtime_t * r = vlib_node_get_runtime (vm, node_index);
79 return r->runtime_data;
83 vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index,
85 u32 n_runtime_data_bytes)
87 vlib_node_t * n = vlib_get_node (vm, node_index);
88 vlib_node_runtime_t * r = vlib_node_get_runtime (vm, node_index);
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);
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));
100 vlib_node_set_state (vlib_main_t * vm, u32 node_index, vlib_node_state_t new_state)
102 vlib_node_main_t * nm = &vm->node_main;
104 vlib_node_runtime_t * r;
106 n = vec_elt (nm->nodes, node_index);
107 if (n->type == VLIB_NODE_TYPE_PROCESS)
109 vlib_process_t * p = vec_elt (nm->processes, n->runtime_index);
110 r = &p->node_runtime;
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);
118 r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
120 ASSERT (new_state < VLIB_N_NODE_STATE);
122 if (n->type == VLIB_NODE_TYPE_INPUT)
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;
129 n->state = new_state;
130 r->state = new_state;
134 vlib_node_set_interrupt_pending (vlib_main_t * vm, u32 node_index)
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);
142 always_inline vlib_process_t *
143 vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node)
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);
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)
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;
163 vlib_frame_index_no_check (vlib_main_t * vm, vlib_frame_t * f)
167 ASSERT (((uword)f & VLIB_CPU_MASK)==0);
169 vm = vlib_mains ? vlib_mains[f->cpu_index] : vm;
171 i = ((u8 *) f - (u8 *) vm->heap_base);
172 return i | f->cpu_index;
175 always_inline vlib_frame_t *
176 vlib_get_frame (vlib_main_t * vm, uword frame_index)
178 vlib_frame_t * f = vlib_get_frame_no_check (vm, frame_index);
179 ASSERT (f->flags & VLIB_FRAME_IS_ALLOCATED);
184 vlib_frame_index (vlib_main_t * vm, vlib_frame_t * f)
186 uword i = vlib_frame_index_no_check (vm, f);
187 ASSERT (vlib_get_frame (vm, i) == f);
191 /* Byte alignment for vector arguments. */
192 #define VLIB_FRAME_VECTOR_ALIGN (1 << 4)
195 vlib_frame_vector_byte_offset (u32 scalar_size)
197 return round_pow2 (sizeof (vlib_frame_t) + scalar_size,
198 VLIB_FRAME_VECTOR_ALIGN);
202 vlib_frame_vector_args (vlib_frame_t * f)
204 return (void *) f + vlib_frame_vector_byte_offset (f->scalar_size);
207 /* Scalar data lies before aligned vector data. */
209 vlib_frame_args (vlib_frame_t * f)
210 { return vlib_frame_vector_args (f) - f->scalar_size; }
212 always_inline vlib_next_frame_t *
213 vlib_node_runtime_get_next_frame (vlib_main_t * vm,
214 vlib_node_runtime_t * n,
217 vlib_node_main_t * nm = &vm->node_main;
218 vlib_next_frame_t * nf;
220 ASSERT (next_index < n->n_next_nodes);
221 nf = vec_elt_at_index (nm->next_frames,
222 n->next_frame_index + next_index);
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);
235 always_inline vlib_next_frame_t *
236 vlib_node_get_next_frame (vlib_main_t * vm,
240 vlib_node_main_t * nm = &vm->node_main;
242 vlib_node_runtime_t * r;
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);
250 vlib_get_next_frame_internal (vlib_main_t * vm,
251 vlib_node_runtime_t * node,
253 u32 alloc_new_frame);
255 #define vlib_get_next_frame_macro(vm,node,next_index,vectors,n_vectors_left,alloc_new_frame) \
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; \
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)
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)
276 vlib_put_next_frame (vlib_main_t * vm,
277 vlib_node_runtime_t * r,
281 /* Combination get plus put. Returns vector argument just added. */
282 #define vlib_set_next_frame(vm,node,next_index,v) \
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); \
292 vlib_set_next_frame_buffer (vlib_main_t * vm,
293 vlib_node_runtime_t * node,
298 p = vlib_set_next_frame (vm, node, next_index, p);
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);
305 always_inline vlib_process_t *
306 vlib_get_current_process (vlib_main_t * vm)
308 vlib_node_main_t * nm = &vm->node_main;
309 return vec_elt (nm->processes, nm->current_process_index);
313 vlib_in_process_context (vlib_main_t * vm)
314 { return vm->node_main.current_process_index != ~0; }
317 vlib_current_process (vlib_main_t * vm)
318 { return vlib_get_current_process (vm)->node_runtime.node_index; }
320 /* Anything less than 1e-6 is considered zero. */
322 vlib_process_suspend_time_is_zero (f64 dt)
323 { return dt < 1e-6; }
326 vlib_process_suspend (vlib_main_t * vm, f64 dt)
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;
333 if (vlib_process_suspend_time_is_zero (dt))
334 return VLIB_PROCESS_RESUME_LONGJMP_RESUME;
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)
340 p->resume_cpu_time = clib_cpu_time_now () + dt_cpu;
341 clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
348 vlib_process_free_event_type (vlib_process_t * p, uword t, uword is_one_time_event)
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);
358 vlib_process_maybe_free_event_type (vlib_process_t * p, uword t)
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);
366 vlib_process_get_event_data (vlib_main_t * vm, uword * return_event_type_opaque)
368 vlib_node_main_t * nm = &vm->node_main;
370 vlib_process_event_type_t * et;
372 void * event_data_vector;
374 p = vec_elt (nm->processes, nm->current_process_index);
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);
382 p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
384 l = _vec_len (p->pending_event_data_by_type_index[t]);
386 event_data_vector = p->pending_event_data_by_type_index[t];
387 p->pending_event_data_by_type_index[t] = 0;
389 et = pool_elt_at_index (p->event_type_pool, t);
391 /* Return user's opaque value and possibly index. */
392 *return_event_type_opaque = et->opaque;
394 vlib_process_maybe_free_event_type (p, t);
396 return event_data_vector;
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. */
402 vlib_process_put_event_data (vlib_main_t * vm, void * event_data)
404 vlib_node_main_t * nm = &vm->node_main;
405 vec_add1 (nm->recycled_event_data_vectors, event_data);
408 /* Return type & add any events to data vector. */
410 vlib_process_get_events (vlib_main_t * vm, uword ** data_vector)
412 vlib_node_main_t * nm = &vm->node_main;
414 vlib_process_event_type_t * et;
417 p = vec_elt (nm->processes, nm->current_process_index);
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);
425 p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
427 l = _vec_len (p->pending_event_data_by_type_index[t]);
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;
432 et = pool_elt_at_index (p->event_type_pool, t);
434 /* Return user's opaque value. */
437 vlib_process_maybe_free_event_type (p, t);
443 vlib_process_get_events_helper (vlib_process_t * p, uword t, uword ** data_vector)
447 p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
449 l = _vec_len (p->pending_event_data_by_type_index[t]);
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;
454 vlib_process_maybe_free_event_type (p, t);
459 /* As above but query as specified type of event. Returns number of
462 vlib_process_get_events_with_type (vlib_main_t * vm, uword ** data_vector,
463 uword with_type_opaque)
465 vlib_node_main_t * nm = &vm->node_main;
469 p = vec_elt (nm->processes, nm->current_process_index);
470 h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
472 /* This can happen when an event has not yet been
473 signaled with given opaque type. */
477 if (! clib_bitmap_get (p->non_empty_event_type_bitmap, t))
480 return vlib_process_get_events_helper (p, t, data_vector);
483 always_inline uword *
484 vlib_process_wait_for_event (vlib_main_t * vm)
486 vlib_node_main_t * nm = &vm->node_main;
490 p = vec_elt (nm->processes, nm->current_process_index);
491 if (clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
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);
499 return p->non_empty_event_type_bitmap;
503 vlib_process_wait_for_one_time_event (vlib_main_t * vm,
504 uword ** data_vector,
505 uword with_type_index)
507 vlib_node_main_t * nm = &vm->node_main;
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))
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);
521 return vlib_process_get_events_helper (p, with_type_index, data_vector);
525 vlib_process_wait_for_event_with_type (vlib_main_t * vm,
526 uword ** data_vector,
527 uword with_type_opaque)
529 vlib_node_main_t * nm = &vm->node_main;
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]))
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);
542 /* See if unknown event type has been signaled now. */
544 h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
547 return vlib_process_get_events_helper (p, h[0], data_vector);
551 vlib_process_wait_for_event_or_clock (vlib_main_t * vm, f64 dt)
553 vlib_node_main_t * nm = &vm->node_main;
558 p = vec_elt (nm->processes, nm->current_process_index);
560 if (vlib_process_suspend_time_is_zero (dt)
561 || ! clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
564 wakeup_time = vlib_time_now (vm) + dt;
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);
570 r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
571 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
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);
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);
583 always_inline vlib_process_event_type_t *
584 vlib_process_new_event_type (vlib_process_t * p, uword with_type_opaque)
586 vlib_process_event_type_t * et;
587 pool_get (p->event_type_pool, et);
588 et->opaque = with_type_opaque;
593 vlib_process_create_one_time_event (vlib_main_t * vm, uword node_index, uword with_type_opaque)
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;
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);
608 vlib_process_delete_one_time_event (vlib_main_t * vm, uword node_index, uword t)
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);
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);
619 vlib_process_signal_event_helper (vlib_node_main_t * nm,
624 uword n_data_elt_bytes)
626 uword p_flags, add_to_pending, delete_from_wheel;
627 void * data_to_be_written_by_caller;
629 ASSERT (! pool_is_free_index (p->event_type_pool, t));
631 vec_validate (p->pending_event_data_by_type_index, t);
633 /* Resize data vector and return caller's data to be written. */
635 void * data_vec = p->pending_event_data_by_type_index[t];
638 if (! data_vec && vec_len (nm->recycled_event_data_vectors))
640 data_vec = vec_pop (nm->recycled_event_data_vectors);
641 _vec_len (data_vec) = 0;
644 l = vec_len (data_vec);
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);
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;
655 p->non_empty_event_type_bitmap = clib_bitmap_ori (p->non_empty_event_type_bitmap, t);
659 /* Event was already signalled? */
660 add_to_pending = (p_flags & VLIB_PROCESS_RESUME_PENDING) == 0;
662 /* Process will resume when suspend time elapses? */
663 delete_from_wheel = 0;
664 if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
666 /* Waiting for both event and clock? */
667 if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)
668 delete_from_wheel = 1;
670 /* Waiting only for clock. Event will be queue and may be
671 handled when timer expires. */
675 /* Never add current process to pending vector since current process is
677 add_to_pending &= nm->current_process_index != n->runtime_index;
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);
688 return data_to_be_written_by_caller;
692 vlib_process_signal_event_data (vlib_main_t * vm,
696 uword n_data_elt_bytes)
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);
703 h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
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);
713 return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts, n_data_elt_bytes);
717 vlib_process_signal_event_at_time (vlib_main_t * vm,
722 uword n_data_elt_bytes)
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);
729 h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
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);
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);
743 vlib_signal_timed_event_data_t * te;
744 u64 dt_cpu = dt * vm->clib_time.clocks_per_second;
746 pool_get_aligned (nm->signal_timed_event_data_pool, te, sizeof (te[0]));
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;
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);
757 te->process_node_index = n->runtime_index;
758 te->event_type_index = t;
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));
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;
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;
776 vlib_process_signal_one_time_event_data (vlib_main_t * vm,
780 uword n_data_elt_bytes)
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);
789 vlib_process_signal_event (vlib_main_t * vm,
794 uword * d = vlib_process_signal_event_data (vm, node_index, type_opaque,
795 1 /* elts */, sizeof (uword));
800 vlib_process_signal_event_pointer (vlib_main_t * vm,
805 void ** d = vlib_process_signal_event_data (vm, node_index, type_opaque,
806 1 /* elts */, sizeof (data));
811 vlib_process_signal_one_time_event (vlib_main_t * vm,
816 uword * d = vlib_process_signal_one_time_event_data (vm, node_index, type_index,
817 1 /* elts */, sizeof (uword));
822 vlib_signal_one_time_waiting_process (vlib_main_t * vm, vlib_one_time_waiting_process_t * p)
824 vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event, /* data */ ~0);
825 memset (p, ~0, sizeof (p[0]));
829 vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm,
830 vlib_one_time_waiting_process_t ** wps)
832 vlib_one_time_waiting_process_t * wp;
833 vec_foreach (wp, *wps)
834 vlib_signal_one_time_waiting_process (vm, wp);
839 vlib_current_process_wait_for_one_time_event (vlib_main_t * vm, vlib_one_time_waiting_process_t * p)
841 p->node_index = vlib_current_process (vm);
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,
850 vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm,
851 vlib_one_time_waiting_process_t ** wps)
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);
859 vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm,
860 vlib_node_runtime_t * node,
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));
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;
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];
886 vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index)
888 vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, node_index);
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);
896 vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index)
898 vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, node_index);
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;
906 vlib_frame_free (vlib_main_t * vm,
907 vlib_node_runtime_t * r,
910 /* Add next node to given node in given slot. */
912 vlib_node_add_next_with_slot (vlib_main_t * vm,
917 /* As above but adds to end of node's next vector. */
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); }
922 /* Add next node to given node in given slot. */
924 vlib_node_add_named_next_with_slot (vlib_main_t * vm,
929 /* As above but adds to end of node's next vector. */
931 vlib_node_add_named_next (vlib_main_t * vm,
934 { return vlib_node_add_named_next_with_slot (vm, node, name, ~0); }
936 /* Query node given name. */
937 vlib_node_t * vlib_get_node_by_name (vlib_main_t * vm, u8 * name);
940 void vlib_node_rename (vlib_main_t * vm, u32 node_index, char * fmt, ...);
942 /* Register new packet processing node. Nodes can be registered
943 dynamically via this call or statically via the VLIB_REGISTER_NODE
945 u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r);
947 /* Register all static nodes registered via VLIB_REGISTER_NODE. */
948 void vlib_register_all_static_nodes (vlib_main_t * vm);
950 /* Start a process. */
951 void vlib_start_process (vlib_main_t * vm, uword process_index);
953 /* Sync up runtime and main node stats. */
955 vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
957 /* Node graph initialization function. */
958 clib_error_t * vlib_node_main_init (vlib_main_t * vm);
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;
970 vlib_node_increment_counter (vlib_main_t *vm, u32 node_index,
971 u32 counter_index, u64 increment)
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;
979 #endif /* included_vlib_node_funcs_h */