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.
15 #include <vlib/vlib.h>
16 #include <vlib/unix/unix.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vnet/ethernet/ethernet.h>
20 #include <api/vpe_msg_enum.h>
23 #include <vnet/devices/dpdk/dpdk.h>
27 vpe_main_init (vlib_main_t * vm)
30 vlib_unix_cli_set_prompt ("DBGvpp# ");
32 vlib_unix_cli_set_prompt ("vpp# ");
34 /* Turn off network stack components which we don't want */
35 vlib_mark_init_function_complete (vm, srp_init);
39 * Load plugins from /usr/lib/vpp_plugins by default
41 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
43 void *vnet_get_handoff_structure (void)
45 static vnet_plugin_handoff_t _rv, *rv = &_rv;
47 rv->vnet_main = vnet_get_main();
48 rv->ethernet_main = ðernet_main;
52 int main (int argc, char * argv[])
55 vlib_main_t * vm = &vlib_global_main;
56 void vl_msg_api_set_first_available_msg_id (u16);
57 uword main_heap_size = (1ULL << 30);
60 void vlib_set_get_handoff_structure_cb (void *cb);
62 #if __x86_64__ && !defined(__clang__)
63 __builtin_cpu_init ();
64 const char * msg = "ERROR: This binary requires CPU with %s extensions.\n";
66 if (!__builtin_cpu_supports(a)) \
68 fprintf(stderr, msg, b); \
94 * Load startup config from file.
95 * usage: vpp -c /etc/vpp/startup.conf
97 if ((argc == 3) && !strncmp(argv[1], "-c", 2))
102 char ** argv_ = NULL;
106 fp = fopen (argv[2], "r");
109 fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
112 argv_ = calloc(1, sizeof(char *));
115 arg = strndup(argv[0], 1024);
121 if (fgets(inbuf, 4096, fp) == 0)
123 p = strtok(inbuf, " \t\n");
128 char ** tmp = realloc(argv_, argc_ * sizeof(char *));
132 arg = strndup(p, 1024);
135 argv_[argc_ - 1] = arg;
136 p = strtok(NULL, " \t\n");
142 char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
153 * Look for and parse the "heapsize" config parameter.
154 * Manual since none of the clib infra has been bootstrapped yet.
156 * Format: heapsize <nn>[mM][gG]
159 for (i = 1; i < (argc-1); i++) {
160 if (!strncmp (argv[i], "plugin_path", 11)) {
162 vlib_plugin_path = argv[++i];
163 } else if (!strncmp (argv[i], "heapsize", 8)) {
164 sizep = (u8 *) argv[i+1];
166 while (*sizep >= '0' && *sizep <= '9') {
168 size += *sizep++ - '0';
173 "warning: heapsize parse error '%s', use default %lld\n",
174 argv[i], (long long int) main_heap_size);
178 main_heap_size = size;
180 if (*sizep == 'g' || *sizep == 'G')
181 main_heap_size <<= 30;
182 else if (*sizep == 'm' || *sizep == 'M')
183 main_heap_size <<= 20;
189 /* Set up the plugin message ID allocator right now... */
190 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
192 /* Allocate main heap */
193 if (clib_mem_init (0, main_heap_size)) {
194 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
198 dpdk_pmd_constructor_init();
201 unix_physmem_init(vm, 0 /* fail_if_physical_memory_not_present */);
203 vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
204 return vlib_unix_main (argc, argv);
207 int rv __attribute__((unused)) =
208 write (2, "Main heap allocation failure!\r\n", 31);
214 static clib_error_t *
215 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
219 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
220 if (unformat (input, "%dm", &junk)
221 || unformat (input, "%dM", &junk)
222 || unformat (input, "%dg", &junk)
223 || unformat (input, "%dG", &junk))
226 return clib_error_return (0, "unknown input '%U'",
227 format_unformat_error, input);
232 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
234 static clib_error_t *
235 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
239 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
240 if (unformat (input, "%s", &junk)) {
245 return clib_error_return (0, "unknown input '%U'",
246 format_unformat_error, input);
251 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
253 void vl_msg_api_post_mortem_dump(void);
257 vl_msg_api_post_mortem_dump();
261 void vhost_user_unmap_all (void) __attribute__((weak));
262 void vhost_user_unmap_all (void) { }
264 void os_exit (int code)
266 static int recursion_block;
275 vl_msg_api_post_mortem_dump();
276 vhost_user_unmap_all();
282 void vl_msg_api_barrier_sync(void)
284 vlib_worker_thread_barrier_sync (vlib_get_main());
287 void vl_msg_api_barrier_release(void)
289 vlib_worker_thread_barrier_release (vlib_get_main());
292 /* This application needs 1 thread stack for the stats pthread */
293 u32 vlib_app_num_thread_stacks_needed (void)
299 * Depending on the configuration selected above,
300 * it may be necessary to generate stub graph nodes.
301 * It is never OK to ignore "node 'x' refers to unknown node 'y'
307 static clib_error_t *
308 test_crash_command_fn (vlib_main_t * vm,
309 unformat_input_t * input,
310 vlib_cli_command_t * cmd)
312 u64 * p = (u64 *)0xdefec8ed;
320 VLIB_CLI_COMMAND (test_crash_command, static) = {
321 .path = "test crash",
322 .short_help = "crash the bus!",
323 .function = test_crash_command_fn,