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 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 vat_plugin_hash_create ();
92 if (!vlib_plugin_path)
93 vpp_find_plugin_path ();
97 * Default path for runtime data
99 char *vlib_default_runtime_dir = "vpp";
102 main (int argc, char *argv[])
105 vlib_main_t *vm = &vlib_global_main;
106 void vl_msg_api_set_first_available_msg_id (u16);
107 uword main_heap_size = (1ULL << 30);
115 CLIB_UNUSED (const char *msg)
116 = "ERROR: This binary requires CPU with %s extensions.\n";
118 if (!clib_cpu_supports_ ## a ()) \
120 fprintf(stderr, msg, b); \
145 * Load startup config from file.
146 * usage: vpp -c /etc/vpp/startup.conf
148 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
157 fp = fopen (argv[2], "r");
160 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
163 argv_ = calloc (1, sizeof (char *));
169 arg = strndup (argv[0], 1024);
180 if (fgets (inbuf, 4096, fp) == 0)
182 p = strtok (inbuf, " \t\n");
188 char **tmp = realloc (argv_, argc_ * sizeof (char *));
192 arg = strndup (p, 1024);
195 argv_[argc_ - 1] = arg;
196 p = strtok (NULL, " \t\n");
202 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
213 * Look for and parse the "heapsize" config parameter.
214 * Manual since none of the clib infra has been bootstrapped yet.
216 * Format: heapsize <nn>[mM][gG]
219 for (i = 1; i < (argc - 1); i++)
221 if (!strncmp (argv[i], "plugin_path", 11))
224 vlib_plugin_path = argv[++i];
226 if (!strncmp (argv[i], "test_plugin_path", 16))
229 vat_plugin_path = argv[++i];
231 else if (!strncmp (argv[i], "heapsize", 8))
233 sizep = (u8 *) argv[i + 1];
235 while (*sizep >= '0' && *sizep <= '9')
238 size += *sizep++ - '0';
244 "warning: heapsize parse error '%s', use default %lld\n",
245 argv[i], (long long int) main_heap_size);
249 main_heap_size = size;
251 if (*sizep == 'g' || *sizep == 'G')
252 main_heap_size <<= 30;
253 else if (*sizep == 'm' || *sizep == 'M')
254 main_heap_size <<= 20;
256 else if (!strncmp (argv[i], "main-core", 9))
261 unsigned long x = strtol (argv[++i], 0, 0);
270 /* set process affinity for main thread */
272 CPU_SET (main_core, &cpuset);
273 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
275 /* Set up the plugin message ID allocator right now... */
276 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
278 /* Allocate main heap */
279 if ((main_heap = clib_mem_init_thread_safe (0, main_heap_size)))
281 vlib_worker_thread_t tmp;
283 /* Figure out which numa runs the main thread */
284 vlib_get_thread_core_numa (&tmp, main_core);
285 __os_numa_index = tmp.numa_id;
287 /* and use the main heap as that numa's numa heap */
288 clib_mem_set_per_numa_heap (main_heap);
290 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
292 return vlib_unix_main (argc, argv);
297 int rv __attribute__ ((unused)) =
298 write (2, "Main heap allocation failure!\r\n", 31);
304 static clib_error_t *
305 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "%dm", &junk)
312 || unformat (input, "%dM", &junk)
313 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
316 return clib_error_return (0, "unknown input '%U'",
317 format_unformat_error, input);
322 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
324 static clib_error_t *
325 dummy_path_config (vlib_main_t * vm, unformat_input_t * input)
329 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
331 if (unformat (input, "%s", &junk))
337 return clib_error_return (0, "unknown input '%U'",
338 format_unformat_error, input);
343 static clib_error_t *
344 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
346 return dummy_path_config (vm, input);
349 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
351 static clib_error_t *
352 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
354 return dummy_path_config (vm, input);
357 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
359 void vl_msg_api_post_mortem_dump (void);
360 void elog_post_mortem_dump (void);
365 vl_msg_api_post_mortem_dump ();
366 elog_post_mortem_dump ();
370 void vhost_user_unmap_all (void) __attribute__ ((weak));
372 vhost_user_unmap_all (void)
379 static int recursion_block;
388 vl_msg_api_post_mortem_dump ();
389 elog_post_mortem_dump ();
390 vhost_user_unmap_all ();
396 #ifdef BARRIER_TRACING
398 vl_msg_api_barrier_trace_context (const char *context)
400 vlib_worker_threads[0].barrier_context = context;
405 vl_msg_api_barrier_sync (void)
407 vlib_worker_thread_barrier_sync (vlib_get_main ());
411 vl_msg_api_barrier_release (void)
413 vlib_worker_thread_barrier_release (vlib_get_main ());
416 /* This application needs 1 thread stack for the stats pthread */
418 vlib_app_num_thread_stacks_needed (void)
424 * Depending on the configuration selected above,
425 * it may be necessary to generate stub graph nodes.
426 * It is never OK to ignore "node 'x' refers to unknown node 'y'
430 #include <vppinfra/bihash_8_8.h>
435 u64 actual_virt_size;
436 u64 configured_virt_size;
440 name_sort_cmp (void *a1, void *a2)
442 name_sort_t *n1 = a1;
443 name_sort_t *n2 = a2;
445 return strcmp ((char *) n1->name, (char *) n2->name);
448 static clib_error_t *
449 show_bihash_command_fn (vlib_main_t * vm,
450 unformat_input_t * input, vlib_cli_command_t * cmd)
453 clib_bihash_8_8_t *h;
454 u64 total_actual_virt_size = 0;
455 u64 total_configured_virt_size = 0;
456 u64 actual_virt_size;
457 u64 configured_virt_size;
458 name_sort_t *names = 0;
462 if (unformat (input, "verbose"))
465 for (i = 0; i < vec_len (clib_all_bihashes); i++)
467 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
468 if (alloc_arena (h) || verbose)
470 vec_add2 (names, this, 1);
471 this->name = format (0, "%s%c", h->name, 0);
472 configured_virt_size = h->memory_size;
473 actual_virt_size = alloc_arena (h) ? h->memory_size : 0ULL;
474 this->actual_virt_size = actual_virt_size;
475 this->configured_virt_size = configured_virt_size;
476 total_actual_virt_size += actual_virt_size;
477 total_configured_virt_size += configured_virt_size;
481 vec_sort_with_function (names, name_sort_cmp);
483 vlib_cli_output (vm, "%-30s %8s %s", "Name", "Actual", "Configured");
485 for (i = 0; i < vec_len (names); i++)
487 vlib_cli_output (vm, "%-30s %8U %U", names[i].name,
489 names[i].actual_virt_size,
490 format_memory_size, names[i].configured_virt_size);
491 vec_free (names[i].name);
496 vlib_cli_output (vm, "%-30s %8U %U", "Total",
497 format_memory_size, total_actual_virt_size,
498 format_memory_size, total_configured_virt_size);
503 VLIB_CLI_COMMAND (show_bihash_command, static) =
505 .path = "show bihash",
506 .short_help = "show bihash",
507 .function = show_bihash_command_fn,
511 #ifdef CLIB_SANITIZE_ADDR
512 /* default options for Address Sanitizer */
514 __asan_default_options (void)
516 return VPP_SANITIZE_ADDR_OPTIONS;
518 #endif /* CLIB_SANITIZE_ADDR */
521 * fd.io coding-style-patch-verification: ON
524 * eval: (c-set-style "gnu")