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 <vlibmemory/memclnt.api_enum.h> /* To get the last static message id */
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/" CLIB_LIB_DIR "/vpp_plugins", path, path);
65 vlib_plugin_path = (char *) s;
67 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
69 vat_plugin_path = (char *) s;
73 vpe_main_init (vlib_main_t * vm)
75 #if VPP_API_TEST_BUILTIN > 0
76 void vat_plugin_hash_create (void);
80 vlib_unix_cli_set_prompt ("DBGvpp# ");
82 vlib_unix_cli_set_prompt ("vpp# ");
84 /* Turn off network stack components which we don't want */
85 vlib_mark_init_function_complete (vm, srp_init);
88 * Create the binary api plugin hashes before loading plugins
90 #if VPP_API_TEST_BUILTIN > 0
91 vat_plugin_hash_create ();
94 if (!vlib_plugin_path)
95 vpp_find_plugin_path ();
99 * Default path for runtime data
101 char *vlib_default_runtime_dir = "vpp";
104 main (int argc, char *argv[])
107 void vl_msg_api_set_first_available_msg_id (u16);
108 uword main_heap_size = (1ULL << 30);
111 clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
112 clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
113 unformat_input_t input, sub_input;
120 CLIB_UNUSED (const char *msg)
121 = "ERROR: This binary requires CPU with %s extensions.\n";
123 if (!clib_cpu_supports_ ## a ()) \
125 fprintf(stderr, msg, b); \
150 * Load startup config from file.
151 * usage: vpp -c /etc/vpp/startup.conf
153 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
162 fp = fopen (argv[2], "r");
165 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
168 argv_ = calloc (1, sizeof (char *));
174 arg = strndup (argv[0], 1024);
185 if (fgets (inbuf, 4096, fp) == 0)
187 p = strtok (inbuf, " \t\n");
193 char **tmp = realloc (argv_, argc_ * sizeof (char *));
197 arg = strndup (p, 1024);
200 argv_[argc_ - 1] = arg;
201 p = strtok (NULL, " \t\n");
207 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
218 * Look for and parse the "heapsize" config parameter.
219 * Manual since none of the clib infra has been bootstrapped yet.
221 * Format: heapsize <nn>[mM][gG]
224 for (i = 1; i < (argc - 1); i++)
226 if (!strncmp (argv[i], "plugin_path", 11))
229 vlib_plugin_path = argv[++i];
231 if (!strncmp (argv[i], "test_plugin_path", 16))
234 vat_plugin_path = argv[++i];
236 else if (!strncmp (argv[i], "heapsize", 8))
238 sizep = (u8 *) argv[i + 1];
240 while (*sizep >= '0' && *sizep <= '9')
243 size += *sizep++ - '0';
249 "warning: heapsize parse error '%s', use default %lld\n",
250 argv[i], (long long int) main_heap_size);
254 main_heap_size = size;
256 if (*sizep == 'g' || *sizep == 'G')
257 main_heap_size <<= 30;
258 else if (*sizep == 'm' || *sizep == 'M')
259 main_heap_size <<= 20;
261 else if (!strncmp (argv[i], "main-core", 9))
266 unsigned long x = strtol (argv[++i], 0, 0);
275 clib_mem_init (0, 1 << 20);
276 unformat_init_command_line (&input, (char **) argv);
278 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
280 if (unformat (&input, "memory %v", &v))
282 unformat_init_vector (&sub_input, v);
284 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
286 if (unformat (&sub_input, "main-heap-size %U",
287 unformat_memory_size, &main_heap_size))
289 else if (unformat (&sub_input, "main-heap-page-size %U",
290 unformat_log2_page_size,
291 &main_heap_log2_page_sz))
293 else if (unformat (&sub_input, "default-hugepage-size %U",
294 unformat_log2_page_size,
295 &default_log2_hugepage_sz))
299 fformat (stderr, "unknown 'memory' config input '%U'\n",
300 format_unformat_error, &sub_input);
305 unformat_free (&sub_input);
307 else if (!unformat (&input, "%s %v", &s, &v))
310 vec_reset_length (s);
311 vec_reset_length (v);
316 unformat_free (&input);
318 /* set process affinity for main thread */
320 CPU_SET (main_core, &cpuset);
321 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
323 /* Set up the plugin message ID allocator right now... */
324 vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
326 /* destroy temporary heap and create main one */
329 if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
330 main_heap_log2_page_sz)))
332 /* Figure out which numa runs the main thread */
333 __os_numa_index = clib_get_current_numa_node ();
335 if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
336 clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
338 /* and use the main heap as that numa's numa heap */
339 clib_mem_set_per_numa_heap (main_heap);
341 vpe_main_init (vlib_get_first_main ());
342 return vlib_unix_main (argc, argv);
347 int rv __attribute__ ((unused)) =
348 write (2, "Main heap allocation failure!\r\n", 31);
354 static clib_error_t *
355 memory_config (vlib_main_t * vm, unformat_input_t * input)
360 VLIB_CONFIG_FUNCTION (memory_config, "memory");
362 static clib_error_t *
363 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
368 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
370 static clib_error_t *
371 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
375 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
377 if (unformat (input, "%s", &junk))
383 return clib_error_return (0, "unknown input '%U'",
384 format_unformat_error, input);
389 static clib_error_t *
390 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
392 return placeholder_path_config (vm, input);
395 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
397 static clib_error_t *
398 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
400 return placeholder_path_config (vm, input);
403 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
405 void vl_msg_api_post_mortem_dump (void);
406 void vlib_post_mortem_dump (void);
411 vl_msg_api_post_mortem_dump ();
412 vlib_post_mortem_dump ();
416 void vhost_user_unmap_all (void) __attribute__ ((weak));
418 vhost_user_unmap_all (void)
425 static int recursion_block;
434 vl_msg_api_post_mortem_dump ();
435 vlib_post_mortem_dump ();
436 vhost_user_unmap_all ();
442 #ifdef BARRIER_TRACING
444 vl_msg_api_barrier_trace_context (const char *context)
446 vlib_worker_threads[0].barrier_context = context;
451 vl_msg_api_barrier_sync (void)
453 vlib_worker_thread_barrier_sync (vlib_get_main ());
457 vl_msg_api_barrier_release (void)
459 vlib_worker_thread_barrier_release (vlib_get_main ());
462 /* This application needs 1 thread stack for the stats pthread */
464 vlib_app_num_thread_stacks_needed (void)
470 * Depending on the configuration selected above,
471 * it may be necessary to generate stub graph nodes.
472 * It is never OK to ignore "node 'x' refers to unknown node 'y'
476 #include <vppinfra/bihash_8_8.h>
478 static clib_error_t *
479 show_bihash_command_fn (vlib_main_t * vm,
480 unformat_input_t * input, vlib_cli_command_t * cmd)
483 clib_bihash_8_8_t *h;
486 if (unformat (input, "verbose"))
489 for (i = 0; i < vec_len (clib_all_bihashes); i++)
491 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
492 vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
499 VLIB_CLI_COMMAND (show_bihash_command, static) =
501 .path = "show bihash",
502 .short_help = "show bihash",
503 .function = show_bihash_command_fn,
507 #ifdef CLIB_SANITIZE_ADDR
508 /* default options for Address Sanitizer */
510 __asan_default_options (void)
512 return VPP_SANITIZE_ADDR_OPTIONS;
514 #endif /* CLIB_SANITIZE_ADDR */
517 * fd.io coding-style-patch-verification: ON
520 * eval: (c-set-style "gnu")