FD.io VPP  v21.06
Vector Packet Processing
main.c
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 #define _GNU_SOURCE
17 #include <pthread.h>
18 #include <sched.h>
19 
20 #include <vppinfra/cpu.h>
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23 #include <vlib/threads.h>
24 #include <vnet/plugin/plugin.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vpp/app/version.h>
27 #include <vpp/vnet/config.h>
28 #include <vpp/api/vpe_msg_enum.h>
29 #include <limits.h>
30 
31 /*
32  * Load plugins from /usr/lib/vpp_plugins by default
33  */
34 char *vlib_plugin_path = NULL;
35 char *vlib_plugin_app_version = VPP_BUILD_VER;
36 char *vat_plugin_path = NULL;
37 
38 static void
40 {
41  extern char *vat_plugin_path;
42  char *p, path[PATH_MAX];
43  int rv;
44  u8 *s;
45 
46  /* find executable path */
47  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
48  return;
49 
50  /* readlink doesn't provide null termination */
51  path[rv] = 0;
52 
53  /* strip filename */
54  if ((p = strrchr (path, '/')) == 0)
55  return;
56  *p = 0;
57 
58  /* strip bin/ */
59  if ((p = strrchr (path, '/')) == 0)
60  return;
61  *p = 0;
62 
63  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_plugins:"
64  "%s/lib/vpp_plugins", path, path);
65  vec_add1 (s, 0);
66  vlib_plugin_path = (char *) s;
67 
68  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
69  "%s/lib/vpp_api_test_plugins", path, path);
70  vec_add1 (s, 0);
71  vat_plugin_path = (char *) s;
72 }
73 
74 static void
76 {
77 #if VPP_API_TEST_BUILTIN > 0
78  void vat_plugin_hash_create (void);
79 #endif
80 
81  if (CLIB_DEBUG > 0)
82  vlib_unix_cli_set_prompt ("DBGvpp# ");
83  else
84  vlib_unix_cli_set_prompt ("vpp# ");
85 
86  /* Turn off network stack components which we don't want */
88 
89  /*
90  * Create the binary api plugin hashes before loading plugins
91  */
92 #if VPP_API_TEST_BUILTIN > 0
94 #endif
95 
96  if (!vlib_plugin_path)
98 }
99 
100 /*
101  * Default path for runtime data
102  */
104 
105 int
106 main (int argc, char *argv[])
107 {
108  int i;
110  uword main_heap_size = (1ULL << 30);
111  u8 *sizep;
112  u32 size;
113  clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
114  unformat_input_t input, sub_input;
115  u8 *s = 0, *v = 0;
116  int main_core = 1;
117  cpu_set_t cpuset;
118  void *main_heap;
119 
120 #if __x86_64__
121  CLIB_UNUSED (const char *msg)
122  = "ERROR: This binary requires CPU with %s extensions.\n";
123 #define _(a,b) \
124  if (!clib_cpu_supports_ ## a ()) \
125  { \
126  fprintf(stderr, msg, b); \
127  exit(1); \
128  }
129 
130 #if __AVX2__
131  _(avx2, "AVX2")
132 #endif
133 #if __AVX__
134  _(avx, "AVX")
135 #endif
136 #if __SSE4_2__
137  _(sse42, "SSE4.2")
138 #endif
139 #if __SSE4_1__
140  _(sse41, "SSE4.1")
141 #endif
142 #if __SSSE3__
143  _(ssse3, "SSSE3")
144 #endif
145 #if __SSE3__
146  _(sse3, "SSE3")
147 #endif
148 #undef _
149 #endif
150  /*
151  * Load startup config from file.
152  * usage: vpp -c /etc/vpp/startup.conf
153  */
154  if ((argc == 3) && !strncmp (argv[1], "-c", 2))
155  {
156  FILE *fp;
157  char inbuf[4096];
158  int argc_ = 1;
159  char **argv_ = NULL;
160  char *arg = NULL;
161  char *p;
162 
163  fp = fopen (argv[2], "r");
164  if (fp == NULL)
165  {
166  fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
167  return 1;
168  }
169  argv_ = calloc (1, sizeof (char *));
170  if (argv_ == NULL)
171  {
172  fclose (fp);
173  return 1;
174  }
175  arg = strndup (argv[0], 1024);
176  if (arg == NULL)
177  {
178  fclose (fp);
179  free (argv_);
180  return 1;
181  }
182  argv_[0] = arg;
183 
184  while (1)
185  {
186  if (fgets (inbuf, 4096, fp) == 0)
187  break;
188  p = strtok (inbuf, " \t\n");
189  while (p != NULL)
190  {
191  if (*p == '#')
192  break;
193  argc_++;
194  char **tmp = realloc (argv_, argc_ * sizeof (char *));
195  if (tmp == NULL)
196  return 1;
197  argv_ = tmp;
198  arg = strndup (p, 1024);
199  if (arg == NULL)
200  return 1;
201  argv_[argc_ - 1] = arg;
202  p = strtok (NULL, " \t\n");
203  }
204  }
205 
206  fclose (fp);
207 
208  char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
209  if (tmp == NULL)
210  return 1;
211  argv_ = tmp;
212  argv_[argc_] = NULL;
213 
214  argc = argc_;
215  argv = argv_;
216  }
217 
218  /*
219  * Look for and parse the "heapsize" config parameter.
220  * Manual since none of the clib infra has been bootstrapped yet.
221  *
222  * Format: heapsize <nn>[mM][gG]
223  */
224 
225  for (i = 1; i < (argc - 1); i++)
226  {
227  if (!strncmp (argv[i], "plugin_path", 11))
228  {
229  if (i < (argc - 1))
230  vlib_plugin_path = argv[++i];
231  }
232  if (!strncmp (argv[i], "test_plugin_path", 16))
233  {
234  if (i < (argc - 1))
235  vat_plugin_path = argv[++i];
236  }
237  else if (!strncmp (argv[i], "heapsize", 8))
238  {
239  sizep = (u8 *) argv[i + 1];
240  size = 0;
241  while (*sizep >= '0' && *sizep <= '9')
242  {
243  size *= 10;
244  size += *sizep++ - '0';
245  }
246  if (size == 0)
247  {
248  fprintf
249  (stderr,
250  "warning: heapsize parse error '%s', use default %lld\n",
251  argv[i], (long long int) main_heap_size);
252  goto defaulted;
253  }
254 
255  main_heap_size = size;
256 
257  if (*sizep == 'g' || *sizep == 'G')
258  main_heap_size <<= 30;
259  else if (*sizep == 'm' || *sizep == 'M')
260  main_heap_size <<= 20;
261  }
262  else if (!strncmp (argv[i], "main-core", 9))
263  {
264  if (i < (argc - 1))
265  {
266  errno = 0;
267  unsigned long x = strtol (argv[++i], 0, 0);
268  if (errno == 0)
269  main_core = x;
270  }
271  }
272  }
273 defaulted:
274 
275  /* temporary heap */
276  clib_mem_init (0, 1 << 20);
277  unformat_init_command_line (&input, (char **) argv);
278 
279  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
280  {
281  if (unformat (&input, "memory %v", &v))
282  {
283  unformat_init_vector (&sub_input, v);
284  v = 0;
285  while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
286  {
287  if (unformat (&sub_input, "main-heap-size %U",
288  unformat_memory_size, &main_heap_size))
289  ;
290  else if (unformat (&sub_input, "main-heap-page-size %U",
292  &main_heap_log2_page_sz))
293  ;
294  else
295  {
296  fformat (stderr, "unknown 'memory' config input '%U'\n",
297  format_unformat_error, &sub_input);
298  exit (1);
299  }
300 
301  }
302  unformat_free (&sub_input);
303  }
304  else if (!unformat (&input, "%s %v", &s, &v))
305  break;
306 
307  vec_reset_length (s);
308  vec_reset_length (v);
309  }
310  vec_free (s);
311  vec_free (v);
312 
313  unformat_free (&input);
314 
315  /* set process affinity for main thread */
316  CPU_ZERO (&cpuset);
317  CPU_SET (main_core, &cpuset);
318  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
319 
320  /* Set up the plugin message ID allocator right now... */
322 
323  /* destroy temporary heap and create main one */
324  clib_mem_destroy ();
325 
326  if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
327  main_heap_log2_page_sz)))
328  {
329  /* Figure out which numa runs the main thread */
330  __os_numa_index = clib_get_current_numa_node ();
331 
332  /* and use the main heap as that numa's numa heap */
333  clib_mem_set_per_numa_heap (main_heap);
334  vlib_main_init ();
336  return vlib_unix_main (argc, argv);
337  }
338  else
339  {
340  {
341  int rv __attribute__ ((unused)) =
342  write (2, "Main heap allocation failure!\r\n", 31);
343  }
344  return 1;
345  }
346 }
347 
348 static clib_error_t *
350 {
351  return 0;
352 }
353 
355 
356 static clib_error_t *
358 {
359  return 0;
360 }
361 
363 
364 static clib_error_t *
366 {
367  u8 *junk;
368 
370  {
371  if (unformat (input, "%s", &junk))
372  {
373  vec_free (junk);
374  return 0;
375  }
376  else
377  return clib_error_return (0, "unknown input '%U'",
378  format_unformat_error, input);
379  }
380  return 0;
381 }
382 
383 static clib_error_t *
385 {
386  return placeholder_path_config (vm, input);
387 }
388 
390 
391 static clib_error_t *
393 {
394  return placeholder_path_config (vm, input);
395 }
396 
397 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
398 
399 void vl_msg_api_post_mortem_dump (void);
400 void vlib_post_mortem_dump (void);
401 
402 void
403 os_panic (void)
404 {
407  abort ();
408 }
409 
410 void vhost_user_unmap_all (void) __attribute__ ((weak));
411 void
413 {
414 }
415 
416 void
417 os_exit (int code)
418 {
419  static int recursion_block;
420 
421  if (code)
422  {
423  if (recursion_block)
424  abort ();
425 
426  recursion_block = 1;
427 
431  abort ();
432  }
433  exit (code);
434 }
435 
436 #ifdef BARRIER_TRACING
437 void
439 {
441 }
442 #endif
443 
444 void
446 {
448 }
449 
450 void
452 {
454 }
455 
456 /* This application needs 1 thread stack for the stats pthread */
457 u32
459 {
460  return 1;
461 }
462 
463 /*
464  * Depending on the configuration selected above,
465  * it may be necessary to generate stub graph nodes.
466  * It is never OK to ignore "node 'x' refers to unknown node 'y'
467  * messages!
468  */
469 
470 #include <vppinfra/bihash_8_8.h>
471 
472 static clib_error_t *
474  unformat_input_t * input, vlib_cli_command_t * cmd)
475 {
476  int i;
477  clib_bihash_8_8_t *h;
478  int verbose = 0;
479 
480  if (unformat (input, "verbose"))
481  verbose = 1;
482 
483  for (i = 0; i < vec_len (clib_all_bihashes); i++)
484  {
485  h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
486  vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
487  }
488 
489  return 0;
490 }
491 
492 /* *INDENT-OFF* */
493 VLIB_CLI_COMMAND (show_bihash_command, static) =
494 {
495  .path = "show bihash",
496  .short_help = "show bihash",
497  .function = show_bihash_command_fn,
498 };
499 /* *INDENT-ON* */
500 
501 #ifdef CLIB_SANITIZE_ADDR
502 /* default options for Address Sanitizer */
503 const char *
504 __asan_default_options (void)
505 {
506  return VPP_SANITIZE_ADDR_OPTIONS;
507 }
508 #endif /* CLIB_SANITIZE_ADDR */
509 
510 /*
511  * fd.io coding-style-patch-verification: ON
512  *
513  * Local Variables:
514  * eval: (c-set-style "gnu")
515  * End:
516  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:694
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:458
static clib_error_t * srp_init(vlib_main_t *vm)
Definition: node.c:864
#define CLIB_UNUSED(x)
Definition: clib.h:90
void vlib_unix_cli_set_prompt(char *prompt)
Set the CLI prompt.
Definition: cli.c:3277
void vl_msg_api_barrier_sync(void)
Definition: main.c:445
static clib_error_t * plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:384
void * clib_mem_init(void *base, uword size)
Definition: mem_dlmalloc.c:266
static clib_error_t * heapsize_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:357
#define vlib_mark_init_function_complete(vm, x)
Definition: init.h:274
void vhost_user_unmap_all(void)
Definition: main.c:412
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:607
void vlib_post_mortem_dump(void)
Definition: main.c:748
static clib_error_t * test_plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:392
vl_api_fib_path_t path
Definition: mfib_types.api:44
#define vl_msg_api_barrier_trace_context(X)
Definition: api_common.h:192
char * vat_plugin_path
Definition: main.c:36
unsigned char u8
Definition: types.h:56
static clib_error_t * show_bihash_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: main.c:473
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
__clib_export void ** clib_all_bihashes
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:194
unsigned int u32
Definition: types.h:88
static void * clib_mem_set_per_numa_heap(void *new_heap)
Definition: mem.h:184
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:466
char * vlib_default_runtime_dir
Definition: main.c:59
description fragment has unexpected format
Definition: map.api:433
#define clib_error_return(e, args...)
Definition: error.h:99
void os_exit(int code)
Definition: main.c:417
int __clib_unused rv
Definition: application.c:491
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:969
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
static clib_error_t * placeholder_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:365
const char * barrier_context
Definition: threads.h:106
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
clib_mem_page_sz_t
Definition: mem.h:57
u32 size
Definition: vhost_user.h:125
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1013
u32 * tmp
static clib_error_t * memory_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:349
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:181
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1037
void * clib_mem_init_with_page_size(uword memory_size, clib_mem_page_sz_t log2_page_sz)
Definition: mem_dlmalloc.c:273
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
char * vlib_plugin_path
Definition: main.c:34
unformat_function_t unformat_log2_page_size
Definition: format.h:294
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
static vlib_main_t * vlib_get_first_main(void)
Definition: global_funcs.h:44
void free(void *p)
Definition: mem.c:42
void clib_mem_destroy(void)
Definition: mem_dlmalloc.c:287
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
void vat_plugin_hash_create(void)
Definition: api_main.c:87
void vl_msg_api_barrier_release(void)
Definition: main.c:451
static void vpe_main_init(vlib_main_t *vm)
Definition: main.c:75
void * realloc(void *p, size_t size)
Definition: mem.c:67
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u32 context
Definition: ip.api:780
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void vlib_main_init()
Definition: main.h:460
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
void * calloc(size_t nmemb, size_t size)
Definition: mem.c:54
unformat_function_t unformat_memory_size
Definition: format.h:288
static void vpp_find_plugin_path()
Definition: main.c:39
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1386
int main(int argc, char *argv[])
Definition: main.c:106
char * vlib_plugin_app_version
Definition: main.c:35
void os_panic(void)
Definition: main.c:403
__clib_export u32 clib_get_current_numa_node()
Definition: cpu.c:234
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:1039
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163