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 = "/usr/lib/vpp_plugins";
33 char *vlib_plugin_app_version = VPP_BUILD_VER;
36 vpp_find_plugin_path ()
38 extern char *vat_plugin_path;
39 char *p, path[PATH_MAX];
43 /* find executable path */
44 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
47 /* readlink doesn't provide null termination */
51 if ((p = strrchr (path, '/')) == 0)
56 if ((p = strrchr (path, '/')) == 0)
60 s = format (0, "%s/lib/vpp_plugins", path);
62 vlib_plugin_path = (char *) s;
64 s = format (0, "%s/lib/vpp_api_test_plugins", path);
66 vat_plugin_path = (char *) s;
70 vpe_main_init (vlib_main_t * vm)
72 void vat_plugin_hash_create (void);
75 vlib_unix_cli_set_prompt ("DBGvpp# ");
77 vlib_unix_cli_set_prompt ("vpp# ");
79 /* Turn off network stack components which we don't want */
80 vlib_mark_init_function_complete (vm, srp_init);
83 * Create the binary api plugin hashes before loading plugins
85 vat_plugin_hash_create ();
87 vpp_find_plugin_path ();
91 * Default path for runtime data
93 char *vlib_default_runtime_dir = "vpp";
96 main (int argc, char *argv[])
99 vlib_main_t *vm = &vlib_global_main;
100 void vl_msg_api_set_first_available_msg_id (u16);
101 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;
237 else if (!strncmp (argv[i], "main-core", 9))
242 unsigned long x = strtol (argv[++i], 0, 0);
251 /* set process affinity for main thread */
253 CPU_SET (main_core, &cpuset);
254 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
256 /* Set up the plugin message ID allocator right now... */
257 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
259 /* Allocate main heap */
260 if (clib_mem_init_thread_safe (0, main_heap_size))
262 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
264 return vlib_unix_main (argc, argv);
269 int rv __attribute__ ((unused)) =
270 write (2, "Main heap allocation failure!\r\n", 31);
276 static clib_error_t *
277 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
281 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
283 if (unformat (input, "%dm", &junk)
284 || unformat (input, "%dM", &junk)
285 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
288 return clib_error_return (0, "unknown input '%U'",
289 format_unformat_error, input);
294 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
296 static clib_error_t *
297 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
301 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
303 if (unformat (input, "%s", &junk))
309 return clib_error_return (0, "unknown input '%U'",
310 format_unformat_error, input);
315 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
317 void vl_msg_api_post_mortem_dump (void);
318 void elog_post_mortem_dump (void);
323 vl_msg_api_post_mortem_dump ();
324 elog_post_mortem_dump ();
328 void vhost_user_unmap_all (void) __attribute__ ((weak));
330 vhost_user_unmap_all (void)
337 static int recursion_block;
346 vl_msg_api_post_mortem_dump ();
347 elog_post_mortem_dump ();
348 vhost_user_unmap_all ();
354 #ifdef BARRIER_TRACING
356 vl_msg_api_barrier_trace_context (const char *context)
358 vlib_worker_threads[0].barrier_context = context;
363 vl_msg_api_barrier_sync (void)
365 vlib_worker_thread_barrier_sync (vlib_get_main ());
369 vl_msg_api_barrier_release (void)
371 vlib_worker_thread_barrier_release (vlib_get_main ());
374 /* This application needs 1 thread stack for the stats pthread */
376 vlib_app_num_thread_stacks_needed (void)
382 * Depending on the configuration selected above,
383 * it may be necessary to generate stub graph nodes.
384 * It is never OK to ignore "node 'x' refers to unknown node 'y'
389 * fd.io coding-style-patch-verification: ON
392 * eval: (c-set-style "gnu")