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/timing_wheel.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 __VA_ARGS__ vlib_node_registration_t x
156 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn)
157 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)
158 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
160 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
162 __attribute__ ((flatten)) \
163 __attribute__ ((target (tgt))) \
165 fn ## _ ## arch ( struct vlib_main_t * vm, \
166 struct vlib_node_runtime_t * node, \
167 struct vlib_frame_t * frame) \
168 { return fn (vm, node, frame); }
170 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
171 foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
173 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn) \
174 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
175 CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
176 static void __attribute__((__constructor__)) \
177 __vlib_node_function_multiarch_select_##node (void) \
178 { node.function = fn ## _multiarch_select(); }
181 always_inline vlib_node_registration_t *
182 vlib_node_next_registered (vlib_node_registration_t * c)
185 clib_elf_section_data_next (c,
186 c->n_next_nodes * sizeof (c->next_nodes[0]));
192 /* Total calls, clock ticks and vector elements processed for this node. */
193 u64 calls, vectors, clocks, suspends;
198 #define foreach_vlib_node_state \
199 /* Input node is called each iteration of main loop. \
200 This is the default (zero). */ \
202 /* Input node is called when device signals an interrupt. */ \
204 /* Input node is never called. */ \
209 #define _(f) VLIB_NODE_STATE_##f,
210 foreach_vlib_node_state
215 typedef struct vlib_node_t
217 /* Vector processing function for this node. */
218 vlib_node_function_t *function;
223 /* Node name index in elog string table. */
224 u32 name_elog_string;
226 /* Total statistics for this node. */
227 vlib_node_stats_t stats_total;
229 /* Saved values as of last clear (or zero if never cleared).
230 Current values are always stats_total - stats_last_clear. */
231 vlib_node_stats_t stats_last_clear;
233 /* Type of this node. */
234 vlib_node_type_t type;
239 /* Index of corresponding node runtime. */
242 /* Runtime data for this node. */
248 /* Processing function keeps frame. Tells node dispatching code not
249 to free frame after dispatch is done. */
250 #define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
252 /* Node counts as output/drop/punt node for stats purposes. */
253 #define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
254 #define VLIB_NODE_FLAG_IS_DROP (1 << 2)
255 #define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
256 #define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
258 /* Set if current node runtime has traced vectors. */
259 #define VLIB_NODE_FLAG_TRACE (1 << 5)
261 #define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
262 #define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
264 /* State for input nodes. */
267 /* Number of bytes of run time data. */
268 u8 runtime_data_bytes;
270 /* Number of error codes used by this node. */
273 /* Size of scalar and vector arguments in bytes. */
274 u16 scalar_size, vector_size;
276 /* Handle/index in error heap for this node. */
277 u32 error_heap_handle;
278 u32 error_heap_index;
280 /* Error strings indexed by error code for this node. */
281 char **error_strings;
283 /* Vector of next node names.
284 Only used before next_nodes array is initialized. */
285 char **next_node_names;
287 /* Next node indices for this node. */
290 /* Name of node that we are sibling of. */
293 /* Bitmap of all of this node's siblings. */
294 uword *sibling_bitmap;
296 /* Total number of vectors sent to each next node. */
297 u64 *n_vectors_by_next_node;
299 /* Hash table mapping next node index into slot in
300 next_nodes vector. Quickly determines whether this node
301 is connected to given next node and, if so, with which slot. */
302 uword *next_slot_by_node;
304 /* Bitmap of node indices which feed this node. */
305 uword *prev_node_bitmap;
307 /* Node/next-index which own enqueue rights with to this node. */
308 u32 owner_node_index, owner_next_index;
310 /* Buffer format/unformat for this node. */
311 format_function_t *format_buffer;
312 unformat_function_t *unformat_buffer;
314 /* Trace buffer format/unformat for this node. */
315 format_function_t *format_trace;
317 /* Function to validate incoming frames. */
318 u8 *(*validate_frame) (struct vlib_main_t * vm,
319 struct vlib_node_runtime_t *,
320 struct vlib_frame_t * f);
321 /* for pretty-printing, not typically valid */
325 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
327 /* Max number of vector elements to process at once per node. */
328 #define VLIB_FRAME_SIZE 256
329 #define VLIB_FRAME_ALIGN VLIB_MAX_CPUS
331 /* Calling frame (think stack frame) for a node. */
332 typedef struct vlib_frame_t
337 /* Number of scalar bytes in arguments. */
340 /* Number of bytes per vector argument. */
343 /* Number of vector elements currently in frame. */
346 /* Owner cpuid / heap id */
349 /* Scalar and vector arguments to next node. */
358 /* Node runtime for this next. */
359 u32 node_runtime_index;
361 /* Next frame flags. */
364 /* Reflects node frame-used flag for this next. */
365 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
366 VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
368 /* This next frame owns enqueue to node
369 corresponding to node_runtime_index. */
370 #define VLIB_FRAME_OWNER (1 << 15)
372 /* Set when frame has been allocated for this next. */
373 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
375 /* Set when frame has been added to pending vector. */
376 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
378 /* Set when frame is to be freed after dispatch. */
379 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
381 /* Set when frame has traced packets. */
382 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
384 /* Number of vectors enqueue to this next since last overflow. */
385 u32 vectors_since_last_overflow;
389 vlib_next_frame_init (vlib_next_frame_t * nf)
391 memset (nf, 0, sizeof (nf[0]));
392 nf->frame_index = ~0;
393 nf->node_runtime_index = ~0;
396 /* A frame pending dispatch by main loop. */
399 /* Node and runtime for this frame. */
400 u32 node_runtime_index;
402 /* Frame index (in the heap). */
405 /* Start of next frames for this node. */
406 u32 next_frame_index;
408 /* Special value for next_frame_index when there is no next frame. */
409 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
410 } vlib_pending_frame_t;
412 typedef struct vlib_node_runtime_t
414 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
415 /* Node function to call. */
416 vlib_node_function_t *function;
418 /* Vector of errors for this node. */
419 vlib_error_t *errors;
421 /* Number of clock cycles. */
422 u32 clocks_since_last_overflow;
424 /* Maximum clock cycle for an invocation. */
427 /* Number of vectors in the recorded max_clock. */
430 /* Number of calls. */
431 u32 calls_since_last_overflow;
433 /* Number of vector elements processed by this node. */
434 u32 vectors_since_last_overflow;
436 /* Start of next frames for this node. */
437 u32 next_frame_index;
442 /* For input nodes: decremented on each main loop interation until it reaches zero
443 and function is called. Allows some input nodes to be called
445 u32 input_main_loops_per_call;
447 /* Saved main loop counter of last dispatch of this node. */
448 u32 main_loop_count_last_dispatch;
450 u32 main_loop_vector_stats[2];
452 /* Copy of main node flags. */
455 /* Input node state. */
460 /* Next frame index that vector arguments were last enqueued to
461 last time this node ran. Set to zero before first run
463 u16 cached_next_index;
465 /* CPU this node runs on */
468 /* Function dependent node-runtime. */
475 /* Number of allocated frames for this scalar/vector size. */
478 /* Vector of free frame indices for this scalar/vector size. */
479 u32 *free_frame_indices;
484 /* Users opaque value for event type. */
486 } vlib_process_event_type_t;
490 /* Node runtime for this process. */
491 vlib_node_runtime_t node_runtime;
493 /* Where to longjmp when process is done. */
494 clib_longjmp_t return_longjmp;
496 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
497 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
499 /* Where to longjmp to resume node after suspend. */
500 clib_longjmp_t resume_longjmp;
501 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
502 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME 1
505 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
506 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
507 /* Set to indicate that this process has been added to resume vector. */
508 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
510 /* Process function is currently running. */
511 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
513 /* Size of process stack. */
514 u16 log2_n_stack_bytes;
516 u32 suspended_process_frame_index;
518 /* Number of times this process was suspended. */
521 /* Vectors of pending event data indexed by event type index. */
522 void **pending_event_data_by_type_index;
524 /* Bitmap of event type-indices with non-empty vectors. */
525 uword *non_empty_event_type_bitmap;
527 /* Bitmap of event type-indices which are one time events. */
528 uword *one_time_event_type_bitmap;
530 /* Type is opaque pointer -- typically a pointer to an event handler
531 function. Hash table to map opaque to a type index. */
532 uword *event_type_index_by_type_opaque;
534 /* Pool of currently valid event types. */
535 vlib_process_event_type_t *event_type_pool;
537 /* When suspending saves cpu cycle counter when process is to be resumed. */
540 /* Default output function and its argument for any CLI outputs
541 within the process. */
542 vlib_cli_output_function_t *output_function;
543 uword output_function_arg;
546 /* Pad to a multiple of the page size so we can mprotect process stacks */
547 #define PAGE_SIZE_MULTIPLE 0x1000
548 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT __attribute__ ((aligned (PAGE_SIZE_MULTIPLE)))
550 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT
553 /* Process stack. Starts here and extends 2^log2_n_stack_bytes
556 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
557 u32 stack[0] ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT;
558 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
561 /* Ensure that the stack is aligned on the multiple of the page size */
563 assert_process_stack_must_be_aligned_exactly_to_page_size_multiple[(sizeof
577 } vlib_one_time_waiting_process_t;
583 u16 n_data_elt_bytes;
585 /* n_data_elts * n_data_elt_bytes */
588 /* Process node & event type to be used to signal event. */
589 u32 process_node_index;
591 u32 event_type_index;
595 u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
597 /* Vector of event data used only when data does not fit inline. */
598 u8 *event_data_as_vector;
601 vlib_signal_timed_event_data_t;
604 vlib_timing_wheel_data_is_timed_event (u32 d)
610 vlib_timing_wheel_data_set_suspended_process (u32 i)
616 vlib_timing_wheel_data_set_timed_event (u32 i)
622 vlib_timing_wheel_data_get_index (u32 d)
632 /* Node index hashed by node name. */
636 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
638 /* Nodes segregated by type for cache locality.
639 Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
640 vlib_node_runtime_t *nodes_by_type[VLIB_N_NODE_TYPE];
642 /* Node runtime indices for input nodes with pending interrupts. */
643 u32 *pending_interrupt_node_runtime_indices;
645 /* Input nodes are switched from/to interrupt to/from polling mode
646 when average vector length goes above/below polling/interrupt
648 u32 polling_threshold_vector_length;
649 u32 interrupt_threshold_vector_length;
651 /* Vector of next frames. */
652 vlib_next_frame_t *next_frames;
654 /* Vector of internal node's frames waiting to be called. */
655 vlib_pending_frame_t *pending_frames;
657 /* Timing wheel for scheduling time-based node dispatch. */
658 timing_wheel_t timing_wheel;
660 vlib_signal_timed_event_data_t *signal_timed_event_data_pool;
662 /* Opaque data vector added via timing_wheel_advance. */
663 u32 *data_from_advancing_timing_wheel;
665 /* CPU time of next process to be ready on timing wheel. */
666 u64 cpu_time_next_process_ready;
668 /* Vector of process nodes.
669 One for each node of type VLIB_NODE_TYPE_PROCESS. */
670 vlib_process_t **processes;
672 /* Current running process or ~0 if no process running. */
673 u32 current_process_index;
675 /* Pool of pending process frames. */
676 vlib_pending_frame_t *suspended_process_frames;
678 /* Vector of event data vectors pending recycle. */
679 void **recycled_event_data_vectors;
681 /* Current counts of nodes in each state. */
682 u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
684 /* Hash of (scalar_size,vector_size) to frame_sizes index. */
685 uword *frame_size_hash;
687 /* Per-size frame allocation information. */
688 vlib_frame_size_t *frame_sizes;
690 /* Time of last node runtime stats clear. */
691 f64 time_last_runtime_stats_clear;
693 /* Node registrations added by constructors */
694 vlib_node_registration_t *node_registrations;
698 #define FRAME_QUEUE_MAX_NELTS 32
701 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
709 i32 n_vectors[FRAME_QUEUE_MAX_NELTS];
710 } frame_queue_trace_t;
714 u64 count[FRAME_QUEUE_MAX_NELTS];
715 } frame_queue_nelt_counter_t;
717 #endif /* included_vlib_node_h */
720 * fd.io coding-style-patch-verification: ON
723 * eval: (c-set-style "gnu")