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.
16 #include <vppinfra/cpu.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vpp/app/version.h>
22 #include <vpp/api/vpe_msg_enum.h>
26 * Load plugins from /usr/lib/vpp_plugins by default
28 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
29 char *vlib_plugin_app_version = VPP_BUILD_VER;
32 vpp_find_plugin_path ()
34 extern char *vat_plugin_path;
35 char *p, path[PATH_MAX];
39 /* find executable path */
40 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
43 /* readlink doesn't provide null termination */
47 if ((p = strrchr (path, '/')) == 0)
52 if ((p = strrchr (path, '/')) == 0)
56 s = format (0, "%s/lib/vpp_plugins", path);
58 s = format (s, ":%s/lib64/vpp_plugins", path);
61 vlib_plugin_path = (char *) s;
63 s = format (0, "%s/lib/vpp_api_test_plugins", path);
65 s = format (s, ":%s/lib64/vpp_api_test_plugins", path);
68 vat_plugin_path = (char *) s;
72 vpe_main_init (vlib_main_t * vm)
74 void vat_plugin_hash_create (void);
77 vlib_unix_cli_set_prompt ("DBGvpp# ");
79 vlib_unix_cli_set_prompt ("vpp# ");
81 /* Turn off network stack components which we don't want */
82 vlib_mark_init_function_complete (vm, srp_init);
85 * Create the binary api plugin hashes before loading plugins
87 vat_plugin_hash_create ();
89 vpp_find_plugin_path ();
93 * Default path for runtime data
95 char *vlib_default_runtime_dir = "vpp";
98 main (int argc, char *argv[])
101 vlib_main_t *vm = &vlib_global_main;
102 void vl_msg_api_set_first_available_msg_id (u16);
103 uword main_heap_size = (1ULL << 30);
108 CLIB_UNUSED (const char *msg)
109 = "ERROR: This binary requires CPU with %s extensions.\n";
111 if (!clib_cpu_supports_ ## a ()) \
113 fprintf(stderr, msg, b); \
138 * Load startup config from file.
139 * usage: vpp -c /etc/vpp/startup.conf
141 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
150 fp = fopen (argv[2], "r");
153 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
156 argv_ = calloc (1, sizeof (char *));
159 arg = strndup (argv[0], 1024);
166 if (fgets (inbuf, 4096, fp) == 0)
168 p = strtok (inbuf, " \t\n");
174 char **tmp = realloc (argv_, argc_ * sizeof (char *));
178 arg = strndup (p, 1024);
181 argv_[argc_ - 1] = arg;
182 p = strtok (NULL, " \t\n");
188 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
199 * Look for and parse the "heapsize" config parameter.
200 * Manual since none of the clib infra has been bootstrapped yet.
202 * Format: heapsize <nn>[mM][gG]
205 for (i = 1; i < (argc - 1); i++)
207 if (!strncmp (argv[i], "plugin_path", 11))
210 vlib_plugin_path = argv[++i];
212 else if (!strncmp (argv[i], "heapsize", 8))
214 sizep = (u8 *) argv[i + 1];
216 while (*sizep >= '0' && *sizep <= '9')
219 size += *sizep++ - '0';
225 "warning: heapsize parse error '%s', use default %lld\n",
226 argv[i], (long long int) main_heap_size);
230 main_heap_size = size;
232 if (*sizep == 'g' || *sizep == 'G')
233 main_heap_size <<= 30;
234 else if (*sizep == 'm' || *sizep == 'M')
235 main_heap_size <<= 20;
241 /* Set up the plugin message ID allocator right now... */
242 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
244 /* Allocate main heap */
245 if (clib_mem_init (0, main_heap_size))
247 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
249 return vlib_unix_main (argc, argv);
254 int rv __attribute__ ((unused)) =
255 write (2, "Main heap allocation failure!\r\n", 31);
261 static clib_error_t *
262 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
266 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
268 if (unformat (input, "%dm", &junk)
269 || unformat (input, "%dM", &junk)
270 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
273 return clib_error_return (0, "unknown input '%U'",
274 format_unformat_error, input);
279 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
281 static clib_error_t *
282 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
286 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
288 if (unformat (input, "%s", &junk))
294 return clib_error_return (0, "unknown input '%U'",
295 format_unformat_error, input);
300 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
302 void vl_msg_api_post_mortem_dump (void);
303 void elog_post_mortem_dump (void);
308 vl_msg_api_post_mortem_dump ();
309 elog_post_mortem_dump ();
313 void vhost_user_unmap_all (void) __attribute__ ((weak));
315 vhost_user_unmap_all (void)
322 static int recursion_block;
331 vl_msg_api_post_mortem_dump ();
332 elog_post_mortem_dump ();
333 vhost_user_unmap_all ();
339 #ifdef BARRIER_TRACING
341 vl_msg_api_barrier_trace_context (const char *context)
343 vlib_worker_threads[0].barrier_context = context;
348 vl_msg_api_barrier_sync (void)
350 vlib_worker_thread_barrier_sync (vlib_get_main ());
354 vl_msg_api_barrier_release (void)
356 vlib_worker_thread_barrier_release (vlib_get_main ());
359 /* This application needs 1 thread stack for the stats pthread */
361 vlib_app_num_thread_stacks_needed (void)
367 * Depending on the configuration selected above,
368 * it may be necessary to generate stub graph nodes.
369 * It is never OK to ignore "node 'x' refers to unknown node 'y'
375 static clib_error_t *
376 test_crash_command_fn (vlib_main_t * vm,
377 unformat_input_t * input, vlib_cli_command_t * cmd)
379 u64 *p = (u64 *) 0xdefec8ed;
381 ELOG_TYPE_DECLARE (e) =
383 .format = "deliberate crash: touching %x",.format_args = "i4",};
385 elog (&vm->elog_main, &e, 0xdefec8ed);
394 VLIB_CLI_COMMAND (test_crash_command, static) = {
395 .path = "test crash",
396 .short_help = "crash the bus!",
397 .function = test_crash_command_fn,
404 * fd.io coding-style-patch-verification: ON
407 * eval: (c-set-style "gnu")