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