FD.io VPP  v20.05.1-5-g09f167997
Vector Packet Processing
main.h
Go to the documentation of this file.
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  * main.h: VLIB main data structure
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_main_h
41 #define included_vlib_main_h
42 
43 #include <vppinfra/elog.h>
44 #include <vppinfra/format.h>
45 #include <vppinfra/longjmp.h>
46 #include <vppinfra/pool.h>
47 #include <vppinfra/random_buffer.h>
48 #include <vppinfra/time.h>
49 #include <vppinfra/pmc.h>
50 #include <vppinfra/pcap.h>
51 
52 #include <pthread.h>
53 
54 
55 /* By default turn off node/error event logging.
56  Override with -DVLIB_ELOG_MAIN_LOOP */
57 #ifndef VLIB_ELOG_MAIN_LOOP
58 #define VLIB_ELOG_MAIN_LOOP 0
59 #endif
60 
61 typedef struct
62 {
63  /* Trace RX pkts */
65  /* Trace TX pkts */
67  /* Trace drop pkts */
74 } vnet_pcap_t;
75 
76 typedef struct
77 {
82 
83 typedef struct vlib_main_t
84 {
85  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
86  /* Instruction level timing state. */
88  /* Offset from main thread time */
91 
92  /* Time stamp of last node dispatch. */
94 
95  /* Time stamp when main loop was entered (time 0). */
97 
98  /* Incremented once for each main loop. */
99  volatile u32 main_loop_count;
100 
101  /* Count of vectors processed this main loop. */
104 
105  /* Internal node vectors, calls */
110 
111  /* Instantaneous vector rate */
113 
114  /* Main loop hw / sw performance counters */
116  u64 *, u64 *,
118  vlib_frame_t *, int);
120  u64 *, u64 *,
121  vlib_node_runtime_t *,
122  vlib_frame_t *, int);
123  /* Every so often we switch to the next counter. */
124 #define VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE 7
125 
126  /* Jump target to exit main loop with given code. */
128  /* Set e.g. in the SIGTERM signal handler, checked in a safe place... */
131 #define VLIB_MAIN_LOOP_EXIT_NONE 0
132 #define VLIB_MAIN_LOOP_EXIT_PANIC 1
133  /* Exit via CLI. */
134 #define VLIB_MAIN_LOOP_EXIT_CLI 2
135 
136  /* Error marker to use when exiting main loop. */
138 
139  /* Name for e.g. syslog. */
140  char *name;
141 
142  /* Start of the heap. */
143  void *heap_base;
144 
145  /* Truncated version, to create frame indices */
147 
148  /* Size of the heap */
150 
151  /* buffer main structure. */
153 
154  /* physical memory main structure. */
156 
157  /* Node graph main structure. */
159 
160  /* Command line interface. */
162 
163  /* Packet trace buffer. */
165 
166  /* Pcap dispatch trace main */
171 
172  /* pcap rx / tx tracing */
174 
175  /* Packet trace capture filter */
177 
178  /* Error handling. */
180 
181  /* Punt packets to underlying operating system for when fast switching
182  code does not know what to do. */
183  void (*os_punt_frame) (struct vlib_main_t * vm,
184  struct vlib_node_runtime_t * node,
185  vlib_frame_t * frame);
186 
187  /* Stream index to use for distribution when MC is enabled. */
189 
191 
192  /* Event logger. */
194 
195  /* Event logger trace flags */
201 
202  /* Node call and return event types. */
205 
207 
208  /* Seed for random number generator. */
210 
211  /* Buffer of random data for various uses. */
213 
214  /* Hash table to record which init functions have been called. */
216 
217  /* thread, cpu and numa_node indices */
221 
222  /* List of init functions to call, setup by constructors */
223  _vlib_init_function_list_elt_t *init_function_registrations;
224  _vlib_init_function_list_elt_t *worker_init_function_registrations;
225  _vlib_init_function_list_elt_t *main_loop_enter_function_registrations;
226  _vlib_init_function_list_elt_t *main_loop_exit_function_registrations;
227  _vlib_init_function_list_elt_t *api_init_function_registrations;
229 
230  /* control-plane API queue signal pending, length indication */
233  void (*queue_signal_callback) (struct vlib_main_t *);
234  u8 **argv;
235 
236  /* Top of (worker) dispatch loop callback */
237  void (**volatile worker_thread_main_loop_callbacks) (struct vlib_main_t *);
238  void (**volatile worker_thread_main_loop_callback_tmp)
239  (struct vlib_main_t *);
241 
242  /* debugging */
243  volatile int parked_at_barrier;
244 
245  /* Attempt to do a post-mortem elog dump */
247 
248  /*
249  * Need to call vlib_worker_thread_node_runtime_update before
250  * releasing worker thread barrier. Only valid in vlib_global_main.
251  */
253 
254  /* Dispatch loop time accounting */
261 
262  /*
263  * Barrier epoch - Set to current time, each time barrier_sync or
264  * barrier_release is called with zero recursion.
265  */
267 
268  /* Earliest barrier can be closed again */
270 
271  /* Need to check the frame queues */
273 
274  /* RPC requests, main thread only */
278 
279  /* buffer fault injector */
282 
283 } vlib_main_t;
284 
285 /* Global main structure. */
287 
288 void vlib_worker_loop (vlib_main_t * vm);
289 
292 {
293 #if CLIB_DEBUG > 0
294  extern __thread uword __os_thread_index;
295 #endif
296  /*
297  * Make sure folks don't pass &vlib_global_main from a worker thread.
298  */
299  ASSERT (vm->thread_index == __os_thread_index);
300  return clib_time_now (&vm->clib_time) + vm->time_offset;
301 }
302 
305 {
306  return clib_time_now_internal (&vm->clib_time, n);
307 }
308 
309 /* Busy wait for specified time. */
310 always_inline void
312 {
313  f64 t = vlib_time_now (vm);
314  f64 limit = t + wait;
315  while (t < limit)
316  t = vlib_time_now (vm);
317 }
318 
319 /* Time a piece of code. */
320 #define vlib_time_code(vm,body) \
321 do { \
322  f64 _t[2]; \
323  _t[0] = vlib_time_now (vm); \
324  do { body; } while (0); \
325  _t[1] = vlib_time_now (vm); \
326  clib_warning ("%.7e", _t[1] - _t[0]); \
327 } while (0)
328 
329 #define vlib_wait_with_timeout(vm,suspend_time,timeout_time,test) \
330 ({ \
331  uword __vlib_wait_with_timeout = 0; \
332  f64 __vlib_wait_time = 0; \
333  while (! (__vlib_wait_with_timeout = (test)) \
334  && __vlib_wait_time < (timeout_time)) \
335  { \
336  vlib_process_suspend (vm, suspend_time); \
337  __vlib_wait_time += suspend_time; \
338  } \
339  __vlib_wait_with_timeout; \
340 })
341 
342 always_inline void
344 {
345  vm->main_loop_error = error;
347 }
348 
349 #define vlib_panic_with_msg(vm,args...) \
350  vlib_panic_with_error (vm, clib_error_return (0, args))
351 
352 always_inline void
354 {
355  vlib_panic_with_error (vm, 0);
356 }
357 
358 
361 {
362  u64 vectors;
363  u64 calls;
364 
366 
367  if (PREDICT_FALSE (calls == 0))
368  return 0.0;
369 
371 
372  return (f64) vectors / (f64) calls;
373 }
374 
375 always_inline void
377 {
380 }
381 
382 always_inline void
384 {
385  vm->main_loop_count++;
387 
390 }
391 
394 {
396 }
397 
399  (vlib_main_t * vm, void (*fp) (vlib_main_t *))
400 {
401  vm->queue_signal_callback = fp;
402 }
403 
404 /* Main routine. */
405 int vlib_main (vlib_main_t * vm, unformat_input_t * input);
406 
407 /* Thread stacks, for os_get_thread_index */
408 extern u8 **vlib_thread_stacks;
409 
410 /* Number of thread stacks that the application needs */
411 u32 vlib_app_num_thread_stacks_needed (void) __attribute__ ((weak));
412 
413 extern void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
414 
415 #define VLIB_PCAP_MAJOR_VERSION 1
416 #define VLIB_PCAP_MINOR_VERSION 0
417 
418 typedef struct
419 {
421  int enable;
422  int status;
427 
429 
430 #endif /* included_vlib_main_h */
431 
432 /*
433  * fd.io coding-style-patch-verification: ON
434  *
435  * Local Variables:
436  * eval: (c-set-style "gnu")
437  * End:
438  */
f64 time_offset
Definition: main.h:89
_vlib_init_function_list_elt_t * worker_init_function_registrations
Definition: main.h:224
volatile u32 main_loop_count
Definition: main.h:99
pcap_main_t pcap_main
Definition: main.h:72
uword * pending_rpc_requests
Definition: main.h:275
u64 internal_node_vectors
Definition: main.h:106
static void vlib_panic_with_error(vlib_main_t *vm, clib_error_t *error)
Definition: main.h:343
clib_spinlock_t pending_rpc_lock
Definition: main.h:277
u32 mc_stream_index
Definition: main.h:188
static void vlib_time_wait(vlib_main_t *vm, f64 wait)
Definition: main.h:311
_vlib_init_function_list_elt_t * init_function_registrations
Definition: main.h:223
uword * processing_rpc_requests
Definition: main.h:276
u32 max_bytes_per_pkt
Definition: main.h:70
int elog_post_mortem_dump
Definition: main.h:246
uword dispatch_pcap_enable
Definition: main.h:168
int elog_trace_cli_commands
Definition: main.h:197
vlib_one_time_waiting_process_t * procs_waiting_for_mc_stream_join
Definition: main.h:190
vlib_physmem_main_t physmem_main
Definition: main.h:155
uword random_seed
Definition: main.h:209
unsigned long u64
Definition: types.h:89
clib_spinlock_t worker_thread_main_loop_callback_lock
Definition: main.h:240
static void vlib_increment_main_loop_counter(vlib_main_t *vm)
Definition: main.h:383
Fixed length block allocator.
f64 loop_interval_start
Definition: main.h:257
PCAP utility definitions.
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:291
int elog_trace_api_messages
Definition: main.h:196
f64 buffer_alloc_success_rate
Definition: main.h:281
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:230
u32 thread_index
Definition: main.h:218
_vlib_init_function_list_elt_t * main_loop_exit_function_registrations
Definition: main.h:226
u32 main_loop_exit_set
Definition: main.h:127
f64 loops_per_second
Definition: main.h:258
void vlib_worker_loop(vlib_main_t *vm)
Definition: main.c:1994
vlib_buffer_main_t * buffer_main
Definition: main.h:152
#define VLIB_MAIN_LOOP_EXIT_CLI
Definition: main.h:134
void clib_longjmp(clib_longjmp_t *save, uword return_value)
clib_time_t clib_time
Definition: main.h:87
u32 numa_node
Definition: main.h:220
vlib_cli_main_t cli_main
Definition: main.h:161
static u32 vlib_last_vectors_per_main_loop(vlib_main_t *vm)
Definition: main.h:393
unsigned char u8
Definition: types.h:56
void(** vlib_node_runtime_perf_counter_cb_tmp)(struct vlib_main_t *, u64 *, u64 *, vlib_node_runtime_t *, vlib_frame_t *, int)
Definition: main.h:119
double f64
Definition: types.h:142
u64 internal_node_calls
Definition: main.h:107
volatile uword check_frame_queues
Definition: main.h:272
volatile int parked_at_barrier
Definition: main.h:243
void(** vlib_node_runtime_perf_counter_cbs)(struct vlib_main_t *, u64 *, u64 *, vlib_node_runtime_t *, vlib_frame_t *, int)
Definition: main.h:115
void(**volatile worker_thread_main_loop_callback_tmp)(struct vlib_main_t *)
Definition: main.h:239
_vlib_init_function_list_elt_t * api_init_function_registrations
Definition: main.h:227
PCAP main state data structure.
Definition: pcap.h:156
u32 main_loop_vectors_processed
Definition: main.h:102
void * heap_aligned_base
Definition: main.h:146
u32 filter_classify_table_index
Definition: main.h:73
u32 cpu_id
Definition: main.h:219
u8 pcap_rx_enable
Definition: main.h:64
elog_event_type_t * error_elog_event_types
Definition: main.h:206
f64 time_last_barrier_release
Definition: main.h:90
unsigned int u32
Definition: types.h:88
u64 cpu_time_main_loop_start
Definition: main.h:96
u8 trace_filter_enable
Definition: main.h:78
u8 * pcap_buffer
Definition: main.h:170
f64 seconds_per_loop
Definition: main.h:259
static void vlib_set_queue_signal_callback(vlib_main_t *vm, void(*fp)(vlib_main_t *))
Definition: main.h:399
u32 trace_filter_set_index
Definition: main.h:80
char * name
Definition: main.h:140
static void vlib_panic(vlib_main_t *vm)
Definition: main.h:353
u8 pcap_drop_enable
Definition: main.h:68
vlib_error_main_t error_main
Definition: main.h:179
struct vlib_main_t vlib_main_t
struct _unformat_input_t unformat_input_t
int vlib_pcap_dispatch_trace_configure(vlib_pcap_dispatch_trace_args_t *)
Definition: main.c:2254
elog_event_type_t * node_return_elog_event_types
Definition: main.h:204
#define PREDICT_FALSE(x)
Definition: clib.h:118
#define always_inline
Definition: ipsec.h:28
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:417
u64 internal_node_calls_last_clear
Definition: main.h:109
u32 pcap_sw_if_index
Definition: main.h:71
uword * init_functions_called
Definition: main.h:215
vlib_main_t * vm
Definition: in2out_ed.c:1599
volatile u32 queue_signal_pending
Definition: main.h:231
f64 damping_constant
Definition: main.h:260
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
u8 ** argv
Definition: main.h:234
#define VLIB_MAIN_LOOP_EXIT_PANIC
Definition: main.h:132
int elog_trace_graph_dispatch
Definition: main.h:198
static void vlib_clear_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:376
void * heap_base
Definition: main.h:143
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:2087
elog_main_t elog_main
Definition: main.h:193
uword heap_size
Definition: main.h:149
CLIB_CACHE_LINE_ALIGN_MARK(cacheline0)
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
#define ASSERT(truth)
u8 pad1
Definition: main.h:69
_vlib_init_function_list_elt_t * main_loop_enter_function_registrations
Definition: main.h:225
u32 main_loop_nodes_processed
Definition: main.h:103
u64 loops_this_reporting_interval
Definition: main.h:255
u32 elog_trace_graph_circuit_node_index
Definition: main.h:200
vlib_trace_main_t trace_main
Definition: main.h:164
int need_vlib_worker_thread_node_runtime_update
Definition: main.h:252
void(**volatile worker_thread_main_loop_callbacks)(struct vlib_main_t *)
Definition: main.h:237
void(* queue_signal_callback)(struct vlib_main_t *)
Definition: main.h:233
vlib_main_t vlib_global_main
Definition: main.c:1999
u32 buffer_alloc_success_seed
Definition: main.h:280
f64 barrier_no_close_before
Definition: main.h:269
vlib_node_main_t node_main
Definition: main.h:158
u64 uword
Definition: types.h:112
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:611
static f64 clib_time_now_internal(clib_time_t *c, u64 n)
Definition: time.h:210
volatile u32 api_queue_nonempty
Definition: main.h:232
f64 barrier_epoch
Definition: main.h:266
elog_event_type_t * node_call_elog_event_types
Definition: main.h:203
volatile u32 main_loop_exit_now
Definition: main.h:129
u64 internal_node_vectors_last_clear
Definition: main.h:108
pcap_main_t dispatch_pcap_main
Definition: main.h:167
f64 loop_interval_end
Definition: main.h:256
static f64 vlib_time_now_ticks(vlib_main_t *vm, u64 n)
Definition: main.h:304
void(* os_punt_frame)(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: main.h:183
vlib_trace_filter_t trace_filter
Definition: main.h:176
u32 internal_node_last_vectors_per_main_loop
Definition: main.h:112
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1600
u8 pcap_tx_enable
Definition: main.h:66
clib_longjmp_t main_loop_exit
Definition: main.h:130
vnet_pcap_t pcap
Definition: main.h:173
vlib_config_function_runtime_t * config_function_registrations
Definition: main.h:228
u8 ** vlib_thread_stacks
Definition: main.c:648
static f64 vlib_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:360
int elog_trace_graph_circuit
Definition: main.h:199
clib_random_buffer_t random_buffer
Definition: main.h:212
u32 trace_classify_table_index
Definition: main.h:79
u32 * dispatch_buffer_trace_nodes
Definition: main.h:169
clib_error_t * main_loop_error
Definition: main.h:137
u64 cpu_time_last_node_dispatch
Definition: main.h:93