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