vnet classifier debug CLI enhancements
[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 #undef _
95 #endif
96
97     /*
98      * Load startup config from file.
99      * usage: vpp -c /etc/vpp/startup.conf
100      */
101     if ((argc == 3) && !strncmp(argv[1], "-c", 2))
102       {
103         FILE * fp;
104         char inbuf[4096];
105         int argc_ = 1;
106         char ** argv_ = NULL;
107         char * arg = NULL;
108         char * p;
109
110         fp = fopen (argv[2], "r");
111         if (fp == NULL)
112           {
113             fprintf(stderr, "open configuration file '%s' failed\n", argv[2]);
114             return 1;
115           }
116         argv_ = calloc(1, sizeof(char *));
117         if (argv_ == NULL)
118           return 1;
119         arg = strndup(argv[0], 1024);
120         if (arg == NULL)
121           return 1;
122         argv_[0] = arg;
123
124         while (1) {
125           if (fgets(inbuf, 4096, fp) == 0)
126             break;
127           p = strtok(inbuf, " \t\n");
128           while (p != NULL) {
129             if (*p == '#')
130               break;
131             argc_++;
132             char ** tmp = realloc(argv_, argc_ * sizeof(char *));
133             if (tmp == NULL)
134               return 1;
135             argv_ = tmp;
136             arg = strndup(p, 1024);
137             if (arg == NULL)
138               return 1;
139             argv_[argc_ - 1] = arg;
140             p = strtok(NULL, " \t\n");
141           }
142         }
143
144         fclose(fp);
145
146         char ** tmp = realloc(argv_, (argc_ + 1) * sizeof(char *));
147         if (tmp == NULL)
148            return 1;
149         argv_ = tmp;
150         argv_[argc_] = NULL;
151
152         argc = argc_;
153         argv = argv_;
154       }
155
156     /* 
157      * Look for and parse the "heapsize" config parameter.
158      * Manual since none of the clib infra has been bootstrapped yet.
159      *
160      * Format: heapsize <nn>[mM][gG] 
161      */
162
163     for (i = 1; i < (argc-1); i++) {
164         if (!strncmp (argv[i], "plugin_path", 11)) {
165             if (i < (argc-1))
166                 vlib_plugin_path = argv[++i];
167         } else if (!strncmp (argv[i], "heapsize", 8)) {
168             sizep = (u8 *) argv[i+1];
169             size = 0;
170             while (*sizep >= '0' && *sizep <= '9') {
171                 size *= 10;
172                 size += *sizep++ - '0';
173             }
174             if (size == 0) {
175                 fprintf
176                     (stderr, 
177                      "warning: heapsize parse error '%s', use default %lld\n",
178                      argv[i], (long long int) main_heap_size);
179                 goto defaulted;
180             }
181
182             main_heap_size = size;
183             
184             if (*sizep == 'g' || *sizep == 'G')
185                 main_heap_size <<= 30;
186             else if (*sizep == 'm' || *sizep == 'M')
187                 main_heap_size <<= 20;
188         }
189     }
190             
191 defaulted:
192
193     /* Set up the plugin message ID allocator right now... */
194     vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
195
196     /* Allocate main heap */
197     if (clib_mem_init (0, main_heap_size)) {
198         vm->init_functions_called = hash_create (0, /* value bytes */ 0);
199         vpe_main_init(vm);
200 #if ! DPDK
201         unix_physmem_init(vm, 0 /* fail_if_physical_memory_not_present */);
202 #endif
203         vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
204         return vlib_unix_main (argc, argv);
205     } else {
206       {
207         int rv __attribute__((unused)) =
208           write (2, "Main heap allocation failure!\r\n", 31);
209       }
210         return 1;
211     }
212 }
213
214 static clib_error_t *
215 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
216 {
217     u32 junk;
218
219     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
220         if (unformat (input, "%dm", &junk)
221             || unformat (input, "%dM", &junk)
222             || unformat (input, "%dg", &junk)
223             || unformat (input, "%dG", &junk))
224             return 0;
225         else
226             return clib_error_return (0, "unknown input '%U'",
227                                       format_unformat_error, input);
228     }
229     return 0;
230 }
231
232 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
233
234 static clib_error_t *
235 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
236 {
237     u8 * junk;
238
239     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
240         if (unformat (input, "%s", &junk)) {
241             vec_free(junk);
242             return 0;
243         }
244         else
245             return clib_error_return (0, "unknown input '%U'",
246                                       format_unformat_error, input);
247         }
248     return 0;
249 }
250
251 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
252
253 void vl_msg_api_post_mortem_dump(void);
254
255 void os_panic (void) 
256
257     vl_msg_api_post_mortem_dump();
258     abort (); 
259 }
260
261 void vhost_user_unmap_all (void) __attribute__((weak));
262 void vhost_user_unmap_all (void) { }
263
264 void os_exit (int code)
265
266     static int recursion_block;
267
268     if (code)
269       {
270         if (recursion_block)
271             abort();
272
273         recursion_block = 1;
274
275         vl_msg_api_post_mortem_dump();
276         vhost_user_unmap_all();
277         abort();
278       }
279     exit (code);
280 }
281
282 void vl_msg_api_barrier_sync(void) 
283
284   vlib_worker_thread_barrier_sync (vlib_get_main());
285 }
286
287 void vl_msg_api_barrier_release(void) 
288
289   vlib_worker_thread_barrier_release (vlib_get_main());
290 }
291
292 /* This application needs 1 thread stack for the stats pthread */
293 u32 vlib_app_num_thread_stacks_needed (void) 
294 {
295   return 1;
296 }
297
298 /* 
299  * Depending on the configuration selected above,
300  * it may be necessary to generate stub graph nodes.
301  * It is never OK to ignore "node 'x' refers to unknown node 'y'
302  * messages!
303  */
304
305 #if CLIB_DEBUG > 0
306
307 static clib_error_t *
308 test_crash_command_fn (vlib_main_t * vm,
309                        unformat_input_t * input,
310                        vlib_cli_command_t * cmd)
311 {
312   u64 * p = (u64 *)0xdefec8ed;
313
314   *p = 0xdeadbeef;
315
316   /* Not so much... */
317   return 0;
318 }
319
320 VLIB_CLI_COMMAND (test_crash_command, static) = {
321     .path = "test crash",
322     .short_help = "crash the bus!",
323     .function = test_crash_command_fn,
324 };
325
326 #endif
327