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;
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/" CLIB_TARGET_TRIPLET "/vpp_plugins:"
61 "%s/lib/vpp_plugins", path, path);
63 vlib_plugin_path = (char *) s;
65 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
66 "%s/lib/vpp_api_test_plugins", path, 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 if (!vlib_plugin_path)
90 vpp_find_plugin_path ();
94 * Default path for runtime data
96 char *vlib_default_runtime_dir = "vpp";
99 main (int argc, char *argv[])
102 vlib_main_t *vm = &vlib_global_main;
103 void vl_msg_api_set_first_available_msg_id (u16);
104 uword main_heap_size = (1ULL << 30);
111 CLIB_UNUSED (const char *msg)
112 = "ERROR: This binary requires CPU with %s extensions.\n";
114 if (!clib_cpu_supports_ ## a ()) \
116 fprintf(stderr, msg, b); \
141 * Load startup config from file.
142 * usage: vpp -c /etc/vpp/startup.conf
144 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
153 fp = fopen (argv[2], "r");
156 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
159 argv_ = calloc (1, sizeof (char *));
165 arg = strndup (argv[0], 1024);
176 if (fgets (inbuf, 4096, fp) == 0)
178 p = strtok (inbuf, " \t\n");
184 char **tmp = realloc (argv_, argc_ * sizeof (char *));
188 arg = strndup (p, 1024);
191 argv_[argc_ - 1] = arg;
192 p = strtok (NULL, " \t\n");
198 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
209 * Look for and parse the "heapsize" config parameter.
210 * Manual since none of the clib infra has been bootstrapped yet.
212 * Format: heapsize <nn>[mM][gG]
215 for (i = 1; i < (argc - 1); i++)
217 if (!strncmp (argv[i], "plugin_path", 11))
220 vlib_plugin_path = argv[++i];
222 else if (!strncmp (argv[i], "heapsize", 8))
224 sizep = (u8 *) argv[i + 1];
226 while (*sizep >= '0' && *sizep <= '9')
229 size += *sizep++ - '0';
235 "warning: heapsize parse error '%s', use default %lld\n",
236 argv[i], (long long int) main_heap_size);
240 main_heap_size = size;
242 if (*sizep == 'g' || *sizep == 'G')
243 main_heap_size <<= 30;
244 else if (*sizep == 'm' || *sizep == 'M')
245 main_heap_size <<= 20;
247 else if (!strncmp (argv[i], "main-core", 9))
252 unsigned long x = strtol (argv[++i], 0, 0);
261 /* set process affinity for main thread */
263 CPU_SET (main_core, &cpuset);
264 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
266 /* Set up the plugin message ID allocator right now... */
267 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
269 /* Allocate main heap */
270 if (clib_mem_init_thread_safe (0, main_heap_size))
272 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
274 return vlib_unix_main (argc, argv);
279 int rv __attribute__ ((unused)) =
280 write (2, "Main heap allocation failure!\r\n", 31);
286 static clib_error_t *
287 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
291 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
293 if (unformat (input, "%dm", &junk)
294 || unformat (input, "%dM", &junk)
295 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
298 return clib_error_return (0, "unknown input '%U'",
299 format_unformat_error, input);
304 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
306 static clib_error_t *
307 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
311 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
313 if (unformat (input, "%s", &junk))
319 return clib_error_return (0, "unknown input '%U'",
320 format_unformat_error, input);
325 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
327 void vl_msg_api_post_mortem_dump (void);
328 void elog_post_mortem_dump (void);
333 vl_msg_api_post_mortem_dump ();
334 elog_post_mortem_dump ();
338 void vhost_user_unmap_all (void) __attribute__ ((weak));
340 vhost_user_unmap_all (void)
347 static int recursion_block;
356 vl_msg_api_post_mortem_dump ();
357 elog_post_mortem_dump ();
358 vhost_user_unmap_all ();
364 #ifdef BARRIER_TRACING
366 vl_msg_api_barrier_trace_context (const char *context)
368 vlib_worker_threads[0].barrier_context = context;
373 vl_msg_api_barrier_sync (void)
375 vlib_worker_thread_barrier_sync (vlib_get_main ());
379 vl_msg_api_barrier_release (void)
381 vlib_worker_thread_barrier_release (vlib_get_main ());
384 /* This application needs 1 thread stack for the stats pthread */
386 vlib_app_num_thread_stacks_needed (void)
392 * Depending on the configuration selected above,
393 * it may be necessary to generate stub graph nodes.
394 * It is never OK to ignore "node 'x' refers to unknown node 'y'
399 * fd.io coding-style-patch-verification: ON
402 * eval: (c-set-style "gnu")