Initial commit of vpp code.
[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/longjmp.h>
44 #include <vppinfra/timing_wheel.h>
45 #include <vlib/trace.h>         /* for vlib_trace_filter_t */
46
47 /* Forward declaration. */
48 struct vlib_node_runtime_t;
49 struct vlib_frame_t;
50
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);
56
57 typedef enum {
58   /* An internal node on the call graph (could be output). */
59   VLIB_NODE_TYPE_INTERNAL,
60
61   /* Nodes which input data into the processing graph.
62      Input nodes are called for each iteration of main loop. */
63   VLIB_NODE_TYPE_INPUT,
64
65   /* Nodes to be called before all input nodes.
66      Used, for example, to clean out driver TX rings before
67      processing input. */
68   VLIB_NODE_TYPE_PRE_INPUT,
69
70   /* "Process" nodes which can be suspended and later resumed. */
71   VLIB_NODE_TYPE_PROCESS,
72
73   VLIB_N_NODE_TYPE,
74 } vlib_node_type_t;
75
76 typedef struct _vlib_node_registration {
77   /* Vector processing function for this node. */
78   vlib_node_function_t * function;
79
80   /* Node name. */
81   char * name;
82
83   /* Name of sibling (if applicable). */
84   char * sibling_of;
85
86   /* Node index filled in by registration. */
87   u32 index;
88
89   /* Type of this node. */
90   vlib_node_type_t type;
91
92   /* Error strings indexed by error code for this node. */
93   char ** error_strings;
94
95   /* Buffer format/unformat for this node. */
96   format_function_t * format_buffer;
97   unformat_function_t * unformat_buffer;
98
99   /* Trace format/unformat for this node. */
100   format_function_t * format_trace;
101   unformat_function_t * unformat_trace;
102
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);
107
108   /* Per-node runtime data. */
109   void * runtime_data;
110
111   /* Process stack size. */
112   u16 process_log2_n_stack_bytes;
113
114   /* Number of bytes of per-node run time data. */
115   u8 runtime_data_bytes;
116
117   /* State for input nodes. */
118   u8 state;
119
120   /* Node flags. */
121   u16 flags;
122
123   /* Size of scalar and vector arguments in bytes. */
124   u16 scalar_size, vector_size;
125
126   /* Number of error codes used by this node. */
127   u16 n_errors;
128
129   /* Number of next node names that follow. */
130   u16 n_next_nodes;
131
132   /* Constructor link-list, don't ask... */
133   struct _vlib_node_registration * next_registration;
134
135   /* Names of next nodes which this node feeds into. */
136   char * next_nodes[];
137
138 } vlib_node_registration_t;
139
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)                     \
145 {                                                                       \
146     vlib_main_t * vm = vlib_get_main();                                 \
147     x.next_registration = vm->node_main.node_registrations;             \
148     vm->node_main.node_registrations = &x;                              \
149 }                                                                       \
150 __VA_ARGS__ vlib_node_registration_t x 
151
152 always_inline vlib_node_registration_t *
153 vlib_node_next_registered (vlib_node_registration_t * c)
154 {
155   c = clib_elf_section_data_next (c, c->n_next_nodes * sizeof (c->next_nodes[0]));
156   return c;
157 }
158
159 typedef struct {
160   /* Total calls, clock ticks and vector elements processed for this node. */
161   u64 calls, vectors, clocks, suspends;
162   u64 max_clock;
163   u64 max_clock_n;
164 } vlib_node_stats_t;
165
166 #define foreach_vlib_node_state                                 \
167   /* Input node is called each iteration of main loop.          \
168      This is the default (zero). */                             \
169   _ (POLLING)                                                   \
170   /* Input node is called when device signals an interrupt. */  \
171   _ (INTERRUPT)                                                 \
172   /* Input node is never called. */                             \
173   _ (DISABLED)
174
175 typedef enum {
176 #define _(f) VLIB_NODE_STATE_##f,
177   foreach_vlib_node_state
178 #undef _
179   VLIB_N_NODE_STATE,
180 } vlib_node_state_t;
181
182 typedef struct vlib_node_t {
183   /* Vector processing function for this node. */
184   vlib_node_function_t * function;
185
186   /* Node name. */
187   u8 * name;
188
189   /* Node name index in elog string table. */
190   u32 name_elog_string;
191
192   /* Total statistics for this node. */
193   vlib_node_stats_t stats_total;
194
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;
198
199   /* Type of this node. */
200   vlib_node_type_t type;
201
202   /* Node index. */
203   u32 index;
204
205   /* Index of corresponding node runtime. */
206   u32 runtime_index;
207
208   /* Runtime data for this node. */
209   void * runtime_data;
210
211   /* Node flags. */
212   u16 flags;
213
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)
217
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)
223
224   /* Set if current node runtime has traced vectors. */
225 #define VLIB_NODE_FLAG_TRACE (1 << 5)
226
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)
229
230   /* State for input nodes. */
231   u8 state;
232
233   /* Number of bytes of run time data. */
234   u8 runtime_data_bytes;
235
236   /* Number of error codes used by this node. */
237   u16 n_errors;
238
239   /* Size of scalar and vector arguments in bytes. */
240   u16 scalar_size, vector_size;
241
242   /* Handle/index in error heap for this node. */
243   u32 error_heap_handle;
244   u32 error_heap_index;
245
246   /* Error strings indexed by error code for this node. */
247   char ** error_strings;
248
249   /* Vector of next node names.
250      Only used before next_nodes array is initialized. */
251   char ** next_node_names;
252
253   /* Next node indices for this node. */
254   u32 * next_nodes;
255
256   /* Name of node that we are sibling of. */
257   char * sibling_of;
258
259   /* Bitmap of all of this node's siblings. */
260   uword * sibling_bitmap;
261
262   /* Total number of vectors sent to each next node. */
263   u64 * n_vectors_by_next_node;
264
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;
269
270   /* Bitmap of node indices which feed this node. */
271   uword * prev_node_bitmap;
272
273   /* Node/next-index which own enqueue rights with to this node. */
274   u32 owner_node_index, owner_next_index;
275
276   /* Buffer format/unformat for this node. */
277   format_function_t * format_buffer;
278   unformat_function_t * unformat_buffer;
279
280   /* Trace buffer format/unformat for this node. */
281   format_function_t * format_trace;
282
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);
287 } vlib_node_t;
288
289 #define VLIB_INVALID_NODE_INDEX ((u32) ~0)
290
291 /* Max number of vector elements to process at once per node. */
292 #define VLIB_FRAME_SIZE 256
293
294 /* Calling frame (think stack frame) for a node. */
295 typedef struct vlib_frame_t {
296   /* Frame flags. */
297   u16 flags;
298
299   /* Number of scalar bytes in arguments. */
300   u8 scalar_size;
301
302   /* Number of bytes per vector argument. */
303   u8 vector_size;
304
305   /* Number of vector elements currently in frame. */
306   u16 n_vectors;
307
308   /* Owner cpuid / heap id */
309   u16 cpu_index;
310
311   /* Scalar and vector arguments to next node. */
312   u8 arguments[0];
313 } vlib_frame_t;
314
315 typedef struct {
316   /* Frame index. */
317   u32 frame_index;
318
319   /* Node runtime for this next. */
320   u32 node_runtime_index;
321
322   /* Next frame flags. */
323   u32 flags;
324
325   /* Reflects node frame-used flag for this next. */
326 #define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
327   VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
328
329   /* This next frame owns enqueue to node
330      corresponding to node_runtime_index. */
331 #define VLIB_FRAME_OWNER (1 << 15)
332
333   /* Set when frame has been allocated for this next. */
334 #define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
335
336   /* Set when frame has been added to pending vector. */
337 #define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
338
339   /* Set when frame is to be freed after dispatch. */
340 #define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
341
342   /* Set when frame has traced packets. */
343 #define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
344
345   /* Number of vectors enqueue to this next since last overflow. */
346   u32 vectors_since_last_overflow;
347 } vlib_next_frame_t;
348
349 always_inline void
350 vlib_next_frame_init (vlib_next_frame_t * nf)
351 {
352   memset (nf, 0, sizeof (nf[0]));
353   nf->frame_index = ~0;
354   nf->node_runtime_index = ~0;
355 }
356
357 /* A frame pending dispatch by main loop. */
358 typedef struct {
359   /* Node and runtime for this frame. */
360   u32 node_runtime_index;
361
362   /* Frame index (in the heap). */
363   u32 frame_index;
364
365   /* Start of next frames for this node. */
366   u32 next_frame_index;
367
368   /* Special value for next_frame_index when there is no next frame. */
369 #define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
370 } vlib_pending_frame_t;
371
372 typedef struct vlib_node_runtime_t {
373   /* Node function to call. */
374   vlib_node_function_t * function;
375
376   /* Vector of errors for this node. */
377   vlib_error_t * errors;
378
379   /* Number of clock cycles. */
380   u32 clocks_since_last_overflow;
381
382   /* Maximum clock cycle for an invocation. */
383   u32 max_clock;
384
385   /* Number of vectors in the recorded max_clock. */
386   u32 max_clock_n;
387
388   /* Number of calls. */
389   u32 calls_since_last_overflow;
390
391   /* Number of vector elements processed by this node. */
392   u32 vectors_since_last_overflow;
393
394   /* Start of next frames for this node. */
395   u32 next_frame_index;
396
397   /* Node index. */
398   u32 node_index;
399
400   /* For input nodes: decremented on each main loop interation until it reaches zero
401      and function is called.  Allows some input nodes to be called
402      more than others. */
403   u32 input_main_loops_per_call;
404
405   /* Saved main loop counter of last dispatch of this node. */
406   u32 main_loop_count_last_dispatch;
407
408   u32 main_loop_vector_stats[2];
409
410   /* Copy of main node flags. */
411   u16 flags;
412
413   /* Input node state. */
414   u16 state;
415
416   u16 n_next_nodes;
417
418   /* Next frame index that vector arguments were last enqueued to
419      last time this node ran.  Set to zero before first run
420      of this node. */
421   u16 cached_next_index;
422
423   /* CPU this node runs on */
424   u16 cpu_index;
425
426   /* Function dependent node-runtime. */
427   uword runtime_data[(128
428                       - 1 * sizeof (vlib_node_function_t *)
429                       - 1 * sizeof (vlib_error_t *)
430                       - 11 * sizeof (u32)
431                       - 5 * sizeof (u16)) / sizeof (uword)];
432 } vlib_node_runtime_t;
433
434 typedef struct {
435   /* Number of allocated frames for this scalar/vector size. */
436   u32 n_alloc_frames;
437
438   /* Vector of free frame indices for this scalar/vector size. */
439   u32 * free_frame_indices;
440 } vlib_frame_size_t;
441
442 typedef struct {
443   /* Users opaque value for event type. */
444   uword opaque;
445 } vlib_process_event_type_t;
446
447 typedef struct {
448   /* Node runtime for this process. */
449   vlib_node_runtime_t node_runtime;
450
451   /* Where to longjmp when process is done. */
452   clib_longjmp_t return_longjmp;
453
454 #define VLIB_PROCESS_RETURN_LONGJMP_RETURN ((uword) ~0 - 0)
455 #define VLIB_PROCESS_RETURN_LONGJMP_SUSPEND ((uword) ~0 - 1)
456
457   /* Where to longjmp to resume node after suspend. */
458   clib_longjmp_t resume_longjmp;
459 #define VLIB_PROCESS_RESUME_LONGJMP_SUSPEND 0
460 #define VLIB_PROCESS_RESUME_LONGJMP_RESUME  1
461
462   u16 flags;
463 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK (1 << 0)
464 #define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT (1 << 1)
465   /* Set to indicate that this process has been added to resume vector. */
466 #define VLIB_PROCESS_RESUME_PENDING (1 << 2)
467
468   /* Process function is currently running. */
469 #define VLIB_PROCESS_IS_RUNNING (1 << 3)
470
471   /* Size of process stack. */
472   u16 log2_n_stack_bytes;
473
474   u32 suspended_process_frame_index;
475
476   /* Number of times this process was suspended. */
477   u32 n_suspends;
478
479   /* Vectors of pending event data indexed by event type index. */
480   void ** pending_event_data_by_type_index;
481
482   /* Bitmap of event type-indices with non-empty vectors. */
483   uword * non_empty_event_type_bitmap;
484
485   /* Bitmap of event type-indices which are one time events. */
486   uword * one_time_event_type_bitmap;
487
488   /* Type is opaque pointer -- typically a pointer to an event handler
489      function.  Hash table to map opaque to a type index. */
490   uword * event_type_index_by_type_opaque;
491
492   /* Pool of currently valid event types. */
493   vlib_process_event_type_t * event_type_pool;
494
495   /* When suspending saves cpu cycle counter when process is to be resumed. */
496   u64 resume_cpu_time;
497
498 #ifdef CLIB_UNIX
499   /* Pad to a multiple of the page size so we can mprotect process stacks */
500   CLIB_PAD_FROM_TO (0x140, 0x1000);
501 #endif
502   /* Process stack.  Starts here and extends 2^log2_n_stack_bytes
503      bytes. */
504
505 #define VLIB_PROCESS_STACK_MAGIC (0xdead7ead)
506   u32 stack[0];
507 } vlib_process_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
508
509 typedef struct {
510     u32 node_index;
511
512     u32 one_time_event;
513 } vlib_one_time_waiting_process_t;
514
515 typedef struct {
516   u16 n_data_elts;
517
518   u16 n_data_elt_bytes;
519
520   /* n_data_elts * n_data_elt_bytes */
521   u32 n_data_bytes;
522
523   /* Process node & event type to be used to signal event. */
524   u32 process_node_index;
525
526   u32 event_type_index;
527
528   union {
529     u8 inline_event_data[64 - 3 * sizeof (u32) - 2 * sizeof (u16)];
530
531     /* Vector of event data used only when data does not fit inline. */
532     u8 * event_data_as_vector;
533   };
534 } vlib_signal_timed_event_data_t;
535
536 always_inline uword
537 vlib_timing_wheel_data_is_timed_event (u32 d)
538 { return d & 1; }
539
540 always_inline u32
541 vlib_timing_wheel_data_set_suspended_process (u32 i)
542 { return 0 + 2*i; }
543
544 always_inline u32
545 vlib_timing_wheel_data_set_timed_event (u32 i)
546 { return 1 + 2*i; }
547
548 always_inline uword
549 vlib_timing_wheel_data_get_index (u32 d)
550 { return d / 2; }
551
552 typedef struct {
553   /* Public nodes. */
554   vlib_node_t ** nodes;
555
556   /* Node index hashed by node name. */
557   uword * node_by_name;
558
559   u32 flags;
560 #define VLIB_NODE_MAIN_RUNTIME_STARTED (1 << 0)
561
562   /* Nodes segregated by type for cache locality.
563      Does not apply to nodes of type VLIB_NODE_TYPE_INTERNAL. */
564   vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE];
565
566   /* Node runtime indices for input nodes with pending interrupts. */
567   u32 * pending_interrupt_node_runtime_indices;
568
569   /* Input nodes are switched from/to interrupt to/from polling mode
570      when average vector length goes above/below polling/interrupt
571      thresholds. */
572   u32 polling_threshold_vector_length;
573   u32 interrupt_threshold_vector_length;
574
575   /* Vector of next frames. */
576   vlib_next_frame_t * next_frames;
577
578   /* Vector of internal node's frames waiting to be called. */
579   vlib_pending_frame_t * pending_frames;
580
581   /* Timing wheel for scheduling time-based node dispatch. */
582   timing_wheel_t timing_wheel;
583
584   vlib_signal_timed_event_data_t * signal_timed_event_data_pool;
585
586   /* Opaque data vector added via timing_wheel_advance. */
587   u32 * data_from_advancing_timing_wheel;
588
589   /* CPU time of next process to be ready on timing wheel. */
590   u64 cpu_time_next_process_ready;
591
592   /* Vector of process nodes.
593      One for each node of type VLIB_NODE_TYPE_PROCESS. */
594   vlib_process_t ** processes;
595
596   /* Current running process or ~0 if no process running. */
597   u32 current_process_index;
598
599   /* Pool of pending process frames. */
600   vlib_pending_frame_t * suspended_process_frames;
601
602   /* Vector of event data vectors pending recycle. */
603   void ** recycled_event_data_vectors;
604
605   /* Current counts of nodes in each state. */
606   u32 input_node_counts_by_state[VLIB_N_NODE_STATE];
607
608   /* Hash of (scalar_size,vector_size) to frame_sizes index. */
609   uword * frame_size_hash;
610
611   /* Per-size frame allocation information. */
612   vlib_frame_size_t * frame_sizes;
613
614   /* Time of last node runtime stats clear. */
615   f64 time_last_runtime_stats_clear;
616
617   /* Node registrations added by constructors */
618   vlib_node_registration_t * node_registrations;
619 } vlib_node_main_t;
620
621 #endif /* included_vlib_node_h */