Don't autodetect plugin path if set on cmdline
[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 = NULL;
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   if (!vlib_plugin_path)
88     vpp_find_plugin_path ();
89 }
90
91 /*
92  * Default path for runtime data
93  */
94 char *vlib_default_runtime_dir = "vpp";
95
96 int
97 main (int argc, char *argv[])
98 {
99   int i;
100   vlib_main_t *vm = &vlib_global_main;
101   void vl_msg_api_set_first_available_msg_id (u16);
102   uword main_heap_size = (1ULL << 30);
103   u8 *sizep;
104   u32 size;
105   int main_core = 1;
106   cpu_set_t cpuset;
107
108 #if __x86_64__
109   CLIB_UNUSED (const char *msg)
110     = "ERROR: This binary requires CPU with %s extensions.\n";
111 #define _(a,b)                                  \
112     if (!clib_cpu_supports_ ## a ())            \
113       {                                         \
114         fprintf(stderr, msg, b);                \
115         exit(1);                                \
116       }
117
118 #if __AVX2__
119   _(avx2, "AVX2")
120 #endif
121 #if __AVX__
122     _(avx, "AVX")
123 #endif
124 #if __SSE4_2__
125     _(sse42, "SSE4.2")
126 #endif
127 #if __SSE4_1__
128     _(sse41, "SSE4.1")
129 #endif
130 #if __SSSE3__
131     _(ssse3, "SSSE3")
132 #endif
133 #if __SSE3__
134     _(sse3, "SSE3")
135 #endif
136 #undef _
137 #endif
138     /*
139      * Load startup config from file.
140      * usage: vpp -c /etc/vpp/startup.conf
141      */
142     if ((argc == 3) && !strncmp (argv[1], "-c", 2))
143     {
144       FILE *fp;
145       char inbuf[4096];
146       int argc_ = 1;
147       char **argv_ = NULL;
148       char *arg = NULL;
149       char *p;
150
151       fp = fopen (argv[2], "r");
152       if (fp == NULL)
153         {
154           fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
155           return 1;
156         }
157       argv_ = calloc (1, sizeof (char *));
158       if (argv_ == NULL)
159         {
160           fclose (fp);
161           return 1;
162         }
163       arg = strndup (argv[0], 1024);
164       if (arg == NULL)
165         {
166           fclose (fp);
167           free (argv_);
168           return 1;
169         }
170       argv_[0] = arg;
171
172       while (1)
173         {
174           if (fgets (inbuf, 4096, fp) == 0)
175             break;
176           p = strtok (inbuf, " \t\n");
177           while (p != NULL)
178             {
179               if (*p == '#')
180                 break;
181               argc_++;
182               char **tmp = realloc (argv_, argc_ * sizeof (char *));
183               if (tmp == NULL)
184                 return 1;
185               argv_ = tmp;
186               arg = strndup (p, 1024);
187               if (arg == NULL)
188                 return 1;
189               argv_[argc_ - 1] = arg;
190               p = strtok (NULL, " \t\n");
191             }
192         }
193
194       fclose (fp);
195
196       char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
197       if (tmp == NULL)
198         return 1;
199       argv_ = tmp;
200       argv_[argc_] = NULL;
201
202       argc = argc_;
203       argv = argv_;
204     }
205
206   /*
207    * Look for and parse the "heapsize" config parameter.
208    * Manual since none of the clib infra has been bootstrapped yet.
209    *
210    * Format: heapsize <nn>[mM][gG]
211    */
212
213   for (i = 1; i < (argc - 1); i++)
214     {
215       if (!strncmp (argv[i], "plugin_path", 11))
216         {
217           if (i < (argc - 1))
218             vlib_plugin_path = argv[++i];
219         }
220       else if (!strncmp (argv[i], "heapsize", 8))
221         {
222           sizep = (u8 *) argv[i + 1];
223           size = 0;
224           while (*sizep >= '0' && *sizep <= '9')
225             {
226               size *= 10;
227               size += *sizep++ - '0';
228             }
229           if (size == 0)
230             {
231               fprintf
232                 (stderr,
233                  "warning: heapsize parse error '%s', use default %lld\n",
234                  argv[i], (long long int) main_heap_size);
235               goto defaulted;
236             }
237
238           main_heap_size = size;
239
240           if (*sizep == 'g' || *sizep == 'G')
241             main_heap_size <<= 30;
242           else if (*sizep == 'm' || *sizep == 'M')
243             main_heap_size <<= 20;
244         }
245       else if (!strncmp (argv[i], "main-core", 9))
246         {
247           if (i < (argc - 1))
248             {
249               errno = 0;
250               unsigned long x = strtol (argv[++i], 0, 0);
251               if (errno == 0)
252                 main_core = x;
253             }
254         }
255     }
256
257 defaulted:
258
259   /* set process affinity for main thread */
260   CPU_ZERO (&cpuset);
261   CPU_SET (main_core, &cpuset);
262   pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
263
264   /* Set up the plugin message ID allocator right now... */
265   vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
266
267   /* Allocate main heap */
268   if (clib_mem_init_thread_safe (0, main_heap_size))
269     {
270       vm->init_functions_called = hash_create (0, /* value bytes */ 0);
271       vpe_main_init (vm);
272       return vlib_unix_main (argc, argv);
273     }
274   else
275     {
276       {
277         int rv __attribute__ ((unused)) =
278           write (2, "Main heap allocation failure!\r\n", 31);
279       }
280       return 1;
281     }
282 }
283
284 static clib_error_t *
285 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
286 {
287   u32 junk;
288
289   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
290     {
291       if (unformat (input, "%dm", &junk)
292           || unformat (input, "%dM", &junk)
293           || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
294         return 0;
295       else
296         return clib_error_return (0, "unknown input '%U'",
297                                   format_unformat_error, input);
298     }
299   return 0;
300 }
301
302 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
303
304 static clib_error_t *
305 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
306 {
307   u8 *junk;
308
309   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
310     {
311       if (unformat (input, "%s", &junk))
312         {
313           vec_free (junk);
314           return 0;
315         }
316       else
317         return clib_error_return (0, "unknown input '%U'",
318                                   format_unformat_error, input);
319     }
320   return 0;
321 }
322
323 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
324
325 void vl_msg_api_post_mortem_dump (void);
326 void elog_post_mortem_dump (void);
327
328 void
329 os_panic (void)
330 {
331   vl_msg_api_post_mortem_dump ();
332   elog_post_mortem_dump ();
333   abort ();
334 }
335
336 void vhost_user_unmap_all (void) __attribute__ ((weak));
337 void
338 vhost_user_unmap_all (void)
339 {
340 }
341
342 void
343 os_exit (int code)
344 {
345   static int recursion_block;
346
347   if (code)
348     {
349       if (recursion_block)
350         abort ();
351
352       recursion_block = 1;
353
354       vl_msg_api_post_mortem_dump ();
355       elog_post_mortem_dump ();
356       vhost_user_unmap_all ();
357       abort ();
358     }
359   exit (code);
360 }
361
362 #ifdef BARRIER_TRACING
363 void
364 vl_msg_api_barrier_trace_context (const char *context)
365 {
366   vlib_worker_threads[0].barrier_context = context;
367 }
368 #endif
369
370 void
371 vl_msg_api_barrier_sync (void)
372 {
373   vlib_worker_thread_barrier_sync (vlib_get_main ());
374 }
375
376 void
377 vl_msg_api_barrier_release (void)
378 {
379   vlib_worker_thread_barrier_release (vlib_get_main ());
380 }
381
382 /* This application needs 1 thread stack for the stats pthread */
383 u32
384 vlib_app_num_thread_stacks_needed (void)
385 {
386   return 1;
387 }
388
389 /*
390  * Depending on the configuration selected above,
391  * it may be necessary to generate stub graph nodes.
392  * It is never OK to ignore "node 'x' refers to unknown node 'y'
393  * messages!
394  */
395
396 /*
397  * fd.io coding-style-patch-verification: ON
398  *
399  * Local Variables:
400  * eval: (c-set-style "gnu")
401  * End:
402  */