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);
59 /* An internal node on the call graph (could be output). */
60 VLIB_NODE_TYPE_INTERNAL,
62 /* Nodes which input data into the processing graph.
63 Input nodes are called for each iteration of main loop. */
66 /* Nodes to be called before all input nodes.
67 Used, for example, to clean out driver TX rings before
69 VLIB_NODE_TYPE_PRE_INPUT,
71 /* "Process" nodes which can be suspended and later resumed. */
72 VLIB_NODE_TYPE_PROCESS,
77 typedef struct _vlib_node_registration {
78 /* Vector processing function for this node. */
79 vlib_node_function_t * function;
84 /* Name of sibling (if applicable). */
87 /* Node index filled in by registration. */
90 /* Type of this node. */
91 vlib_node_type_t type;
93 /* Error strings indexed by error code for this node. */
94 char ** error_strings;
96 /* Buffer format/unformat for this node. */
97 format_function_t * format_buffer;
98 unformat_function_t * unformat_buffer;
100 /* Trace format/unformat for this node. */
101 format_function_t * format_trace;
102 unformat_function_t * unformat_trace;
104 /* Function to validate incoming frames. */
105 u8 * (* validate_frame) (struct vlib_main_t * vm,
106 struct vlib_node_runtime_t *,
107 struct vlib_frame_t * f);
109 /* Per-node runtime data. */
112 /* Process stack size. */
113 u16 process_log2_n_stack_bytes;
115 /* Number of bytes of per-node run time data. */
116 u8 runtime_data_bytes;
118 /* State for input nodes. */
124 /* Size of scalar and vector arguments in bytes. */
125 u16 scalar_size, vector_size;
127 /* Number of error codes used by this node. */
130 /* Number of next node names that follow. */
133 /* Constructor link-list, don't ask... */
134 struct _vlib_node_registration * next_registration;
136 /* Names of next nodes which this node feeds into. */
139 } vlib_node_registration_t;
141 #define VLIB_REGISTER_NODE(x,...) \
142 __VA_ARGS__ vlib_node_registration_t x; \
143 static void __vlib_add_node_registration_##x (void) \
144 __attribute__((__constructor__)) ; \
145 static void __vlib_add_node_registration_##x (void) \
147 vlib_main_t * vm = vlib_get_main(); \
148 x.next_registration = vm->node_main.node_registrations; \
149 vm->node_main.node_registrations = &x; \
151 __VA_ARGS__ vlib_node_registration_t x
154 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn)
155 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)
156 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
158 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
160 __attribute__ ((flatten)) \
161 __attribute__ ((target (tgt))) \
163 fn ## _ ## arch ( struct vlib_main_t * vm, \
164 struct vlib_node_runtime_t * node, \
165 struct vlib_frame_t * frame) \
166 { return fn (vm, node, frame); }
168 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
169 foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
171 #define VLIB_NODE_FUNCTION_MULTIARCH(node, fn) \
172 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
173 CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
174 static void __attribute__((__constructor__)) \
175 __vlib_node_function_multiarch_select_##node (void) \
176 { node.function = fn ## _multiarch_select(); }
179 always_inline vlib_node_registration_t *
180 vlib_node_next_registered (vlib_node_registration_t * c)
182 c = clib_elf_section_data_next (c, c->n_next_nodes * sizeof (c->next_nodes[0]));
187 /* Total calls, clock ticks and vector elements processed for this node. */
188 u64 calls, vectors, clocks, suspends;
193 #define foreach_vlib_node_state \
194 /* Input node is called each iteration of main loop. \
195 This is the default (zero). */ \
197 /* Input node is called when device signals an interrupt. */ \
199 /* Input node is never called. */ \
203 #define _(f) VLIB_NODE_STATE_##f,
204 foreach_vlib_node_state
209 typedef struct vlib_node_t {
210 /* Vector processing function for this node. */
211 vlib_node_function_t * function;
216 /* Node name index in elog string table. */
217 u32 name_elog_string;
219 /* Total statistics for this node. */
220 vlib_node_stats_t stats_total;
222 /* Saved values as of last clear (or zero if never cleared).
223 Current values are always stats_total - stats_last_clear. */
224 vlib_node_stats_t stats_last_clear;
226 /* Type of this node. */
227 vlib_node_type_t type;
232 /* Index of corresponding node runtime. */
235 /* Runtime data for this node. */
241 /* Processing function keeps frame. Tells node dispatching code not
242 to free frame after dispatch is done. */
243 #define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
245 /* Node counts as output/drop/punt node for stats purposes. */
246 #define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
247 #define VLIB_NODE_FLAG_IS_DROP (1 << 2)
248 #define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
249 #define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
251 /* Set if current node runtime has traced vectors. */
252 #define VLIB_NODE_FLAG_TRACE (1 << 5)
254 #define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
255 #define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
257 /* State for input nodes. */
260 /* Number of bytes of run time data. */
261 u8 runtime_data_bytes;
263 /* Number of error codes used by this node. */
266 /* Size of scalar and vector arguments in bytes. */
267 u16 scalar_size, vector_size;
269 /* Handle/index in error heap for this node. */
270 u32 error_heap_handle;
271 u32 error_heap_index;
273 /* Error strings indexed by error code for this node. */
274 char ** error_strings;
276 /* Vector of next node names.
277 Only used before next_nodes array is initialized. */
278 char ** next_node_names;
280 /* Next node indices for this node. */
283 /* Name of node that we are sibling of. */
286 /* Bitmap of all of this node's siblings. */
287 uword * sibling_bitmap;
289 /* Total number of vectors sent to each next node. */
290 u64 * n_vectors_by_next_node;
292 /* Hash table mapping next node index into slot in
293 next_nodes vector. Quickly determines whether this node
294 is connected to given next node and, if so, with which slot. */
295 uword * next_slot_by_node;
297 /* Bitmap of node indices which feed this node. */
298 uword * prev_node_bitmap;
300 /* Node/next-index which own enqueue rights with to this node. */
301 u32 owner_node_index, owner_next_index;
303 /* Buffer format/unformat for this node. */
304 format_function_t * format_buffer;
305 unformat_function_t * unformat_buffer;
307 /* Trace buffer format/unformat for this node. */
308 format_function_t * format_trace;
310 /* Function to validate incoming frames. */
311 u8 * (* validate_frame) (struct vlib_main_t * vm,
312 struct vlib_node_runtime_t *,
313 struct vlib_frame_t * f);
316 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
318 /* Max number of vector elements to process at once per node. */
319 #define VLIB_FRAME_SIZE 256
320 #define VLIB_FRAME_ALIGN VLIB_MAX_CPUS
322 /* Calling frame (think stack frame) for a node. */
323 typedef struct vlib_frame_t {
327 /* Number of scalar bytes in arguments. */
330 /* Number of bytes per vector argument. */
333 /* Number of vector elements currently in frame. */
336 /* Owner cpuid / heap id */
339 /* Scalar and vector arguments to next node. */
347 /* Node runtime for this next. */
348 u32 node_runtime_index;
350 /* Next frame flags. */
353 /* Reflects node frame-used flag for this next. */
354 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
355 VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
357 /* This next frame owns enqueue to node
358 corresponding to node_runtime_index. */
359 #define VLIB_FRAME_OWNER (1 << 15)
361 /* Set when frame has been allocated for this next. */
362 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
364 /* Set when frame has been added to pending vector. */
365 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
367 /* Set when frame is to be freed after dispatch. */
368 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
370 /* Set when frame has traced packets. */
371 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
373 /* Number of vectors enqueue to this next since last overflow. */
374 u32 vectors_since_last_overflow;
378 vlib_next_frame_init (vlib_next_frame_t * nf)
380 memset (nf, 0, sizeof (nf[0]));
381 nf->frame_index = ~0;
382 nf->node_runtime_index = ~0;
385 /* A frame pending dispatch by main loop. */
387 /* Node and runtime for this frame. */
388 u32 node_runtime_index;
390 /* Frame index (in the heap). */
393 /* Start of next frames for this node. */
394 u32 next_frame_index;
396 /* Special value for next_frame_index when there is no next frame. */
397 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
398 } vlib_pending_frame_t;
400 typedef struct vlib_node_runtime_t {
401 /* Node function to call. */
402 vlib_node_function_t * function;
404 /* Vector of errors for this node. */
405 vlib_error_t * errors;
407 /* Number of clock cycles. */
408 u32 clocks_since_last_overflow;
410 /* Maximum clock cycle for an invocation. */
413 /* Number of vectors in the recorded max_clock. */
416 /* Number of calls. */
417 u32 calls_since_last_overflow;
419 /* Number of vector elements processed by this node. */
420 u32 vectors_since_last_overflow;
422 /* Start of next frames for this node. */
423 u32 next_frame_index;
428 /* For input nodes: decremented on each main loop interation until it reaches zero
429 and function is called. Allows some input nodes to be called
431 u32 input_main_loops_per_call;
433 /* Saved main loop counter of last dispatch of this node. */
434 u32 main_loop_count_last_dispatch;
436 u32 main_loop_vector_stats[2];
438 /* Copy of main node flags. */
441 /* Input node state. */
446 /* Next frame index that vector arguments were last enqueued to
447 last time this node ran. Set to zero before first run
449 u16 cached_next_index;
451 /* CPU this node runs on */
454 /* Function dependent node-runtime. */
455 uword runtime_data[(128
456 - 1 * sizeof (vlib_node_function_t *)
457 - 1 * sizeof (vlib_error_t *)
459 - 5 * sizeof (u16)) / sizeof (uword)];
460 } vlib_node_runtime_t;
463 /* Number of allocated frames for this scalar/vector size. */
466 /* Vector of free frame indices for this scalar/vector size. */
467 u32 * free_frame_indices;
471 /* Users opaque value for event type. */
473 } vlib_process_event_type_t;
476 /* Node runtime for this process. */
477 vlib_node_runtime_t node_runtime;
479 /* Where to longjmp when process is done. */
480 clib_longjmp_t return_longjmp;
482 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
483 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
485 /* Where to longjmp to resume node after suspend. */
486 clib_longjmp_t resume_longjmp;
487 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
488 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME 1
491 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
492 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
493 /* Set to indicate that this process has been added to resume vector. */
494 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
496 /* Process function is currently running. */
497 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
499 /* Size of process stack. */
500 u16 log2_n_stack_bytes;
502 u32 suspended_process_frame_index;
504 /* Number of times this process was suspended. */
507 /* Vectors of pending event data indexed by event type index. */
508 void ** pending_event_data_by_type_index;
510 /* Bitmap of event type-indices with non-empty vectors. */
511 uword * non_empty_event_type_bitmap;
513 /* Bitmap of event type-indices which are one time events. */
514 uword * one_time_event_type_bitmap;
516 /* Type is opaque pointer -- typically a pointer to an event handler
517 function. Hash table to map opaque to a type index. */
518 uword * event_type_index_by_type_opaque;
520 /* Pool of currently valid event types. */
521 vlib_process_event_type_t * event_type_pool;
523 /* When suspending saves cpu cycle counter when process is to be resumed. */
526 /* Default output function and its argument for any CLI outputs
527 within the process. */
528 vlib_cli_output_function_t *output_function;
529 uword output_function_arg;
532 /* Pad to a multiple of the page size so we can mprotect process stacks */
533 #define PAGE_SIZE_MULTIPLE 0x1000
534 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT __attribute__ ((aligned (PAGE_SIZE_MULTIPLE)))
536 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT
539 /* Process stack. Starts here and extends 2^log2_n_stack_bytes
542 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
543 u32 stack[0] ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT;
544 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
547 /* Ensure that the stack is aligned on the multiple of the page size */
548 typedef char assert_process_stack_must_be_aligned_exactly_to_page_size_multiple
549 [(sizeof(vlib_process_t) - PAGE_SIZE_MULTIPLE) == 0 ? 0 : -1];
556 } vlib_one_time_waiting_process_t;
561 u16 n_data_elt_bytes;
563 /* n_data_elts * n_data_elt_bytes */
566 /* Process node & event type to be used to signal event. */
567 u32 process_node_index;
569 u32 event_type_index;
572 u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
574 /* Vector of event data used only when data does not fit inline. */
575 u8 * event_data_as_vector;
577 } vlib_signal_timed_event_data_t;
580 vlib_timing_wheel_data_is_timed_event (u32 d)
584 vlib_timing_wheel_data_set_suspended_process (u32 i)
588 vlib_timing_wheel_data_set_timed_event (u32 i)
592 vlib_timing_wheel_data_get_index (u32 d)
597 vlib_node_t ** nodes;
599 /* Node index hashed by node name. */
600 uword * node_by_name;
603 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
605 /* Nodes segregated by type for cache locality.
606 Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
607 vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE];
609 /* Node runtime indices for input nodes with pending interrupts. */
610 u32 * pending_interrupt_node_runtime_indices;
612 /* Input nodes are switched from/to interrupt to/from polling mode
613 when average vector length goes above/below polling/interrupt
615 u32 polling_threshold_vector_length;
616 u32 interrupt_threshold_vector_length;
618 /* Vector of next frames. */
619 vlib_next_frame_t * next_frames;
621 /* Vector of internal node's frames waiting to be called. */
622 vlib_pending_frame_t * pending_frames;
624 /* Timing wheel for scheduling time-based node dispatch. */
625 timing_wheel_t timing_wheel;
627 vlib_signal_timed_event_data_t * signal_timed_event_data_pool;
629 /* Opaque data vector added via timing_wheel_advance. */
630 u32 * data_from_advancing_timing_wheel;
632 /* CPU time of next process to be ready on timing wheel. */
633 u64 cpu_time_next_process_ready;
635 /* Vector of process nodes.
636 One for each node of type VLIB_NODE_TYPE_PROCESS. */
637 vlib_process_t ** processes;
639 /* Current running process or ~0 if no process running. */
640 u32 current_process_index;
642 /* Pool of pending process frames. */
643 vlib_pending_frame_t * suspended_process_frames;
645 /* Vector of event data vectors pending recycle. */
646 void ** recycled_event_data_vectors;
648 /* Current counts of nodes in each state. */
649 u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
651 /* Hash of (scalar_size,vector_size) to frame_sizes index. */
652 uword * frame_size_hash;
654 /* Per-size frame allocation information. */
655 vlib_frame_size_t * frame_sizes;
657 /* Time of last node runtime stats clear. */
658 f64 time_last_runtime_stats_clear;
660 /* Node registrations added by constructors */
661 vlib_node_registration_t * node_registrations;
664 #endif /* included_vlib_node_h */