FD.io VPP  v18.10-34-gcce845e
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  */
32 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
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/vpp_plugins", path);
61  vec_add1 (s, 0);
62  vlib_plugin_path = (char *) s;
63 
64  s = format (0, "%s/lib/vpp_api_test_plugins", path);
65  vec_add1 (s, 0);
66  vat_plugin_path = (char *) s;
67 }
68 
69 static void
71 {
72  void vat_plugin_hash_create (void);
73 
74  if (CLIB_DEBUG > 0)
75  vlib_unix_cli_set_prompt ("DBGvpp# ");
76  else
77  vlib_unix_cli_set_prompt ("vpp# ");
78 
79  /* Turn off network stack components which we don't want */
81 
82  /*
83  * Create the binary api plugin hashes before loading plugins
84  */
86 
88 }
89 
90 /*
91  * Default path for runtime data
92  */
94 
95 int
96 main (int argc, char *argv[])
97 {
98  int i;
101  uword main_heap_size = (1ULL << 30);
102  u8 *sizep;
103  u32 size;
104  int main_core = 1;
105  cpu_set_t cpuset;
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  else if (!strncmp (argv[i], "main-core", 9))
238  {
239  if (i < (argc - 1))
240  {
241  errno = 0;
242  unsigned long x = strtol (argv[++i], 0, 0);
243  if (errno == 0)
244  main_core = x;
245  }
246  }
247  }
248 
249 defaulted:
250 
251  /* set process affinity for main thread */
252  CPU_ZERO (&cpuset);
253  CPU_SET (main_core, &cpuset);
254  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
255 
256  /* Set up the plugin message ID allocator right now... */
258 
259  /* Allocate main heap */
260  if (clib_mem_init_thread_safe (0, main_heap_size))
261  {
262  vm->init_functions_called = hash_create (0, /* value bytes */ 0);
263  vpe_main_init (vm);
264  return vlib_unix_main (argc, argv);
265  }
266  else
267  {
268  {
269  int rv __attribute__ ((unused)) =
270  write (2, "Main heap allocation failure!\r\n", 31);
271  }
272  return 1;
273  }
274 }
275 
276 static clib_error_t *
278 {
279  u32 junk;
280 
282  {
283  if (unformat (input, "%dm", &junk)
284  || unformat (input, "%dM", &junk)
285  || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
286  return 0;
287  else
288  return clib_error_return (0, "unknown input '%U'",
289  format_unformat_error, input);
290  }
291  return 0;
292 }
293 
295 
296 static clib_error_t *
298 {
299  u8 *junk;
300 
302  {
303  if (unformat (input, "%s", &junk))
304  {
305  vec_free (junk);
306  return 0;
307  }
308  else
309  return clib_error_return (0, "unknown input '%U'",
310  format_unformat_error, input);
311  }
312  return 0;
313 }
314 
316 
317 void vl_msg_api_post_mortem_dump (void);
318 void elog_post_mortem_dump (void);
319 
320 void
321 os_panic (void)
322 {
325  abort ();
326 }
327 
328 void vhost_user_unmap_all (void) __attribute__ ((weak));
329 void
331 {
332 }
333 
334 void
335 os_exit (int code)
336 {
337  static int recursion_block;
338 
339  if (code)
340  {
341  if (recursion_block)
342  abort ();
343 
344  recursion_block = 1;
345 
349  abort ();
350  }
351  exit (code);
352 }
353 
354 #ifdef BARRIER_TRACING
355 void
356 vl_msg_api_barrier_trace_context (const char *context)
357 {
359 }
360 #endif
361 
362 void
364 {
366 }
367 
368 void
370 {
372 }
373 
374 /* This application needs 1 thread stack for the stats pthread */
375 u32
377 {
378  return 1;
379 }
380 
381 /*
382  * Depending on the configuration selected above,
383  * it may be necessary to generate stub graph nodes.
384  * It is never OK to ignore "node 'x' refers to unknown node 'y'
385  * messages!
386  */
387 
388 /*
389  * fd.io coding-style-patch-verification: ON
390  *
391  * Local Variables:
392  * eval: (c-set-style "gnu")
393  * End:
394  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:631
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:376
vlib_main_t vlib_global_main
Definition: main.c:1638
#define CLIB_UNUSED(x)
Definition: clib.h:81
void vl_msg_api_barrier_sync(void)
Definition: main.c:363
static clib_error_t * plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:297
#define NULL
Definition: clib.h:57
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:277
#define vlib_mark_init_function_complete(vm, x)
Definition: init.h:274
void vhost_user_unmap_all(void)
Definition: main.c:330
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: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:335
static clib_error_t * srp_init(vlib_main_t *vm)
Definition: node.c:852
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:787
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:707
uword * init_functions_called
Definition: main.h:176
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
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:32
#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:369
static void vpe_main_init(vlib_main_t *vm)
Definition: main.c:70
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:1455
int main(int argc, char *argv[])
Definition: main.c:96
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:321
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:857
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