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