Add support for multiple microarchitectures in single binary
[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
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>
21
22 #include <api/vpe_msg_enum.h>
23
24 #if DPDK
25 #include <vnet/devices/dpdk/dpdk.h>
26 #endif
27
28 static void
29 vpe_main_init (vlib_main_t * vm)
30 {
31     if (CLIB_DEBUG > 0)
32         vlib_unix_cli_set_prompt ("DBGvpp# ");
33     else
34         vlib_unix_cli_set_prompt ("vpp# ");
35
36     /* Turn off network stack components which we don't want */
37     vlib_mark_init_function_complete (vm, srp_init);
38 }
39
40 /* 
41  * Load plugins from /usr/lib/vpp_plugins by default
42  */
43 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
44                                                 
45 void *vnet_get_handoff_structure (void)
46 {
47     static vnet_plugin_handoff_t _rv, *rv = &_rv;
48
49     rv->vnet_main = vnet_get_main();
50     rv->ethernet_main = &ethernet_main;
51     return (void *)rv;
52 }
53
54 int main (int argc, char * argv[])
55 {
56     int i;
57     vlib_main_t * vm = &vlib_global_main;
58     void vl_msg_api_set_first_available_msg_id (u16);
59     uword main_heap_size = (1ULL << 30);
60     u8 * sizep;
61     u32 size;
62     void vlib_set_get_handoff_structure_cb (void *cb);
63
64 #if __x86_64__
65     const char * msg = "ERROR: This binary requires CPU with %s extensions.\n";
66 #define _(a,b) \
67     if (!clib_cpu_supports_ ## a ())    \
68       {                                 \
69         fprintf(stderr, msg, b);        \
70         exit(1);                        \
71       }
72
73 #if __AVX2__
74       _(avx2, "AVX2")
75 #endif
76 #if __AVX__
77       _(avx, "AVX")
78 #endif
79 #if __SSE4_2__
80       _(sse42, "SSE4.2")
81 #endif
82 #if __SSE4_1__
83       _(sse41, "SSE4.1")
84 #endif
85 #if __SSSE3__
86       _(ssse3, "SSSE3")
87 #endif
88 #if __SSE3__
89       _(sse3, "SSE3")
90 #endif
91 #undef _
92 #endif
93
94     /*
95      * Load startup config from file.
96      * usage: vpp -c /etc/vpp/startup.conf
97      */
98     if ((argc == 3) && !strncmp(argv[1], "-c", 2))
99       {
100         FILE * fp;
101         char inbuf[4096];
102         int argc_ = 1;
103         char ** argv_ = NULL;
104         char * arg = NULL;
105         char * p;
106
107         fp = fopen (argv[2], "r");
108         if (fp == NULL)
109           {
110             fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
111             return 1;
112           }
113         argv_ = calloc(1, sizeof(char *));
114         if (argv_ == NULL)
115           return 1;
116         arg = strndup(argv[0], 1024);
117         if (arg == NULL)
118           return 1;
119         argv_[0] = arg;
120
121         while (1) {
122           if (fgets(inbuf, 4096, fp) == 0)
123             break;
124           p = strtok(inbuf, " \t\n");
125           while (p != NULL) {
126             if (*p == '#')
127               break;
128             argc_++;
129             char ** tmp = realloc(argv_, argc_ * sizeof(char *));
130             if (tmp == NULL)
131               return 1;
132             argv_ = tmp;
133             arg = strndup(p, 1024);
134             if (arg == NULL)
135               return 1;
136             argv_[argc_ - 1] = arg;
137             p = strtok(NULL, " \t\n");
138           }
139         }
140
141         fclose(fp);
142
143         char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
144         if (tmp == NULL)
145            return 1;
146         argv_ = tmp;
147         argv_[argc_] = NULL;
148
149         argc = argc_;
150         argv = argv_;
151       }
152
153     /* 
154      * Look for and parse the "heapsize" config parameter.
155      * Manual since none of the clib infra has been bootstrapped yet.
156      *
157      * Format: heapsize <nn>[mM][gG] 
158      */
159
160     for (i = 1; i < (argc-1); i++) {
161         if (!strncmp (argv[i], "plugin_path", 11)) {
162             if (i < (argc-1))
163                 vlib_plugin_path = argv[++i];
164         } else if (!strncmp (argv[i], "heapsize", 8)) {
165             sizep = (u8 *) argv[i+1];
166             size = 0;
167             while (*sizep >= '0' && *sizep <= '9') {
168                 size *= 10;
169                 size += *sizep++ - '0';
170             }
171             if (size == 0) {
172                 fprintf
173                     (stderr, 
174                      "warning: heapsize parse error '%s', use default %lld\n",
175                      argv[i], (long long int) main_heap_size);
176                 goto defaulted;
177             }
178
179             main_heap_size = size;
180             
181             if (*sizep == 'g' || *sizep == 'G')
182                 main_heap_size <<= 30;
183             else if (*sizep == 'm' || *sizep == 'M')
184                 main_heap_size <<= 20;
185         }
186     }
187             
188 defaulted:
189
190     /* Set up the plugin message ID allocator right now... */
191     vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
192
193     /* Allocate main heap */
194     if (clib_mem_init (0, main_heap_size)) {
195         vm->init_functions_called = hash_create (0, /* value bytes */ 0);
196         vpe_main_init(vm);
197 #if DPDK
198 #if !DPDK_SHARED_LIB
199         dpdk_pmd_constructor_init();
200 #endif
201 #else
202         unix_physmem_init(vm, 0 /* fail_if_physical_memory_not_present */);
203 #endif
204         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
205         return vlib_unix_main (argc, argv);
206     } else {
207       {
208         int rv __attribute__((unused)) =
209           write (2, "Main heap allocation failure!\r\n", 31);
210       }
211         return 1;
212     }
213 }
214
215 static clib_error_t *
216 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
217 {
218     u32 junk;
219
220     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
221         if (unformat (input, "%dm", &junk)
222             || unformat (input, "%dM", &junk)
223             || unformat (input, "%dg", &junk)
224             || unformat (input, "%dG", &junk))
225             return 0;
226         else
227             return clib_error_return (0, "unknown input '%U'",
228                                       format_unformat_error, input);
229     }
230     return 0;
231 }
232
233 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
234
235 static clib_error_t *
236 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
237 {
238     u8 * junk;
239
240     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
241         if (unformat (input, "%s", &junk)) {
242             vec_free(junk);
243             return 0;
244         }
245         else
246             return clib_error_return (0, "unknown input '%U'",
247                                       format_unformat_error, input);
248         }
249     return 0;
250 }
251
252 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
253
254 void vl_msg_api_post_mortem_dump(void);
255
256 void os_panic (void) 
257
258     vl_msg_api_post_mortem_dump();
259     abort (); 
260 }
261
262 void vhost_user_unmap_all (void) __attribute__((weak));
263 void vhost_user_unmap_all (void) { }
264
265 void os_exit (int code)
266
267     static int recursion_block;
268
269     if (code)
270       {
271         if (recursion_block)
272             abort();
273
274         recursion_block = 1;
275
276         vl_msg_api_post_mortem_dump();
277         vhost_user_unmap_all();
278         abort();
279       }
280     exit (code);
281 }
282
283 void vl_msg_api_barrier_sync(void) 
284
285   vlib_worker_thread_barrier_sync (vlib_get_main());
286 }
287
288 void vl_msg_api_barrier_release(void) 
289
290   vlib_worker_thread_barrier_release (vlib_get_main());
291 }
292
293 /* This application needs 1 thread stack for the stats pthread */
294 u32 vlib_app_num_thread_stacks_needed (void) 
295 {
296   return 1;
297 }
298
299 /* 
300  * Depending on the configuration selected above,
301  * it may be necessary to generate stub graph nodes.
302  * It is never OK to ignore "node 'x' refers to unknown node 'y'
303  * messages!
304  */
305
306 #if CLIB_DEBUG > 0
307
308 static clib_error_t *
309 test_crash_command_fn (vlib_main_t * vm,
310                        unformat_input_t * input,
311                        vlib_cli_command_t * cmd)
312 {
313   u64 * p = (u64 *)0xdefec8ed;
314
315   *p = 0xdeadbeef;
316
317   /* Not so much... */
318   return 0;
319 }
320
321 VLIB_CLI_COMMAND (test_crash_command, static) = {
322     .path = "test crash",
323     .short_help = "crash the bus!",
324     .function = test_crash_command_fn,
325 };
326
327 #endif
328