9a4bed6a6eaf5bea4a4beb49af77501381f170fe
[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     }
273 defaulted:
274
275   /* temporary heap */
276   clib_mem_init (0, 1 << 20);
277   unformat_init_command_line (&input, (char **) argv);
278
279   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
280     {
281       if (unformat (&input, "memory %v", &v))
282         {
283           unformat_init_vector (&sub_input, v);
284           v = 0;
285           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
286             {
287               if (unformat (&sub_input, "main-heap-size %U",
288                             unformat_memory_size, &main_heap_size))
289                 ;
290               else if (unformat (&sub_input, "main-heap-page-size %U",
291                                  unformat_log2_page_size,
292                                  &main_heap_log2_page_sz))
293                 ;
294               else if (unformat (&sub_input, "default-hugepage-size %U",
295                                  unformat_log2_page_size,
296                                  &default_log2_hugepage_sz))
297                 ;
298               else
299                 {
300                   fformat (stderr, "unknown 'memory' config input '%U'\n",
301                            format_unformat_error, &sub_input);
302                   exit (1);
303                 }
304
305             }
306           unformat_free (&sub_input);
307         }
308       else if (!unformat (&input, "%s %v", &s, &v))
309         break;
310
311       vec_reset_length (s);
312       vec_reset_length (v);
313     }
314   vec_free (s);
315   vec_free (v);
316
317   unformat_free (&input);
318
319   /* set process affinity for main thread */
320   if (main_core != ~0)
321     {
322       CPU_ZERO (&cpuset);
323       CPU_SET (main_core, &cpuset);
324       pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
325     }
326
327   /* Set up the plugin message ID allocator right now... */
328   vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
329
330   /* destroy temporary heap and create main one */
331   clib_mem_destroy ();
332
333   if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
334                                                  main_heap_log2_page_sz)))
335     {
336       /* Figure out which numa runs the main thread */
337       __os_numa_index = clib_get_current_numa_node ();
338
339       if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
340         clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
341
342       /* and use the main heap as that numa's numa heap */
343       clib_mem_set_per_numa_heap (main_heap);
344       vlib_main_init ();
345       vpe_main_init (vlib_get_first_main ());
346       return vlib_unix_main (argc, argv);
347     }
348   else
349     {
350       {
351         int rv __attribute__ ((unused)) =
352           write (2, "Main heap allocation failure!\r\n", 31);
353       }
354       return 1;
355     }
356 }
357
358 static clib_error_t *
359 memory_config (vlib_main_t * vm, unformat_input_t * input)
360 {
361   return 0;
362 }
363
364 VLIB_CONFIG_FUNCTION (memory_config, "memory");
365
366 static clib_error_t *
367 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
368 {
369   return 0;
370 }
371
372 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
373
374 static clib_error_t *
375 placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
376 {
377   u8 *junk;
378
379   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
380     {
381       if (unformat (input, "%s", &junk))
382         {
383           vec_free (junk);
384           return 0;
385         }
386       else
387         return clib_error_return (0, "unknown input '%U'",
388                                   format_unformat_error, input);
389     }
390   return 0;
391 }
392
393 static clib_error_t *
394 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
395 {
396   return placeholder_path_config (vm, input);
397 }
398
399 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
400
401 static clib_error_t *
402 test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
403 {
404   return placeholder_path_config (vm, input);
405 }
406
407 VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
408
409 void vl_msg_api_post_mortem_dump (void);
410 void vlib_post_mortem_dump (void);
411
412 void
413 os_panic (void)
414 {
415   vl_msg_api_post_mortem_dump ();
416   vlib_post_mortem_dump ();
417   abort ();
418 }
419
420 void vhost_user_unmap_all (void) __attribute__ ((weak));
421 void
422 vhost_user_unmap_all (void)
423 {
424 }
425
426 void
427 os_exit (int code)
428 {
429   static int recursion_block;
430
431   if (code)
432     {
433       if (recursion_block)
434         abort ();
435
436       recursion_block = 1;
437
438       vl_msg_api_post_mortem_dump ();
439       vlib_post_mortem_dump ();
440       vhost_user_unmap_all ();
441       abort ();
442     }
443   exit (code);
444 }
445
446 #ifdef BARRIER_TRACING
447 void
448 vl_msg_api_barrier_trace_context (const char *context)
449 {
450   vlib_worker_threads[0].barrier_context = context;
451 }
452 #endif
453
454 void
455 vl_msg_api_barrier_sync (void)
456 {
457   vlib_worker_thread_barrier_sync (vlib_get_main ());
458 }
459
460 void
461 vl_msg_api_barrier_release (void)
462 {
463   vlib_worker_thread_barrier_release (vlib_get_main ());
464 }
465
466 /* This application needs 1 thread stack for the stats pthread */
467 u32
468 vlib_app_num_thread_stacks_needed (void)
469 {
470   return 1;
471 }
472
473 /*
474  * Depending on the configuration selected above,
475  * it may be necessary to generate stub graph nodes.
476  * It is never OK to ignore "node 'x' refers to unknown node 'y'
477  * messages!
478  */
479
480 #include <vppinfra/bihash_8_8.h>
481
482 static clib_error_t *
483 show_bihash_command_fn (vlib_main_t * vm,
484                         unformat_input_t * input, vlib_cli_command_t * cmd)
485 {
486   int i;
487   clib_bihash_8_8_t *h;
488   int verbose = 0;
489
490   if (unformat (input, "verbose"))
491     verbose = 1;
492
493   for (i = 0; i < vec_len (clib_all_bihashes); i++)
494     {
495       h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
496       vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
497     }
498
499   return 0;
500 }
501
502 /* *INDENT-OFF* */
503 VLIB_CLI_COMMAND (show_bihash_command, static) =
504 {
505   .path = "show bihash",
506   .short_help = "show bihash",
507   .function = show_bihash_command_fn,
508 };
509 /* *INDENT-ON* */
510
511 #ifdef CLIB_SANITIZE_ADDR
512 /* default options for Address Sanitizer */
513 const char *
514 __asan_default_options (void)
515 {
516   return VPP_SANITIZE_ADDR_OPTIONS;
517 }
518 #endif /* CLIB_SANITIZE_ADDR */
519
520 /*
521  * fd.io coding-style-patch-verification: ON
522  *
523  * Local Variables:
524  * eval: (c-set-style "gnu")
525  * End:
526  */