vpp: detect early nosyslog and interactive flags
[vpp.git] / src / 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
16 #define _GNU_SOURCE
17 #include <pthread.h>
18 #include <sched.h>
19
20 #include <vppinfra/clib.h>
21 #include <vppinfra/cpu.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/threads.h>
25 #include <vnet/plugin/plugin.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vpp/app/version.h>
28 #include <vpp/vnet/config.h>
29 #include <vlibmemory/memclnt.api_enum.h> /* To get the last static message id */
30 #include <limits.h>
31
32 /*
33  * Load plugins from /usr/lib/vpp_plugins by default
34  */
35 char *vlib_plugin_path = NULL;
36 char *vlib_plugin_app_version = VPP_BUILD_VER;
37 char *vat_plugin_path = NULL;
38
39 static void
40 vpp_find_plugin_path ()
41 {
42   extern char *vat_plugin_path;
43   char *p, path[PATH_MAX];
44   int rv;
45   u8 *s;
46
47   /* find executable path */
48   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
49     return;
50
51   /* readlink doesn't provide null termination */
52   path[rv] = 0;
53
54   /* strip filename */
55   if ((p = strrchr (path, '/')) == 0)
56     return;
57   *p = 0;
58
59   /* strip bin/ */
60   if ((p = strrchr (path, '/')) == 0)
61     return;
62   *p = 0;
63
64   s = format (0, "%s/" CLIB_LIB_DIR "/vpp_plugins", path, path);
65   vec_add1 (s, 0);
66   vlib_plugin_path = (char *) s;
67
68   s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
69   vec_add1 (s, 0);
70   vat_plugin_path = (char *) s;
71 }
72
73 static void
74 vpe_main_init (vlib_main_t * vm)
75 {
76 #if VPP_API_TEST_BUILTIN > 0
77   void vat_plugin_hash_create (void);
78 #endif
79
80   if (CLIB_DEBUG > 0)
81     vlib_unix_cli_set_prompt ("DBGvpp# ");
82   else
83     vlib_unix_cli_set_prompt ("vpp# ");
84
85   /* Turn off network stack components which we don't want */
86   vlib_mark_init_function_complete (vm, srp_init);
87
88   /*
89    * Create the binary api plugin hashes before loading plugins
90    */
91 #if VPP_API_TEST_BUILTIN > 0
92   vat_plugin_hash_create ();
93 #endif
94
95   if (!vlib_plugin_path)
96     vpp_find_plugin_path ();
97 }
98
99 /*
100  * Default path for runtime data
101  */
102 char *vlib_default_runtime_dir = "vpp";
103
104 int
105 main (int argc, char *argv[])
106 {
107   int i;
108   void vl_msg_api_set_first_available_msg_id (u16);
109   uword main_heap_size = (1ULL << 30);
110   u8 *sizep;
111   u32 size;
112   clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
113   clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
114   unformat_input_t input, sub_input;
115   u8 *s = 0, *v = 0;
116   int main_core = ~0;
117   cpu_set_t cpuset;
118   void *main_heap;
119
120 #if __x86_64__
121   CLIB_UNUSED (const char *msg)
122     = "ERROR: This binary requires CPU with %s extensions.\n";
123 #define _(a,b)                                  \
124     if (!clib_cpu_supports_ ## a ())            \
125       {                                         \
126         fprintf(stderr, msg, b);                \
127         exit(1);                                \
128       }
129
130 #if __AVX2__
131   _(avx2, "AVX2")
132 #endif
133 #if __AVX__
134     _(avx, "AVX")
135 #endif
136 #if __SSE4_2__
137     _(sse42, "SSE4.2")
138 #endif
139 #if __SSE4_1__
140     _(sse41, "SSE4.1")
141 #endif
142 #if __SSSE3__
143     _(ssse3, "SSSE3")
144 #endif
145 #if __SSE3__
146     _(sse3, "SSE3")
147 #endif
148 #undef _
149 #endif
150     /*
151      * Load startup config from file.
152      * usage: vpp -c /etc/vpp/startup.conf
153      */
154     if ((argc == 3) && !strncmp (argv[1], "-c", 2))
155     {
156       FILE *fp;
157       char inbuf[4096];
158       int argc_ = 1;
159       char **argv_ = NULL;
160       char *arg = NULL;
161       char *p;
162
163       fp = fopen (argv[2], "r");
164       if (fp == NULL)
165         {
166           fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
167           return 1;
168         }
169       argv_ = calloc (1, sizeof (char *));
170       if (argv_ == NULL)
171         {
172           fclose (fp);
173           return 1;
174         }
175       arg = strndup (argv[0], 1024);
176       if (arg == NULL)
177         {
178           fclose (fp);
179           free (argv_);
180           return 1;
181         }
182       argv_[0] = arg;
183
184       while (1)
185         {
186           if (fgets (inbuf, 4096, fp) == 0)
187             break;
188           p = strtok (inbuf, " \t\n");
189           while (p != NULL)
190             {
191               if (*p == '#')
192                 break;
193               argc_++;
194               char **tmp = realloc (argv_, argc_ * sizeof (char *));
195               if (tmp == NULL)
196                 return 1;
197               argv_ = tmp;
198               arg = strndup (p, 1024);
199               if (arg == NULL)
200                 return 1;
201               argv_[argc_ - 1] = arg;
202               p = strtok (NULL, " \t\n");
203             }
204         }
205
206       fclose (fp);
207
208       char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
209       if (tmp == NULL)
210         return 1;
211       argv_ = tmp;
212       argv_[argc_] = NULL;
213
214       argc = argc_;
215       argv = argv_;
216     }
217
218   /*
219    * Look for and parse the "heapsize" config parameter.
220    * Manual since none of the clib infra has been bootstrapped yet.
221    *
222    * Format: heapsize <nn>[mM][gG]
223    */
224
225   for (i = 1; i < (argc - 1); i++)
226     {
227       if (!strncmp (argv[i], "plugin_path", 11))
228         {
229           if (i < (argc - 1))
230             vlib_plugin_path = argv[++i];
231         }
232       if (!strncmp (argv[i], "test_plugin_path", 16))
233         {
234           if (i < (argc - 1))
235             vat_plugin_path = argv[++i];
236         }
237       else if (!strncmp (argv[i], "heapsize", 8))
238         {
239           sizep = (u8 *) argv[i + 1];
240           size = 0;
241           while (*sizep >= '0' && *sizep <= '9')
242             {
243               size *= 10;
244               size += *sizep++ - '0';
245             }
246           if (size == 0)
247             {
248               fprintf
249                 (stderr,
250                  "warning: heapsize parse error '%s', use default %lld\n",
251                  argv[i], (long long int) main_heap_size);
252               goto defaulted;
253             }
254
255           main_heap_size = size;
256
257           if (*sizep == 'g' || *sizep == 'G')
258             main_heap_size <<= 30;
259           else if (*sizep == 'm' || *sizep == 'M')
260             main_heap_size <<= 20;
261         }
262       else if (!strncmp (argv[i], "main-core", 9))
263         {
264           if (i < (argc - 1))
265             {
266               errno = 0;
267               unsigned long x = strtol (argv[++i], 0, 0);
268               if (errno == 0)
269                 main_core = x;
270             }
271         }
272       else if (!strncmp (argv[i], "interactive", 11))
273         unix_main.flags |= UNIX_FLAG_INTERACTIVE;
274       else if (!strncmp (argv[i], "nosyslog", 8))
275         unix_main.flags |= UNIX_FLAG_NOSYSLOG;
276     }
277 defaulted:
278
279   /* temporary heap */
280   clib_mem_init (0, 1 << 20);
281   unformat_init_command_line (&input, (char **) argv);
282
283   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
284     {
285       if (unformat (&input, "memory %v", &v))
286         {
287           unformat_init_vector (&sub_input, v);
288           v = 0;
289           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
290             {
291               if (unformat (&sub_input, "main-heap-size %U",
292                             unformat_memory_size, &main_heap_size))
293                 ;
294               else if (unformat (&sub_input, "main-heap-page-size %U",
295                                  unformat_log2_page_size,
296                                  &main_heap_log2_page_sz))
297                 ;
298               else if (unformat (&sub_input, "default-hugepage-size %U",
299                                  unformat_log2_page_size,
300                                  &default_log2_hugepage_sz))
301                 ;
302               else
303                 {
304                   fformat (stderr, "unknown 'memory' config input '%U'\n",
305                            format_unformat_error, &sub_input);
306                   exit (1);
307                 }
308
309             }
310           unformat_free (&sub_input);
311         }
312       else if (!unformat (&input, "%s %v", &s, &v))
313         break;
314
315       vec_reset_length (s);
316       vec_reset_length (v);
317     }
318   vec_free (s);
319   vec_free (v);
320
321   unformat_free (&input);
322
323   /* set process affinity for main thread */
324   if (main_core != ~0)
325     {
326       CPU_ZERO (&cpuset);
327       CPU_SET (main_core, &cpuset);
328       pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
329     }
330
331   /* Set up the plugin message ID allocator right now... */
332   vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
333
334   /* destroy temporary heap and create main one */
335   clib_mem_destroy ();
336
337   if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
338                                                  main_heap_log2_page_sz)))
339     {
340       /* Figure out which numa runs the main thread */
341       __os_numa_index = clib_get_current_numa_node ();
342
343       if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
344         clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
345
346       /* and use the main heap as that numa's numa heap */
347       clib_mem_set_per_numa_heap (main_heap);
348       vlib_main_init ();
349       vpe_main_init (vlib_get_first_main ());
350       return vlib_unix_main (argc, argv);
351     }
352   else
353     {
354       {
355         int rv __attribute__ ((unused)) =
356           write (2, "Main heap allocation failure!\r\n", 31);
357       }
358       return 1;
359     }
360 }
361
362 static clib_error_t *
363 memory_config (vlib_main_t * vm, unformat_input_t * input)
364 {
365   return 0;
366 }
367
368 VLIB_CONFIG_FUNCTION (memory_config, "memory");
369
370 static clib_error_t *
371 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
372 {
373   return 0;
374 }
375
376 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
377
378 static clib_error_t *
379 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
380 {
381   u8 *junk;
382
383   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
384     {
385       if (unformat (input, "%s", &junk))
386         {
387           vec_free (junk);
388           return 0;
389         }
390       else
391         return clib_error_return (0, "unknown input '%U'",
392                                   format_unformat_error, input);
393     }
394   return 0;
395 }
396
397 static clib_error_t *
398 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
399 {
400   return placeholder_path_config (vm, input);
401 }
402
403 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
404
405 static clib_error_t *
406 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
407 {
408   return placeholder_path_config (vm, input);
409 }
410
411 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
412
413 void vl_msg_api_post_mortem_dump (void);
414 void vlib_post_mortem_dump (void);
415
416 void
417 os_panic (void)
418 {
419   vl_msg_api_post_mortem_dump ();
420   vlib_post_mortem_dump ();
421   abort ();
422 }
423
424 void vhost_user_unmap_all (void) __attribute__ ((weak));
425 void
426 vhost_user_unmap_all (void)
427 {
428 }
429
430 void
431 os_exit (int code)
432 {
433   static int recursion_block;
434
435   if (code)
436     {
437       if (recursion_block)
438         abort ();
439
440       recursion_block = 1;
441
442       vl_msg_api_post_mortem_dump ();
443       vlib_post_mortem_dump ();
444       vhost_user_unmap_all ();
445       abort ();
446     }
447   exit (code);
448 }
449
450 #ifdef BARRIER_TRACING
451 void
452 vl_msg_api_barrier_trace_context (const char *context)
453 {
454   vlib_worker_threads[0].barrier_context = context;
455 }
456 #endif
457
458 void
459 vl_msg_api_barrier_sync (void)
460 {
461   vlib_worker_thread_barrier_sync (vlib_get_main ());
462 }
463
464 void
465 vl_msg_api_barrier_release (void)
466 {
467   vlib_worker_thread_barrier_release (vlib_get_main ());
468 }
469
470 /* This application needs 1 thread stack for the stats pthread */
471 u32
472 vlib_app_num_thread_stacks_needed (void)
473 {
474   return 1;
475 }
476
477 /*
478  * Depending on the configuration selected above,
479  * it may be necessary to generate stub graph nodes.
480  * It is never OK to ignore "node 'x' refers to unknown node 'y'
481  * messages!
482  */
483
484 #include <vppinfra/bihash_8_8.h>
485
486 static clib_error_t *
487 show_bihash_command_fn (vlib_main_t * vm,
488                         unformat_input_t * input, vlib_cli_command_t * cmd)
489 {
490   int i;
491   clib_bihash_8_8_t *h;
492   int verbose = 0;
493
494   if (unformat (input, "verbose"))
495     verbose = 1;
496
497   for (i = 0; i < vec_len (clib_all_bihashes); i++)
498     {
499       h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
500       vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
501     }
502
503   return 0;
504 }
505
506 /* *INDENT-OFF* */
507 VLIB_CLI_COMMAND (show_bihash_command, static) =
508 {
509   .path = "show bihash",
510   .short_help = "show bihash",
511   .function = show_bihash_command_fn,
512 };
513 /* *INDENT-ON* */
514
515 #ifdef CLIB_SANITIZE_ADDR
516 /* default options for Address Sanitizer */
517 const char *
518 __asan_default_options (void)
519 {
520   return VPP_SANITIZE_ADDR_OPTIONS;
521 }
522 #endif /* CLIB_SANITIZE_ADDR */
523
524 /*
525  * fd.io coding-style-patch-verification: ON
526  *
527  * Local Variables:
528  * eval: (c-set-style "gnu")
529  * End:
530  */