FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
main.c
Go to the documentation of this file.
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 #include <vppinfra/cpu.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vpp/app/version.h>
22 #include <vpp/api/vpe_msg_enum.h>
23 #include <limits.h>
24 
25 /*
26  * Load plugins from /usr/lib/vpp_plugins by default
27  */
28 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
29 char *vlib_plugin_app_version = VPP_BUILD_VER;
30 
31 static void
33 {
34  extern char *vat_plugin_path;
35  char *p, path[PATH_MAX];
36  int rv;
37  u8 *s;
38 
39  /* find executable path */
40  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
41  return;
42 
43  /* readlink doesn't provide null termination */
44  path[rv] = 0;
45 
46  /* strip filename */
47  if ((p = strrchr (path, '/')) == 0)
48  return;
49  *p = 0;
50 
51  /* strip bin/ */
52  if ((p = strrchr (path, '/')) == 0)
53  return;
54  *p = 0;
55 
56  s = format (0, "%s/lib/vpp_plugins", path);
57 #if uword_bits == 64
58  s = format (s, ":%s/lib64/vpp_plugins", path);
59 #endif
60  vec_add1 (s, 0);
61  vlib_plugin_path = (char *) s;
62 
63  s = format (0, "%s/lib/vpp_api_test_plugins", path);
64 #if uword_bits == 64
65  s = format (s, ":%s/lib64/vpp_api_test_plugins", path);
66 #endif
67  vec_add1 (s, 0);
68  vat_plugin_path = (char *) s;
69 }
70 
71 static void
73 {
74  void vat_plugin_hash_create (void);
75 
76  if (CLIB_DEBUG > 0)
77  vlib_unix_cli_set_prompt ("DBGvpp# ");
78  else
79  vlib_unix_cli_set_prompt ("vpp# ");
80 
81  /* Turn off network stack components which we don't want */
83 
84  /*
85  * Create the binary api plugin hashes before loading plugins
86  */
88 
90 }
91 
92 /*
93  * Default path for runtime data
94  */
96 
97 int
98 main (int argc, char *argv[])
99 {
100  int i;
103  uword main_heap_size = (1ULL << 30);
104  u8 *sizep;
105  u32 size;
106 
107 #if __x86_64__
108  CLIB_UNUSED (const char *msg)
109  = "ERROR: This binary requires CPU with %s extensions.\n";
110 #define _(a,b) \
111  if (!clib_cpu_supports_ ## a ()) \
112  { \
113  fprintf(stderr, msg, b); \
114  exit(1); \
115  }
116 
117 #if __AVX2__
118  _(avx2, "AVX2")
119 #endif
120 #if __AVX__
121  _(avx, "AVX")
122 #endif
123 #if __SSE4_2__
124  _(sse42, "SSE4.2")
125 #endif
126 #if __SSE4_1__
127  _(sse41, "SSE4.1")
128 #endif
129 #if __SSSE3__
130  _(ssse3, "SSSE3")
131 #endif
132 #if __SSE3__
133  _(sse3, "SSE3")
134 #endif
135 #undef _
136 #endif
137  /*
138  * Load startup config from file.
139  * usage: vpp -c /etc/vpp/startup.conf
140  */
141  if ((argc == 3) && !strncmp (argv[1], "-c", 2))
142  {
143  FILE *fp;
144  char inbuf[4096];
145  int argc_ = 1;
146  char **argv_ = NULL;
147  char *arg = NULL;
148  char *p;
149 
150  fp = fopen (argv[2], "r");
151  if (fp == NULL)
152  {
153  fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
154  return 1;
155  }
156  argv_ = calloc (1, sizeof (char *));
157  if (argv_ == NULL)
158  return 1;
159  arg = strndup (argv[0], 1024);
160  if (arg == NULL)
161  return 1;
162  argv_[0] = arg;
163 
164  while (1)
165  {
166  if (fgets (inbuf, 4096, fp) == 0)
167  break;
168  p = strtok (inbuf, " \t\n");
169  while (p != NULL)
170  {
171  if (*p == '#')
172  break;
173  argc_++;
174  char **tmp = realloc (argv_, argc_ * sizeof (char *));
175  if (tmp == NULL)
176  return 1;
177  argv_ = tmp;
178  arg = strndup (p, 1024);
179  if (arg == NULL)
180  return 1;
181  argv_[argc_ - 1] = arg;
182  p = strtok (NULL, " \t\n");
183  }
184  }
185 
186  fclose (fp);
187 
188  char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
189  if (tmp == NULL)
190  return 1;
191  argv_ = tmp;
192  argv_[argc_] = NULL;
193 
194  argc = argc_;
195  argv = argv_;
196  }
197 
198  /*
199  * Look for and parse the "heapsize" config parameter.
200  * Manual since none of the clib infra has been bootstrapped yet.
201  *
202  * Format: heapsize <nn>[mM][gG]
203  */
204 
205  for (i = 1; i < (argc - 1); i++)
206  {
207  if (!strncmp (argv[i], "plugin_path", 11))
208  {
209  if (i < (argc - 1))
210  vlib_plugin_path = argv[++i];
211  }
212  else if (!strncmp (argv[i], "heapsize", 8))
213  {
214  sizep = (u8 *) argv[i + 1];
215  size = 0;
216  while (*sizep >= '0' && *sizep <= '9')
217  {
218  size *= 10;
219  size += *sizep++ - '0';
220  }
221  if (size == 0)
222  {
223  fprintf
224  (stderr,
225  "warning: heapsize parse error '%s', use default %lld\n",
226  argv[i], (long long int) main_heap_size);
227  goto defaulted;
228  }
229 
230  main_heap_size = size;
231 
232  if (*sizep == 'g' || *sizep == 'G')
233  main_heap_size <<= 30;
234  else if (*sizep == 'm' || *sizep == 'M')
235  main_heap_size <<= 20;
236  }
237  }
238 
239 defaulted:
240 
241  /* Set up the plugin message ID allocator right now... */
243 
244  /* Allocate main heap */
245  if (clib_mem_init (0, main_heap_size))
246  {
247  vm->init_functions_called = hash_create (0, /* value bytes */ 0);
248  vpe_main_init (vm);
249  return vlib_unix_main (argc, argv);
250  }
251  else
252  {
253  {
254  int rv __attribute__ ((unused)) =
255  write (2, "Main heap allocation failure!\r\n", 31);
256  }
257  return 1;
258  }
259 }
260 
261 static clib_error_t *
263 {
264  u32 junk;
265 
267  {
268  if (unformat (input, "%dm", &junk)
269  || unformat (input, "%dM", &junk)
270  || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
271  return 0;
272  else
273  return clib_error_return (0, "unknown input '%U'",
274  format_unformat_error, input);
275  }
276  return 0;
277 }
278 
280 
281 static clib_error_t *
283 {
284  u8 *junk;
285 
287  {
288  if (unformat (input, "%s", &junk))
289  {
290  vec_free (junk);
291  return 0;
292  }
293  else
294  return clib_error_return (0, "unknown input '%U'",
295  format_unformat_error, input);
296  }
297  return 0;
298 }
299 
301 
302 void vl_msg_api_post_mortem_dump (void);
303 void elog_post_mortem_dump (void);
304 
305 void
306 os_panic (void)
307 {
310  abort ();
311 }
312 
313 void vhost_user_unmap_all (void) __attribute__ ((weak));
314 void
316 {
317 }
318 
319 void
320 os_exit (int code)
321 {
322  static int recursion_block;
323 
324  if (code)
325  {
326  if (recursion_block)
327  abort ();
328 
329  recursion_block = 1;
330 
334  abort ();
335  }
336  exit (code);
337 }
338 
339 #ifdef BARRIER_TRACING
340 void
341 vl_msg_api_barrier_trace_context (const char *context)
342 {
343  vlib_worker_threads[0].barrier_context = context;
344 }
345 #endif
346 
347 void
349 {
351 }
352 
353 void
355 {
357 }
358 
359 /* This application needs 1 thread stack for the stats pthread */
360 u32
362 {
363  return 1;
364 }
365 
366 /*
367  * Depending on the configuration selected above,
368  * it may be necessary to generate stub graph nodes.
369  * It is never OK to ignore "node 'x' refers to unknown node 'y'
370  * messages!
371  */
372 
373 #if CLIB_DEBUG > 0
374 
375 static clib_error_t *
377  unformat_input_t * input, vlib_cli_command_t * cmd)
378 {
379  u64 *p = (u64 *) 0xdefec8ed;
380 
381  ELOG_TYPE_DECLARE (e) =
382  {
383  .format = "deliberate crash: touching %x",.format_args = "i4",};
384 
385  elog (&vm->elog_main, &e, 0xdefec8ed);
386 
387  *p = 0xdeadbeef;
388 
389  /* Not so much... */
390  return 0;
391 }
392 
393 /* *INDENT-OFF* */
394 VLIB_CLI_COMMAND (test_crash_command, static) = {
395  .path = "test crash",
396  .short_help = "crash the bus!",
397  .function = test_crash_command_fn,
398 };
399 /* *INDENT-ON* */
400 
401 #endif
402 
403 /*
404  * fd.io coding-style-patch-verification: ON
405  *
406  * Local Variables:
407  * eval: (c-set-style "gnu")
408  * End:
409  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:631
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:361
vlib_main_t vlib_global_main
Definition: main.c:1644
#define CLIB_UNUSED(x)
Definition: clib.h:79
void vl_msg_api_barrier_sync(void)
Definition: main.c:348
static clib_error_t * plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:282
unsigned long u64
Definition: types.h:89
#define NULL
Definition: clib.h:55
void vlib_unix_cli_set_prompt(char *prompt)
Set the CLI prompt.
Definition: cli.c:3029
static clib_error_t * heapsize_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:262
#define vlib_mark_init_function_complete(vm, x)
Definition: init.h:241
void vhost_user_unmap_all(void)
Definition: main.c:315
char * vat_plugin_path
Definition: plugin.c:183
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define vl_msg_api_barrier_trace_context(X)
Definition: api_common.h:167
unsigned char u8
Definition: types.h:56
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:212
char * vlib_default_runtime_dir
Definition: main.c:59
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
void os_exit(int code)
Definition: main.c:320
static clib_error_t * srp_init(vlib_main_t *vm)
Definition: node.c:920
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:794
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
uword size
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:164
void elog_post_mortem_dump(void)
Definition: main.c:706
static void elog(elog_main_t *em, elog_event_type_t *type, u32 data)
Log a single-datum event.
Definition: elog.h:364
uword * init_functions_called
Definition: main.h:173
void * clib_mem_init(void *heap, uword size)
Definition: mem_mheap.c:60
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
char * vlib_plugin_path
Definition: main.c:28
static clib_error_t * test_crash_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: main.c:376
elog_main_t elog_main
Definition: main.h:158
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:439
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define hash_create(elts, value_bytes)
Definition: hash.h:696
void vat_plugin_hash_create(void)
Definition: api_main.c:65
void vl_msg_api_barrier_release(void)
Definition: main.c:354
static void vpe_main_init(vlib_main_t *vm)
Definition: main.c:72
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
static void vpp_find_plugin_path()
Definition: main.c:32
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1508
int main(int argc, char *argv[])
Definition: main.c:98
char * vlib_plugin_app_version
Definition: main.c:29
void os_panic(void)
Definition: main.c:306
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:864
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169