FD.io VPP  v19.04.1-1-ge4a0f9f
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 #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 <vnet/plugin/plugin.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vpp/app/version.h>
26 #include <vpp/api/vpe_msg_enum.h>
27 #include <limits.h>
28 
29 /*
30  * Load plugins from /usr/lib/vpp_plugins by default
31  */
33 char *vlib_plugin_app_version = VPP_BUILD_VER;
34 
35 static void
37 {
38  extern char *vat_plugin_path;
39  char *p, path[PATH_MAX];
40  int rv;
41  u8 *s;
42 
43  /* find executable path */
44  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
45  return;
46 
47  /* readlink doesn't provide null termination */
48  path[rv] = 0;
49 
50  /* strip filename */
51  if ((p = strrchr (path, '/')) == 0)
52  return;
53  *p = 0;
54 
55  /* strip bin/ */
56  if ((p = strrchr (path, '/')) == 0)
57  return;
58  *p = 0;
59 
60  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_plugins:"
61  "%s/lib/vpp_plugins", path, path);
62  vec_add1 (s, 0);
63  vlib_plugin_path = (char *) s;
64 
65  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
66  "%s/lib/vpp_api_test_plugins", path, path);
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 
89  if (!vlib_plugin_path)
91 }
92 
93 /*
94  * Default path for runtime data
95  */
97 
98 int
99 main (int argc, char *argv[])
100 {
101  int i;
104  uword main_heap_size = (1ULL << 30);
105  u8 *sizep;
106  u32 size;
107  int main_core = 1;
108  cpu_set_t cpuset;
109 
110 #if __x86_64__
111  CLIB_UNUSED (const char *msg)
112  = "ERROR: This binary requires CPU with %s extensions.\n";
113 #define _(a,b) \
114  if (!clib_cpu_supports_ ## a ()) \
115  { \
116  fprintf(stderr, msg, b); \
117  exit(1); \
118  }
119 
120 #if __AVX2__
121  _(avx2, "AVX2")
122 #endif
123 #if __AVX__
124  _(avx, "AVX")
125 #endif
126 #if __SSE4_2__
127  _(sse42, "SSE4.2")
128 #endif
129 #if __SSE4_1__
130  _(sse41, "SSE4.1")
131 #endif
132 #if __SSSE3__
133  _(ssse3, "SSSE3")
134 #endif
135 #if __SSE3__
136  _(sse3, "SSE3")
137 #endif
138 #undef _
139 #endif
140  /*
141  * Load startup config from file.
142  * usage: vpp -c /etc/vpp/startup.conf
143  */
144  if ((argc == 3) && !strncmp (argv[1], "-c", 2))
145  {
146  FILE *fp;
147  char inbuf[4096];
148  int argc_ = 1;
149  char **argv_ = NULL;
150  char *arg = NULL;
151  char *p;
152 
153  fp = fopen (argv[2], "r");
154  if (fp == NULL)
155  {
156  fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
157  return 1;
158  }
159  argv_ = calloc (1, sizeof (char *));
160  if (argv_ == NULL)
161  {
162  fclose (fp);
163  return 1;
164  }
165  arg = strndup (argv[0], 1024);
166  if (arg == NULL)
167  {
168  fclose (fp);
169  free (argv_);
170  return 1;
171  }
172  argv_[0] = arg;
173 
174  while (1)
175  {
176  if (fgets (inbuf, 4096, fp) == 0)
177  break;
178  p = strtok (inbuf, " \t\n");
179  while (p != NULL)
180  {
181  if (*p == '#')
182  break;
183  argc_++;
184  char **tmp = realloc (argv_, argc_ * sizeof (char *));
185  if (tmp == NULL)
186  return 1;
187  argv_ = tmp;
188  arg = strndup (p, 1024);
189  if (arg == NULL)
190  return 1;
191  argv_[argc_ - 1] = arg;
192  p = strtok (NULL, " \t\n");
193  }
194  }
195 
196  fclose (fp);
197 
198  char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
199  if (tmp == NULL)
200  return 1;
201  argv_ = tmp;
202  argv_[argc_] = NULL;
203 
204  argc = argc_;
205  argv = argv_;
206  }
207 
208  /*
209  * Look for and parse the "heapsize" config parameter.
210  * Manual since none of the clib infra has been bootstrapped yet.
211  *
212  * Format: heapsize <nn>[mM][gG]
213  */
214 
215  for (i = 1; i < (argc - 1); i++)
216  {
217  if (!strncmp (argv[i], "plugin_path", 11))
218  {
219  if (i < (argc - 1))
220  vlib_plugin_path = argv[++i];
221  }
222  else if (!strncmp (argv[i], "heapsize", 8))
223  {
224  sizep = (u8 *) argv[i + 1];
225  size = 0;
226  while (*sizep >= '0' && *sizep <= '9')
227  {
228  size *= 10;
229  size += *sizep++ - '0';
230  }
231  if (size == 0)
232  {
233  fprintf
234  (stderr,
235  "warning: heapsize parse error '%s', use default %lld\n",
236  argv[i], (long long int) main_heap_size);
237  goto defaulted;
238  }
239 
240  main_heap_size = size;
241 
242  if (*sizep == 'g' || *sizep == 'G')
243  main_heap_size <<= 30;
244  else if (*sizep == 'm' || *sizep == 'M')
245  main_heap_size <<= 20;
246  }
247  else if (!strncmp (argv[i], "main-core", 9))
248  {
249  if (i < (argc - 1))
250  {
251  errno = 0;
252  unsigned long x = strtol (argv[++i], 0, 0);
253  if (errno == 0)
254  main_core = x;
255  }
256  }
257  }
258 
259 defaulted:
260 
261  /* set process affinity for main thread */
262  CPU_ZERO (&cpuset);
263  CPU_SET (main_core, &cpuset);
264  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
265 
266  /* Set up the plugin message ID allocator right now... */
268 
269  /* Allocate main heap */
270  if (clib_mem_init_thread_safe (0, main_heap_size))
271  {
272  vm->init_functions_called = hash_create (0, /* value bytes */ 0);
273  vpe_main_init (vm);
274  return vlib_unix_main (argc, argv);
275  }
276  else
277  {
278  {
279  int rv __attribute__ ((unused)) =
280  write (2, "Main heap allocation failure!\r\n", 31);
281  }
282  return 1;
283  }
284 }
285 
286 static clib_error_t *
288 {
289  u32 junk;
290 
292  {
293  if (unformat (input, "%dm", &junk)
294  || unformat (input, "%dM", &junk)
295  || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
296  return 0;
297  else
298  return clib_error_return (0, "unknown input '%U'",
299  format_unformat_error, input);
300  }
301  return 0;
302 }
303 
305 
306 static clib_error_t *
308 {
309  u8 *junk;
310 
312  {
313  if (unformat (input, "%s", &junk))
314  {
315  vec_free (junk);
316  return 0;
317  }
318  else
319  return clib_error_return (0, "unknown input '%U'",
320  format_unformat_error, input);
321  }
322  return 0;
323 }
324 
326 
327 void vl_msg_api_post_mortem_dump (void);
328 void elog_post_mortem_dump (void);
329 
330 void
331 os_panic (void)
332 {
335  abort ();
336 }
337 
338 void vhost_user_unmap_all (void) __attribute__ ((weak));
339 void
341 {
342 }
343 
344 void
345 os_exit (int code)
346 {
347  static int recursion_block;
348 
349  if (code)
350  {
351  if (recursion_block)
352  abort ();
353 
354  recursion_block = 1;
355 
359  abort ();
360  }
361  exit (code);
362 }
363 
364 #ifdef BARRIER_TRACING
365 void
367 {
369 }
370 #endif
371 
372 void
374 {
376 }
377 
378 void
380 {
382 }
383 
384 /* This application needs 1 thread stack for the stats pthread */
385 u32
387 {
388  return 1;
389 }
390 
391 /*
392  * Depending on the configuration selected above,
393  * it may be necessary to generate stub graph nodes.
394  * It is never OK to ignore "node 'x' refers to unknown node 'y'
395  * messages!
396  */
397 
398 /*
399  * fd.io coding-style-patch-verification: ON
400  *
401  * Local Variables:
402  * eval: (c-set-style "gnu")
403  * End:
404  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:641
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:386
vlib_main_t vlib_global_main
Definition: main.c:1931
#define CLIB_UNUSED(x)
Definition: clib.h:82
void vl_msg_api_barrier_sync(void)
Definition: main.c:373
static clib_error_t * plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:307
#define NULL
Definition: clib.h:58
void vlib_unix_cli_set_prompt(char *prompt)
Set the CLI prompt.
Definition: cli.c:3061
static clib_error_t * heapsize_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:287
#define vlib_mark_init_function_complete(vm, x)
Definition: init.h:274
void vhost_user_unmap_all(void)
Definition: main.c:340
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:522
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define vl_msg_api_barrier_trace_context(X)
Definition: api_common.h:168
unsigned char u8
Definition: types.h:56
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
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:345
static clib_error_t * srp_init(vlib_main_t *vm)
Definition: node.c:864
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:802
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:36
uword size
const char * barrier_context
Definition: threads.h:107
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:172
void elog_post_mortem_dump(void)
Definition: main.c:756
uword * init_functions_called
Definition: main.h:194
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
vlib_main_t * vm
Definition: buffer.c:312
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
char * vlib_plugin_path
Definition: main.c:32
u32 context
Definition: ipsec_gre.api:33
#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:379
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:36
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:1487
int main(int argc, char *argv[])
Definition: main.c:99
char * vlib_plugin_app_version
Definition: main.c:33
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:227
void os_panic(void)
Definition: main.c:331
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:872
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:170