FD.io VPP  v17.07.01-10-g3be13f0
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 
24 
25 static void
27 {
28  void vat_plugin_hash_create (void);
29 
30  if (CLIB_DEBUG > 0)
31  vlib_unix_cli_set_prompt ("DBGvpp# ");
32  else
33  vlib_unix_cli_set_prompt ("vpp# ");
34 
35  /* Turn off network stack components which we don't want */
37 
38  /*
39  * Create the binary api plugin hashes before loading plugins
40  */
42 }
43 
44 /*
45  * Load plugins from /usr/lib/vpp_plugins by default
46  */
47 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
48 char *vlib_plugin_app_version = VPP_BUILD_VER;
49 
50 int
51 main (int argc, char *argv[])
52 {
53  int i;
56  uword main_heap_size = (1ULL << 30);
57  u8 *sizep;
58  u32 size;
59 
60 #if __x86_64__
61  CLIB_UNUSED (const char *msg)
62  = "ERROR: This binary requires CPU with %s extensions.\n";
63 #define _(a,b) \
64  if (!clib_cpu_supports_ ## a ()) \
65  { \
66  fprintf(stderr, msg, b); \
67  exit(1); \
68  }
69 
70 #if __AVX2__
71  _(avx2, "AVX2")
72 #endif
73 #if __AVX__
74  _(avx, "AVX")
75 #endif
76 #if __SSE4_2__
77  _(sse42, "SSE4.2")
78 #endif
79 #if __SSE4_1__
80  _(sse41, "SSE4.1")
81 #endif
82 #if __SSSE3__
83  _(ssse3, "SSSE3")
84 #endif
85 #if __SSE3__
86  _(sse3, "SSE3")
87 #endif
88 #undef _
89 #endif
90  /*
91  * Load startup config from file.
92  * usage: vpp -c /etc/vpp/startup.conf
93  */
94  if ((argc == 3) && !strncmp (argv[1], "-c", 2))
95  {
96  FILE *fp;
97  char inbuf[4096];
98  int argc_ = 1;
99  char **argv_ = NULL;
100  char *arg = NULL;
101  char *p;
102 
103  fp = fopen (argv[2], "r");
104  if (fp == NULL)
105  {
106  fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
107  return 1;
108  }
109  argv_ = calloc (1, sizeof (char *));
110  if (argv_ == NULL)
111  return 1;
112  arg = strndup (argv[0], 1024);
113  if (arg == NULL)
114  return 1;
115  argv_[0] = arg;
116 
117  while (1)
118  {
119  if (fgets (inbuf, 4096, fp) == 0)
120  break;
121  p = strtok (inbuf, " \t\n");
122  while (p != NULL)
123  {
124  if (*p == '#')
125  break;
126  argc_++;
127  char **tmp = realloc (argv_, argc_ * sizeof (char *));
128  if (tmp == NULL)
129  return 1;
130  argv_ = tmp;
131  arg = strndup (p, 1024);
132  if (arg == NULL)
133  return 1;
134  argv_[argc_ - 1] = arg;
135  p = strtok (NULL, " \t\n");
136  }
137  }
138 
139  fclose (fp);
140 
141  char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
142  if (tmp == NULL)
143  return 1;
144  argv_ = tmp;
145  argv_[argc_] = NULL;
146 
147  argc = argc_;
148  argv = argv_;
149  }
150 
151  /*
152  * Look for and parse the "heapsize" config parameter.
153  * Manual since none of the clib infra has been bootstrapped yet.
154  *
155  * Format: heapsize <nn>[mM][gG]
156  */
157 
158  for (i = 1; i < (argc - 1); i++)
159  {
160  if (!strncmp (argv[i], "plugin_path", 11))
161  {
162  if (i < (argc - 1))
163  vlib_plugin_path = argv[++i];
164  }
165  else if (!strncmp (argv[i], "heapsize", 8))
166  {
167  sizep = (u8 *) argv[i + 1];
168  size = 0;
169  while (*sizep >= '0' && *sizep <= '9')
170  {
171  size *= 10;
172  size += *sizep++ - '0';
173  }
174  if (size == 0)
175  {
176  fprintf
177  (stderr,
178  "warning: heapsize parse error '%s', use default %lld\n",
179  argv[i], (long long int) main_heap_size);
180  goto defaulted;
181  }
182 
183  main_heap_size = size;
184 
185  if (*sizep == 'g' || *sizep == 'G')
186  main_heap_size <<= 30;
187  else if (*sizep == 'm' || *sizep == 'M')
188  main_heap_size <<= 20;
189  }
190  }
191 
192 defaulted:
193 
194  /* Set up the plugin message ID allocator right now... */
196 
197  /* Allocate main heap */
198  if (clib_mem_init (0, main_heap_size))
199  {
200  vm->init_functions_called = hash_create (0, /* value bytes */ 0);
201  vpe_main_init (vm);
202  return vlib_unix_main (argc, argv);
203  }
204  else
205  {
206  {
207  int rv __attribute__ ((unused)) =
208  write (2, "Main heap allocation failure!\r\n", 31);
209  }
210  return 1;
211  }
212 }
213 
214 static clib_error_t *
216 {
217  u32 junk;
218 
220  {
221  if (unformat (input, "%dm", &junk)
222  || unformat (input, "%dM", &junk)
223  || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
224  return 0;
225  else
226  return clib_error_return (0, "unknown input '%U'",
227  format_unformat_error, input);
228  }
229  return 0;
230 }
231 
233 
234 static clib_error_t *
236 {
237  u8 *junk;
238 
240  {
241  if (unformat (input, "%s", &junk))
242  {
243  vec_free (junk);
244  return 0;
245  }
246  else
247  return clib_error_return (0, "unknown input '%U'",
248  format_unformat_error, input);
249  }
250  return 0;
251 }
252 
254 
255 void vl_msg_api_post_mortem_dump (void);
256 void elog_post_mortem_dump (void);
257 
258 void
259 os_panic (void)
260 {
263  abort ();
264 }
265 
266 void vhost_user_unmap_all (void) __attribute__ ((weak));
267 void
269 {
270 }
271 
272 void
273 os_exit (int code)
274 {
275  static int recursion_block;
276 
277  if (code)
278  {
279  if (recursion_block)
280  abort ();
281 
282  recursion_block = 1;
283 
287  abort ();
288  }
289  exit (code);
290 }
291 
292 void
294 {
296 }
297 
298 void
300 {
302 }
303 
304 /* This application needs 1 thread stack for the stats pthread */
305 u32
307 {
308  return 1;
309 }
310 
311 /*
312  * Depending on the configuration selected above,
313  * it may be necessary to generate stub graph nodes.
314  * It is never OK to ignore "node 'x' refers to unknown node 'y'
315  * messages!
316  */
317 
318 #if CLIB_DEBUG > 0
319 
320 static clib_error_t *
322  unformat_input_t * input, vlib_cli_command_t * cmd)
323 {
324  u64 *p = (u64 *) 0xdefec8ed;
325 
326  ELOG_TYPE_DECLARE (e) =
327  {
328  .format = "deliberate crash: touching %x",.format_args = "i4",};
329 
330  elog (&vm->elog_main, &e, 0xdefec8ed);
331 
332  *p = 0xdeadbeef;
333 
334  /* Not so much... */
335  return 0;
336 }
337 
338 /* *INDENT-OFF* */
339 VLIB_CLI_COMMAND (test_crash_command, static) = {
340  .path = "test crash",
341  .short_help = "crash the bus!",
342  .function = test_crash_command_fn,
343 };
344 /* *INDENT-ON* */
345 
346 #endif
347 
348 /*
349  * fd.io coding-style-patch-verification: ON
350  *
351  * Local Variables:
352  * eval: (c-set-style "gnu")
353  * End:
354  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
char * vlib_plugin_path
Definition: main.c:47
#define CLIB_UNUSED(x)
Definition: clib.h:79
void vl_msg_api_barrier_sync(void)
Definition: main.c:293
int main(int argc, char *argv[])
Definition: main.c:51
#define NULL
Definition: clib.h:55
void vlib_unix_cli_set_prompt(char *prompt)
Set the CLI prompt.
Definition: cli.c:2693
#define vlib_mark_init_function_complete(vm, x)
Definition: init.h:176
char * vlib_plugin_app_version
Definition: main.c:48
void vl_msg_api_barrier_release(void)
Definition: main.c:299
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned long u64
Definition: types.h:89
static void vpe_main_init(vlib_main_t *vm)
Definition: main.c:26
static clib_error_t * srp_init(vlib_main_t *vm)
Definition: node.c:918
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:829
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:759
void os_panic(void)
Definition: main.c:259
vlib_main_t vlib_global_main
Definition: main.c:1653
struct _unformat_input_t unformat_input_t
static clib_error_t * plugin_path_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:235
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:119
void vhost_user_unmap_all(void)
Definition: main.c:268
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:156
void * clib_mem_init(void *heap, uword size)
Definition: mem_mheap.c:60
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
void elog_post_mortem_dump(void)
Definition: main.c:711
elog_main_t elog_main
Definition: main.h:141
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1166
#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:658
unsigned int u32
Definition: types.h:88
void vat_plugin_hash_create(void)
Definition: api_main.c:62
void os_exit(int code)
Definition: main.c:273
u64 size
Definition: vhost-user.h:75
u32 vlib_app_num_thread_stacks_needed(void)
Definition: main.c:306
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
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:1198
static clib_error_t * heapsize_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:215
static clib_error_t * test_crash_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: main.c:321
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
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:538