FD.io VPP  v19.08.3-2-gbabecb413
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  /*
255  * Barrier epoch - Set to current time, each time barrier_sync or
256  * barrier_release is called with zero recursion.
257  */
259 
260  /* Earliest barrier can be closed again */
262 
263  /* Need to check the frame queues */
265 
266  /* RPC requests, main thread only */
270 
271 } vlib_main_t;
272 
273 /* Global main structure. */
275 
276 void vlib_worker_loop (vlib_main_t * vm);
277 
280 {
281 #if CLIB_DEBUG > 0
282  extern __thread uword __os_thread_index;
283 #endif
284  /*
285  * Make sure folks don't pass &vlib_global_main from a worker thread.
286  */
287  ASSERT (vm->thread_index == __os_thread_index);
288  return clib_time_now (&vm->clib_time) + vm->time_offset;
289 }
290 
293 {
294  return clib_time_now_internal (&vm->clib_time, n);
295 }
296 
297 /* Busy wait for specified time. */
298 always_inline void
300 {
301  f64 t = vlib_time_now (vm);
302  f64 limit = t + wait;
303  while (t < limit)
304  t = vlib_time_now (vm);
305 }
306 
307 /* Time a piece of code. */
308 #define vlib_time_code(vm,body) \
309 do { \
310  f64 _t[2]; \
311  _t[0] = vlib_time_now (vm); \
312  do { body; } while (0); \
313  _t[1] = vlib_time_now (vm); \
314  clib_warning ("%.7e", _t[1] - _t[0]); \
315 } while (0)
316 
317 #define vlib_wait_with_timeout(vm,suspend_time,timeout_time,test) \
318 ({ \
319  uword __vlib_wait_with_timeout = 0; \
320  f64 __vlib_wait_time = 0; \
321  while (! (__vlib_wait_with_timeout = (test)) \
322  && __vlib_wait_time < (timeout_time)) \
323  { \
324  vlib_process_suspend (vm, suspend_time); \
325  __vlib_wait_time += suspend_time; \
326  } \
327  __vlib_wait_with_timeout; \
328 })
329 
330 always_inline void
332 {
333  vm->main_loop_error = error;
335 }
336 
337 #define vlib_panic_with_msg(vm,args...) \
338  vlib_panic_with_error (vm, clib_error_return (0, args))
339 
340 always_inline void
342 {
343  vlib_panic_with_error (vm, 0);
344 }
345 
346 
349 {
350  u64 vectors;
351  u64 calls;
352 
354 
355  if (PREDICT_FALSE (calls == 0))
356  return 0.0;
357 
359 
360  return (f64) vectors / (f64) calls;
361 }
362 
363 always_inline void
365 {
368 }
369 
370 always_inline void
372 {
373  vm->main_loop_count++;
375 
378 }
379 
382 {
384 }
385 
387  (vlib_main_t * vm, void (*fp) (vlib_main_t *))
388 {
389  vm->queue_signal_callback = fp;
390 }
391 
392 /* Main routine. */
393 int vlib_main (vlib_main_t * vm, unformat_input_t * input);
394 
395 /* Thread stacks, for os_get_thread_index */
396 extern u8 **vlib_thread_stacks;
397 
398 /* Number of thread stacks that the application needs */
399 u32 vlib_app_num_thread_stacks_needed (void) __attribute__ ((weak));
400 
401 extern void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
402 
403 #define VLIB_PCAP_MAJOR_VERSION 1
404 #define VLIB_PCAP_MINOR_VERSION 0
405 
406 typedef struct
407 {
409  int enable;
410  int status;
415 
417 
418 #endif /* included_vlib_main_h */
419 
420 /*
421  * fd.io coding-style-patch-verification: ON
422  *
423  * Local Variables:
424  * eval: (c-set-style "gnu")
425  * End:
426  */
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:267
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:331
clib_spinlock_t pending_rpc_lock
Definition: main.h:269
u32 mc_stream_index
Definition: main.h:188
static void vlib_time_wait(vlib_main_t *vm, f64 wait)
Definition: main.h:299
_vlib_init_function_list_elt_t * init_function_registrations
Definition: main.h:223
uword * processing_rpc_requests
Definition: main.h:268
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:371
Fixed length block allocator.
PCAP utility definitions.
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
int elog_trace_api_messages
Definition: main.h:196
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
void vlib_worker_loop(vlib_main_t *vm)
Definition: main.c:1935
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:381
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:264
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
#define always_inline
Definition: clib.h:99
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
static void vlib_set_queue_signal_callback(vlib_main_t *vm, void(*fp)(vlib_main_t *))
Definition: main.h:387
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:341
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:2166
elog_event_type_t * node_return_elog_event_types
Definition: main.h:204
#define PREDICT_FALSE(x)
Definition: clib.h:112
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:406
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
volatile u32 queue_signal_pending
Definition: main.h:231
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
vlib_main_t * vm
Definition: buffer.c:323
int elog_trace_graph_dispatch
Definition: main.h:198
static void vlib_clear_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:364
void * heap_base
Definition: main.h:143
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:2014
elog_main_t elog_main
Definition: main.h:193
uword heap_size
Definition: main.h:149
CLIB_CACHE_LINE_ALIGN_MARK(cacheline0)
#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
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:1940
f64 barrier_no_close_before
Definition: main.h:261
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:584
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:258
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
static f64 vlib_time_now_ticks(vlib_main_t *vm, u64 n)
Definition: main.h:292
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
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:635
static f64 vlib_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:348
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