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.
16 #include <vppinfra/cpu.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/ethernet/ethernet.h>
22 #include <vpp-api/vpe_msg_enum.h>
25 #include <vnet/devices/dpdk/dpdk.h>
28 * Called by the dpdk driver's rte_delay_us() function.
29 * Return 0 to have the dpdk do a regular delay loop.
30 * Return 1 if to skip the delay loop because we are suspending
31 * the calling vlib process instead.
34 rte_delay_us_override (unsigned us)
38 /* Don't bother intercepting for short delays */
43 * Only intercept if we are in a vlib process.
44 * If we are called from a vlib worker thread or the vlib main
45 * thread then do not intercept. (Must not be called from an
46 * independent pthread).
48 if (os_get_cpu_number () == 0)
51 * We're in the vlib main thread or a vlib process. Make sure
52 * the process is running and we're not still initializing.
54 vm = vlib_get_main ();
55 if (vlib_in_process_context (vm))
57 /* Only suspend for the admin_down_process */
58 vlib_process_t *proc = vlib_get_current_process (vm);
59 if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
60 (proc->node_runtime.function != admin_up_down_process))
63 f64 delay = 1e-6 * us;
64 vlib_process_suspend (vm, delay);
68 return 0; // no override
73 vpe_main_init (vlib_main_t * vm)
76 vlib_unix_cli_set_prompt ("DBGvpp# ");
78 vlib_unix_cli_set_prompt ("vpp# ");
80 /* Turn off network stack components which we don't want */
81 vlib_mark_init_function_complete (vm, srp_init);
85 * Load plugins from /usr/lib/vpp_plugins by default
87 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
90 vnet_get_handoff_structure (void)
92 static vnet_plugin_handoff_t _rv, *rv = &_rv;
94 rv->vnet_main = vnet_get_main ();
95 rv->ethernet_main = ðernet_main;
100 main (int argc, char *argv[])
103 vlib_main_t *vm = &vlib_global_main;
104 void vl_msg_api_set_first_available_msg_id (u16);
105 uword main_heap_size = (1ULL << 30);
108 void vlib_set_get_handoff_structure_cb (void *cb);
111 const char *msg = "ERROR: This binary requires CPU with %s extensions.\n";
113 if (!clib_cpu_supports_ ## a ()) \
115 fprintf(stderr, msg, b); \
140 * Load startup config from file.
141 * usage: vpp -c /etc/vpp/startup.conf
143 if ((argc == 3) && !strncmp (argv[1], "-c", 2))
152 fp = fopen (argv[2], "r");
155 fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
158 argv_ = calloc (1, sizeof (char *));
161 arg = strndup (argv[0], 1024);
168 if (fgets (inbuf, 4096, fp) == 0)
170 p = strtok (inbuf, " \t\n");
176 char **tmp = realloc (argv_, argc_ * sizeof (char *));
180 arg = strndup (p, 1024);
183 argv_[argc_ - 1] = arg;
184 p = strtok (NULL, " \t\n");
190 char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
201 * Look for and parse the "heapsize" config parameter.
202 * Manual since none of the clib infra has been bootstrapped yet.
204 * Format: heapsize <nn>[mM][gG]
207 for (i = 1; i < (argc - 1); i++)
209 if (!strncmp (argv[i], "plugin_path", 11))
212 vlib_plugin_path = argv[++i];
214 else if (!strncmp (argv[i], "heapsize", 8))
216 sizep = (u8 *) argv[i + 1];
218 while (*sizep >= '0' && *sizep <= '9')
221 size += *sizep++ - '0';
227 "warning: heapsize parse error '%s', use default %lld\n",
228 argv[i], (long long int) main_heap_size);
232 main_heap_size = size;
234 if (*sizep == 'g' || *sizep == 'G')
235 main_heap_size <<= 30;
236 else if (*sizep == 'm' || *sizep == 'M')
237 main_heap_size <<= 20;
243 /* Set up the plugin message ID allocator right now... */
244 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
246 /* Allocate main heap */
247 if (clib_mem_init (0, main_heap_size))
249 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
253 dpdk_pmd_constructor_init ();
256 unix_physmem_init (vm, 0 /* fail_if_physical_memory_not_present */ );
258 vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
259 return vlib_unix_main (argc, argv);
264 int rv __attribute__ ((unused)) =
265 write (2, "Main heap allocation failure!\r\n", 31);
271 static clib_error_t *
272 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
276 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
278 if (unformat (input, "%dm", &junk)
279 || unformat (input, "%dM", &junk)
280 || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
283 return clib_error_return (0, "unknown input '%U'",
284 format_unformat_error, input);
289 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
291 static clib_error_t *
292 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
296 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
298 if (unformat (input, "%s", &junk))
304 return clib_error_return (0, "unknown input '%U'",
305 format_unformat_error, input);
310 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
312 void vl_msg_api_post_mortem_dump (void);
317 vl_msg_api_post_mortem_dump ();
321 void vhost_user_unmap_all (void) __attribute__ ((weak));
323 vhost_user_unmap_all (void)
330 static int recursion_block;
339 vl_msg_api_post_mortem_dump ();
340 vhost_user_unmap_all ();
347 vl_msg_api_barrier_sync (void)
349 vlib_worker_thread_barrier_sync (vlib_get_main ());
353 vl_msg_api_barrier_release (void)
355 vlib_worker_thread_barrier_release (vlib_get_main ());
358 /* This application needs 1 thread stack for the stats pthread */
360 vlib_app_num_thread_stacks_needed (void)
366 * Depending on the configuration selected above,
367 * it may be necessary to generate stub graph nodes.
368 * It is never OK to ignore "node 'x' refers to unknown node 'y'
374 static clib_error_t *
375 test_crash_command_fn (vlib_main_t * vm,
376 unformat_input_t * input, vlib_cli_command_t * cmd)
378 u64 *p = (u64 *) 0xdefec8ed;
387 VLIB_CLI_COMMAND (test_crash_command, static) = {
388 .path = "test crash",
389 .short_help = "crash the bus!",
390 .function = test_crash_command_fn,
397 * fd.io coding-style-patch-verification: ON
400 * eval: (c-set-style "gnu")