1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __FIB_TYPES_H__
#define __FIB_TYPES_H__
#include <stdbool.h>
#include <vlib/vlib.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/mpls/packet.h>
#include <vnet/dpo/dpo.h>
#include <vnet/bier/bier_types.h>
/**
* A typedef of a node index.
* we make this typedef so the code becomes easier for a human to parse.
*/
typedef u32 fib_node_index_t;
#define FIB_NODE_INDEX_INVALID ((fib_node_index_t)(~0))
/**
* Protocol Type. packed so it consumes a u8 only
*/
typedef enum fib_protocol_t_ {
FIB_PROTOCOL_IP4 = DPO_PROTO_IP4,
FIB_PROTOCOL_IP6 = DPO_PROTO_IP6,
FIB_PROTOCOL_MPLS = DPO_PROTO_MPLS,
} __attribute__ ((packed)) fib_protocol_t;
#define FIB_PROTOCOLS { \
[FIB_PROTOCOL_IP4] = "ipv4", \
[FIB_PROTOCOL_IP6] = "ipv6", \
[FIB_PROTOCOL_MPLS] = "MPLS", \
}
/**
* Definition outside of enum so it does not need to be included in non-defaulted
* switch statements
*/
#define FIB_PROTOCOL_MAX (FIB_PROTOCOL_MPLS + 1)
/**
* Definition outside of enum so it does not need to be included in non-defaulted
* switch statements
*/
#define FIB_PROTOCOL_IP_MAX (FIB_PROTOCOL_IP6 + 1)
/**
* Not part of the enum so it does not have to be handled in switch statements
*/
#define FIB_PROTOCOL_NONE (FIB_PROTOCOL_MAX+1)
#define FOR_EACH_FIB_PROTOCOL(_item) \
for (_item = FIB_PROTOCOL_IP4; \
_item <= FIB_PROTOCOL_MPLS; \
_item++)
#define FOR_EACH_FIB_IP_PROTOCOL(_item) \
for (_item = FIB_PROTOCOL_IP4; \
_item <= FIB_PROTOCOL_IP6; \
_item++)
/**
* @brief Convert from boolean is_ip6 to FIB protocol.
* Drop MPLS on the floor in favor of IPv4.
*/
static inline fib_protocol_t
fib_ip_proto(bool is_ip6)
{
return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
}
/**
* @brief Convert from fib_protocol to ip46_type
*/
extern ip46_type_t /*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* node.h: VLIB processing nodes
*
* Copyright (c) 2008 Eliot Dresselhaus
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef included_vlib_node_h
#define included_vlib_node_h
#include <vppinfra/cpu.h>
#include <vppinfra/longjmp.h>
#include <vppinfra/lock.h>
#include <vlib/trace.h> /* for vlib_trace_filter_t */
/* Forward declaration. */
struct vlib_node_runtime_t;
struct vlib_frame_t;
/* Internal nodes (including output nodes) move data from node to
node (or out of the graph for output nodes). */
typedef uword (vlib_node_function_t) (struct vlib_main_t * vm,
struct vlib_node_runtime_t * node,
struct vlib_frame_t * frame);
typedef enum
{
/* An internal node on the call graph (could be output). */
VLIB_NODE_TYPE_INTERNAL,
/* Nodes which input data into the processing graph.
Input nodes are called for each iteration of main loop. */
VLIB_NODE_TYPE_INPUT,
/* Nodes to be called before all input nodes.
Used, for example, to clean out driver TX rings before
processing input. */
VLIB_NODE_TYPE_PRE_INPUT,
/* "Process" nodes which can be suspended and later resumed. */
VLIB_NODE_TYPE_PROCESS,
VLIB_N_NODE_TYPE,
} vlib_node_type_t;
typedef struct _vlib_node_fn_registration
{
vlib_node_function_t *function;
int priority;
struct _vlib_node_fn_registration *next_registration;
} vlib_node_fn_registration_t;
typedef struct _vlib_node_registration
{
/* Vector processing function for this node. */
vlib_node_function_t *function;
/* Node function candidate registration with priority */
vlib_node_fn_registration_t *node_fn_registrations;
/* Node name. */
char *name;
/* Name of sibling (if applicable). */
char *sibling_of;
/* Node index filled in by registration. */
u32 index;
/* Type of this node. */
vlib_node_type_t type;
/* Error strings indexed by error code for this node. */
char **error_strings;
/* Buffer format/unformat for this node. */
format_function_t *format_buffer;
unformat_function_t *unformat_buffer;
/* Trace format/unformat for this node. */
format_function_t *format_trace;
unformat_function_t *unformat_trace;
/* Function to validate incoming frames. */
u8 *(*validate_frame) (struct vlib_main_t * vm,
struct vlib_node_runtime_t *,
struct vlib_frame_t * f);
/* Per-node runtime data. */
void *runtime_data;
/* Process stack size. */
u16 process_log2_n_stack_bytes;
/* Number of bytes of per-node run time data. */
u8 runtime_data_bytes;
/* State for input nodes. */
u8 state;
/* Node flags. */
u16 flags;
/* Size of scalar and vector arguments in bytes. */
u16 scalar_size, vector_size;
/* Number of error codes used by this node. */
u16 n_errors;
/* Number of next node names that follow. */
u16 n_next_nodes;
/* Constructor link-list, don't ask... */
struct _vlib_node_registration *next_registration;
/* Names of next nodes which this node feeds into. */
char *next_nodes[];
} vlib_node_registration_t;
#define VLIB_REGISTER_NODE(x,...) \
__VA_ARGS__ vlib_node_registration_t x; \
static void __vlib_add_node_registration_##x (void) \
__attribute__((__constructor__)) ; \
static void __vlib_add_node_registration_##x (void) \
{ \
vlib_main_t * vm = vlib_get_main(); \
x.next_registration = vm->node_main.node_registrations; \
vm->node_main.node_registrations = &x; \
} \
static void __vlib_rm_node_registration_##x (void) \
__attribute__((__destructor__)) ; \
static void __vlib_rm_node_registration_##x (void) \
{ \
vlib_main_t * vm = vlib_get_main(); \
VLIB_REMOVE_FROM_LINKED_LIST (vm->node_main.node_registrations, \
&x, next_registration); \
} \
__VA_ARGS__ vlib_node_registration_t x
#define VLIB_NODE_FN(node) \
uword CLIB_MARCH_SFX (node##_fn)(); \
static vlib_node_fn_registration_t \
CLIB_MARCH_SFX(node##_fn_registration) = \
{ .function = &CLIB_MARCH_SFX (node##_fn), }; \
\
static void __clib_constructor \
CLIB_MARCH_SFX (node##_multiarch_register) (void) \
{ \
extern vlib_node_registration_t node; \
vlib_node_fn_registration_t *r; \
r = & CLIB_MARCH_SFX (node##_fn_registration); \
r->priority = CLIB_MARCH_FN_PRIORITY(); \
r->next_registration = node.node_fn_registrations; \
node.node_fn_registrations = r; \
} \
uword CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (node##_fn)
#if CLIB_DEBUG > 0
#define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn)
#define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn)
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
#else
#define VLIB_NODE_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
uword \
__attribute__ ((flatten)) \
__attribute__ ((target (tgt))) \
CLIB_CPU_OPTIMIZED \
fn ## _ ## arch ( struct vlib_main_t * vm, \
struct vlib_node_runtime_t * node, \
struct vlib_frame_t * frame) \
{ return fn (vm, node, frame); }
#define VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
foreach_march_variant(VLIB_NODE_FUNCTION_CLONE_TEMPLATE, fn)
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn) \
VLIB_NODE_FUNCTION_MULTIARCH_CLONE(fn) \
CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
static void __attribute__((__constructor__)) \
__vlib_node_function_multiarch_select_##node (void) \
{ node.function = fn ## _multiarch_select(); }
#endif
always_inline vlib_node_registration_t *
vlib_node_next_registered (vlib_node_registration_t * c)
{
c =
clib_elf_section_data_next (c,
c->n_next_nodes * sizeof (c->next_nodes[0]));
return c;
}
typedef struct
{
/* Total calls, clock ticks and vector elements processed for this node. */
u64 calls, vectors, clocks, suspends;
u64 max_clock;
u64 max_clock_n;
} vlib_node_stats_t;
#define foreach_vlib_node_state \
/* Input node is called each iteration of main loop. \
This is the default (zero). */ \
_ (POLLING) \
/* Input node is called when device signals an interrupt. */ \
_ (INTERRUPT) \
/* Input node is never called. */ \
_ (DISABLED)
typedef enum
{
#define _(f) VLIB_NODE_STATE_##f,
foreach_vlib_node_state
#undef _
VLIB_N_NODE_STATE,
} vlib_node_state_t;
typedef struct vlib_node_t
{
/* Vector processing function for this node. */
vlib_node_function_t *function;
/* Node name. */
u8 *name;
/* Node name index in elog string table. */
u32 name_elog_string;
/* Total statistics for this node. */
vlib_node_stats_t stats_total;
/* Saved values as of last clear (or zero if never cleared).
Current values are always stats_total - stats_last_clear. */
vlib_node_stats_t stats_last_clear;
/* Type of this node. */
vlib_node_type_t type;
/* Node index. */
u32 index;
/* Index of corresponding node runtime. */
u32 runtime_index;
/* Runtime data for this node. */
void *runtime_data;
/* Node flags. */
u16 flags;
/* Processing function keeps frame. Tells node dispatching code not
to free frame after dispatch is done. */
#define VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH (1 << 0)
/* Node counts as output/drop/punt node for stats purposes. */
#define VLIB_NODE_FLAG_IS_OUTPUT (1 << 1)
#define VLIB_NODE_FLAG_IS_DROP (1 << 2)
#define VLIB_NODE_FLAG_IS_PUNT (1 << 3)
#define VLIB_NODE_FLAG_IS_HANDOFF (1 << 4)
/* Set if current node runtime has traced vectors. */
#define VLIB_NODE_FLAG_TRACE (1 << 5)
#define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE (1 << 6)
#define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE (1 << 7)
/* State for input nodes. */
u8 state;
/* Number of bytes of run time data. */
u8 runtime_data_bytes;
/* Number of error codes used by this node. */
u16 n_errors;
/* Size of scalar and vector arguments in bytes. */
u16 scalar_size, vector_size;
/* Handle/index in error heap for this node. */
u32 error_heap_handle;
u32 error_heap_index;
/* Error strings indexed by error code for this node. */
char **error_strings;
/* Vector of next node names.
Only used before next_nodes array is initialized. */
char **next_node_names;
/* Next node indices for this node. */
u32 *next_nodes;
/* Name of node that we are sibling of. */
char *sibling_of;
/* Bitmap of all of this node's siblings. */
uword *sibling_bitmap;
/* Total number of vectors sent to each next node. */
u64 *n_vectors_by_next_node;
/* Hash table mapping next node index into slot in
next_nodes vector. Quickly determines whether this node
is connected to given next node and, if so, with which slot. */
uword *next_slot_by_node;
/* Bitmap of node indices which feed this node. */
uword *prev_node_bitmap;
/* Node/next-index which own enqueue rights with to this node. */
u32 owner_node_index, owner_next_index;
/* Buffer format/unformat for this node. */
format_function_t *format_buffer;
unformat_function_t *unformat_buffer;
/* Trace buffer format/unformat for this node. */
format_function_t *format_trace;
/* Function to validate incoming frames. */
u8 *(*validate_frame) (struct vlib_main_t * vm,
struct vlib_node_runtime_t *,
struct vlib_frame_t * f);
/* for pretty-printing, not typically valid */
u8 *state_string;
} vlib_node_t;
#define VLIB_INVALID_NODE_INDEX ((u32) ~0)
/* Max number of vector elements to process at once per node. */
#define VLIB_FRAME_SIZE 256
#define VLIB_FRAME_ALIGN CLIB_CACHE_LINE_BYTES
/* Calling frame (think stack frame) for a node. */
typedef struct vlib_frame_t
{
/* Frame flags. */
u16 flags;
/* Number of scalar bytes in arguments. */
u8 scalar_size;
/* Number of bytes per vector argument. */
u8 vector_size;
/* Number of vector elements currently in frame. */
u16 n_vectors;
/* Scalar and vector arguments to next node. */
u8 arguments[0];
} vlib_frame_t;
typedef struct
{
/* Frame index. */
u32 frame_index;
/* Node runtime for this next. */
u32 node_runtime_index;
/* Next frame flags. */
u32 flags;
/* Reflects node frame-used flag for this next. */
#define VLIB_FRAME_NO_FREE_AFTER_DISPATCH \
VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
/* This next frame owns enqueue to node
corresponding to node_runtime_index. */
#define VLIB_FRAME_OWNER (1 << 15)
/* Set when frame has been allocated for this next. */
#define VLIB_FRAME_IS_ALLOCATED VLIB_NODE_FLAG_IS_OUTPUT
/* Set when frame has been added to pending vector. */
#define VLIB_FRAME_PENDING VLIB_NODE_FLAG_IS_DROP
/* Set when frame is to be freed after dispatch. */
#define VLIB_FRAME_FREE_AFTER_DISPATCH VLIB_NODE_FLAG_IS_PUNT
/* Set when frame has traced packets. */
#define VLIB_FRAME_TRACE VLIB_NODE_FLAG_TRACE
/* Number of vectors enqueue to this next since last overflow. */
u32 vectors_since_last_overflow;
} vlib_next_frame_t;
always_inline void
vlib_next_frame_init (vlib_next_frame_t * nf)
{
memset (nf, 0, sizeof (nf[0]));
nf->frame_index = ~0;
nf->node_runtime_index = ~0;
}
/* A frame pending dispatch by main loop. */
typedef struct
{
/* Node and runtime for this frame. */
u32 node_runtime_index;
/* Frame index (in the heap). */
u32 frame_index;
/* Start of next frames for this node. */
u32 next_frame_index;
/* Special value for next_frame_index when there is no next frame. */
#define VLIB_PENDING_FRAME_NO_NEXT_FRAME ((u32) ~0)
} vlib_pending_frame_t;
typedef struct vlib_node_runtime_t
{
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); /**< cacheline mark */
vlib_node_function_t *function; /**< Node function to call. */
vlib_error_t *errors; /**< Vector of errors for this node. */
#if __SIZEOF_POINTER__ == 4
u8 pad[8];
#endif
u32 clocks_since_last_overflow; /**< Number of clock cycles. */
u32 max_clock; /**< Maximum clock cycle for an
invocation. */
u32 max_clock_n; /**< Number of vectors in the recorded
max_clock. */
u32 calls_since_last_overflow; /**< Number of calls. */
u32 vectors_since_last_overflow; /**< Number of vector elements
processed by this node. */
u32 next_frame_index; /**< Start of next frames for this
node. */
u32 node_index; /**< Node index. */
u32 input_main_loops_per_call; /**< For input nodes: decremented
on each main loop interation until
it reaches zero and function is
called. Allows some input nodes to
be called more than others. */
u32 main_loop_count_last_dispatch; /**< Saved main loop counter of last
dispatch of this node. */
u32 main_loop_vector_stats[2];
u16 flags; /**< Copy of main node flags. */
u16 state; /**< Input node state. */
u16 n_next_nodes;
u16 cached_next_index; /**< Next frame index that vector
arguments were last enqueued to
last time this node ran. Set to
zero before first run of this
node. */
u16 thread_index; /**< thread this node runs on */
u8 runtime_data[0]; /**< Function dependent
node-runtime data. This data is
thread local, and it is not
cloned from main thread. It needs
to be initialized for each thread
before it is used unless
runtime_data template exists in
vlib_node_t. */
}
vlib_node_runtime_t;
#define VLIB_NODE_RUNTIME_DATA_SIZE (sizeof (vlib_node_runtime_t) - STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data))
typedef struct
{
/* Number of allocated frames for this scalar/vector size. */
u32 n_alloc_frames;
/* Vector of free frame indices for this scalar/vector size. */
u32 *free_frame_indices;
} vlib_frame_size_t;
typedef struct
{
/* Users opaque value for event type. */
uword opaque;
} vlib_process_event_type_t;
typedef struct
{
/* Node runtime for this process. */
vlib_node_runtime_t node_runtime;
/* Where to longjmp when process is done. */
clib_longjmp_t return_longjmp |