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