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