d60b489e936ddbe82e2baf58a56c86ea96b2a00f
[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 clib_error_t *
31 vpe_main_init (vlib_main_t * vm)
32 {
33     clib_error_t * error = 0;
34     void vnet_library_plugin_reference(void);
35
36     if (CLIB_DEBUG > 0)
37         vlib_unix_cli_set_prompt ("DBGvpp# ");
38     else
39         vlib_unix_cli_set_prompt ("vpp# ");
40
41     vnet_library_plugin_reference();
42
43     if ((error = vlib_call_init_function (vm, pg_init)))
44         return error;
45     if ((error = vlib_call_init_function (vm, ip_main_init)))
46         return error;
47     if ((error = vlib_call_init_function (vm, osi_init)))
48         return error;
49     if ((error = vlib_call_init_function (vm, l2_init)))
50         return error;
51     if ((error = vlib_call_init_function (vm, ethernet_init)))
52         return error;
53     if ((error = vlib_call_init_function (vm, ethernet_arp_init)))
54         return error;
55     if ((error = vlib_call_init_function (vm, sr_init)))
56         return error;
57     if ((error = vlib_call_init_function (vm, map_init)))
58         return error;
59     if ((error = vlib_call_init_function (vm, sixrd_init)))
60         return error;
61     if ((error = vlib_call_init_function (vm, nsh_gre_init)))
62         return error;
63     if ((error = vlib_call_init_function (vm, nsh_vxlan_gpe_init)))
64         return error;
65     if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
66         return error;
67
68 #if DPDK == 1
69     if ((error = vlib_call_init_function (vm, dpdk_init)))
70         return error;
71     if ((error = vlib_call_init_function (vm, dpdk_thread_init)))
72         return error;
73     if ((error = vlib_call_init_function (vm, vhost_user_init)))
74         return error;
75     if ((error = vlib_call_init_function (vm, ipsec_init)))
76         return error;
77 #endif    
78     if ((error = vlib_call_init_function (vm, vlibmemory_init)))
79         return error;
80     if ((error = vlib_call_init_function (vm, l2tp_init)))
81        return error;
82     if ((error = vlib_call_init_function (vm, gre_init)))
83         return error;
84     if ((error = vlib_call_init_function (vm, gre_interface_init)))
85         return error;
86     if ((error = vlib_call_init_function (vm, mpls_init)))
87         return error;
88     if ((error = vlib_call_init_function (vm, mpls_interface_init)))
89         return error;
90     if ((error = vlib_call_init_function (vm, dhcp_proxy_init)))
91         return error;
92     if ((error = vlib_call_init_function (vm, dhcpv6_proxy_init)))
93         return error;
94     if ((error = vlib_call_init_function (vm, tapcli_init)))
95         return error;
96     if ((error = vlib_call_init_function (vm, tuntap_init)))
97         return error;
98     if ((error = vlib_call_init_function (vm, gdb_func_init)))
99         return error;
100     if ((error = unix_physmem_init
101          (vm, 0 /* fail_if_physical_memory_not_present */)))
102         return error;
103     if ((error = vlib_call_init_function (vm, tuntap_init)))
104         return error;
105     if ((error = vlib_call_init_function (vm, sr_init)))
106         return error;
107     if ((error = vlib_call_init_function (vm, l2_classify_init)))
108         return error;
109     if ((error = vlib_call_init_function (vm, policer_init)))
110         return error;
111     if ((error = vlib_call_init_function (vm, vxlan_init)))
112         return error;
113     if ((error = vlib_call_init_function (vm, vcgn_init)))
114         return error;
115     if ((error = vlib_call_init_function (vm, li_init)))
116         return error;
117
118     return error;
119 }
120
121 VLIB_INIT_FUNCTION (vpe_main_init);
122
123 /* 
124  * Load plugins from /usr/lib/vpp_plugins by default
125  */
126 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
127                                                 
128 void *vnet_get_handoff_structure (void)
129 {
130     static vnet_plugin_handoff_t _rv, *rv = &_rv;
131
132     rv->vnet_main = vnet_get_main();
133     rv->ethernet_main = &ethernet_main;
134     return (void *)rv;
135 }
136
137 int main (int argc, char * argv[])
138 {
139     int i;
140     void vl_msg_api_set_first_available_msg_id (u16);
141     uword main_heap_size = (1ULL << 30);
142     u8 * sizep;
143     u32 size;
144     void vlib_set_get_handoff_structure_cb (void *cb);
145
146     /*
147      * Load startup config from file.
148      * usage: vpp -c /etc/vpp/startup.conf
149      */
150     if ((argc == 3) && !strncmp(argv[1], "-c", 2))
151       {
152         FILE * fp;
153         char inbuf[4096];
154         int argc_ = 1;
155         char ** argv_ = NULL;
156         char * arg = NULL;
157         char * p;
158
159         fp = fopen (argv[2], "r");
160         if (fp == NULL)
161           {
162             fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
163             return 1;
164           }
165         argv_ = calloc(1, sizeof(char *));
166         if (argv_ == NULL)
167           return 1;
168         arg = strndup(argv[0], 1024);
169         if (arg == NULL)
170           return 1;
171         argv_[0] = arg;
172
173         while (1) {
174           if (fgets(inbuf, 4096, fp) == 0)
175             break;
176           p = strtok(inbuf, " \t\n");
177           while (p != NULL) {
178             if (*p == '#')
179               break;
180             argc_++;
181             char ** tmp = realloc(argv_, argc_ * sizeof(char *));
182             if (tmp == NULL)
183               return 1;
184             argv_ = tmp;
185             arg = strndup(p, 1024);
186             if (arg == NULL)
187               return 1;
188             argv_[argc_ - 1] = arg;
189             p = strtok(NULL, " \t\n");
190           }
191         }
192
193         fclose(fp);
194
195         char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
196         if (tmp == NULL)
197            return 1;
198         argv_ = tmp;
199         argv_[argc_] = NULL;
200
201         argc = argc_;
202         argv = argv_;
203       }
204
205     /* 
206      * Look for and parse the "heapsize" config parameter.
207      * Manual since none of the clib infra has been bootstrapped yet.
208      *
209      * Format: heapsize <nn>[mM][gG] 
210      */
211
212     for (i = 1; i < (argc-1); i++) {
213         if (!strncmp (argv[i], "plugin_path", 11)) {
214             if (i < (argc-1))
215                 vlib_plugin_path = argv[++i];
216         } else if (!strncmp (argv[i], "heapsize", 8)) {
217             sizep = (u8 *) argv[i+1];
218             size = 0;
219             while (*sizep >= '0' && *sizep <= '9') {
220                 size *= 10;
221                 size += *sizep++ - '0';
222             }
223             if (size == 0) {
224                 fprintf
225                     (stderr, 
226                      "warning: heapsize parse error '%s', use default %lld\n",
227                      argv[i], (long long int) main_heap_size);
228                 goto defaulted;
229             }
230
231             main_heap_size = size;
232             
233             if (*sizep == 'g' || *sizep == 'G')
234                 main_heap_size <<= 30;
235             else if (*sizep == 'm' || *sizep == 'M')
236                 main_heap_size <<= 20;
237         }
238     }
239             
240 defaulted:
241
242     /* Set up the plugin message ID allocator right now... */
243     vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
244
245     /* Allocate main heap */
246     if (clib_mem_init (0, main_heap_size)) {
247         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
248         return vlib_unix_main (argc, argv);
249     } else {
250       {
251         int rv __attribute__((unused)) =
252           write (2, "Main heap allocation failure!\r\n", 31);
253       }
254         return 1;
255     }
256 }
257
258 static clib_error_t *
259 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
260 {
261     u32 junk;
262
263     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
264         if (unformat (input, "%dm", &junk)
265             || unformat (input, "%dM", &junk)
266             || unformat (input, "%dg", &junk)
267             || unformat (input, "%dG", &junk))
268             return 0;
269         else
270             return clib_error_return (0, "unknown input '%U'",
271                                       format_unformat_error, input);
272     }
273     return 0;
274 }
275
276 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
277
278 static clib_error_t *
279 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
280 {
281     u8 * junk;
282
283     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
284         if (unformat (input, "%s", &junk)) {
285             vec_free(junk);
286             return 0;
287         }
288         else
289             return clib_error_return (0, "unknown input '%U'",
290                                       format_unformat_error, input);
291         }
292     return 0;
293 }
294
295 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
296
297 void vl_msg_api_post_mortem_dump(void);
298
299 void os_panic (void) 
300
301     vl_msg_api_post_mortem_dump();
302     abort (); 
303 }
304
305 void vhost_user_unmap_all (void) __attribute__((weak));
306 void vhost_user_unmap_all (void) { }
307
308 void os_exit (int code)
309
310     static int recursion_block;
311
312     if (code)
313       {
314         if (recursion_block)
315             abort();
316
317         recursion_block = 1;
318
319         vl_msg_api_post_mortem_dump();
320         vhost_user_unmap_all();
321         abort();
322       }
323     exit (code);
324 }
325
326 void vl_msg_api_barrier_sync(void) 
327
328   vlib_worker_thread_barrier_sync (vlib_get_main());
329 }
330
331 void vl_msg_api_barrier_release(void) 
332
333   vlib_worker_thread_barrier_release (vlib_get_main());
334 }
335
336 /* This application needs 1 thread stack for the stats pthread */
337 u32 vlib_app_num_thread_stacks_needed (void) 
338 {
339   return 1;
340 }
341
342 #if CLIB_DEBUG > 0
343
344 static clib_error_t *
345 test_crash_command_fn (vlib_main_t * vm,
346                        unformat_input_t * input,
347                        vlib_cli_command_t * cmd)
348 {
349   u64 * p = (u64 *)0xdefec8ed;
350
351   *p = 0xdeadbeef;
352
353   /* Not so much... */
354   return 0;
355 }
356
357 VLIB_CLI_COMMAND (test_crash_command, static) = {
358     .path = "test crash",
359     .short_help = "crash the bus!",
360     .function = test_crash_command_fn,
361 };
362
363 #endif
364