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/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 if (!vlib_plugin_path)
88 vpp_find_plugin_path ();
92 * Default path for runtime data
94 char *vlib_default_runtime_dir = "vpp";
97 main (int argc, char *argv[])
100 vlib_main_t *vm = &vlib_global_main;
101 void vl_msg_api_set_first_available_msg_id (u16);
102 uword main_heap_size = (1ULL << 30);
109 CLIB_UNUSED (const char *msg)
110 = "ERROR: This binary requires CPU with %s extensions.\n";
112 if (!clib_cpu_supports_ ## a ()) \
114 fprintf(stderr, msg, b); \
139 * Load startup config from file.
140 * usage: vpp -c /etc/vpp/startup.conf
142 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
151 fp = fopen (argv[2], "r");
154 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
157 argv_ = calloc (1, sizeof (char *));
163 arg = strndup (argv[0], 1024);
174 if (fgets (inbuf, 4096, fp) == 0)
176 p = strtok (inbuf, " \t\n");
182 char **tmp = realloc (argv_, argc_ * sizeof (char *));
186 arg = strndup (p, 1024);
189 argv_[argc_ - 1] = arg;
190 p = strtok (NULL, " \t\n");
196 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
207 * Look for and parse the "heapsize" config parameter.
208 * Manual since none of the clib infra has been bootstrapped yet.
210 * Format: heapsize <nn>[mM][gG]
213 for (i = 1; i < (argc - 1); i++)
215 if (!strncmp (argv[i], "plugin_path", 11))
218 vlib_plugin_path = argv[++i];
220 else if (!strncmp (argv[i], "heapsize", 8))
222 sizep = (u8 *) argv[i + 1];
224 while (*sizep >= '0' && *sizep <= '9')
227 size += *sizep++ - '0';
233 "warning: heapsize parse error '%s', use default %lld\n",
234 argv[i], (long long int) main_heap_size);
238 main_heap_size = size;
240 if (*sizep == 'g' || *sizep == 'G')
241 main_heap_size <<= 30;
242 else if (*sizep == 'm' || *sizep == 'M')
243 main_heap_size <<= 20;
245 else if (!strncmp (argv[i], "main-core", 9))
250 unsigned long x = strtol (argv[++i], 0, 0);
259 /* set process affinity for main thread */
261 CPU_SET (main_core, &cpuset);
262 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
264 /* Set up the plugin message ID allocator right now... */
265 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
267 /* Allocate main heap */
268 if (clib_mem_init_thread_safe (0, main_heap_size))
270 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
272 return vlib_unix_main (argc, argv);
277 int rv __attribute__ ((unused)) =
278 write (2, "Main heap allocation failure!\r\n", 31);
284 static clib_error_t *
285 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
289 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
291 if (unformat (input, "%dm", &junk)
292 || unformat (input, "%dM", &junk)
293 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
296 return clib_error_return (0, "unknown input '%U'",
297 format_unformat_error, input);
302 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
304 static clib_error_t *
305 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "%s", &junk))
317 return clib_error_return (0, "unknown input '%U'",
318 format_unformat_error, input);
323 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
325 void vl_msg_api_post_mortem_dump (void);
326 void elog_post_mortem_dump (void);
331 vl_msg_api_post_mortem_dump ();
332 elog_post_mortem_dump ();
336 void vhost_user_unmap_all (void) __attribute__ ((weak));
338 vhost_user_unmap_all (void)
345 static int recursion_block;
354 vl_msg_api_post_mortem_dump ();
355 elog_post_mortem_dump ();
356 vhost_user_unmap_all ();
362 #ifdef BARRIER_TRACING
364 vl_msg_api_barrier_trace_context (const char *context)
366 vlib_worker_threads[0].barrier_context = context;
371 vl_msg_api_barrier_sync (void)
373 vlib_worker_thread_barrier_sync (vlib_get_main ());
377 vl_msg_api_barrier_release (void)
379 vlib_worker_thread_barrier_release (vlib_get_main ());
382 /* This application needs 1 thread stack for the stats pthread */
384 vlib_app_num_thread_stacks_needed (void)
390 * Depending on the configuration selected above,
391 * it may be necessary to generate stub graph nodes.
392 * It is never OK to ignore "node 'x' refers to unknown node 'y'
397 * fd.io coding-style-patch-verification: ON
400 * eval: (c-set-style "gnu")