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 s = format (s, ":%s/lib64/vpp_plugins", path);
65 vlib_plugin_path = (char *) s;
67 s = format (0, "%s/lib/vpp_api_test_plugins", path);
69 s = format (s, ":%s/lib64/vpp_api_test_plugins", path);
72 vat_plugin_path = (char *) s;
76 vpe_main_init (vlib_main_t * vm)
78 void vat_plugin_hash_create (void);
81 vlib_unix_cli_set_prompt ("DBGvpp# ");
83 vlib_unix_cli_set_prompt ("vpp# ");
85 /* Turn off network stack components which we don't want */
86 vlib_mark_init_function_complete (vm, srp_init);
89 * Create the binary api plugin hashes before loading plugins
91 vat_plugin_hash_create ();
93 vpp_find_plugin_path ();
97 * Default path for runtime data
99 char *vlib_default_runtime_dir = "vpp";
102 main (int argc, char *argv[])
105 vlib_main_t *vm = &vlib_global_main;
106 void vl_msg_api_set_first_available_msg_id (u16);
107 uword main_heap_size = (1ULL << 30);
114 CLIB_UNUSED (const char *msg)
115 = "ERROR: This binary requires CPU with %s extensions.\n";
117 if (!clib_cpu_supports_ ## a ()) \
119 fprintf(stderr, msg, b); \
144 * Load startup config from file.
145 * usage: vpp -c /etc/vpp/startup.conf
147 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
156 fp = fopen (argv[2], "r");
159 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
162 argv_ = calloc (1, sizeof (char *));
165 arg = strndup (argv[0], 1024);
172 if (fgets (inbuf, 4096, fp) == 0)
174 p = strtok (inbuf, " \t\n");
180 char **tmp = realloc (argv_, argc_ * sizeof (char *));
184 arg = strndup (p, 1024);
187 argv_[argc_ - 1] = arg;
188 p = strtok (NULL, " \t\n");
194 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
205 * Look for and parse the "heapsize" config parameter.
206 * Manual since none of the clib infra has been bootstrapped yet.
208 * Format: heapsize <nn>[mM][gG]
211 for (i = 1; i < (argc - 1); i++)
213 if (!strncmp (argv[i], "plugin_path", 11))
216 vlib_plugin_path = argv[++i];
218 else if (!strncmp (argv[i], "heapsize", 8))
220 sizep = (u8 *) argv[i + 1];
222 while (*sizep >= '0' && *sizep <= '9')
225 size += *sizep++ - '0';
231 "warning: heapsize parse error '%s', use default %lld\n",
232 argv[i], (long long int) main_heap_size);
236 main_heap_size = size;
238 if (*sizep == 'g' || *sizep == 'G')
239 main_heap_size <<= 30;
240 else if (*sizep == 'm' || *sizep == 'M')
241 main_heap_size <<= 20;
243 else if (!strncmp (argv[i], "main-core", 9))
248 unsigned long x = strtol (argv[++i], 0, 0);
257 /* set process affinity for main thread */
259 CPU_SET (main_core, &cpuset);
260 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
262 /* Set up the plugin message ID allocator right now... */
263 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
265 /* Allocate main heap */
266 if (clib_mem_init_thread_safe (0, main_heap_size))
268 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
270 return vlib_unix_main (argc, argv);
275 int rv __attribute__ ((unused)) =
276 write (2, "Main heap allocation failure!\r\n", 31);
282 static clib_error_t *
283 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
287 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
289 if (unformat (input, "%dm", &junk)
290 || unformat (input, "%dM", &junk)
291 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
294 return clib_error_return (0, "unknown input '%U'",
295 format_unformat_error, input);
300 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
302 static clib_error_t *
303 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
307 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309 if (unformat (input, "%s", &junk))
315 return clib_error_return (0, "unknown input '%U'",
316 format_unformat_error, input);
321 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
323 void vl_msg_api_post_mortem_dump (void);
324 void elog_post_mortem_dump (void);
329 vl_msg_api_post_mortem_dump ();
330 elog_post_mortem_dump ();
334 void vhost_user_unmap_all (void) __attribute__ ((weak));
336 vhost_user_unmap_all (void)
343 static int recursion_block;
352 vl_msg_api_post_mortem_dump ();
353 elog_post_mortem_dump ();
354 vhost_user_unmap_all ();
360 #ifdef BARRIER_TRACING
362 vl_msg_api_barrier_trace_context (const char *context)
364 vlib_worker_threads[0].barrier_context = context;
369 vl_msg_api_barrier_sync (void)
371 vlib_worker_thread_barrier_sync (vlib_get_main ());
375 vl_msg_api_barrier_release (void)
377 vlib_worker_thread_barrier_release (vlib_get_main ());
380 /* This application needs 1 thread stack for the stats pthread */
382 vlib_app_num_thread_stacks_needed (void)
388 * Depending on the configuration selected above,
389 * it may be necessary to generate stub graph nodes.
390 * It is never OK to ignore "node 'x' refers to unknown node 'y'
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")