Reincarnate call to unix_physmem_init() for non-dpdk use
[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 #if ! DPDK
200         unix_physmem_init(vm, 0 /* fail_if_physical_memory_not_present */);
201 #endif
202         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
203         return vlib_unix_main (argc, argv);
204     } else {
205       {
206         int rv __attribute__((unused)) =
207           write (2, "Main heap allocation failure!\r\n", 31);
208       }
209         return 1;
210     }
211 }
212
213 static clib_error_t *
214 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
215 {
216     u32 junk;
217
218     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
219         if (unformat (input, "%dm", &junk)
220             || unformat (input, "%dM", &junk)
221             || unformat (input, "%dg", &junk)
222             || unformat (input, "%dG", &junk))
223             return 0;
224         else
225             return clib_error_return (0, "unknown input '%U'",
226                                       format_unformat_error, input);
227     }
228     return 0;
229 }
230
231 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
232
233 static clib_error_t *
234 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
235 {
236     u8 * junk;
237
238     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
239         if (unformat (input, "%s", &junk)) {
240             vec_free(junk);
241             return 0;
242         }
243         else
244             return clib_error_return (0, "unknown input '%U'",
245                                       format_unformat_error, input);
246         }
247     return 0;
248 }
249
250 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
251
252 void vl_msg_api_post_mortem_dump(void);
253
254 void os_panic (void) 
255
256     vl_msg_api_post_mortem_dump();
257     abort (); 
258 }
259
260 void vhost_user_unmap_all (void) __attribute__((weak));
261 void vhost_user_unmap_all (void) { }
262
263 void os_exit (int code)
264
265     static int recursion_block;
266
267     if (code)
268       {
269         if (recursion_block)
270             abort();
271
272         recursion_block = 1;
273
274         vl_msg_api_post_mortem_dump();
275         vhost_user_unmap_all();
276         abort();
277       }
278     exit (code);
279 }
280
281 void vl_msg_api_barrier_sync(void) 
282
283   vlib_worker_thread_barrier_sync (vlib_get_main());
284 }
285
286 void vl_msg_api_barrier_release(void) 
287
288   vlib_worker_thread_barrier_release (vlib_get_main());
289 }
290
291 /* This application needs 1 thread stack for the stats pthread */
292 u32 vlib_app_num_thread_stacks_needed (void) 
293 {
294   return 1;
295 }
296
297 /* 
298  * Depending on the configuration selected above,
299  * it may be necessary to generate stub graph nodes.
300  * It is never OK to ignore "node 'x' refers to unknown node 'y'
301  * messages!
302  */
303
304 #if CLIB_DEBUG > 0
305
306 static clib_error_t *
307 test_crash_command_fn (vlib_main_t * vm,
308                        unformat_input_t * input,
309                        vlib_cli_command_t * cmd)
310 {
311   u64 * p = (u64 *)0xdefec8ed;
312
313   *p = 0xdeadbeef;
314
315   /* Not so much... */
316   return 0;
317 }
318
319 VLIB_CLI_COMMAND (test_crash_command, static) = {
320     .path = "test crash",
321     .short_help = "crash the bus!",
322     .function = test_crash_command_fn,
323 };
324
325 #endif
326