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 <vnet/plugin/plugin.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vpp/app/version.h>
26 #include <vpp/api/vpe_msg_enum.h>
30 * Load plugins from /usr/lib/vpp_plugins by default
32 char *vlib_plugin_path = NULL;
33 char *vlib_plugin_app_version = VPP_BUILD_VER;
34 char *vat_plugin_path = NULL;
37 vpp_find_plugin_path ()
39 extern char *vat_plugin_path;
40 char *p, path[PATH_MAX];
44 /* find executable path */
45 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
48 /* readlink doesn't provide null termination */
52 if ((p = strrchr (path, '/')) == 0)
57 if ((p = strrchr (path, '/')) == 0)
61 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_plugins:"
62 "%s/lib/vpp_plugins", path, path);
64 vlib_plugin_path = (char *) s;
66 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
67 "%s/lib/vpp_api_test_plugins", path, path);
69 vat_plugin_path = (char *) s;
73 vpe_main_init (vlib_main_t * vm)
75 void vat_plugin_hash_create (void);
78 vlib_unix_cli_set_prompt ("DBGvpp# ");
80 vlib_unix_cli_set_prompt ("vpp# ");
82 /* Turn off network stack components which we don't want */
83 vlib_mark_init_function_complete (vm, srp_init);
86 * Create the binary api plugin hashes before loading plugins
88 vat_plugin_hash_create ();
90 if (!vlib_plugin_path)
91 vpp_find_plugin_path ();
95 * Default path for runtime data
97 char *vlib_default_runtime_dir = "vpp";
100 main (int argc, char *argv[])
103 vlib_main_t *vm = &vlib_global_main;
104 void vl_msg_api_set_first_available_msg_id (u16);
105 uword main_heap_size = (1ULL << 30);
112 CLIB_UNUSED (const char *msg)
113 = "ERROR: This binary requires CPU with %s extensions.\n";
115 if (!clib_cpu_supports_ ## a ()) \
117 fprintf(stderr, msg, b); \
142 * Load startup config from file.
143 * usage: vpp -c /etc/vpp/startup.conf
145 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
154 fp = fopen (argv[2], "r");
157 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
160 argv_ = calloc (1, sizeof (char *));
166 arg = strndup (argv[0], 1024);
177 if (fgets (inbuf, 4096, fp) == 0)
179 p = strtok (inbuf, " \t\n");
185 char **tmp = realloc (argv_, argc_ * sizeof (char *));
189 arg = strndup (p, 1024);
192 argv_[argc_ - 1] = arg;
193 p = strtok (NULL, " \t\n");
199 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
210 * Look for and parse the "heapsize" config parameter.
211 * Manual since none of the clib infra has been bootstrapped yet.
213 * Format: heapsize <nn>[mM][gG]
216 for (i = 1; i < (argc - 1); i++)
218 if (!strncmp (argv[i], "plugin_path", 11))
221 vlib_plugin_path = argv[++i];
223 if (!strncmp (argv[i], "test_plugin_path", 16))
226 vat_plugin_path = argv[++i];
228 else if (!strncmp (argv[i], "heapsize", 8))
230 sizep = (u8 *) argv[i + 1];
232 while (*sizep >= '0' && *sizep <= '9')
235 size += *sizep++ - '0';
241 "warning: heapsize parse error '%s', use default %lld\n",
242 argv[i], (long long int) main_heap_size);
246 main_heap_size = size;
248 if (*sizep == 'g' || *sizep == 'G')
249 main_heap_size <<= 30;
250 else if (*sizep == 'm' || *sizep == 'M')
251 main_heap_size <<= 20;
253 else if (!strncmp (argv[i], "main-core", 9))
258 unsigned long x = strtol (argv[++i], 0, 0);
267 /* set process affinity for main thread */
269 CPU_SET (main_core, &cpuset);
270 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
272 /* Set up the plugin message ID allocator right now... */
273 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
275 /* Allocate main heap */
276 if (clib_mem_init_thread_safe (0, main_heap_size))
278 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
280 return vlib_unix_main (argc, argv);
285 int rv __attribute__ ((unused)) =
286 write (2, "Main heap allocation failure!\r\n", 31);
292 static clib_error_t *
293 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
297 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
299 if (unformat (input, "%dm", &junk)
300 || unformat (input, "%dM", &junk)
301 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
304 return clib_error_return (0, "unknown input '%U'",
305 format_unformat_error, input);
310 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
312 static clib_error_t *
313 dummy_path_config (vlib_main_t * vm, unformat_input_t * input)
317 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
319 if (unformat (input, "%s", &junk))
325 return clib_error_return (0, "unknown input '%U'",
326 format_unformat_error, input);
331 static clib_error_t *
332 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
334 return dummy_path_config (vm, input);
337 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
339 static clib_error_t *
340 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
342 return dummy_path_config (vm, input);
345 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
347 void vl_msg_api_post_mortem_dump (void);
348 void elog_post_mortem_dump (void);
353 vl_msg_api_post_mortem_dump ();
354 elog_post_mortem_dump ();
358 void vhost_user_unmap_all (void) __attribute__ ((weak));
360 vhost_user_unmap_all (void)
367 static int recursion_block;
376 vl_msg_api_post_mortem_dump ();
377 elog_post_mortem_dump ();
378 vhost_user_unmap_all ();
384 #ifdef BARRIER_TRACING
386 vl_msg_api_barrier_trace_context (const char *context)
388 vlib_worker_threads[0].barrier_context = context;
393 vl_msg_api_barrier_sync (void)
395 vlib_worker_thread_barrier_sync (vlib_get_main ());
399 vl_msg_api_barrier_release (void)
401 vlib_worker_thread_barrier_release (vlib_get_main ());
404 /* This application needs 1 thread stack for the stats pthread */
406 vlib_app_num_thread_stacks_needed (void)
412 * Depending on the configuration selected above,
413 * it may be necessary to generate stub graph nodes.
414 * It is never OK to ignore "node 'x' refers to unknown node 'y'
418 #include <vppinfra/bihash_8_8.h>
423 u64 actual_virt_size;
424 u64 configured_virt_size;
428 name_sort_cmp (void *a1, void *a2)
430 name_sort_t *n1 = a1;
431 name_sort_t *n2 = a2;
433 return strcmp ((char *) n1->name, (char *) n2->name);
436 static clib_error_t *
437 show_bihash_command_fn (vlib_main_t * vm,
438 unformat_input_t * input, vlib_cli_command_t * cmd)
441 clib_bihash_8_8_t *h;
442 u64 total_actual_virt_size = 0;
443 u64 total_configured_virt_size = 0;
444 u64 actual_virt_size;
445 u64 configured_virt_size;
446 name_sort_t *names = 0;
450 if (unformat (input, "verbose"))
453 for (i = 0; i < vec_len (clib_all_bihashes); i++)
455 h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
456 if (alloc_arena (h) || verbose)
458 vec_add2 (names, this, 1);
459 this->name = format (0, "%s%c", h->name, 0);
460 configured_virt_size = h->memory_size;
461 actual_virt_size = alloc_arena (h) ? h->memory_size : 0ULL;
462 this->actual_virt_size = actual_virt_size;
463 this->configured_virt_size = configured_virt_size;
464 total_actual_virt_size += actual_virt_size;
465 total_configured_virt_size += configured_virt_size;
469 vec_sort_with_function (names, name_sort_cmp);
471 vlib_cli_output (vm, "%-30s %8s %s", "Name", "Actual", "Configured");
473 for (i = 0; i < vec_len (names); i++)
475 vlib_cli_output (vm, "%-30s %8U %U", names[i].name,
477 names[i].actual_virt_size,
478 format_memory_size, names[i].configured_virt_size);
479 vec_free (names[i].name);
484 vlib_cli_output (vm, "%-30s %8U %U", "Total",
485 format_memory_size, total_actual_virt_size,
486 format_memory_size, total_configured_virt_size);
491 VLIB_CLI_COMMAND (show_bihash_command, static) =
493 .path = "show bihash",
494 .short_help = "show bihash",
495 .function = show_bihash_command_fn,
500 * fd.io coding-style-patch-verification: ON
503 * eval: (c-set-style "gnu")