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