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