db28662de4ced60c9f1d1b813af0dacb1bffa865
[vpp.git] / src / vpp / vnet / main.c
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
36 vpp_find_plugin_path ()
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
70 vpe_main_init (vlib_main_t * vm)
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 */
80   vlib_mark_init_function_complete (vm, srp_init);
81
82   /*
83    * Create the binary api plugin hashes before loading plugins
84    */
85   vat_plugin_hash_create ();
86
87   vpp_find_plugin_path ();
88 }
89
90 /*
91  * Default path for runtime data
92  */
93 char *vlib_default_runtime_dir = "vpp";
94
95 int
96 main (int argc, char *argv[])
97 {
98   int i;
99   vlib_main_t *vm = &vlib_global_main;
100   void vl_msg_api_set_first_available_msg_id (u16);
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... */
257   vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
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 *
277 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
278 {
279   u32 junk;
280
281   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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
294 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
295
296 static clib_error_t *
297 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
298 {
299   u8 *junk;
300
301   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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
315 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
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 {
323   vl_msg_api_post_mortem_dump ();
324   elog_post_mortem_dump ();
325   abort ();
326 }
327
328 void vhost_user_unmap_all (void) __attribute__ ((weak));
329 void
330 vhost_user_unmap_all (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
346       vl_msg_api_post_mortem_dump ();
347       elog_post_mortem_dump ();
348       vhost_user_unmap_all ();
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 {
358   vlib_worker_threads[0].barrier_context = context;
359 }
360 #endif
361
362 void
363 vl_msg_api_barrier_sync (void)
364 {
365   vlib_worker_thread_barrier_sync (vlib_get_main ());
366 }
367
368 void
369 vl_msg_api_barrier_release (void)
370 {
371   vlib_worker_thread_barrier_release (vlib_get_main ());
372 }
373
374 /* This application needs 1 thread stack for the stats pthread */
375 u32
376 vlib_app_num_thread_stacks_needed (void)
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  */