Add support for multiple microarchitectures in single binary
[vpp.git] / vlib / vlib / node.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * node.h: VLIB processing nodes
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #ifndef included_vlib_node_h
41 #define included_vlib_node_h
42
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 */
47
48 /* Forward declaration. */
49 struct vlib_node_runtime_t;
50 struct vlib_frame_t;
51
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);
57
58 typedef enum {
59   /* An internal node on the call graph (could be output). */
60   VLIB_NODE_TYPE_INTERNAL,
61
62   /* Nodes which input data into the processing graph.
63      Input nodes are called for each iteration of main loop. */
64   VLIB_NODE_TYPE_INPUT,
65
66   /* Nodes to be called before all input nodes.
67      Used, for example, to clean out driver TX rings before
68      processing input. */
69   VLIB_NODE_TYPE_PRE_INPUT,
70
71   /* "Process" nodes which can be suspended and later resumed. */
72   VLIB_NODE_TYPE_PROCESS,
73
74   VLIB_N_NODE_TYPE,
75 } vlib_node_type_t;
76
77 typedef struct _vlib_node_registration {
78   /* Vector processing function for this node. */
79   vlib_node_function_t * function;
80
81   /* Node name. */
82   char * name;
83
84   /* Name of sibling (if applicable). */
85   char * sibling_of;
86
87   /* Node index filled in by registration. */
88   u32 index;
89
90   /* Type of this node. */
91   vlib_node_type_t type;
92
93   /* Error strings indexed by error code for this node. */
94   char ** error_strings;
95
96   /* Buffer format/unformat for this node. */
97   format_function_t * format_buffer;
98   unformat_function_t * unformat_buffer;
99
100   /* Trace format/unformat for this node. */
101   format_function_t * format_trace;
102   unformat_function_t * unformat_trace;
103
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);
108
109   /* Per-node runtime data. */
110   void * runtime_data;
111
112   /* Process stack size. */
113   u16 process_log2_n_stack_bytes;
114
115   /* Number of bytes of per-node run time data. */
116   u8 runtime_data_bytes;
117
118   /* State for input nodes. */
119   u8 state;
120
121   /* Node flags. */
122   u16 flags;
123
124   /* Size of scalar and vector arguments in bytes. */
125   u16 scalar_size, vector_size;
126
127   /* Number of error codes used by this node. */
128   u16 n_errors;
129
130   /* Number of next node names that follow. */
131   u16 n_next_nodes;
132
133   /* Constructor link-list, don't ask... */
134   struct _vlib_node_registration * next_registration;
135
136   /* Names of next nodes which this node feeds into. */
137   char * next_nodes[];
138
139 } vlib_node_registration_t;
140
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)                     \
146 {                                                                       \
147     vlib_main_t * vm = vlib_get_main();                                 \
148     x.next_registration = vm->node_main.node_registrations;             \
149     vm->node_main.node_registrations = &x;                              \
150 }                                                                       \
151 __VA_ARGS__ vlib_node_registration_t x 
152
153 #if CLIB_DEBUG > 0
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)
157 #else
158 #define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt)                \
159   uword                                                                 \
160   __attribute__ ((flatten))                                             \
161   __attribute__ ((target (tgt)))                                        \
162   CLIB_CPU_OPTIMIZED                                                    \
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); }
167
168 #define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)                          \
169   foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
170
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(); }
177 #endif
178
179 always_inline vlib_node_registration_t *
180 vlib_node_next_registered (vlib_node_registration_t * c)
181 {
182   c = clib_elf_section_data_next (c, c->n_next_nodes * sizeof (c->next_nodes[0]));
183   return c;
184 }
185
186 typedef struct {
187   /* Total calls, clock ticks and vector elements processed for this node. */
188   u64 calls, vectors, clocks, suspends;
189   u64 max_clock;
190   u64 max_clock_n;
191 } vlib_node_stats_t;
192
193 #define foreach_vlib_node_state                                 \
194   /* Input node is called each iteration of main loop.          \
195      This is the default (zero). */                             \
196   _ (POLLING)                                                   \
197   /* Input node is called when device signals an interrupt. */  \
198   _ (INTERRUPT)                                                 \
199   /* Input node is never called. */                             \
200   _ (DISABLED)
201
202 typedef enum {
203 #define _(f) VLIB_NODE_STATE_##f,
204   foreach_vlib_node_state
205 #undef _
206   VLIB_N_NODE_STATE,
207 } vlib_node_state_t;
208
209 typedef struct vlib_node_t {
210   /* Vector processing function for this node. */
211   vlib_node_function_t * function;
212
213   /* Node name. */
214   u8 * name;
215
216   /* Node name index in elog string table. */
217   u32 name_elog_string;
218
219   /* Total statistics for this node. */
220   vlib_node_stats_t stats_total;
221
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;
225
226   /* Type of this node. */
227   vlib_node_type_t type;
228
229   /* Node index. */
230   u32 index;
231
232   /* Index of corresponding node runtime. */
233   u32 runtime_index;
234
235   /* Runtime data for this node. */
236   void * runtime_data;
237
238   /* Node flags. */
239   u16 flags;
240
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)
244
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)
250
251   /* Set if current node runtime has traced vectors. */
252 #define VLIB_NODE_FLAG_TRACE (1 << 5)
253
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)
256
257   /* State for input nodes. */
258   u8 state;
259
260   /* Number of bytes of run time data. */
261   u8 runtime_data_bytes;
262
263   /* Number of error codes used by this node. */
264   u16 n_errors;
265
266   /* Size of scalar and vector arguments in bytes. */
267   u16 scalar_size, vector_size;
268
269   /* Handle/index in error heap for this node. */
270   u32 error_heap_handle;
271   u32 error_heap_index;
272
273   /* Error strings indexed by error code for this node. */
274   char ** error_strings;
275
276   /* Vector of next node names.
277      Only used before next_nodes array is initialized. */
278   char ** next_node_names;
279
280   /* Next node indices for this node. */
281   u32 * next_nodes;
282
283   /* Name of node that we are sibling of. */
284   char * sibling_of;
285
286   /* Bitmap of all of this node's siblings. */
287   uword * sibling_bitmap;
288
289   /* Total number of vectors sent to each next node. */
290   u64 * n_vectors_by_next_node;
291
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;
296
297   /* Bitmap of node indices which feed this node. */
298   uword * prev_node_bitmap;
299
300   /* Node/next-index which own enqueue rights with to this node. */
301   u32 owner_node_index, owner_next_index;
302
303   /* Buffer format/unformat for this node. */
304   format_function_t * format_buffer;
305   unformat_function_t * unformat_buffer;
306
307   /* Trace buffer format/unformat for this node. */
308   format_function_t * format_trace;
309
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);
314 } vlib_node_t;
315
316 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
317
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
321
322 /* Calling frame (think stack frame) for a node. */
323 typedef struct vlib_frame_t {
324   /* Frame flags. */
325   u16 flags;
326
327   /* Number of scalar bytes in arguments. */
328   u8 scalar_size;
329
330   /* Number of bytes per vector argument. */
331   u8 vector_size;
332
333   /* Number of vector elements currently in frame. */
334   u16 n_vectors;
335
336   /* Owner cpuid / heap id */
337   u16 cpu_index;
338
339   /* Scalar and vector arguments to next node. */
340   u8 arguments[0];
341 } vlib_frame_t;
342
343 typedef struct {
344   /* Frame index. */
345   u32 frame_index;
346
347   /* Node runtime for this next. */
348   u32 node_runtime_index;
349
350   /* Next frame flags. */
351   u32 flags;
352
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
356
357   /* This next frame owns enqueue to node
358      corresponding to node_runtime_index. */
359 #define VLIB_FRAME_OWNER (1 << 15)
360
361   /* Set when frame has been allocated for this next. */
362 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
363
364   /* Set when frame has been added to pending vector. */
365 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
366
367   /* Set when frame is to be freed after dispatch. */
368 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
369
370   /* Set when frame has traced packets. */
371 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
372
373   /* Number of vectors enqueue to this next since last overflow. */
374   u32 vectors_since_last_overflow;
375 } vlib_next_frame_t;
376
377 always_inline void
378 vlib_next_frame_init (vlib_next_frame_t * nf)
379 {
380   memset (nf, 0, sizeof (nf[0]));
381   nf->frame_index = ~0;
382   nf->node_runtime_index = ~0;
383 }
384
385 /* A frame pending dispatch by main loop. */
386 typedef struct {
387   /* Node and runtime for this frame. */
388   u32 node_runtime_index;
389
390   /* Frame index (in the heap). */
391   u32 frame_index;
392
393   /* Start of next frames for this node. */
394   u32 next_frame_index;
395
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;
399
400 typedef struct vlib_node_runtime_t {
401   /* Node function to call. */
402   vlib_node_function_t * function;
403
404   /* Vector of errors for this node. */
405   vlib_error_t * errors;
406
407   /* Number of clock cycles. */
408   u32 clocks_since_last_overflow;
409
410   /* Maximum clock cycle for an invocation. */
411   u32 max_clock;
412
413   /* Number of vectors in the recorded max_clock. */
414   u32 max_clock_n;
415
416   /* Number of calls. */
417   u32 calls_since_last_overflow;
418
419   /* Number of vector elements processed by this node. */
420   u32 vectors_since_last_overflow;
421
422   /* Start of next frames for this node. */
423   u32 next_frame_index;
424
425   /* Node index. */
426   u32 node_index;
427
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
430      more than others. */
431   u32 input_main_loops_per_call;
432
433   /* Saved main loop counter of last dispatch of this node. */
434   u32 main_loop_count_last_dispatch;
435
436   u32 main_loop_vector_stats[2];
437
438   /* Copy of main node flags. */
439   u16 flags;
440
441   /* Input node state. */
442   u16 state;
443
444   u16 n_next_nodes;
445
446   /* Next frame index that vector arguments were last enqueued to
447      last time this node ran.  Set to zero before first run
448      of this node. */
449   u16 cached_next_index;
450
451   /* CPU this node runs on */
452   u16 cpu_index;
453
454   /* Function dependent node-runtime. */
455   uword runtime_data[(128
456                       - 1 * sizeof (vlib_node_function_t *)
457                       - 1 * sizeof (vlib_error_t *)
458                       - 11 * sizeof (u32)
459                       - 5 * sizeof (u16)) / sizeof (uword)];
460 } vlib_node_runtime_t;
461
462 typedef struct {
463   /* Number of allocated frames for this scalar/vector size. */
464   u32 n_alloc_frames;
465
466   /* Vector of free frame indices for this scalar/vector size. */
467   u32 * free_frame_indices;
468 } vlib_frame_size_t;
469
470 typedef struct {
471   /* Users opaque value for event type. */
472   uword opaque;
473 } vlib_process_event_type_t;
474
475 typedef struct {
476   /* Node runtime for this process. */
477   vlib_node_runtime_t node_runtime;
478
479   /* Where to longjmp when process is done. */
480   clib_longjmp_t return_longjmp;
481
482 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
483 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
484
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
489
490   u16 flags;
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)
495
496   /* Process function is currently running. */
497 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
498
499   /* Size of process stack. */
500   u16 log2_n_stack_bytes;
501
502   u32 suspended_process_frame_index;
503
504   /* Number of times this process was suspended. */
505   u32 n_suspends;
506
507   /* Vectors of pending event data indexed by event type index. */
508   void ** pending_event_data_by_type_index;
509
510   /* Bitmap of event type-indices with non-empty vectors. */
511   uword * non_empty_event_type_bitmap;
512
513   /* Bitmap of event type-indices which are one time events. */
514   uword * one_time_event_type_bitmap;
515
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;
519
520   /* Pool of currently valid event types. */
521   vlib_process_event_type_t * event_type_pool;
522
523   /* When suspending saves cpu cycle counter when process is to be resumed. */
524   u64 resume_cpu_time;
525
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;
530
531 #ifdef CLIB_UNIX
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)))
535 #else
536 #define ALIGN_ON_MULTIPLE_PAGE_BOUNDARY_FOR_MPROTECT
537 #endif
538
539   /* Process stack.  Starts here and extends 2^log2_n_stack_bytes
540      bytes. */
541
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)));
545
546 #ifdef CLIB_UNIX
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];
550 #endif
551
552 typedef struct {
553     u32 node_index;
554
555     u32 one_time_event;
556 } vlib_one_time_waiting_process_t;
557
558 typedef struct {
559   u16 n_data_elts;
560
561   u16 n_data_elt_bytes;
562
563   /* n_data_elts * n_data_elt_bytes */
564   u32 n_data_bytes;
565
566   /* Process node & event type to be used to signal event. */
567   u32 process_node_index;
568
569   u32 event_type_index;
570
571   union {
572     u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
573
574     /* Vector of event data used only when data does not fit inline. */
575     u8 * event_data_as_vector;
576   };
577 } vlib_signal_timed_event_data_t;
578
579 always_inline uword
580 vlib_timing_wheel_data_is_timed_event (u32 d)
581 { return d & 1; }
582
583 always_inline u32
584 vlib_timing_wheel_data_set_suspended_process (u32 i)
585 { return 0 + 2*i; }
586
587 always_inline u32
588 vlib_timing_wheel_data_set_timed_event (u32 i)
589 { return 1 + 2*i; }
590
591 always_inline uword
592 vlib_timing_wheel_data_get_index (u32 d)
593 { return d / 2; }
594
595 typedef struct {
596   /* Public nodes. */
597   vlib_node_t ** nodes;
598
599   /* Node index hashed by node name. */
600   uword * node_by_name;
601
602   u32 flags;
603 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
604
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];
608
609   /* Node runtime indices for input nodes with pending interrupts. */
610   u32 * pending_interrupt_node_runtime_indices;
611
612   /* Input nodes are switched from/to interrupt to/from polling mode
613      when average vector length goes above/below polling/interrupt
614      thresholds. */
615   u32 polling_threshold_vector_length;
616   u32 interrupt_threshold_vector_length;
617
618   /* Vector of next frames. */
619   vlib_next_frame_t * next_frames;
620
621   /* Vector of internal node's frames waiting to be called. */
622   vlib_pending_frame_t * pending_frames;
623
624   /* Timing wheel for scheduling time-based node dispatch. */
625   timing_wheel_t timing_wheel;
626
627   vlib_signal_timed_event_data_t * signal_timed_event_data_pool;
628
629   /* Opaque data vector added via timing_wheel_advance. */
630   u32 * data_from_advancing_timing_wheel;
631
632   /* CPU time of next process to be ready on timing wheel. */
633   u64 cpu_time_next_process_ready;
634
635   /* Vector of process nodes.
636      One for each node of type VLIB_NODE_TYPE_PROCESS. */
637   vlib_process_t ** processes;
638
639   /* Current running process or ~0 if no process running. */
640   u32 current_process_index;
641
642   /* Pool of pending process frames. */
643   vlib_pending_frame_t * suspended_process_frames;
644
645   /* Vector of event data vectors pending recycle. */
646   void ** recycled_event_data_vectors;
647
648   /* Current counts of nodes in each state. */
649   u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
650
651   /* Hash of (scalar_size,vector_size) to frame_sizes index. */
652   uword * frame_size_hash;
653
654   /* Per-size frame allocation information. */
655   vlib_frame_size_t * frame_sizes;
656
657   /* Time of last node runtime stats clear. */
658   f64 time_last_runtime_stats_clear;
659
660   /* Node registrations added by constructors */
661   vlib_node_registration_t * node_registrations;
662 } vlib_node_main_t;
663
664 #endif /* included_vlib_node_h */