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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <vppinfra/clib.h>
21 #include <vppinfra/cpu.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/threads.h>
25 #include <vnet/plugin/plugin.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vpp/app/version.h>
28 #include <vpp/vnet/config.h>
29 #include <vlibmemory/memclnt.api_enum.h> /* To get the last static message id */
33 * Load plugins from /usr/lib/vpp_plugins by default
35 char *vlib_plugin_path = NULL;
36 char *vlib_plugin_app_version = VPP_BUILD_VER;
37 char *vat_plugin_path = NULL;
40 vpp_find_plugin_path ()
42 extern char *vat_plugin_path;
43 char *p, path[PATH_MAX];
47 /* find executable path */
48 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
51 /* readlink doesn't provide null termination */
55 if ((p = strrchr (path, '/')) == 0)
60 if ((p = strrchr (path, '/')) == 0)
64 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_plugins", path, path);
66 vlib_plugin_path = (char *) s;
68 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
70 vat_plugin_path = (char *) s;
74 vpe_main_init (vlib_main_t * vm)
76 #if VPP_API_TEST_BUILTIN > 0
77 void vat_plugin_hash_create (void);
81 vlib_unix_cli_set_prompt ("DBGvpp# ");
83 vlib_unix_cli_set_prompt ("vpp# ");
85 /* Turn off network stack components which we don't want */
86 vlib_mark_init_function_complete (vm, srp_init);
89 * Create the binary api plugin hashes before loading plugins
91 #if VPP_API_TEST_BUILTIN > 0
92 vat_plugin_hash_create ();
95 if (!vlib_plugin_path)
96 vpp_find_plugin_path ();
100 * Default path for runtime data
102 char *vlib_default_runtime_dir = "vpp";
105 main (int argc, char *argv[])
108 void vl_msg_api_set_first_available_msg_id (u16);
109 uword main_heap_size = (1ULL << 30);
112 clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
113 clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
114 unformat_input_t input, sub_input;
121 CLIB_UNUSED (const char *msg)
122 = "ERROR: This binary requires CPU with %s extensions.\n";
124 if (!clib_cpu_supports_ ## a ()) \
126 fprintf(stderr, msg, b); \
151 * Load startup config from file.
152 * usage: vpp -c /etc/vpp/startup.conf
154 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
163 fp = fopen (argv[2], "r");
166 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
169 argv_ = calloc (1, sizeof (char *));
175 arg = strndup (argv[0], 1024);
186 if (fgets (inbuf, 4096, fp) == 0)
188 p = strtok (inbuf, " \t\n");
194 char **tmp = realloc (argv_, argc_ * sizeof (char *));
198 arg = strndup (p, 1024);
201 argv_[argc_ - 1] = arg;
202 p = strtok (NULL, " \t\n");
208 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
219 * Look for and parse the "heapsize" config parameter.
220 * Manual since none of the clib infra has been bootstrapped yet.
222 * Format: heapsize <nn>[mM][gG]
225 for (i = 1; i < (argc - 1); i++)
227 if (!strncmp (argv[i], "plugin_path", 11))
230 vlib_plugin_path = argv[++i];
232 if (!strncmp (argv[i], "test_plugin_path", 16))
235 vat_plugin_path = argv[++i];
237 else if (!strncmp (argv[i], "heapsize", 8))
239 sizep = (u8 *) argv[i + 1];
241 while (*sizep >= '0' && *sizep <= '9')
244 size += *sizep++ - '0';
250 "warning: heapsize parse error '%s', use default %lld\n",
251 argv[i], (long long int) main_heap_size);
255 main_heap_size = size;
257 if (*sizep == 'g' || *sizep == 'G')
258 main_heap_size <<= 30;
259 else if (*sizep == 'm' || *sizep == 'M')
260 main_heap_size <<= 20;
262 else if (!strncmp (argv[i], "main-core", 9))
267 unsigned long x = strtol (argv[++i], 0, 0);
272 else if (!strncmp (argv[i], "interactive", 11))
273 unix_main.flags |= UNIX_FLAG_INTERACTIVE;
274 else if (!strncmp (argv[i], "nosyslog", 8))
275 unix_main.flags |= UNIX_FLAG_NOSYSLOG;
280 clib_mem_init (0, 1 << 20);
281 unformat_init_command_line (&input, (char **) argv);
283 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
285 if (unformat (&input, "memory %v", &v))
287 unformat_init_vector (&sub_input, v);
289 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
291 if (unformat (&sub_input, "main-heap-size %U",
292 unformat_memory_size, &main_heap_size))
294 else if (unformat (&sub_input, "main-heap-page-size %U",
295 unformat_log2_page_size,
296 &main_heap_log2_page_sz))
298 else if (unformat (&sub_input, "default-hugepage-size %U",
299 unformat_log2_page_size,
300 &default_log2_hugepage_sz))
304 fformat (stderr, "unknown 'memory' config input '%U'\n",
305 format_unformat_error, &sub_input);
310 unformat_free (&sub_input);
312 else if (!unformat (&input, "%s %v", &s, &v))
315 vec_reset_length (s);
316 vec_reset_length (v);
321 unformat_free (&input);
323 /* set process affinity for main thread */
327 CPU_SET (main_core, &cpuset);
328 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
331 /* Set up the plugin message ID allocator right now... */
332 vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
334 /* destroy temporary heap and create main one */
337 if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
338 main_heap_log2_page_sz)))
340 /* Figure out which numa runs the main thread */
341 __os_numa_index = clib_get_current_numa_node ();
343 if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
344 clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
346 /* and use the main heap as that numa's numa heap */
347 clib_mem_set_per_numa_heap (main_heap);
349 vpe_main_init (vlib_get_first_main ());
350 return vlib_unix_main (argc, argv);
355 int rv __attribute__ ((unused)) =
356 write (2, "Main heap allocation failure!\r\n", 31);
362 static clib_error_t *
363 memory_config (vlib_main_t * vm, unformat_input_t * input)
368 VLIB_CONFIG_FUNCTION (memory_config, "memory");
370 static clib_error_t *
371 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
376 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
378 static clib_error_t *
379 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
383 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
385 if (unformat (input, "%s", &junk))
391 return clib_error_return (0, "unknown input '%U'",
392 format_unformat_error, input);
397 static clib_error_t *
398 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
400 return placeholder_path_config (vm, input);
403 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
405 static clib_error_t *
406 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
408 return placeholder_path_config (vm, input);
411 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
413 void vl_msg_api_post_mortem_dump (void);
414 void vlib_post_mortem_dump (void);
419 vl_msg_api_post_mortem_dump ();
420 vlib_post_mortem_dump ();
424 void vhost_user_unmap_all (void) __attribute__ ((weak));
426 vhost_user_unmap_all (void)
433 static int recursion_block;
442 vl_msg_api_post_mortem_dump ();
443 vlib_post_mortem_dump ();
444 vhost_user_unmap_all ();
450 #ifdef BARRIER_TRACING
452 vl_msg_api_barrier_trace_context (const char *context)
454 vlib_worker_threads[0].barrier_context = context;
459 vl_msg_api_barrier_sync (void)
461 vlib_worker_thread_barrier_sync (vlib_get_main ());
465 vl_msg_api_barrier_release (void)
467 vlib_worker_thread_barrier_release (vlib_get_main ());
470 /* This application needs 1 thread stack for the stats pthread */
472 vlib_app_num_thread_stacks_needed (void)
478 * Depending on the configuration selected above,
479 * it may be necessary to generate stub graph nodes.
480 * It is never OK to ignore "node 'x' refers to unknown node 'y'
484 #include <vppinfra/bihash_8_8.h>
486 static clib_error_t *
487 show_bihash_command_fn (vlib_main_t * vm,
488 unformat_input_t * input, vlib_cli_command_t * cmd)
491 clib_bihash_8_8_t *h;
494 if (unformat (input, "verbose"))
497 for (i = 0; i < vec_len (clib_all_bihashes); i++)
499 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
500 vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
507 VLIB_CLI_COMMAND (show_bihash_command, static) =
509 .path = "show bihash",
510 .short_help = "show bihash",
511 .function = show_bihash_command_fn,
515 #ifdef CLIB_SANITIZE_ADDR
516 /* default options for Address Sanitizer */
518 __asan_default_options (void)
520 return VPP_SANITIZE_ADDR_OPTIONS;
522 #endif /* CLIB_SANITIZE_ADDR */
525 * fd.io coding-style-patch-verification: ON
528 * eval: (c-set-style "gnu")