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)
34 vlib_unix_cli_set_prompt ("DBGvpp# ");
36 vlib_unix_cli_set_prompt ("vpp# ");
38 /* Turn off network stack components which we don't want */
39 vlib_mark_init_function_complete (vm, srp_init);
43 * Load plugins from /usr/lib/vpp_plugins by default
45 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
47 void *vnet_get_handoff_structure (void)
49 static vnet_plugin_handoff_t _rv, *rv = &_rv;
51 rv->vnet_main = vnet_get_main();
52 rv->ethernet_main = ðernet_main;
56 int main (int argc, char * argv[])
59 vlib_main_t * vm = &vlib_global_main;
60 void vl_msg_api_set_first_available_msg_id (u16);
61 uword main_heap_size = (1ULL << 30);
64 void vlib_set_get_handoff_structure_cb (void *cb);
67 * Load startup config from file.
68 * usage: vpp -c /etc/vpp/startup.conf
70 if ((argc == 3) && !strncmp(argv[1], "-c", 2))
79 fp = fopen (argv[2], "r");
82 fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
85 argv_ = calloc(1, sizeof(char *));
88 arg = strndup(argv[0], 1024);
94 if (fgets(inbuf, 4096, fp) == 0)
96 p = strtok(inbuf, " \t\n");
101 char ** tmp = realloc(argv_, argc_ * sizeof(char *));
105 arg = strndup(p, 1024);
108 argv_[argc_ - 1] = arg;
109 p = strtok(NULL, " \t\n");
115 char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
126 * Look for and parse the "heapsize" config parameter.
127 * Manual since none of the clib infra has been bootstrapped yet.
129 * Format: heapsize <nn>[mM][gG]
132 for (i = 1; i < (argc-1); i++) {
133 if (!strncmp (argv[i], "plugin_path", 11)) {
135 vlib_plugin_path = argv[++i];
136 } else if (!strncmp (argv[i], "heapsize", 8)) {
137 sizep = (u8 *) argv[i+1];
139 while (*sizep >= '0' && *sizep <= '9') {
141 size += *sizep++ - '0';
146 "warning: heapsize parse error '%s', use default %lld\n",
147 argv[i], (long long int) main_heap_size);
151 main_heap_size = size;
153 if (*sizep == 'g' || *sizep == 'G')
154 main_heap_size <<= 30;
155 else if (*sizep == 'm' || *sizep == 'M')
156 main_heap_size <<= 20;
162 /* Set up the plugin message ID allocator right now... */
163 vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
165 /* Allocate main heap */
166 if (clib_mem_init (0, main_heap_size)) {
167 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
169 vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
170 return vlib_unix_main (argc, argv);
173 int rv __attribute__((unused)) =
174 write (2, "Main heap allocation failure!\r\n", 31);
180 static clib_error_t *
181 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
185 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
186 if (unformat (input, "%dm", &junk)
187 || unformat (input, "%dM", &junk)
188 || unformat (input, "%dg", &junk)
189 || unformat (input, "%dG", &junk))
192 return clib_error_return (0, "unknown input '%U'",
193 format_unformat_error, input);
198 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
200 static clib_error_t *
201 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
205 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
206 if (unformat (input, "%s", &junk)) {
211 return clib_error_return (0, "unknown input '%U'",
212 format_unformat_error, input);
217 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
219 void vl_msg_api_post_mortem_dump(void);
223 vl_msg_api_post_mortem_dump();
227 void vhost_user_unmap_all (void) __attribute__((weak));
228 void vhost_user_unmap_all (void) { }
230 void os_exit (int code)
232 static int recursion_block;
241 vl_msg_api_post_mortem_dump();
242 vhost_user_unmap_all();
248 void vl_msg_api_barrier_sync(void)
250 vlib_worker_thread_barrier_sync (vlib_get_main());
253 void vl_msg_api_barrier_release(void)
255 vlib_worker_thread_barrier_release (vlib_get_main());
258 /* This application needs 1 thread stack for the stats pthread */
259 u32 vlib_app_num_thread_stacks_needed (void)
265 * Depending on the configuration selected above,
266 * it may be necessary to generate stub graph nodes.
267 * It is never OK to ignore "node 'x' refers to unknown node 'y'
272 #define foreach_ipv6_sr_stub_node \
273 _(ipsec-output, ipsec_output)
275 #define foreach_ipv6_sr_stub_node
280 m##_node_fn (vlib_main_t *vm, \
281 vlib_node_runtime_t *node, \
282 vlib_frame_t *frame) \
284 clib_warning("unimplemented, leaking buffers..."); \
288 VLIB_REGISTER_NODE(m##_node) = { \
289 .function = m##_node_fn, \
291 .vector_size = sizeof(u32), \
292 .type = VLIB_NODE_TYPE_INTERNAL, \
294 foreach_ipv6_sr_stub_node;
299 static clib_error_t *
300 test_crash_command_fn (vlib_main_t * vm,
301 unformat_input_t * input,
302 vlib_cli_command_t * cmd)
304 u64 * p = (u64 *)0xdefec8ed;
312 VLIB_CLI_COMMAND (test_crash_command, static) = {
313 .path = "test crash",
314 .short_help = "crash the bus!",
315 .function = test_crash_command_fn,