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 *));
162 arg = strndup (argv[0], 1024);
173 if (fgets (inbuf, 4096, fp) == 0)
175 p = strtok (inbuf, " \t\n");
181 char **tmp = realloc (argv_, argc_ * sizeof (char *));
185 arg = strndup (p, 1024);
188 argv_[argc_ - 1] = arg;
189 p = strtok (NULL, " \t\n");
195 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
206 * Look for and parse the "heapsize" config parameter.
207 * Manual since none of the clib infra has been bootstrapped yet.
209 * Format: heapsize <nn>[mM][gG]
212 for (i = 1; i < (argc - 1); i++)
214 if (!strncmp (argv[i], "plugin_path", 11))
217 vlib_plugin_path = argv[++i];
219 else if (!strncmp (argv[i], "heapsize", 8))
221 sizep = (u8 *) argv[i + 1];
223 while (*sizep >= '0' && *sizep <= '9')
226 size += *sizep++ - '0';
232 "warning: heapsize parse error '%s', use default %lld\n",
233 argv[i], (long long int) main_heap_size);
237 main_heap_size = size;
239 if (*sizep == 'g' || *sizep == 'G')
240 main_heap_size <<= 30;
241 else if (*sizep == 'm' || *sizep == 'M')
242 main_heap_size <<= 20;
244 else if (!strncmp (argv[i], "main-core", 9))
249 unsigned long x = strtol (argv[++i], 0, 0);
258 /* set process affinity for main thread */
260 CPU_SET (main_core, &cpuset);
261 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
263 /* Set up the plugin message ID allocator right now... */
264 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
266 /* Allocate main heap */
267 if (clib_mem_init_thread_safe (0, main_heap_size))
269 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
271 return vlib_unix_main (argc, argv);
276 int rv __attribute__ ((unused)) =
277 write (2, "Main heap allocation failure!\r\n", 31);
283 static clib_error_t *
284 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
288 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
290 if (unformat (input, "%dm", &junk)
291 || unformat (input, "%dM", &junk)
292 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
295 return clib_error_return (0, "unknown input '%U'",
296 format_unformat_error, input);
301 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
303 static clib_error_t *
304 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
308 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
310 if (unformat (input, "%s", &junk))
316 return clib_error_return (0, "unknown input '%U'",
317 format_unformat_error, input);
322 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
324 void vl_msg_api_post_mortem_dump (void);
325 void elog_post_mortem_dump (void);
330 vl_msg_api_post_mortem_dump ();
331 elog_post_mortem_dump ();
335 void vhost_user_unmap_all (void) __attribute__ ((weak));
337 vhost_user_unmap_all (void)
344 static int recursion_block;
353 vl_msg_api_post_mortem_dump ();
354 elog_post_mortem_dump ();
355 vhost_user_unmap_all ();
361 #ifdef BARRIER_TRACING
363 vl_msg_api_barrier_trace_context (const char *context)
365 vlib_worker_threads[0].barrier_context = context;
370 vl_msg_api_barrier_sync (void)
372 vlib_worker_thread_barrier_sync (vlib_get_main ());
376 vl_msg_api_barrier_release (void)
378 vlib_worker_thread_barrier_release (vlib_get_main ());
381 /* This application needs 1 thread stack for the stats pthread */
383 vlib_app_num_thread_stacks_needed (void)
389 * Depending on the configuration selected above,
390 * it may be necessary to generate stub graph nodes.
391 * It is never OK to ignore "node 'x' refers to unknown node 'y'
396 * fd.io coding-style-patch-verification: ON
399 * eval: (c-set-style "gnu")