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.
19 #include <pthread_np.h>
20 #endif /* __FreeBSD__ */
23 #include <vppinfra/clib.h>
24 #include <vppinfra/cpu.h>
25 #include <vppinfra/bitmap.h>
26 #include <vppinfra/unix.h>
27 #include <vlib/vlib.h>
28 #include <vlib/unix/unix.h>
29 #include <vlib/threads.h>
30 #include <vnet/plugin/plugin.h>
31 #include <vnet/ethernet/ethernet.h>
32 #include <vpp/app/version.h>
33 #include <vpp/vnet/config.h>
34 #include <vlibmemory/memclnt.api_enum.h> /* To get the last static message id */
38 * Load plugins from /usr/lib/vpp_plugins by default
40 char *vlib_plugin_path = NULL;
41 char *vlib_plugin_app_version = VPP_BUILD_VER;
42 char *vat_plugin_path = NULL;
45 vpp_find_plugin_path ()
47 extern char *vat_plugin_path;
51 /* find executable path */
52 path = os_get_exec_path ();
57 /* add null termination */
61 if ((p = strrchr ((char *) path, '/')) == 0)
66 if ((p = strrchr ((char *) path, '/')) == 0)
70 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_plugins", path, path);
72 vlib_plugin_path = (char *) s;
74 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
76 vat_plugin_path = (char *) s;
83 vpe_main_init (vlib_main_t * vm)
85 #if VPP_API_TEST_BUILTIN > 0
86 void vat_plugin_hash_create (void);
90 vlib_unix_cli_set_prompt ("DBGvpp# ");
92 vlib_unix_cli_set_prompt ("vpp# ");
94 /* Turn off network stack components which we don't want */
95 vlib_mark_init_function_complete (vm, srp_init);
98 * Create the binary api plugin hashes before loading plugins
100 #if VPP_API_TEST_BUILTIN > 0
101 vat_plugin_hash_create ();
104 if (!vlib_plugin_path)
105 vpp_find_plugin_path ();
109 * Default path for runtime data
111 char *vlib_default_runtime_dir = "vpp";
114 main (int argc, char *argv[])
117 void vl_msg_api_set_first_available_msg_id (u16);
118 uword main_heap_size = (1ULL << 30);
121 clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
122 clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
123 unformat_input_t input, sub_input;
130 CLIB_UNUSED (const char *msg)
131 = "ERROR: This binary requires CPU with %s extensions.\n";
133 if (!clib_cpu_supports_ ## a ()) \
135 fprintf(stderr, msg, b); \
160 * Load startup config from file.
161 * usage: vpp -c /etc/vpp/startup.conf
163 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
172 fp = fopen (argv[2], "r");
175 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
178 argv_ = calloc (1, sizeof (char *));
184 arg = strndup (argv[0], 1024);
195 if (fgets (inbuf, 4096, fp) == 0)
197 p = strtok (inbuf, " \t\n");
203 char **tmp = realloc (argv_, argc_ * sizeof (char *));
207 arg = strndup (p, 1024);
210 argv_[argc_ - 1] = arg;
211 p = strtok (NULL, " \t\n");
217 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
228 * Look for and parse the "heapsize" config parameter.
229 * Manual since none of the clib infra has been bootstrapped yet.
231 * Format: heapsize <nn>[mM][gG]
234 for (i = 1; i < (argc - 1); i++)
236 if (!strncmp (argv[i], "plugin_path", 11))
239 vlib_plugin_path = argv[++i];
241 if (!strncmp (argv[i], "test_plugin_path", 16))
244 vat_plugin_path = argv[++i];
246 else if (!strncmp (argv[i], "heapsize", 8))
248 sizep = (u8 *) argv[i + 1];
250 while (*sizep >= '0' && *sizep <= '9')
253 size += *sizep++ - '0';
259 "warning: heapsize parse error '%s', use default %lld\n",
260 argv[i], (long long int) main_heap_size);
264 main_heap_size = size;
266 if (*sizep == 'g' || *sizep == 'G')
267 main_heap_size <<= 30;
268 else if (*sizep == 'm' || *sizep == 'M')
269 main_heap_size <<= 20;
271 else if (!strncmp (argv[i], "main-core", 9))
276 unsigned long x = strtol (argv[++i], 0, 0);
281 else if (!strncmp (argv[i], "interactive", 11))
282 unix_main.flags |= UNIX_FLAG_INTERACTIVE;
283 else if (!strncmp (argv[i], "nosyslog", 8))
284 unix_main.flags |= UNIX_FLAG_NOSYSLOG;
289 clib_mem_init (0, 1 << 20);
290 unformat_init_command_line (&input, (char **) argv);
292 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
294 if (unformat (&input, "memory %v", &v))
296 unformat_init_vector (&sub_input, v);
298 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
300 if (unformat (&sub_input, "main-heap-size %U",
301 unformat_memory_size, &main_heap_size))
303 else if (unformat (&sub_input, "main-heap-page-size %U",
304 unformat_log2_page_size,
305 &main_heap_log2_page_sz))
307 else if (unformat (&sub_input, "default-hugepage-size %U",
308 unformat_log2_page_size,
309 &default_log2_hugepage_sz))
313 fformat (stderr, "unknown 'memory' config input '%U'\n",
314 format_unformat_error, &sub_input);
319 unformat_free (&sub_input);
321 else if (!unformat (&input, "%s %v", &s, &v))
324 vec_reset_length (s);
325 vec_reset_length (v);
330 unformat_free (&input);
332 /* set process affinity for main thread */
336 CPU_SET (main_core, &cpuset);
337 if (pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t),
341 "pthread_setaffinity_np() on cpu %d failed for main thread",
346 /* Set up the plugin message ID allocator right now... */
347 vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
349 /* destroy temporary heap and create main one */
352 if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
353 main_heap_log2_page_sz)))
355 /* Figure out which numa runs the main thread */
356 __os_numa_index = clib_get_current_numa_node ();
358 if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
359 clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
361 /* and use the main heap as that numa's numa heap */
362 clib_mem_set_per_numa_heap (main_heap);
364 vpe_main_init (vlib_get_first_main ());
365 return vlib_unix_main (argc, argv);
370 int rv __attribute__ ((unused)) =
371 write (2, "Main heap allocation failure!\r\n", 31);
377 static clib_error_t *
378 memory_config (vlib_main_t * vm, unformat_input_t * input)
383 VLIB_CONFIG_FUNCTION (memory_config, "memory");
385 static clib_error_t *
386 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
391 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
393 static clib_error_t *
394 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
398 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
400 if (unformat (input, "%s", &junk))
406 return clib_error_return (0, "unknown input '%U'",
407 format_unformat_error, input);
412 static clib_error_t *
413 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
415 return placeholder_path_config (vm, input);
418 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
420 static clib_error_t *
421 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
423 return placeholder_path_config (vm, input);
426 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
428 void vl_msg_api_post_mortem_dump (void);
429 void vlib_post_mortem_dump (void);
434 vl_msg_api_post_mortem_dump ();
435 vlib_post_mortem_dump ();
439 void vhost_user_unmap_all (void) __attribute__ ((weak));
441 vhost_user_unmap_all (void)
448 static int recursion_block;
457 vl_msg_api_post_mortem_dump ();
458 vlib_post_mortem_dump ();
459 vhost_user_unmap_all ();
465 #ifdef BARRIER_TRACING
467 vl_msg_api_barrier_trace_context (const char *context)
469 vlib_worker_threads[0].barrier_context = context;
474 vl_msg_api_barrier_sync (void)
476 vlib_worker_thread_barrier_sync (vlib_get_main ());
480 vl_msg_api_barrier_release (void)
482 vlib_worker_thread_barrier_release (vlib_get_main ());
485 /* This application needs 1 thread stack for the stats pthread */
487 vlib_app_num_thread_stacks_needed (void)
493 * Depending on the configuration selected above,
494 * it may be necessary to generate stub graph nodes.
495 * It is never OK to ignore "node 'x' refers to unknown node 'y'
499 #include <vppinfra/bihash_8_8.h>
501 static clib_error_t *
502 show_bihash_command_fn (vlib_main_t * vm,
503 unformat_input_t * input, vlib_cli_command_t * cmd)
506 clib_bihash_8_8_t *h;
509 if (unformat (input, "verbose"))
512 for (i = 0; i < vec_len (clib_all_bihashes); i++)
514 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
515 vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
521 VLIB_CLI_COMMAND (show_bihash_command, static) =
523 .path = "show bihash",
524 .short_help = "show bihash",
525 .function = show_bihash_command_fn,
528 #ifdef CLIB_SANITIZE_ADDR
529 /* default options for Address Sanitizer */
531 __asan_default_options (void)
533 return VPP_SANITIZE_ADDR_OPTIONS;
535 #endif /* CLIB_SANITIZE_ADDR */
538 * fd.io coding-style-patch-verification: ON
541 * eval: (c-set-style "gnu")