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/longjmp.h>
44 #include <vppinfra/timing_wheel.h>
45 #include <vlib/trace.h> /* for vlib_trace_filter_t */
47 /* Forward declaration. */
48 struct vlib_node_runtime_t;
51 /* Internal nodes (including output nodes) move data from node to
52 node (or out of the graph for output nodes). */
53 typedef uword (vlib_node_function_t) (struct vlib_main_t * vm,
54 struct vlib_node_runtime_t * node,
55 struct vlib_frame_t * frame);
58 /* An internal node on the call graph (could be output). */
59 VLIB_NODE_TYPE_INTERNAL,
61 /* Nodes which input data into the processing graph.
62 Input nodes are called for each iteration of main loop. */
65 /* Nodes to be called before all input nodes.
66 Used, for example, to clean out driver TX rings before
68 VLIB_NODE_TYPE_PRE_INPUT,
70 /* "Process" nodes which can be suspended and later resumed. */
71 VLIB_NODE_TYPE_PROCESS,
76 typedef struct _vlib_node_registration {
77 /* Vector processing function for this node. */
78 vlib_node_function_t * function;
83 /* Name of sibling (if applicable). */
86 /* Node index filled in by registration. */
89 /* Type of this node. */
90 vlib_node_type_t type;
92 /* Error strings indexed by error code for this node. */
93 char ** error_strings;
95 /* Buffer format/unformat for this node. */
96 format_function_t * format_buffer;
97 unformat_function_t * unformat_buffer;
99 /* Trace format/unformat for this node. */
100 format_function_t * format_trace;
101 unformat_function_t * unformat_trace;
103 /* Function to validate incoming frames. */
104 u8 * (* validate_frame) (struct vlib_main_t * vm,
105 struct vlib_node_runtime_t *,
106 struct vlib_frame_t * f);
108 /* Per-node runtime data. */
111 /* Process stack size. */
112 u16 process_log2_n_stack_bytes;
114 /* Number of bytes of per-node run time data. */
115 u8 runtime_data_bytes;
117 /* State for input nodes. */
123 /* Size of scalar and vector arguments in bytes. */
124 u16 scalar_size, vector_size;
126 /* Number of error codes used by this node. */
129 /* Number of next node names that follow. */
132 /* Constructor link-list, don't ask... */
133 struct _vlib_node_registration * next_registration;
135 /* Names of next nodes which this node feeds into. */
138 } vlib_node_registration_t;
140 #define VLIB_REGISTER_NODE(x,...) \
141 __VA_ARGS__ vlib_node_registration_t x; \
142 static void __vlib_add_node_registration_##x (void) \
143 __attribute__((__constructor__)) ; \
144 static void __vlib_add_node_registration_##x (void) \
146 vlib_main_t * vm = vlib_get_main(); \
147 x.next_registration = vm->node_main.node_registrations; \
148 vm->node_main.node_registrations = &x; \
150 __VA_ARGS__ vlib_node_registration_t x
152 always_inline vlib_node_registration_t *
153 vlib_node_next_registered (vlib_node_registration_t * c)
155 c = clib_elf_section_data_next (c, c->n_next_nodes * sizeof (c->next_nodes[0]));
160 /* Total calls, clock ticks and vector elements processed for this node. */
161 u64 calls, vectors, clocks, suspends;
166 #define foreach_vlib_node_state \
167 /* Input node is called each iteration of main loop. \
168 This is the default (zero). */ \
170 /* Input node is called when device signals an interrupt. */ \
172 /* Input node is never called. */ \
176 #define _(f) VLIB_NODE_STATE_##f,
177 foreach_vlib_node_state
182 typedef struct vlib_node_t {
183 /* Vector processing function for this node. */
184 vlib_node_function_t * function;
189 /* Node name index in elog string table. */
190 u32 name_elog_string;
192 /* Total statistics for this node. */
193 vlib_node_stats_t stats_total;
195 /* Saved values as of last clear (or zero if never cleared).
196 Current values are always stats_total - stats_last_clear. */
197 vlib_node_stats_t stats_last_clear;
199 /* Type of this node. */
200 vlib_node_type_t type;
205 /* Index of corresponding node runtime. */
208 /* Runtime data for this node. */
214 /* Processing function keeps frame. Tells node dispatching code not
215 to free frame after dispatch is done. */
216 #define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
218 /* Node counts as output/drop/punt node for stats purposes. */
219 #define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
220 #define VLIB_NODE_FLAG_IS_DROP (1 << 2)
221 #define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
222 #define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
224 /* Set if current node runtime has traced vectors. */
225 #define VLIB_NODE_FLAG_TRACE (1 << 5)
227 #define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
228 #define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
230 /* State for input nodes. */
233 /* Number of bytes of run time data. */
234 u8 runtime_data_bytes;
236 /* Number of error codes used by this node. */
239 /* Size of scalar and vector arguments in bytes. */
240 u16 scalar_size, vector_size;
242 /* Handle/index in error heap for this node. */
243 u32 error_heap_handle;
244 u32 error_heap_index;
246 /* Error strings indexed by error code for this node. */
247 char ** error_strings;
249 /* Vector of next node names.
250 Only used before next_nodes array is initialized. */
251 char ** next_node_names;
253 /* Next node indices for this node. */
256 /* Name of node that we are sibling of. */
259 /* Bitmap of all of this node's siblings. */
260 uword * sibling_bitmap;
262 /* Total number of vectors sent to each next node. */
263 u64 * n_vectors_by_next_node;
265 /* Hash table mapping next node index into slot in
266 next_nodes vector. Quickly determines whether this node
267 is connected to given next node and, if so, with which slot. */
268 uword * next_slot_by_node;
270 /* Bitmap of node indices which feed this node. */
271 uword * prev_node_bitmap;
273 /* Node/next-index which own enqueue rights with to this node. */
274 u32 owner_node_index, owner_next_index;
276 /* Buffer format/unformat for this node. */
277 format_function_t * format_buffer;
278 unformat_function_t * unformat_buffer;
280 /* Trace buffer format/unformat for this node. */
281 format_function_t * format_trace;
283 /* Function to validate incoming frames. */
284 u8 * (* validate_frame) (struct vlib_main_t * vm,
285 struct vlib_node_runtime_t *,
286 struct vlib_frame_t * f);
289 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
291 /* Max number of vector elements to process at once per node. */
292 #define VLIB_FRAME_SIZE 256
293 #define VLIB_FRAME_ALIGN VLIB_MAX_CPUS
295 /* Calling frame (think stack frame) for a node. */
296 typedef struct vlib_frame_t {
300 /* Number of scalar bytes in arguments. */
303 /* Number of bytes per vector argument. */
306 /* Number of vector elements currently in frame. */
309 /* Owner cpuid / heap id */
312 /* Scalar and vector arguments to next node. */
320 /* Node runtime for this next. */
321 u32 node_runtime_index;
323 /* Next frame flags. */
326 /* Reflects node frame-used flag for this next. */
327 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
328 VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
330 /* This next frame owns enqueue to node
331 corresponding to node_runtime_index. */
332 #define VLIB_FRAME_OWNER (1 << 15)
334 /* Set when frame has been allocated for this next. */
335 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
337 /* Set when frame has been added to pending vector. */
338 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
340 /* Set when frame is to be freed after dispatch. */
341 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
343 /* Set when frame has traced packets. */
344 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
346 /* Number of vectors enqueue to this next since last overflow. */
347 u32 vectors_since_last_overflow;
351 vlib_next_frame_init (vlib_next_frame_t * nf)
353 memset (nf, 0, sizeof (nf[0]));
354 nf->frame_index = ~0;
355 nf->node_runtime_index = ~0;
358 /* A frame pending dispatch by main loop. */
360 /* Node and runtime for this frame. */
361 u32 node_runtime_index;
363 /* Frame index (in the heap). */
366 /* Start of next frames for this node. */
367 u32 next_frame_index;
369 /* Special value for next_frame_index when there is no next frame. */
370 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
371 } vlib_pending_frame_t;
373 typedef struct vlib_node_runtime_t {
374 /* Node function to call. */
375 vlib_node_function_t * function;
377 /* Vector of errors for this node. */
378 vlib_error_t * errors;
380 /* Number of clock cycles. */
381 u32 clocks_since_last_overflow;
383 /* Maximum clock cycle for an invocation. */
386 /* Number of vectors in the recorded max_clock. */
389 /* Number of calls. */
390 u32 calls_since_last_overflow;
392 /* Number of vector elements processed by this node. */
393 u32 vectors_since_last_overflow;
395 /* Start of next frames for this node. */
396 u32 next_frame_index;
401 /* For input nodes: decremented on each main loop interation until it reaches zero
402 and function is called. Allows some input nodes to be called
404 u32 input_main_loops_per_call;
406 /* Saved main loop counter of last dispatch of this node. */
407 u32 main_loop_count_last_dispatch;
409 u32 main_loop_vector_stats[2];
411 /* Copy of main node flags. */
414 /* Input node state. */
419 /* Next frame index that vector arguments were last enqueued to
420 last time this node ran. Set to zero before first run
422 u16 cached_next_index;
424 /* CPU this node runs on */
427 /* Function dependent node-runtime. */
428 uword runtime_data[(128
429 - 1 * sizeof (vlib_node_function_t *)
430 - 1 * sizeof (vlib_error_t *)
432 - 5 * sizeof (u16)) / sizeof (uword)];
433 } vlib_node_runtime_t;
436 /* Number of allocated frames for this scalar/vector size. */
439 /* Vector of free frame indices for this scalar/vector size. */
440 u32 * free_frame_indices;
444 /* Users opaque value for event type. */
446 } vlib_process_event_type_t;
449 /* Node runtime for this process. */
450 vlib_node_runtime_t node_runtime;
452 /* Where to longjmp when process is done. */
453 clib_longjmp_t return_longjmp;
455 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
456 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
458 /* Where to longjmp to resume node after suspend. */
459 clib_longjmp_t resume_longjmp;
460 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
461 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME 1
464 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
465 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
466 /* Set to indicate that this process has been added to resume vector. */
467 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
469 /* Process function is currently running. */
470 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
472 /* Size of process stack. */
473 u16 log2_n_stack_bytes;
475 u32 suspended_process_frame_index;
477 /* Number of times this process was suspended. */
480 /* Vectors of pending event data indexed by event type index. */
481 void ** pending_event_data_by_type_index;
483 /* Bitmap of event type-indices with non-empty vectors. */
484 uword * non_empty_event_type_bitmap;
486 /* Bitmap of event type-indices which are one time events. */
487 uword * one_time_event_type_bitmap;
489 /* Type is opaque pointer -- typically a pointer to an event handler
490 function. Hash table to map opaque to a type index. */
491 uword * event_type_index_by_type_opaque;
493 /* Pool of currently valid event types. */
494 vlib_process_event_type_t * event_type_pool;
496 /* When suspending saves cpu cycle counter when process is to be resumed. */
500 /* Pad to a multiple of the page size so we can mprotect process stacks */
501 CLIB_PAD_FROM_TO (0x140, 0x1000);
503 /* Process stack. Starts here and extends 2^log2_n_stack_bytes
506 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
508 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
514 } vlib_one_time_waiting_process_t;
519 u16 n_data_elt_bytes;
521 /* n_data_elts * n_data_elt_bytes */
524 /* Process node & event type to be used to signal event. */
525 u32 process_node_index;
527 u32 event_type_index;
530 u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
532 /* Vector of event data used only when data does not fit inline. */
533 u8 * event_data_as_vector;
535 } vlib_signal_timed_event_data_t;
538 vlib_timing_wheel_data_is_timed_event (u32 d)
542 vlib_timing_wheel_data_set_suspended_process (u32 i)
546 vlib_timing_wheel_data_set_timed_event (u32 i)
550 vlib_timing_wheel_data_get_index (u32 d)
555 vlib_node_t ** nodes;
557 /* Node index hashed by node name. */
558 uword * node_by_name;
561 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
563 /* Nodes segregated by type for cache locality.
564 Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
565 vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE];
567 /* Node runtime indices for input nodes with pending interrupts. */
568 u32 * pending_interrupt_node_runtime_indices;
570 /* Input nodes are switched from/to interrupt to/from polling mode
571 when average vector length goes above/below polling/interrupt
573 u32 polling_threshold_vector_length;
574 u32 interrupt_threshold_vector_length;
576 /* Vector of next frames. */
577 vlib_next_frame_t * next_frames;
579 /* Vector of internal node's frames waiting to be called. */
580 vlib_pending_frame_t * pending_frames;
582 /* Timing wheel for scheduling time-based node dispatch. */
583 timing_wheel_t timing_wheel;
585 vlib_signal_timed_event_data_t * signal_timed_event_data_pool;
587 /* Opaque data vector added via timing_wheel_advance. */
588 u32 * data_from_advancing_timing_wheel;
590 /* CPU time of next process to be ready on timing wheel. */
591 u64 cpu_time_next_process_ready;
593 /* Vector of process nodes.
594 One for each node of type VLIB_NODE_TYPE_PROCESS. */
595 vlib_process_t ** processes;
597 /* Current running process or ~0 if no process running. */
598 u32 current_process_index;
600 /* Pool of pending process frames. */
601 vlib_pending_frame_t * suspended_process_frames;
603 /* Vector of event data vectors pending recycle. */
604 void ** recycled_event_data_vectors;
606 /* Current counts of nodes in each state. */
607 u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
609 /* Hash of (scalar_size,vector_size) to frame_sizes index. */
610 uword * frame_size_hash;
612 /* Per-size frame allocation information. */
613 vlib_frame_size_t * frame_sizes;
615 /* Time of last node runtime stats clear. */
616 f64 time_last_runtime_stats_clear;
618 /* Node registrations added by constructors */
619 vlib_node_registration_t * node_registrations;
622 #endif /* included_vlib_node_h */