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/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>
32 * Load plugins from /usr/lib/vpp_plugins by default
34 char *vlib_plugin_path = NULL;
35 char *vlib_plugin_app_version = VPP_BUILD_VER;
36 char *vat_plugin_path = NULL;
39 vpp_find_plugin_path ()
41 extern char *vat_plugin_path;
42 char *p, path[PATH_MAX];
46 /* find executable path */
47 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
50 /* readlink doesn't provide null termination */
54 if ((p = strrchr (path, '/')) == 0)
59 if ((p = strrchr (path, '/')) == 0)
63 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_plugins:"
64 "%s/lib/vpp_plugins", path, path);
66 vlib_plugin_path = (char *) s;
68 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
69 "%s/lib/vpp_api_test_plugins", path, path);
71 vat_plugin_path = (char *) s;
75 vpe_main_init (vlib_main_t * vm)
77 #if VPP_API_TEST_BUILTIN > 0
78 void vat_plugin_hash_create (void);
82 vlib_unix_cli_set_prompt ("DBGvpp# ");
84 vlib_unix_cli_set_prompt ("vpp# ");
86 /* Turn off network stack components which we don't want */
87 vlib_mark_init_function_complete (vm, srp_init);
90 * Create the binary api plugin hashes before loading plugins
92 #if VPP_API_TEST_BUILTIN > 0
93 vat_plugin_hash_create ();
96 if (!vlib_plugin_path)
97 vpp_find_plugin_path ();
101 * Default path for runtime data
103 char *vlib_default_runtime_dir = "vpp";
106 main (int argc, char *argv[])
109 void vl_msg_api_set_first_available_msg_id (u16);
110 uword main_heap_size = (1ULL << 30);
113 clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
114 clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
115 unformat_input_t input, sub_input;
122 CLIB_UNUSED (const char *msg)
123 = "ERROR: This binary requires CPU with %s extensions.\n";
125 if (!clib_cpu_supports_ ## a ()) \
127 fprintf(stderr, msg, b); \
152 * Load startup config from file.
153 * usage: vpp -c /etc/vpp/startup.conf
155 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
164 fp = fopen (argv[2], "r");
167 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
170 argv_ = calloc (1, sizeof (char *));
176 arg = strndup (argv[0], 1024);
187 if (fgets (inbuf, 4096, fp) == 0)
189 p = strtok (inbuf, " \t\n");
195 char **tmp = realloc (argv_, argc_ * sizeof (char *));
199 arg = strndup (p, 1024);
202 argv_[argc_ - 1] = arg;
203 p = strtok (NULL, " \t\n");
209 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
220 * Look for and parse the "heapsize" config parameter.
221 * Manual since none of the clib infra has been bootstrapped yet.
223 * Format: heapsize <nn>[mM][gG]
226 for (i = 1; i < (argc - 1); i++)
228 if (!strncmp (argv[i], "plugin_path", 11))
231 vlib_plugin_path = argv[++i];
233 if (!strncmp (argv[i], "test_plugin_path", 16))
236 vat_plugin_path = argv[++i];
238 else if (!strncmp (argv[i], "heapsize", 8))
240 sizep = (u8 *) argv[i + 1];
242 while (*sizep >= '0' && *sizep <= '9')
245 size += *sizep++ - '0';
251 "warning: heapsize parse error '%s', use default %lld\n",
252 argv[i], (long long int) main_heap_size);
256 main_heap_size = size;
258 if (*sizep == 'g' || *sizep == 'G')
259 main_heap_size <<= 30;
260 else if (*sizep == 'm' || *sizep == 'M')
261 main_heap_size <<= 20;
263 else if (!strncmp (argv[i], "main-core", 9))
268 unsigned long x = strtol (argv[++i], 0, 0);
277 clib_mem_init (0, 1 << 20);
278 unformat_init_command_line (&input, (char **) argv);
280 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
282 if (unformat (&input, "memory %v", &v))
284 unformat_init_vector (&sub_input, v);
286 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
288 if (unformat (&sub_input, "main-heap-size %U",
289 unformat_memory_size, &main_heap_size))
291 else if (unformat (&sub_input, "main-heap-page-size %U",
292 unformat_log2_page_size,
293 &main_heap_log2_page_sz))
295 else if (unformat (&sub_input, "default-hugepage-size %U",
296 unformat_log2_page_size,
297 &default_log2_hugepage_sz))
301 fformat (stderr, "unknown 'memory' config input '%U'\n",
302 format_unformat_error, &sub_input);
307 unformat_free (&sub_input);
309 else if (!unformat (&input, "%s %v", &s, &v))
312 vec_reset_length (s);
313 vec_reset_length (v);
318 unformat_free (&input);
320 /* set process affinity for main thread */
322 CPU_SET (main_core, &cpuset);
323 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
325 /* Set up the plugin message ID allocator right now... */
326 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
328 /* destroy temporary heap and create main one */
331 if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
332 main_heap_log2_page_sz)))
334 /* Figure out which numa runs the main thread */
335 __os_numa_index = clib_get_current_numa_node ();
337 if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
338 clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
340 /* and use the main heap as that numa's numa heap */
341 clib_mem_set_per_numa_heap (main_heap);
343 vpe_main_init (vlib_get_first_main ());
344 return vlib_unix_main (argc, argv);
349 int rv __attribute__ ((unused)) =
350 write (2, "Main heap allocation failure!\r\n", 31);
356 static clib_error_t *
357 memory_config (vlib_main_t * vm, unformat_input_t * input)
362 VLIB_CONFIG_FUNCTION (memory_config, "memory");
364 static clib_error_t *
365 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
370 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
372 static clib_error_t *
373 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
377 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
379 if (unformat (input, "%s", &junk))
385 return clib_error_return (0, "unknown input '%U'",
386 format_unformat_error, input);
391 static clib_error_t *
392 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
394 return placeholder_path_config (vm, input);
397 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
399 static clib_error_t *
400 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
402 return placeholder_path_config (vm, input);
405 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
407 void vl_msg_api_post_mortem_dump (void);
408 void vlib_post_mortem_dump (void);
413 vl_msg_api_post_mortem_dump ();
414 vlib_post_mortem_dump ();
418 void vhost_user_unmap_all (void) __attribute__ ((weak));
420 vhost_user_unmap_all (void)
427 static int recursion_block;
436 vl_msg_api_post_mortem_dump ();
437 vlib_post_mortem_dump ();
438 vhost_user_unmap_all ();
444 #ifdef BARRIER_TRACING
446 vl_msg_api_barrier_trace_context (const char *context)
448 vlib_worker_threads[0].barrier_context = context;
453 vl_msg_api_barrier_sync (void)
455 vlib_worker_thread_barrier_sync (vlib_get_main ());
459 vl_msg_api_barrier_release (void)
461 vlib_worker_thread_barrier_release (vlib_get_main ());
464 /* This application needs 1 thread stack for the stats pthread */
466 vlib_app_num_thread_stacks_needed (void)
472 * Depending on the configuration selected above,
473 * it may be necessary to generate stub graph nodes.
474 * It is never OK to ignore "node 'x' refers to unknown node 'y'
478 #include <vppinfra/bihash_8_8.h>
480 static clib_error_t *
481 show_bihash_command_fn (vlib_main_t * vm,
482 unformat_input_t * input, vlib_cli_command_t * cmd)
485 clib_bihash_8_8_t *h;
488 if (unformat (input, "verbose"))
491 for (i = 0; i < vec_len (clib_all_bihashes); i++)
493 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
494 vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
501 VLIB_CLI_COMMAND (show_bihash_command, static) =
503 .path = "show bihash",
504 .short_help = "show bihash",
505 .function = show_bihash_command_fn,
509 #ifdef CLIB_SANITIZE_ADDR
510 /* default options for Address Sanitizer */
512 __asan_default_options (void)
514 return VPP_SANITIZE_ADDR_OPTIONS;
516 #endif /* CLIB_SANITIZE_ADDR */
519 * fd.io coding-style-patch-verification: ON
522 * eval: (c-set-style "gnu")