Link the vpp application against libvnet.so, not libvnet.a
[vpp.git] / vpp / vnet / main.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vlib/vlib.h>
16 #include <vlib/unix/unix.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vnet/ethernet/ethernet.h>
19
20 #include <api/vpe_msg_enum.h>
21
22 /** \mainpage Virtual Packet Edge Documentation
23  * \section intro_sec Introduction
24  * 
25  * VPE is a specific vector packet processing application,
26  * designed to steer packets to/from tenant virtual machines.
27  *
28  */
29
30 static void
31 vpe_main_init (vlib_main_t * vm)
32 {
33     if (CLIB_DEBUG > 0)
34         vlib_unix_cli_set_prompt ("DBGvpp# ");
35     else
36         vlib_unix_cli_set_prompt ("vpp# ");
37
38     /* Turn off network stack components which we don't want */
39     vlib_mark_init_function_complete (vm, srp_init);
40 }
41
42 /* 
43  * Load plugins from /usr/lib/vpp_plugins by default
44  */
45 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
46                                                 
47 void *vnet_get_handoff_structure (void)
48 {
49     static vnet_plugin_handoff_t _rv, *rv = &_rv;
50
51     rv->vnet_main = vnet_get_main();
52     rv->ethernet_main = &ethernet_main;
53     return (void *)rv;
54 }
55
56 int main (int argc, char * argv[])
57 {
58     int i;
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);
62     u8 * sizep;
63     u32 size;
64     void vlib_set_get_handoff_structure_cb (void *cb);
65
66     /*
67      * Load startup config from file.
68      * usage: vpp -c /etc/vpp/startup.conf
69      */
70     if ((argc == 3) && !strncmp(argv[1], "-c", 2))
71       {
72         FILE * fp;
73         char inbuf[4096];
74         int argc_ = 1;
75         char ** argv_ = NULL;
76         char * arg = NULL;
77         char * p;
78
79         fp = fopen (argv[2], "r");
80         if (fp == NULL)
81           {
82             fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
83             return 1;
84           }
85         argv_ = calloc(1, sizeof(char *));
86         if (argv_ == NULL)
87           return 1;
88         arg = strndup(argv[0], 1024);
89         if (arg == NULL)
90           return 1;
91         argv_[0] = arg;
92
93         while (1) {
94           if (fgets(inbuf, 4096, fp) == 0)
95             break;
96           p = strtok(inbuf, " \t\n");
97           while (p != NULL) {
98             if (*p == '#')
99               break;
100             argc_++;
101             char ** tmp = realloc(argv_, argc_ * sizeof(char *));
102             if (tmp == NULL)
103               return 1;
104             argv_ = tmp;
105             arg = strndup(p, 1024);
106             if (arg == NULL)
107               return 1;
108             argv_[argc_ - 1] = arg;
109             p = strtok(NULL, " \t\n");
110           }
111         }
112
113         fclose(fp);
114
115         char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
116         if (tmp == NULL)
117            return 1;
118         argv_ = tmp;
119         argv_[argc_] = NULL;
120
121         argc = argc_;
122         argv = argv_;
123       }
124
125     /* 
126      * Look for and parse the "heapsize" config parameter.
127      * Manual since none of the clib infra has been bootstrapped yet.
128      *
129      * Format: heapsize <nn>[mM][gG] 
130      */
131
132     for (i = 1; i < (argc-1); i++) {
133         if (!strncmp (argv[i], "plugin_path", 11)) {
134             if (i < (argc-1))
135                 vlib_plugin_path = argv[++i];
136         } else if (!strncmp (argv[i], "heapsize", 8)) {
137             sizep = (u8 *) argv[i+1];
138             size = 0;
139             while (*sizep >= '0' && *sizep <= '9') {
140                 size *= 10;
141                 size += *sizep++ - '0';
142             }
143             if (size == 0) {
144                 fprintf
145                     (stderr, 
146                      "warning: heapsize parse error '%s', use default %lld\n",
147                      argv[i], (long long int) main_heap_size);
148                 goto defaulted;
149             }
150
151             main_heap_size = size;
152             
153             if (*sizep == 'g' || *sizep == 'G')
154                 main_heap_size <<= 30;
155             else if (*sizep == 'm' || *sizep == 'M')
156                 main_heap_size <<= 20;
157         }
158     }
159             
160 defaulted:
161
162     /* Set up the plugin message ID allocator right now... */
163     vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
164
165     /* Allocate main heap */
166     if (clib_mem_init (0, main_heap_size)) {
167         vm->init_functions_called = hash_create (0, /* value bytes */ 0);
168         vpe_main_init(vm);
169         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
170         return vlib_unix_main (argc, argv);
171     } else {
172       {
173         int rv __attribute__((unused)) =
174           write (2, "Main heap allocation failure!\r\n", 31);
175       }
176         return 1;
177     }
178 }
179
180 static clib_error_t *
181 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
182 {
183     u32 junk;
184
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))
190             return 0;
191         else
192             return clib_error_return (0, "unknown input '%U'",
193                                       format_unformat_error, input);
194     }
195     return 0;
196 }
197
198 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
199
200 static clib_error_t *
201 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
202 {
203     u8 * junk;
204
205     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
206         if (unformat (input, "%s", &junk)) {
207             vec_free(junk);
208             return 0;
209         }
210         else
211             return clib_error_return (0, "unknown input '%U'",
212                                       format_unformat_error, input);
213         }
214     return 0;
215 }
216
217 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
218
219 void vl_msg_api_post_mortem_dump(void);
220
221 void os_panic (void) 
222
223     vl_msg_api_post_mortem_dump();
224     abort (); 
225 }
226
227 void vhost_user_unmap_all (void) __attribute__((weak));
228 void vhost_user_unmap_all (void) { }
229
230 void os_exit (int code)
231
232     static int recursion_block;
233
234     if (code)
235       {
236         if (recursion_block)
237             abort();
238
239         recursion_block = 1;
240
241         vl_msg_api_post_mortem_dump();
242         vhost_user_unmap_all();
243         abort();
244       }
245     exit (code);
246 }
247
248 void vl_msg_api_barrier_sync(void) 
249
250   vlib_worker_thread_barrier_sync (vlib_get_main());
251 }
252
253 void vl_msg_api_barrier_release(void) 
254
255   vlib_worker_thread_barrier_release (vlib_get_main());
256 }
257
258 /* This application needs 1 thread stack for the stats pthread */
259 u32 vlib_app_num_thread_stacks_needed (void) 
260 {
261   return 1;
262 }
263
264 /* 
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'
268  * messages!
269  */
270
271 #if IPV6SR == 0
272 #define foreach_ipv6_sr_stub_node \
273 _(ipsec-output, ipsec_output)
274 #else
275 #define foreach_ipv6_sr_stub_node
276 #endif
277
278 #define _(n,m)                                          \
279 static uword                                            \
280 m##_node_fn (vlib_main_t *vm,                           \
281            vlib_node_runtime_t *node,                   \
282            vlib_frame_t *frame)                         \
283 {                                                       \
284   clib_warning("unimplemented, leaking buffers...");    \
285   return 0;                                             \
286 }                                                       \
287                                                         \
288 VLIB_REGISTER_NODE(m##_node) = {                        \
289   .function = m##_node_fn,                              \
290   .name = #n,                                           \
291   .vector_size = sizeof(u32),                           \
292   .type = VLIB_NODE_TYPE_INTERNAL,                      \
293 };
294 foreach_ipv6_sr_stub_node;
295 #undef _
296
297 #if CLIB_DEBUG > 0
298
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)
303 {
304   u64 * p = (u64 *)0xdefec8ed;
305
306   *p = 0xdeadbeef;
307
308   /* Not so much... */
309   return 0;
310 }
311
312 VLIB_CLI_COMMAND (test_crash_command, static) = {
313     .path = "test crash",
314     .short_help = "crash the bus!",
315     .function = test_crash_command_fn,
316 };
317
318 #endif
319