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_registration
80 /* Vector processing function for this node. */
81 vlib_node_function_t *function;
86 /* Name of sibling (if applicable). */
89 /* Node index filled in by registration. */
92 /* Type of this node. */
93 vlib_node_type_t type;
95 /* Error strings indexed by error code for this node. */
98 /* Buffer format/unformat for this node. */
99 format_function_t *format_buffer;
100 unformat_function_t *unformat_buffer;
102 /* Trace format/unformat for this node. */
103 format_function_t *format_trace;
104 unformat_function_t *unformat_trace;
106 /* Function to validate incoming frames. */
107 u8 *(*validate_frame) (struct vlib_main_t * vm,
108 struct vlib_node_runtime_t *,
109 struct vlib_frame_t * f);
111 /* Per-node runtime data. */
114 /* Process stack size. */
115 u16 process_log2_n_stack_bytes;
117 /* Number of bytes of per-node run time data. */
118 u8 runtime_data_bytes;
120 /* State for input nodes. */
126 /* Size of scalar and vector arguments in bytes. */
127 u16 scalar_size, vector_size;
129 /* Number of error codes used by this node. */
132 /* Number of next node names that follow. */
135 /* Constructor link-list, don't ask... */
136 struct _vlib_node_registration *next_registration;
138 /* Names of next nodes which this node feeds into. */
141 } vlib_node_registration_t;
143 #define VLIB_REGISTER_NODE(x,...) \
144 __VA_ARGS__ vlib_node_registration_t x; \
145 static void __vlib_add_node_registration_##x (void) \
146 __attribute__((__constructor__)) ; \
147 static void __vlib_add_node_registration_##x (void) \
149 vlib_main_t * vm = vlib_get_main(); \
150 x.next_registration = vm->node_main.node_registrations; \
151 vm->node_main.node_registrations = &x; \
153 static void __vlib_rm_node_registration_##x (void) \
154 __attribute__((__destructor__)) ; \
155 static void __vlib_rm_node_registration_##x (void) \
157 vlib_main_t * vm = vlib_get_main(); \
158 VLIB_REMOVE_FROM_LINKED_LIST (vm->node_main.node_registrations, \
159 &x, next_registration); \
161 __VA_ARGS__ vlib_node_registration_t x
164 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn)
165 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)
166 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
168 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
170 __attribute__ ((flatten)) \
171 __attribute__ ((target (tgt))) \
173 fn ## _ ## arch ( struct vlib_main_t * vm, \
174 struct vlib_node_runtime_t * node, \
175 struct vlib_frame_t * frame) \
176 { return fn (vm, node, frame); }
178 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
179 foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
181 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn) \
182 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
183 CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
184 static void __attribute__((__constructor__)) \
185 __vlib_node_function_multiarch_select_##node (void) \
186 { node.function = fn ## _multiarch_select(); }
189 always_inline vlib_node_registration_t *
190 vlib_node_next_registered (vlib_node_registration_t * c)
193 clib_elf_section_data_next (c,
194 c->n_next_nodes * sizeof (c->next_nodes[0]));
200 /* Total calls, clock ticks and vector elements processed for this node. */
201 u64 calls, vectors, clocks, suspends;
206 #define foreach_vlib_node_state \
207 /* Input node is called each iteration of main loop. \
208 This is the default (zero). */ \
210 /* Input node is called when device signals an interrupt. */ \
212 /* Input node is never called. */ \
217 #define _(f) VLIB_NODE_STATE_##f,
218 foreach_vlib_node_state
223 typedef struct vlib_node_t
225 /* Vector processing function for this node. */
226 vlib_node_function_t *function;
231 /* Node name index in elog string table. */
232 u32 name_elog_string;
234 /* Total statistics for this node. */
235 vlib_node_stats_t stats_total;
237 /* Saved values as of last clear (or zero if never cleared).
238 Current values are always stats_total - stats_last_clear. */
239 vlib_node_stats_t stats_last_clear;
241 /* Type of this node. */
242 vlib_node_type_t type;
247 /* Index of corresponding node runtime. */
250 /* Runtime data for this node. */
256 /* Processing function keeps frame. Tells node dispatching code not
257 to free frame after dispatch is done. */
258 #define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
260 /* Node counts as output/drop/punt node for stats purposes. */
261 #define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
262 #define VLIB_NODE_FLAG_IS_DROP (1 << 2)
263 #define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
264 #define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
266 /* Set if current node runtime has traced vectors. */
267 #define VLIB_NODE_FLAG_TRACE (1 << 5)
269 #define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
270 #define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
272 /* State for input nodes. */
275 /* Number of bytes of run time data. */
276 u8 runtime_data_bytes;
278 /* Number of error codes used by this node. */
281 /* Size of scalar and vector arguments in bytes. */
282 u16 scalar_size, vector_size;
284 /* Handle/index in error heap for this node. */
285 u32 error_heap_handle;
286 u32 error_heap_index;
288 /* Error strings indexed by error code for this node. */
289 char **error_strings;
291 /* Vector of next node names.
292 Only used before next_nodes array is initialized. */
293 char **next_node_names;
295 /* Next node indices for this node. */
298 /* Name of node that we are sibling of. */
301 /* Bitmap of all of this node's siblings. */
302 uword *sibling_bitmap;
304 /* Total number of vectors sent to each next node. */
305 u64 *n_vectors_by_next_node;
307 /* Hash table mapping next node index into slot in
308 next_nodes vector. Quickly determines whether this node
309 is connected to given next node and, if so, with which slot. */
310 uword *next_slot_by_node;
312 /* Bitmap of node indices which feed this node. */
313 uword *prev_node_bitmap;
315 /* Node/next-index which own enqueue rights with to this node. */
316 u32 owner_node_index, owner_next_index;
318 /* Buffer format/unformat for this node. */
319 format_function_t *format_buffer;
320 unformat_function_t *unformat_buffer;
322 /* Trace buffer format/unformat for this node. */
323 format_function_t *format_trace;
325 /* Function to validate incoming frames. */
326 u8 *(*validate_frame) (struct vlib_main_t * vm,
327 struct vlib_node_runtime_t *,
328 struct vlib_frame_t * f);
329 /* for pretty-printing, not typically valid */
333 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
335 /* Max number of vector elements to process at once per node. */
336 #define VLIB_FRAME_SIZE 256
337 #define VLIB_FRAME_ALIGN CLIB_CACHE_LINE_BYTES
339 /* Calling frame (think stack frame) for a node. */
340 typedef struct vlib_frame_t
345 /* Number of scalar bytes in arguments. */
348 /* Number of bytes per vector argument. */
351 /* Number of vector elements currently in frame. */
354 /* Scalar and vector arguments to next node. */
363 /* Node runtime for this next. */
364 u32 node_runtime_index;
366 /* Next frame flags. */
369 /* Reflects node frame-used flag for this next. */
370 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
371 VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
373 /* This next frame owns enqueue to node
374 corresponding to node_runtime_index. */
375 #define VLIB_FRAME_OWNER (1 << 15)
377 /* Set when frame has been allocated for this next. */
378 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
380 /* Set when frame has been added to pending vector. */
381 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
383 /* Set when frame is to be freed after dispatch. */
384 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
386 /* Set when frame has traced packets. */
387 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
389 /* Number of vectors enqueue to this next since last overflow. */
390 u32 vectors_since_last_overflow;
394 vlib_next_frame_init (vlib_next_frame_t * nf)
396 memset (nf, 0, sizeof (nf[0]));
397 nf->frame_index = ~0;
398 nf->node_runtime_index = ~0;
401 /* A frame pending dispatch by main loop. */
404 /* Node and runtime for this frame. */
405 u32 node_runtime_index;
407 /* Frame index (in the heap). */
410 /* Start of next frames for this node. */
411 u32 next_frame_index;
413 /* Special value for next_frame_index when there is no next frame. */
414 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
415 } vlib_pending_frame_t;
417 typedef struct vlib_node_runtime_t
419 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); /**< cacheline mark */
421 vlib_node_function_t *function; /**< Node function to call. */
423 vlib_error_t *errors; /**< Vector of errors for this node. */
425 #if __SIZEOF_POINTER__ == 4
429 u32 clocks_since_last_overflow; /**< Number of clock cycles. */
431 u32 max_clock; /**< Maximum clock cycle for an
434 u32 max_clock_n; /**< Number of vectors in the recorded
437 u32 calls_since_last_overflow; /**< Number of calls. */
439 u32 vectors_since_last_overflow; /**< Number of vector elements
440 processed by this node. */
442 u32 next_frame_index; /**< Start of next frames for this
445 u32 node_index; /**< Node index. */
447 u32 input_main_loops_per_call; /**< For input nodes: decremented
448 on each main loop interation until
449 it reaches zero and function is
450 called. Allows some input nodes to
451 be called more than others. */
453 u32 main_loop_count_last_dispatch; /**< Saved main loop counter of last
454 dispatch of this node. */
456 u32 main_loop_vector_stats[2];
458 u16 flags; /**< Copy of main node flags. */
460 u16 state; /**< Input node state. */
464 u16 cached_next_index; /**< Next frame index that vector
465 arguments were last enqueued to
466 last time this node ran. Set to
467 zero before first run of this
470 u16 thread_index; /**< thread this node runs on */
472 u8 runtime_data[0]; /**< Function dependent
473 node-runtime data. This data is
474 thread local, and it is not
475 cloned from main thread. It needs
476 to be initialized for each thread
477 before it is used unless
478 runtime_data template exists in
483 #define VLIB_NODE_RUNTIME_DATA_SIZE (sizeof (vlib_node_runtime_t) - STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data))
487 /* Number of allocated frames for this scalar/vector size. */
490 /* Vector of free frame indices for this scalar/vector size. */
491 u32 *free_frame_indices;
496 /* Users opaque value for event type. */
498 } vlib_process_event_type_t;
502 /* Node runtime for this process. */
503 vlib_node_runtime_t node_runtime;
505 /* Where to longjmp when process is done. */
506 clib_longjmp_t return_longjmp;
508 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
509 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
511 /* Where to longjmp to resume node after suspend. */
512 clib_longjmp_t resume_longjmp;
513 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
514 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME 1
517 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
518 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
519 /* Set to indicate that this process has been added to resume vector. */
520 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
522 /* Process function is currently running. */
523 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
525 /* Size of process stack. */
526 u16 log2_n_stack_bytes;
528 u32 suspended_process_frame_index;
530 /* Number of times this process was suspended. */
533 /* Vectors of pending event data indexed by event type index. */
534 void **pending_event_data_by_type_index;
536 /* Bitmap of event type-indices with non-empty vectors. */
537 uword *non_empty_event_type_bitmap;
539 /* Bitmap of event type-indices which are one time events. */
540 uword *one_time_event_type_bitmap;
542 /* Type is opaque pointer -- typically a pointer to an event handler
543 function. Hash table to map opaque to a type index. */
544 uword *event_type_index_by_type_opaque;
546 /* Pool of currently valid event types. */
547 vlib_process_event_type_t *event_type_pool;
550 * When suspending saves clock time (10us ticks) when process
553 u64 resume_clock_interval;
555 /* Handle from timer code, to cancel an unexpired timer */
556 u32 stop_timer_handle;
558 /* Default output function and its argument for any CLI outputs
559 within the process. */
560 vlib_cli_output_function_t *output_function;
561 uword output_function_arg;
564 /* Pad to a multiple of the page size so we can mprotect process stacks */
565 #define PAGE_SIZE_MULTIPLE 0x1000
566 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT __attribute__ ((aligned (PAGE_SIZE_MULTIPLE)))
568 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT
571 /* Process stack. Starts here and extends 2^log2_n_stack_bytes
574 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
575 u32 stack[0] ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT;
576 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
579 /* Ensure that the stack is aligned on the multiple of the page size */
581 assert_process_stack_must_be_aligned_exactly_to_page_size_multiple[(sizeof
595 } vlib_one_time_waiting_process_t;
601 u16 n_data_elt_bytes;
603 /* n_data_elts * n_data_elt_bytes */
606 /* Process node & event type to be used to signal event. */
607 u32 process_node_index;
609 u32 event_type_index;
613 u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
615 /* Vector of event data used only when data does not fit inline. */
616 u8 *event_data_as_vector;
619 vlib_signal_timed_event_data_t;
622 vlib_timing_wheel_data_is_timed_event (u32 d)
628 vlib_timing_wheel_data_set_suspended_process (u32 i)
634 vlib_timing_wheel_data_set_timed_event (u32 i)
640 vlib_timing_wheel_data_get_index (u32 d)
650 /* Node index hashed by node name. */
654 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
656 /* Nodes segregated by type for cache locality.
657 Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
658 vlib_node_runtime_t *nodes_by_type[VLIB_N_NODE_TYPE];
660 /* Node runtime indices for input nodes with pending interrupts. */
661 u32 *pending_interrupt_node_runtime_indices;
662 clib_spinlock_t pending_interrupt_lock;
664 /* Input nodes are switched from/to interrupt to/from polling mode
665 when average vector length goes above/below polling/interrupt
667 u32 polling_threshold_vector_length;
668 u32 interrupt_threshold_vector_length;
670 /* Vector of next frames. */
671 vlib_next_frame_t *next_frames;
673 /* Vector of internal node's frames waiting to be called. */
674 vlib_pending_frame_t *pending_frames;
676 /* Timing wheel for scheduling time-based node dispatch. */
679 vlib_signal_timed_event_data_t *signal_timed_event_data_pool;
681 /* Opaque data vector added via timing_wheel_advance. */
682 u32 *data_from_advancing_timing_wheel;
684 /* CPU time of next process to be ready on timing wheel. */
685 f64 time_next_process_ready;
687 /* Vector of process nodes.
688 One for each node of type VLIB_NODE_TYPE_PROCESS. */
689 vlib_process_t **processes;
691 /* Current running process or ~0 if no process running. */
692 u32 current_process_index;
694 /* Pool of pending process frames. */
695 vlib_pending_frame_t *suspended_process_frames;
697 /* Vector of event data vectors pending recycle. */
698 void **recycled_event_data_vectors;
700 /* Current counts of nodes in each state. */
701 u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
703 /* Hash of (scalar_size,vector_size) to frame_sizes index. */
704 uword *frame_size_hash;
706 /* Per-size frame allocation information. */
707 vlib_frame_size_t *frame_sizes;
709 /* Time of last node runtime stats clear. */
710 f64 time_last_runtime_stats_clear;
712 /* Node registrations added by constructors */
713 vlib_node_registration_t *node_registrations;
717 #define FRAME_QUEUE_MAX_NELTS 32
720 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
728 i32 n_vectors[FRAME_QUEUE_MAX_NELTS];
729 } frame_queue_trace_t;
733 u64 count[FRAME_QUEUE_MAX_NELTS];
734 } frame_queue_nelt_counter_t;
736 #endif /* included_vlib_node_h */
739 * fd.io coding-style-patch-verification: ON
742 * eval: (c-set-style "gnu")