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