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>
22 /** \mainpage Virtual Packet Edge Documentation
23 * \section intro_sec Introduction
25 * VPE is a specific vector packet processing application,
26 * designed to steer packets to/from tenant virtual machines.
31 vpe_main_init (vlib_main_t * vm)
33 clib_error_t * error = 0;
34 void vnet_library_plugin_reference(void);
37 vlib_unix_cli_set_prompt ("DBGvpp# ");
39 vlib_unix_cli_set_prompt ("vpp# ");
41 vnet_library_plugin_reference();
43 if ((error = vlib_call_init_function (vm, pg_init)))
45 if ((error = vlib_call_init_function (vm, ip_main_init)))
47 if ((error = vlib_call_init_function (vm, osi_init)))
49 if ((error = vlib_call_init_function (vm, l2_init)))
51 if ((error = vlib_call_init_function (vm, ethernet_init)))
53 if ((error = vlib_call_init_function (vm, ethernet_arp_init)))
55 if ((error = vlib_call_init_function (vm, map_init)))
57 if ((error = vlib_call_init_function (vm, sixrd_init)))
59 if ((error = vlib_call_init_function (vm, llc_init)))
61 if ((error = vlib_call_init_function (vm, snap_init)))
63 if ((error = vlib_call_init_function (vm, cdp_init)))
65 if ((error = vlib_call_init_function (vm, nsh_gre_init)))
67 if ((error = vlib_call_init_function (vm, nsh_vxlan_gpe_init)))
69 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
73 if ((error = vlib_call_init_function (vm, dpdk_init)))
75 if ((error = vlib_call_init_function (vm, dpdk_thread_init)))
77 if ((error = vlib_call_init_function (vm, vhost_user_init)))
80 if ((error = vlib_call_init_function (vm, ipsec_init)))
84 if ((error = vlib_call_init_function (vm, vlibmemory_init)))
86 if ((error = vlib_call_init_function (vm, l2tp_init)))
88 if ((error = vlib_call_init_function (vm, gre_init)))
90 if ((error = vlib_call_init_function (vm, gre_interface_init)))
92 if ((error = vlib_call_init_function (vm, mpls_init)))
94 if ((error = vlib_call_init_function (vm, mpls_interface_init)))
96 if ((error = vlib_call_init_function (vm, dhcp_proxy_init)))
98 if ((error = vlib_call_init_function (vm, dhcpv6_proxy_init)))
100 if ((error = vlib_call_init_function (vm, tapcli_init)))
102 if ((error = vlib_call_init_function (vm, gdb_func_init)))
104 if ((error = unix_physmem_init
105 (vm, 0 /* fail_if_physical_memory_not_present */)))
107 if ((error = vlib_call_init_function (vm, tuntap_init)))
110 if ((error = vlib_call_init_function (vm, sr_init)))
113 if ((error = vlib_call_init_function (vm, l2_classify_init)))
115 if ((error = vlib_call_init_function (vm, policer_init)))
117 if ((error = vlib_call_init_function (vm, vxlan_init)))
120 if ((error = vlib_call_init_function (vm, vcgn_init)))
123 if ((error = vlib_call_init_function (vm, li_init)))
129 VLIB_INIT_FUNCTION (vpe_main_init);
132 * Load plugins from /usr/lib/vpp_plugins by default
134 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
136 void *vnet_get_handoff_structure (void)
138 static vnet_plugin_handoff_t _rv, *rv = &_rv;
140 rv->vnet_main = vnet_get_main();
141 rv->ethernet_main = ðernet_main;
145 int main (int argc, char * argv[])
148 void vl_msg_api_set_first_available_msg_id (u16);
149 uword main_heap_size = (1ULL << 30);
152 void vlib_set_get_handoff_structure_cb (void *cb);
155 * Load startup config from file.
156 * usage: vpp -c /etc/vpp/startup.conf
158 if ((argc == 3) && !strncmp(argv[1], "-c", 2))
163 char ** argv_ = NULL;
167 fp = fopen (argv[2], "r");
170 fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
173 argv_ = calloc(1, sizeof(char *));
176 arg = strndup(argv[0], 1024);
182 if (fgets(inbuf, 4096, fp) == 0)
184 p = strtok(inbuf, " \t\n");
189 char ** tmp = realloc(argv_, argc_ * sizeof(char *));
193 arg = strndup(p, 1024);
196 argv_[argc_ - 1] = arg;
197 p = strtok(NULL, " \t\n");
203 char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
214 * Look for and parse the "heapsize" config parameter.
215 * Manual since none of the clib infra has been bootstrapped yet.
217 * Format: heapsize <nn>[mM][gG]
220 for (i = 1; i < (argc-1); i++) {
221 if (!strncmp (argv[i], "plugin_path", 11)) {
223 vlib_plugin_path = argv[++i];
224 } else if (!strncmp (argv[i], "heapsize", 8)) {
225 sizep = (u8 *) argv[i+1];
227 while (*sizep >= '0' && *sizep <= '9') {
229 size += *sizep++ - '0';
234 "warning: heapsize parse error '%s', use default %lld\n",
235 argv[i], (long long int) main_heap_size);
239 main_heap_size = size;
241 if (*sizep == 'g' || *sizep == 'G')
242 main_heap_size <<= 30;
243 else if (*sizep == 'm' || *sizep == 'M')
244 main_heap_size <<= 20;
250 /* Set up the plugin message ID allocator right now... */
251 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
253 /* Allocate main heap */
254 if (clib_mem_init (0, main_heap_size)) {
255 vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
256 return vlib_unix_main (argc, argv);
259 int rv __attribute__((unused)) =
260 write (2, "Main heap allocation failure!\r\n", 31);
266 static clib_error_t *
267 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
271 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
272 if (unformat (input, "%dm", &junk)
273 || unformat (input, "%dM", &junk)
274 || unformat (input, "%dg", &junk)
275 || unformat (input, "%dG", &junk))
278 return clib_error_return (0, "unknown input '%U'",
279 format_unformat_error, input);
284 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
286 static clib_error_t *
287 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
291 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
292 if (unformat (input, "%s", &junk)) {
297 return clib_error_return (0, "unknown input '%U'",
298 format_unformat_error, input);
303 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
305 void vl_msg_api_post_mortem_dump(void);
309 vl_msg_api_post_mortem_dump();
313 void vhost_user_unmap_all (void) __attribute__((weak));
314 void vhost_user_unmap_all (void) { }
316 void os_exit (int code)
318 static int recursion_block;
327 vl_msg_api_post_mortem_dump();
328 vhost_user_unmap_all();
334 void vl_msg_api_barrier_sync(void)
336 vlib_worker_thread_barrier_sync (vlib_get_main());
339 void vl_msg_api_barrier_release(void)
341 vlib_worker_thread_barrier_release (vlib_get_main());
344 /* This application needs 1 thread stack for the stats pthread */
345 u32 vlib_app_num_thread_stacks_needed (void)
351 * Depending on the configuration selected above,
352 * it may be necessary to generate stub graph nodes.
353 * It is never OK to ignore "node 'x' refers to unknown node 'y'
358 #define foreach_ipv6_sr_stub_node \
359 _(ipsec-output, ipsec_output)
361 #define foreach_ipv6_sr_stub_node
366 m##_node_fn (vlib_main_t *vm, \
367 vlib_node_runtime_t *node, \
368 vlib_frame_t *frame) \
370 clib_warning("unimplemented, leaking buffers..."); \
374 VLIB_REGISTER_NODE(m##_node) = { \
375 .function = m##_node_fn, \
377 .vector_size = sizeof(u32), \
378 .type = VLIB_NODE_TYPE_INTERNAL, \
380 foreach_ipv6_sr_stub_node;
385 static clib_error_t *
386 test_crash_command_fn (vlib_main_t * vm,
387 unformat_input_t * input,
388 vlib_cli_command_t * cmd)
390 u64 * p = (u64 *)0xdefec8ed;
398 VLIB_CLI_COMMAND (test_crash_command, static) = {
399 .path = "test crash",
400 .short_help = "crash the bus!",
401 .function = test_crash_command_fn,