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.h: VLIB processing nodes
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_h
41 #define included_vlib_node_h
43 #include <vppinfra/cpu.h>
44 #include <vppinfra/longjmp.h>
45 #include <vppinfra/lock.h>
46 #include <vlib/trace.h> /* for vlib_trace_filter_t */
48 /* Forward declaration. */
49 struct vlib_node_runtime_t;
52 /* Internal nodes (including output nodes) move data from node to
53 node (or out of the graph for output nodes). */
54 typedef uword (vlib_node_function_t) (struct vlib_main_t * vm,
55 struct vlib_node_runtime_t * node,
56 struct vlib_frame_t * frame);
60 /* An internal node on the call graph (could be output). */
61 VLIB_NODE_TYPE_INTERNAL,
63 /* Nodes which input data into the processing graph.
64 Input nodes are called for each iteration of main loop. */
67 /* Nodes to be called before all input nodes.
68 Used, for example, to clean out driver TX rings before
70 VLIB_NODE_TYPE_PRE_INPUT,
72 /* "Process" nodes which can be suspended and later resumed. */
73 VLIB_NODE_TYPE_PROCESS,
78 typedef struct _vlib_node_fn_registration
80 vlib_node_function_t *function;
82 struct _vlib_node_fn_registration *next_registration;
84 } vlib_node_fn_registration_t;
86 typedef struct _vlib_node_registration
88 /* Vector processing function for this node. */
89 vlib_node_function_t *function;
91 /* Node function candidate registration with priority */
92 vlib_node_fn_registration_t *node_fn_registrations;
97 /* Name of sibling (if applicable). */
100 /* Node index filled in by registration. */
103 /* Type of this node. */
104 vlib_node_type_t type;
106 /* Error strings indexed by error code for this node. */
107 char **error_strings;
109 /* Buffer format/unformat for this node. */
110 format_function_t *format_buffer;
111 unformat_function_t *unformat_buffer;
113 /* Trace format/unformat for this node. */
114 format_function_t *format_trace;
115 unformat_function_t *unformat_trace;
117 /* Function to validate incoming frames. */
118 u8 *(*validate_frame) (struct vlib_main_t * vm,
119 struct vlib_node_runtime_t *,
120 struct vlib_frame_t * f);
122 /* Per-node runtime data. */
125 /* Process stack size. */
126 u16 process_log2_n_stack_bytes;
128 /* Number of bytes of per-node run time data. */
129 u8 runtime_data_bytes;
131 /* State for input nodes. */
137 /* Size of scalar and vector arguments in bytes. */
138 u16 scalar_size, vector_size;
140 /* Number of error codes used by this node. */
143 /* Number of next node names that follow. */
146 /* Constructor link-list, don't ask... */
147 struct _vlib_node_registration *next_registration;
149 /* Names of next nodes which this node feeds into. */
152 } vlib_node_registration_t;
154 #ifndef CLIB_MARCH_VARIANT
155 #define VLIB_REGISTER_NODE(x,...) \
156 __VA_ARGS__ vlib_node_registration_t x; \
157 static void __vlib_add_node_registration_##x (void) \
158 __attribute__((__constructor__)) ; \
159 static void __vlib_add_node_registration_##x (void) \
161 vlib_main_t * vm = vlib_get_main(); \
162 x.next_registration = vm->node_main.node_registrations; \
163 vm->node_main.node_registrations = &x; \
165 static void __vlib_rm_node_registration_##x (void) \
166 __attribute__((__destructor__)) ; \
167 static void __vlib_rm_node_registration_##x (void) \
169 vlib_main_t * vm = vlib_get_main(); \
170 VLIB_REMOVE_FROM_LINKED_LIST (vm->node_main.node_registrations, \
171 &x, next_registration); \
173 __VA_ARGS__ vlib_node_registration_t x
175 #define VLIB_REGISTER_NODE(x,...) \
176 static __clib_unused vlib_node_registration_t __clib_unused_##x
179 #ifndef CLIB_MARCH_VARIANT
180 #define CLIB_MARCH_VARIANT_STR "default"
182 #define _CLIB_MARCH_VARIANT_STR(s) __CLIB_MARCH_VARIANT_STR(s)
183 #define __CLIB_MARCH_VARIANT_STR(s) #s
184 #define CLIB_MARCH_VARIANT_STR _CLIB_MARCH_VARIANT_STR(CLIB_MARCH_VARIANT)
187 #define VLIB_NODE_FN(node) \
188 uword CLIB_MARCH_SFX (node##_fn)(); \
189 static vlib_node_fn_registration_t \
190 CLIB_MARCH_SFX(node##_fn_registration) = \
191 { .function = &CLIB_MARCH_SFX (node##_fn), }; \
193 static void __clib_constructor \
194 CLIB_MARCH_SFX (node##_multiarch_register) (void) \
196 extern vlib_node_registration_t node; \
197 vlib_node_fn_registration_t *r; \
198 r = & CLIB_MARCH_SFX (node##_fn_registration); \
199 r->priority = CLIB_MARCH_FN_PRIORITY(); \
200 r->name = CLIB_MARCH_VARIANT_STR; \
201 r->next_registration = node.node_fn_registrations; \
202 node.node_fn_registrations = r; \
204 uword CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (node##_fn)
207 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn)
208 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)
209 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
211 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
213 __attribute__ ((flatten)) \
214 __attribute__ ((target (tgt))) \
216 fn ## _ ## arch ( struct vlib_main_t * vm, \
217 struct vlib_node_runtime_t * node, \
218 struct vlib_frame_t * frame) \
219 { return fn (vm, node, frame); }
221 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
222 foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
224 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn) \
225 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
226 CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
227 static void __attribute__((__constructor__)) \
228 __vlib_node_function_multiarch_select_##node (void) \
229 { node.function = fn ## _multiarch_select(); }
232 always_inline vlib_node_registration_t *
233 vlib_node_next_registered (vlib_node_registration_t * c)
236 clib_elf_section_data_next (c,
237 c->n_next_nodes * sizeof (c->next_nodes[0]));
243 /* Total calls, clock ticks and vector elements processed for this node. */
244 u64 calls, vectors, clocks, suspends;
249 #define foreach_vlib_node_state \
250 /* Input node is called each iteration of main loop. \
251 This is the default (zero). */ \
253 /* Input node is called when device signals an interrupt. */ \
255 /* Input node is never called. */ \
260 #define _(f) VLIB_NODE_STATE_##f,
261 foreach_vlib_node_state
266 typedef struct vlib_node_t
268 /* Vector processing function for this node. */
269 vlib_node_function_t *function;
274 /* Node name index in elog string table. */
275 u32 name_elog_string;
277 /* Total statistics for this node. */
278 vlib_node_stats_t stats_total;
280 /* Saved values as of last clear (or zero if never cleared).
281 Current values are always stats_total - stats_last_clear. */
282 vlib_node_stats_t stats_last_clear;
284 /* Type of this node. */
285 vlib_node_type_t type;
290 /* Index of corresponding node runtime. */
293 /* Runtime data for this node. */
299 /* Processing function keeps frame. Tells node dispatching code not
300 to free frame after dispatch is done. */
301 #define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
303 /* Node counts as output/drop/punt node for stats purposes. */
304 #define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
305 #define VLIB_NODE_FLAG_IS_DROP (1 << 2)
306 #define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
307 #define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
309 /* Set if current node runtime has traced vectors. */
310 #define VLIB_NODE_FLAG_TRACE (1 << 5)
312 #define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
313 #define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
315 /* State for input nodes. */
318 /* Number of bytes of run time data. */
319 u8 runtime_data_bytes;
321 /* Number of error codes used by this node. */
324 /* Size of scalar and vector arguments in bytes. */
325 u16 scalar_size, vector_size;
327 /* Handle/index in error heap for this node. */
328 u32 error_heap_handle;
329 u32 error_heap_index;
331 /* Error strings indexed by error code for this node. */
332 char **error_strings;
334 /* Vector of next node names.
335 Only used before next_nodes array is initialized. */
336 char **next_node_names;
338 /* Next node indices for this node. */
341 /* Name of node that we are sibling of. */
344 /* Bitmap of all of this node's siblings. */
345 uword *sibling_bitmap;
347 /* Total number of vectors sent to each next node. */
348 u64 *n_vectors_by_next_node;
350 /* Hash table mapping next node index into slot in
351 next_nodes vector. Quickly determines whether this node
352 is connected to given next node and, if so, with which slot. */
353 uword *next_slot_by_node;
355 /* Bitmap of node indices which feed this node. */
356 uword *prev_node_bitmap;
358 /* Node/next-index which own enqueue rights with to this node. */
359 u32 owner_node_index, owner_next_index;
361 /* Buffer format/unformat for this node. */
362 format_function_t *format_buffer;
363 unformat_function_t *unformat_buffer;
365 /* Trace buffer format/unformat for this node. */
366 format_function_t *format_trace;
368 /* Function to validate incoming frames. */
369 u8 *(*validate_frame) (struct vlib_main_t * vm,
370 struct vlib_node_runtime_t *,
371 struct vlib_frame_t * f);
372 /* for pretty-printing, not typically valid */
375 /* Node function candidate registration with priority */
376 vlib_node_fn_registration_t *node_fn_registrations;
379 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
381 /* Max number of vector elements to process at once per node. */
382 #define VLIB_FRAME_SIZE 256
383 #define VLIB_FRAME_ALIGN CLIB_CACHE_LINE_BYTES
385 /* Calling frame (think stack frame) for a node. */
386 typedef struct vlib_frame_t
391 /* Number of scalar bytes in arguments. */
394 /* Number of bytes per vector argument. */
397 /* Number of vector elements currently in frame. */
400 /* Scalar and vector arguments to next node. */
409 /* Node runtime for this next. */
410 u32 node_runtime_index;
412 /* Next frame flags. */
415 /* Reflects node frame-used flag for this next. */
416 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
417 VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
419 /* This next frame owns enqueue to node
420 corresponding to node_runtime_index. */
421 #define VLIB_FRAME_OWNER (1 << 15)
423 /* Set when frame has been allocated for this next. */
424 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
426 /* Set when frame has been added to pending vector. */
427 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
429 /* Set when frame is to be freed after dispatch. */
430 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
432 /* Set when frame has traced packets. */
433 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
435 /* Number of vectors enqueue to this next since last overflow. */
436 u32 vectors_since_last_overflow;
440 vlib_next_frame_init (vlib_next_frame_t * nf)
442 memset (nf, 0, sizeof (nf[0]));
443 nf->frame_index = ~0;
444 nf->node_runtime_index = ~0;
447 /* A frame pending dispatch by main loop. */
450 /* Node and runtime for this frame. */
451 u32 node_runtime_index;
453 /* Frame index (in the heap). */
456 /* Start of next frames for this node. */
457 u32 next_frame_index;
459 /* Special value for next_frame_index when there is no next frame. */
460 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
461 } vlib_pending_frame_t;
463 typedef struct vlib_node_runtime_t
465 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); /**< cacheline mark */
467 vlib_node_function_t *function; /**< Node function to call. */
469 vlib_error_t *errors; /**< Vector of errors for this node. */
471 #if __SIZEOF_POINTER__ == 4
475 u32 clocks_since_last_overflow; /**< Number of clock cycles. */
477 u32 max_clock; /**< Maximum clock cycle for an
480 u32 max_clock_n; /**< Number of vectors in the recorded
483 u32 calls_since_last_overflow; /**< Number of calls. */
485 u32 vectors_since_last_overflow; /**< Number of vector elements
486 processed by this node. */
488 u32 next_frame_index; /**< Start of next frames for this
491 u32 node_index; /**< Node index. */
493 u32 input_main_loops_per_call; /**< For input nodes: decremented
494 on each main loop interation until
495 it reaches zero and function is
496 called. Allows some input nodes to
497 be called more than others. */
499 u32 main_loop_count_last_dispatch; /**< Saved main loop counter of last
500 dispatch of this node. */
502 u32 main_loop_vector_stats[2];
504 u16 flags; /**< Copy of main node flags. */
506 u16 state; /**< Input node state. */
510 u16 cached_next_index; /**< Next frame index that vector
511 arguments were last enqueued to
512 last time this node ran. Set to
513 zero before first run of this
516 u16 thread_index; /**< thread this node runs on */
518 u8 runtime_data[0]; /**< Function dependent
519 node-runtime data. This data is
520 thread local, and it is not
521 cloned from main thread. It needs
522 to be initialized for each thread
523 before it is used unless
524 runtime_data template exists in
529 #define VLIB_NODE_RUNTIME_DATA_SIZE (sizeof (vlib_node_runtime_t) - STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data))
533 /* Number of allocated frames for this scalar/vector size. */
536 /* Vector of free frame indices for this scalar/vector size. */
537 u32 *free_frame_indices;
542 /* Users opaque value for event type. */
544 } vlib_process_event_type_t;
548 /* Node runtime for this process. */
549 vlib_node_runtime_t node_runtime;
551 /* Where to longjmp when process is done. */
552 clib_longjmp_t return_longjmp;
554 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
555 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
557 /* Where to longjmp to resume node after suspend. */
558 clib_longjmp_t resume_longjmp;
559 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
560 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME 1
563 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
564 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
565 /* Set to indicate that this process has been added to resume vector. */
566 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
568 /* Process function is currently running. */
569 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
571 /* Size of process stack. */
572 u16 log2_n_stack_bytes;
574 u32 suspended_process_frame_index;
576 /* Number of times this process was suspended. */
579 /* Vectors of pending event data indexed by event type index. */
580 void **pending_event_data_by_type_index;
582 /* Bitmap of event type-indices with non-empty vectors. */
583 uword *non_empty_event_type_bitmap;
585 /* Bitmap of event type-indices which are one time events. */
586 uword *one_time_event_type_bitmap;
588 /* Type is opaque pointer -- typically a pointer to an event handler
589 function. Hash table to map opaque to a type index. */
590 uword *event_type_index_by_type_opaque;
592 /* Pool of currently valid event types. */
593 vlib_process_event_type_t *event_type_pool;
596 * When suspending saves clock time (10us ticks) when process
599 u64 resume_clock_interval;
601 /* Handle from timer code, to cancel an unexpired timer */
602 u32 stop_timer_handle;
604 /* Default output function and its argument for any CLI outputs
605 within the process. */
606 vlib_cli_output_function_t *output_function;
607 uword output_function_arg;
610 /* Pad to a multiple of the page size so we can mprotect process stacks */
611 #define PAGE_SIZE_MULTIPLE 0x1000
612 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT __attribute__ ((aligned (PAGE_SIZE_MULTIPLE)))
614 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT
617 /* Process stack. Starts here and extends 2^log2_n_stack_bytes
620 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
621 u32 stack[0] ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT;
622 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
625 /* Ensure that the stack is aligned on the multiple of the page size */
627 assert_process_stack_must_be_aligned_exactly_to_page_size_multiple[(sizeof
641 } vlib_one_time_waiting_process_t;
647 u16 n_data_elt_bytes;
649 /* n_data_elts * n_data_elt_bytes */
652 /* Process node & event type to be used to signal event. */
653 u32 process_node_index;
655 u32 event_type_index;
659 u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
661 /* Vector of event data used only when data does not fit inline. */
662 u8 *event_data_as_vector;
665 vlib_signal_timed_event_data_t;
668 vlib_timing_wheel_data_is_timed_event (u32 d)
674 vlib_timing_wheel_data_set_suspended_process (u32 i)
680 vlib_timing_wheel_data_set_timed_event (u32 i)
686 vlib_timing_wheel_data_get_index (u32 d)
696 /* Node index hashed by node name. */
700 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
702 /* Nodes segregated by type for cache locality.
703 Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
704 vlib_node_runtime_t *nodes_by_type[VLIB_N_NODE_TYPE];
706 /* Node runtime indices for input nodes with pending interrupts. */
707 u32 *pending_interrupt_node_runtime_indices;
708 clib_spinlock_t pending_interrupt_lock;
710 /* Input nodes are switched from/to interrupt to/from polling mode
711 when average vector length goes above/below polling/interrupt
713 u32 polling_threshold_vector_length;
714 u32 interrupt_threshold_vector_length;
716 /* Vector of next frames. */
717 vlib_next_frame_t *next_frames;
719 /* Vector of internal node's frames waiting to be called. */
720 vlib_pending_frame_t *pending_frames;
722 /* Timing wheel for scheduling time-based node dispatch. */
725 vlib_signal_timed_event_data_t *signal_timed_event_data_pool;
727 /* Opaque data vector added via timing_wheel_advance. */
728 u32 *data_from_advancing_timing_wheel;
730 /* CPU time of next process to be ready on timing wheel. */
731 f64 time_next_process_ready;
733 /* Vector of process nodes.
734 One for each node of type VLIB_NODE_TYPE_PROCESS. */
735 vlib_process_t **processes;
737 /* Current running process or ~0 if no process running. */
738 u32 current_process_index;
740 /* Pool of pending process frames. */
741 vlib_pending_frame_t *suspended_process_frames;
743 /* Vector of event data vectors pending recycle. */
744 void **recycled_event_data_vectors;
746 /* Current counts of nodes in each state. */
747 u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
749 /* Hash of (scalar_size,vector_size) to frame_sizes index. */
750 uword *frame_size_hash;
752 /* Per-size frame allocation information. */
753 vlib_frame_size_t *frame_sizes;
755 /* Time of last node runtime stats clear. */
756 f64 time_last_runtime_stats_clear;
758 /* Node registrations added by constructors */
759 vlib_node_registration_t *node_registrations;
763 #define FRAME_QUEUE_MAX_NELTS 32
766 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
774 i32 n_vectors[FRAME_QUEUE_MAX_NELTS];
775 } frame_queue_trace_t;
779 u64 count[FRAME_QUEUE_MAX_NELTS];
780 } frame_queue_nelt_counter_t;
782 #endif /* included_vlib_node_h */
785 * fd.io coding-style-patch-verification: ON
788 * eval: (c-set-style "gnu")