Line data Source code
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 575 : 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 575 : if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
49 0 : return;
50 :
51 : /* readlink doesn't provide null termination */
52 575 : path[rv] = 0;
53 :
54 : /* strip filename */
55 575 : if ((p = strrchr (path, '/')) == 0)
56 0 : return;
57 575 : *p = 0;
58 :
59 : /* strip bin/ */
60 575 : if ((p = strrchr (path, '/')) == 0)
61 0 : return;
62 575 : *p = 0;
63 :
64 575 : s = format (0, "%s/" CLIB_LIB_DIR "/vpp_plugins", path, path);
65 575 : vec_add1 (s, 0);
66 575 : vlib_plugin_path = (char *) s;
67 :
68 575 : s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
69 575 : vec_add1 (s, 0);
70 575 : vat_plugin_path = (char *) s;
71 : }
72 :
73 : static void
74 575 : 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 575 : 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 575 : 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 575 : vat_plugin_hash_create ();
93 : #endif
94 :
95 575 : if (!vlib_plugin_path)
96 575 : vpp_find_plugin_path ();
97 575 : }
98 :
99 : /*
100 : * Default path for runtime data
101 : */
102 : char *vlib_default_runtime_dir = "vpp";
103 :
104 : int
105 575 : main (int argc, char *argv[])
106 : {
107 : int i;
108 : void vl_msg_api_set_first_available_msg_id (u16);
109 575 : uword main_heap_size = (1ULL << 30);
110 : u8 *sizep;
111 : u32 size;
112 575 : clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
113 575 : clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
114 : unformat_input_t input, sub_input;
115 575 : u8 *s = 0, *v = 0;
116 575 : int main_core = ~0;
117 : cpu_set_t cpuset;
118 : void *main_heap;
119 :
120 : #if __x86_64__
121 575 : 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 575 : _(sse42, "SSE4.2")
138 : #endif
139 : #if __SSE4_1__
140 575 : _(sse41, "SSE4.1")
141 : #endif
142 : #if __SSSE3__
143 575 : _(ssse3, "SSSE3")
144 : #endif
145 : #if __SSE3__
146 575 : _(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 575 : if ((argc == 3) && !strncmp (argv[1], "-c", 2))
155 : {
156 : FILE *fp;
157 : char inbuf[4096];
158 0 : int argc_ = 1;
159 0 : char **argv_ = NULL;
160 0 : char *arg = NULL;
161 : char *p;
162 :
163 0 : fp = fopen (argv[2], "r");
164 0 : if (fp == NULL)
165 : {
166 0 : fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
167 0 : return 1;
168 : }
169 0 : argv_ = calloc (1, sizeof (char *));
170 0 : if (argv_ == NULL)
171 : {
172 0 : fclose (fp);
173 0 : return 1;
174 : }
175 0 : arg = strndup (argv[0], 1024);
176 0 : if (arg == NULL)
177 : {
178 0 : fclose (fp);
179 0 : free (argv_);
180 0 : return 1;
181 : }
182 0 : argv_[0] = arg;
183 :
184 : while (1)
185 : {
186 0 : if (fgets (inbuf, 4096, fp) == 0)
187 0 : break;
188 0 : p = strtok (inbuf, " \t\n");
189 0 : while (p != NULL)
190 : {
191 0 : if (*p == '#')
192 0 : break;
193 0 : argc_++;
194 0 : char **tmp = realloc (argv_, argc_ * sizeof (char *));
195 0 : if (tmp == NULL)
196 0 : return 1;
197 0 : argv_ = tmp;
198 0 : arg = strndup (p, 1024);
199 0 : if (arg == NULL)
200 0 : return 1;
201 0 : argv_[argc_ - 1] = arg;
202 0 : p = strtok (NULL, " \t\n");
203 : }
204 : }
205 :
206 0 : fclose (fp);
207 :
208 0 : char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
209 0 : if (tmp == NULL)
210 0 : return 1;
211 0 : argv_ = tmp;
212 0 : argv_[argc_] = NULL;
213 :
214 0 : argc = argc_;
215 0 : 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 39440 : for (i = 1; i < (argc - 1); i++)
226 : {
227 38865 : if (!strncmp (argv[i], "plugin_path", 11))
228 : {
229 0 : if (i < (argc - 1))
230 0 : vlib_plugin_path = argv[++i];
231 : }
232 38865 : if (!strncmp (argv[i], "test_plugin_path", 16))
233 : {
234 0 : if (i < (argc - 1))
235 0 : vat_plugin_path = argv[++i];
236 : }
237 38865 : else if (!strncmp (argv[i], "heapsize", 8))
238 : {
239 0 : sizep = (u8 *) argv[i + 1];
240 0 : size = 0;
241 0 : while (*sizep >= '0' && *sizep <= '9')
242 : {
243 0 : size *= 10;
244 0 : size += *sizep++ - '0';
245 : }
246 0 : if (size == 0)
247 : {
248 0 : fprintf
249 : (stderr,
250 : "warning: heapsize parse error '%s', use default %lld\n",
251 0 : argv[i], (long long int) main_heap_size);
252 0 : goto defaulted;
253 : }
254 :
255 0 : main_heap_size = size;
256 :
257 0 : if (*sizep == 'g' || *sizep == 'G')
258 0 : main_heap_size <<= 30;
259 0 : else if (*sizep == 'm' || *sizep == 'M')
260 0 : main_heap_size <<= 20;
261 : }
262 38865 : else if (!strncmp (argv[i], "main-core", 9))
263 : {
264 575 : if (i < (argc - 1))
265 : {
266 575 : errno = 0;
267 575 : unsigned long x = strtol (argv[++i], 0, 0);
268 575 : if (errno == 0)
269 575 : main_core = x;
270 : }
271 : }
272 38290 : else if (!strncmp (argv[i], "interactive", 11))
273 0 : unix_main.flags |= UNIX_FLAG_INTERACTIVE;
274 38290 : else if (!strncmp (argv[i], "nosyslog", 8))
275 0 : unix_main.flags |= UNIX_FLAG_NOSYSLOG;
276 : }
277 575 : defaulted:
278 :
279 : /* temporary heap */
280 575 : clib_mem_init (0, 1 << 20);
281 575 : unformat_init_command_line (&input, (char **) argv);
282 :
283 6412 : while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
284 : {
285 5837 : if (unformat (&input, "memory %v", &v))
286 : {
287 0 : unformat_init_vector (&sub_input, v);
288 0 : v = 0;
289 0 : while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
290 : {
291 0 : if (unformat (&sub_input, "main-heap-size %U",
292 : unformat_memory_size, &main_heap_size))
293 : ;
294 0 : else if (unformat (&sub_input, "main-heap-page-size %U",
295 : unformat_log2_page_size,
296 : &main_heap_log2_page_sz))
297 : ;
298 0 : else if (unformat (&sub_input, "default-hugepage-size %U",
299 : unformat_log2_page_size,
300 : &default_log2_hugepage_sz))
301 : ;
302 : else
303 : {
304 0 : fformat (stderr, "unknown 'memory' config input '%U'\n",
305 : format_unformat_error, &sub_input);
306 0 : exit (1);
307 : }
308 :
309 : }
310 0 : unformat_free (&sub_input);
311 : }
312 5837 : else if (!unformat (&input, "%s %v", &s, &v))
313 0 : break;
314 :
315 5837 : vec_reset_length (s);
316 5837 : vec_reset_length (v);
317 : }
318 575 : vec_free (s);
319 575 : vec_free (v);
320 :
321 575 : unformat_free (&input);
322 :
323 : /* set process affinity for main thread */
324 575 : if (main_core != ~0)
325 : {
326 575 : CPU_ZERO (&cpuset);
327 575 : CPU_SET (main_core, &cpuset);
328 575 : pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
329 : }
330 :
331 : /* Set up the plugin message ID allocator right now... */
332 575 : vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);
333 :
334 : /* destroy temporary heap and create main one */
335 575 : clib_mem_destroy ();
336 :
337 575 : if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
338 : main_heap_log2_page_sz)))
339 : {
340 : /* Figure out which numa runs the main thread */
341 575 : __os_numa_index = clib_get_current_numa_node ();
342 :
343 575 : if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
344 0 : clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);
345 :
346 : /* and use the main heap as that numa's numa heap */
347 575 : clib_mem_set_per_numa_heap (main_heap);
348 575 : vlib_main_init ();
349 575 : vpe_main_init (vlib_get_first_main ());
350 575 : return vlib_unix_main (argc, argv);
351 : }
352 : else
353 : {
354 : {
355 0 : int rv __attribute__ ((unused)) =
356 0 : write (2, "Main heap allocation failure!\r\n", 31);
357 : }
358 0 : return 1;
359 : }
360 : }
361 :
362 : static clib_error_t *
363 575 : memory_config (vlib_main_t * vm, unformat_input_t * input)
364 : {
365 575 : return 0;
366 : }
367 :
368 7514 : VLIB_CONFIG_FUNCTION (memory_config, "memory");
369 :
370 : static clib_error_t *
371 575 : heapsize_config (vlib_main_t * vm, unformat_input_t * input)
372 : {
373 575 : return 0;
374 : }
375 :
376 7514 : VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
377 :
378 : static clib_error_t *
379 1150 : placeholder_path_config (vlib_main_t * vm, unformat_input_t * input)
380 : {
381 : u8 *junk;
382 :
383 1150 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
384 : {
385 0 : if (unformat (input, "%s", &junk))
386 : {
387 0 : vec_free (junk);
388 0 : return 0;
389 : }
390 : else
391 0 : return clib_error_return (0, "unknown input '%U'",
392 : format_unformat_error, input);
393 : }
394 1150 : return 0;
395 : }
396 :
397 : static clib_error_t *
398 575 : plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
399 : {
400 575 : return placeholder_path_config (vm, input);
401 : }
402 :
403 7514 : VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
404 :
405 : static clib_error_t *
406 575 : test_plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
407 : {
408 575 : return placeholder_path_config (vm, input);
409 : }
410 :
411 7514 : VLIB_CONFIG_FUNCTION (test_plugin_path_config, "test_plugin_path");
412 :
413 : void vl_msg_api_post_mortem_dump (void);
414 : void vlib_post_mortem_dump (void);
415 :
416 : void
417 0 : os_panic (void)
418 : {
419 0 : vl_msg_api_post_mortem_dump ();
420 0 : vlib_post_mortem_dump ();
421 0 : abort ();
422 : }
423 :
424 : void vhost_user_unmap_all (void) __attribute__ ((weak));
425 : void
426 0 : vhost_user_unmap_all (void)
427 : {
428 0 : }
429 :
430 : void
431 0 : os_exit (int code)
432 : {
433 : static int recursion_block;
434 :
435 0 : if (code)
436 : {
437 0 : if (recursion_block)
438 0 : abort ();
439 :
440 0 : recursion_block = 1;
441 :
442 0 : vl_msg_api_post_mortem_dump ();
443 0 : vlib_post_mortem_dump ();
444 0 : vhost_user_unmap_all ();
445 0 : abort ();
446 : }
447 0 : exit (code);
448 : }
449 :
450 : #ifdef BARRIER_TRACING
451 : void
452 : vl_msg_api_barrier_trace_context (const char *context)
453 : {
454 : vlib_worker_threads[0].barrier_context = context;
455 : }
456 : #endif
457 :
458 : void
459 399924 : vl_msg_api_barrier_sync (void)
460 : {
461 399924 : vlib_worker_thread_barrier_sync (vlib_get_main ());
462 399924 : }
463 :
464 : void
465 399924 : vl_msg_api_barrier_release (void)
466 : {
467 399924 : vlib_worker_thread_barrier_release (vlib_get_main ());
468 399924 : }
469 :
470 : /* This application needs 1 thread stack for the stats pthread */
471 : u32
472 0 : vlib_app_num_thread_stacks_needed (void)
473 : {
474 0 : return 1;
475 : }
476 :
477 : /*
478 : * Depending on the configuration selected above,
479 : * it may be necessary to generate stub graph nodes.
480 : * It is never OK to ignore "node 'x' refers to unknown node 'y'
481 : * messages!
482 : */
483 :
484 : #include <vppinfra/bihash_8_8.h>
485 :
486 : static clib_error_t *
487 1577 : show_bihash_command_fn (vlib_main_t * vm,
488 : unformat_input_t * input, vlib_cli_command_t * cmd)
489 : {
490 : int i;
491 : clib_bihash_8_8_t *h;
492 1577 : int verbose = 0;
493 :
494 1577 : if (unformat (input, "verbose"))
495 0 : verbose = 1;
496 :
497 50272 : for (i = 0; i < vec_len (clib_all_bihashes); i++)
498 : {
499 48695 : h = (clib_bihash_8_8_t *) clib_all_bihashes[i];
500 48695 : vlib_cli_output (vm, "\n%U", h->fmt_fn, h, verbose);
501 : }
502 :
503 1577 : return 0;
504 : }
505 :
506 : /* *INDENT-OFF* */
507 285289 : VLIB_CLI_COMMAND (show_bihash_command, static) =
508 : {
509 : .path = "show bihash",
510 : .short_help = "show bihash",
511 : .function = show_bihash_command_fn,
512 : };
513 : /* *INDENT-ON* */
514 :
515 : #ifdef CLIB_SANITIZE_ADDR
516 : /* default options for Address Sanitizer */
517 : const char *
518 : __asan_default_options (void)
519 : {
520 : return VPP_SANITIZE_ADDR_OPTIONS;
521 : }
522 : #endif /* CLIB_SANITIZE_ADDR */
523 :
524 : /*
525 : * fd.io coding-style-patch-verification: ON
526 : *
527 : * Local Variables:
528 : * eval: (c-set-style "gnu")
529 : * End:
530 : */
|