Initial commit of vpp code.
[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 == 0
69     if ((error = vlib_call_init_function (vm, ixge_init)))
70         return error;
71     if ((error = vlib_call_init_function (vm, ixgev_init)))
72         return error;
73     if ((error = vlib_call_init_function (vm, ige_init)))
74         return error;
75     if ((error = vlib_call_init_function (vm, vice_init)))
76         return error;
77 #else
78     if ((error = vlib_call_init_function (vm, dpdk_init)))
79         return error;
80     if ((error = vlib_call_init_function (vm, vhost_user_init)))
81         return error;
82     if ((error = vlib_call_init_function (vm, ipsec_init)))
83         return error;
84 #endif    
85     if ((error = vlib_call_init_function (vm, vlibmemory_init)))
86         return error;
87     if ((error = vlib_call_init_function (vm, l2tp_init)))
88        return error;
89     if ((error = vlib_call_init_function (vm, gre_init)))
90         return error;
91     if ((error = vlib_call_init_function (vm, gre_interface_init)))
92         return error;
93     if ((error = vlib_call_init_function (vm, mpls_init)))
94         return error;
95     if ((error = vlib_call_init_function (vm, mpls_interface_init)))
96         return error;
97     if ((error = vlib_call_init_function (vm, dhcp_proxy_init)))
98         return error;
99     if ((error = vlib_call_init_function (vm, dhcpv6_proxy_init)))
100         return error;
101     if ((error = vlib_call_init_function (vm, tapcli_init)))
102         return error;
103     if ((error = vlib_call_init_function (vm, tuntap_init)))
104         return error;
105     if ((error = vlib_call_init_function (vm, gdb_func_init)))
106         return error;
107     if ((error = unix_physmem_init
108          (vm, 0 /* fail_if_physical_memory_not_present */)))
109         return error;
110     if ((error = vlib_call_init_function (vm, tuntap_init)))
111         return error;
112     if ((error = vlib_call_init_function (vm, sr_init)))
113         return error;
114     if ((error = vlib_call_init_function (vm, l2_classify_init)))
115         return error;
116     if ((error = vlib_call_init_function (vm, policer_init)))
117         return error;
118     if ((error = vlib_call_init_function (vm, vxlan_init)))
119         return error;
120     if ((error = vlib_call_init_function (vm, vcgn_init)))
121         return error;
122     if ((error = vlib_call_init_function (vm, li_init)))
123         return error;
124
125     return error;
126 }
127
128 VLIB_INIT_FUNCTION (vpe_main_init);
129
130 /* 
131  * Load plugins from /usr/lib/vpp_plugins by default
132  */
133 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
134                                                 
135 void *vnet_get_handoff_structure (void)
136 {
137     static vnet_plugin_handoff_t _rv, *rv = &_rv;
138
139     rv->vnet_main = vnet_get_main();
140     rv->ethernet_main = &ethernet_main;
141     return (void *)rv;
142 }
143
144 int main (int argc, char * argv[])
145 {
146     int i;
147     void vl_msg_api_set_first_available_msg_id (u16);
148     uword main_heap_size = (1ULL << 30);
149     u8 * sizep;
150     u32 size;
151     void vlib_set_get_handoff_structure_cb (void *cb);
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         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
196         return vlib_unix_main (argc, argv);
197     } else {
198       {
199         int rv __attribute__((unused)) =
200           write (2, "Main heap allocation failure!\r\n", 31);
201       }
202         return 1;
203     }
204 }
205
206 static clib_error_t *
207 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
208 {
209     u32 junk;
210
211     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
212         if (unformat (input, "%dm", &junk)
213             || unformat (input, "%dM", &junk)
214             || unformat (input, "%dg", &junk)
215             || unformat (input, "%dG", &junk))
216             return 0;
217         else
218             return clib_error_return (0, "unknown input '%U'",
219                                       format_unformat_error, input);
220     }
221     return 0;
222 }
223
224 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
225
226 static clib_error_t *
227 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
228 {
229     u8 * junk;
230
231     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
232         if (unformat (input, "%s", &junk)) {
233             vec_free(junk);
234             return 0;
235         }
236         else
237             return clib_error_return (0, "unknown input '%U'",
238                                       format_unformat_error, input);
239         }
240     return 0;
241 }
242
243 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
244
245 void vl_msg_api_post_mortem_dump(void);
246
247 void os_panic (void) 
248
249     vl_msg_api_post_mortem_dump();
250     abort (); 
251 }
252
253 void vhost_user_unmap_all (void) __attribute__((weak));
254 void vhost_user_unmap_all (void) { }
255
256 void os_exit (int code)
257
258     static int recursion_block;
259
260     if (code)
261       {
262         if (recursion_block)
263             abort();
264
265         recursion_block = 1;
266
267         vl_msg_api_post_mortem_dump();
268         vhost_user_unmap_all();
269         abort();
270       }
271     exit (code);
272 }
273
274 void vl_msg_api_barrier_sync(void) 
275
276   vlib_worker_thread_barrier_sync (vlib_get_main());
277 }
278
279 void vl_msg_api_barrier_release(void) 
280
281   vlib_worker_thread_barrier_release (vlib_get_main());
282 }
283
284 /* This application needs 1 thread stack for the stats pthread */
285 u32 vlib_app_num_thread_stacks_needed (void) 
286 {
287   return 1;
288 }
289
290 #if CLIB_DEBUG > 0
291
292 static clib_error_t *
293 test_crash_command_fn (vlib_main_t * vm,
294                        unformat_input_t * input,
295                        vlib_cli_command_t * cmd)
296 {
297   u64 * p = (u64 *)0xdefec8ed;
298
299   *p = 0xdeadbeef;
300
301   /* Not so much... */
302   return 0;
303 }
304
305 VLIB_CLI_COMMAND (test_crash_command, static) = {
306     .path = "test crash",
307     .short_help = "crash the bus!",
308     .function = test_crash_command_fn,
309 };
310
311 #endif
312