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