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 vlib_main_t *vm = &vlib_global_main;
110 void vl_msg_api_set_first_available_msg_id (u16);
111 uword main_heap_size = (1ULL << 30);
119 CLIB_UNUSED (const char *msg)
120 = "ERROR: This binary requires CPU with %s extensions.\n";
122 if (!clib_cpu_supports_ ## a ()) \
124 fprintf(stderr, msg, b); \
149 * Load startup config from file.
150 * usage: vpp -c /etc/vpp/startup.conf
152 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
161 fp = fopen (argv[2], "r");
164 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
167 argv_ = calloc (1, sizeof (char *));
173 arg = strndup (argv[0], 1024);
184 if (fgets (inbuf, 4096, fp) == 0)
186 p = strtok (inbuf, " \t\n");
192 char **tmp = realloc (argv_, argc_ * sizeof (char *));
196 arg = strndup (p, 1024);
199 argv_[argc_ - 1] = arg;
200 p = strtok (NULL, " \t\n");
206 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
217 * Look for and parse the "heapsize" config parameter.
218 * Manual since none of the clib infra has been bootstrapped yet.
220 * Format: heapsize <nn>[mM][gG]
223 for (i = 1; i < (argc - 1); i++)
225 if (!strncmp (argv[i], "plugin_path", 11))
228 vlib_plugin_path = argv[++i];
230 if (!strncmp (argv[i], "test_plugin_path", 16))
233 vat_plugin_path = argv[++i];
235 else if (!strncmp (argv[i], "heapsize", 8))
237 sizep = (u8 *) argv[i + 1];
239 while (*sizep >= '0' && *sizep <= '9')
242 size += *sizep++ - '0';
248 "warning: heapsize parse error '%s', use default %lld\n",
249 argv[i], (long long int) main_heap_size);
253 main_heap_size = size;
255 if (*sizep == 'g' || *sizep == 'G')
256 main_heap_size <<= 30;
257 else if (*sizep == 'm' || *sizep == 'M')
258 main_heap_size <<= 20;
260 else if (!strncmp (argv[i], "main-core", 9))
265 unsigned long x = strtol (argv[++i], 0, 0);
274 /* set process affinity for main thread */
276 CPU_SET (main_core, &cpuset);
277 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
279 /* Set up the plugin message ID allocator right now... */
280 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
282 /* Allocate main heap */
283 if ((main_heap = clib_mem_init_thread_safe (0, main_heap_size)))
285 vlib_worker_thread_t tmp;
287 /* Figure out which numa runs the main thread */
288 vlib_get_thread_core_numa (&tmp, main_core);
289 __os_numa_index = tmp.numa_id;
291 /* and use the main heap as that numa's numa heap */
292 clib_mem_set_per_numa_heap (main_heap);
294 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
296 return vlib_unix_main (argc, argv);
301 int rv __attribute__ ((unused)) =
302 write (2, "Main heap allocation failure!\r\n", 31);
308 static clib_error_t *
309 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
313 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
315 if (unformat (input, "%dm", &junk)
316 || unformat (input, "%dM", &junk)
317 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
320 return clib_error_return (0, "unknown input '%U'",
321 format_unformat_error, input);
326 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
328 static clib_error_t *
329 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
333 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
335 if (unformat (input, "%s", &junk))
341 return clib_error_return (0, "unknown input '%U'",
342 format_unformat_error, input);
347 static clib_error_t *
348 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
350 return placeholder_path_config (vm, input);
353 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
355 static clib_error_t *
356 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
358 return placeholder_path_config (vm, input);
361 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
363 void vl_msg_api_post_mortem_dump (void);
364 void elog_post_mortem_dump (void);
369 vl_msg_api_post_mortem_dump ();
370 elog_post_mortem_dump ();
374 void vhost_user_unmap_all (void) __attribute__ ((weak));
376 vhost_user_unmap_all (void)
383 static int recursion_block;
392 vl_msg_api_post_mortem_dump ();
393 elog_post_mortem_dump ();
394 vhost_user_unmap_all ();
400 #ifdef BARRIER_TRACING
402 vl_msg_api_barrier_trace_context (const char *context)
404 vlib_worker_threads[0].barrier_context = context;
409 vl_msg_api_barrier_sync (void)
411 vlib_worker_thread_barrier_sync (vlib_get_main ());
415 vl_msg_api_barrier_release (void)
417 vlib_worker_thread_barrier_release (vlib_get_main ());
420 /* This application needs 1 thread stack for the stats pthread */
422 vlib_app_num_thread_stacks_needed (void)
428 * Depending on the configuration selected above,
429 * it may be necessary to generate stub graph nodes.
430 * It is never OK to ignore "node 'x' refers to unknown node 'y'
434 #include <vppinfra/bihash_8_8.h>
439 u64 actual_virt_size;
440 u64 configured_virt_size;
444 name_sort_cmp (void *a1, void *a2)
446 name_sort_t *n1 = a1;
447 name_sort_t *n2 = a2;
449 return strcmp ((char *) n1->name, (char *) n2->name);
452 static clib_error_t *
453 show_bihash_command_fn (vlib_main_t * vm,
454 unformat_input_t * input, vlib_cli_command_t * cmd)
457 clib_bihash_8_8_t *h;
458 u64 total_actual_virt_size = 0;
459 u64 total_configured_virt_size = 0;
460 u64 actual_virt_size;
461 u64 configured_virt_size;
462 name_sort_t *names = 0;
466 if (unformat (input, "verbose"))
469 for (i = 0; i < vec_len (clib_all_bihashes); i++)
471 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
472 if (alloc_arena (h) || verbose)
474 vec_add2 (names, this, 1);
475 this->name = format (0, "%s%c", h->name, 0);
476 configured_virt_size = h->memory_size;
477 actual_virt_size = alloc_arena (h) ? h->memory_size : 0ULL;
478 this->actual_virt_size = actual_virt_size;
479 this->configured_virt_size = configured_virt_size;
480 total_actual_virt_size += actual_virt_size;
481 total_configured_virt_size += configured_virt_size;
485 vec_sort_with_function (names, name_sort_cmp);
487 vlib_cli_output (vm, "%-30s %8s %s", "Name", "Actual", "Configured");
489 for (i = 0; i < vec_len (names); i++)
491 vlib_cli_output (vm, "%-30s %8U %U", names[i].name,
493 names[i].actual_virt_size,
494 format_memory_size, names[i].configured_virt_size);
495 vec_free (names[i].name);
500 vlib_cli_output (vm, "%-30s %8U %U", "Total",
501 format_memory_size, total_actual_virt_size,
502 format_memory_size, total_configured_virt_size);
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")