Autodetect plugin path
[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 #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 #include <limits.h>
24
25 /*
26  * Load plugins from /usr/lib/vpp_plugins by default
27  */
28 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
29 char *vlib_plugin_app_version = VPP_BUILD_VER;
30
31 static void
32 vpp_find_plugin_path ()
33 {
34   extern char *vat_plugin_path;
35   char *p, path[PATH_MAX];
36   int rv;
37   u8 *s;
38
39   /* find executable path */
40   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
41     return;
42
43   /* readlink doesn't provide null termination */
44   path[rv] = 0;
45
46   /* strip filename */
47   if ((p = strrchr (path, '/')) == 0)
48     return;
49   *p = 0;
50
51   /* strip bin/ */
52   if ((p = strrchr (path, '/')) == 0)
53     return;
54   *p = 0;
55
56   s = format (0, "%s/lib/vpp_plugins", path);
57 #if uword_bits == 64
58   s = format (s, ":%s/lib64/vpp_plugins", path);
59 #endif
60   vec_add1 (s, 0);
61   vlib_plugin_path = (char *) s;
62
63   s = format (0, "%s/lib/vpp_api_test_plugins", path);
64 #if uword_bits == 64
65   s = format (s, ":%s/lib64/vpp_api_test_plugins", path);
66 #endif
67   vec_add1 (s, 0);
68   vat_plugin_path = (char *) s;
69 }
70
71 static void
72 vpe_main_init (vlib_main_t * vm)
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 */
82   vlib_mark_init_function_complete (vm, srp_init);
83
84   /*
85    * Create the binary api plugin hashes before loading plugins
86    */
87   vat_plugin_hash_create ();
88
89   vpp_find_plugin_path ();
90 }
91
92 /*
93  * Default path for runtime data
94  */
95 char *vlib_default_runtime_dir = "vpp";
96
97 int
98 main (int argc, char *argv[])
99 {
100   int i;
101   vlib_main_t *vm = &vlib_global_main;
102   void vl_msg_api_set_first_available_msg_id (u16);
103   uword main_heap_size = (1ULL << 30);
104   u8 *sizep;
105   u32 size;
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     }
238
239 defaulted:
240
241   /* Set up the plugin message ID allocator right now... */
242   vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
243
244   /* Allocate main heap */
245   if (clib_mem_init (0, main_heap_size))
246     {
247       vm->init_functions_called = hash_create (0, /* value bytes */ 0);
248       vpe_main_init (vm);
249       return vlib_unix_main (argc, argv);
250     }
251   else
252     {
253       {
254         int rv __attribute__ ((unused)) =
255           write (2, "Main heap allocation failure!\r\n", 31);
256       }
257       return 1;
258     }
259 }
260
261 static clib_error_t *
262 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
263 {
264   u32 junk;
265
266   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
267     {
268       if (unformat (input, "%dm", &junk)
269           || unformat (input, "%dM", &junk)
270           || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
271         return 0;
272       else
273         return clib_error_return (0, "unknown input '%U'",
274                                   format_unformat_error, input);
275     }
276   return 0;
277 }
278
279 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
280
281 static clib_error_t *
282 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
283 {
284   u8 *junk;
285
286   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287     {
288       if (unformat (input, "%s", &junk))
289         {
290           vec_free (junk);
291           return 0;
292         }
293       else
294         return clib_error_return (0, "unknown input '%U'",
295                                   format_unformat_error, input);
296     }
297   return 0;
298 }
299
300 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
301
302 void vl_msg_api_post_mortem_dump (void);
303 void elog_post_mortem_dump (void);
304
305 void
306 os_panic (void)
307 {
308   vl_msg_api_post_mortem_dump ();
309   elog_post_mortem_dump ();
310   abort ();
311 }
312
313 void vhost_user_unmap_all (void) __attribute__ ((weak));
314 void
315 vhost_user_unmap_all (void)
316 {
317 }
318
319 void
320 os_exit (int code)
321 {
322   static int recursion_block;
323
324   if (code)
325     {
326       if (recursion_block)
327         abort ();
328
329       recursion_block = 1;
330
331       vl_msg_api_post_mortem_dump ();
332       elog_post_mortem_dump ();
333       vhost_user_unmap_all ();
334       abort ();
335     }
336   exit (code);
337 }
338
339 #ifdef BARRIER_TRACING
340 void
341 vl_msg_api_barrier_trace_context (const char *context)
342 {
343   vlib_worker_threads[0].barrier_context = context;
344 }
345 #endif
346
347 void
348 vl_msg_api_barrier_sync (void)
349 {
350   vlib_worker_thread_barrier_sync (vlib_get_main ());
351 }
352
353 void
354 vl_msg_api_barrier_release (void)
355 {
356   vlib_worker_thread_barrier_release (vlib_get_main ());
357 }
358
359 /* This application needs 1 thread stack for the stats pthread */
360 u32
361 vlib_app_num_thread_stacks_needed (void)
362 {
363   return 1;
364 }
365
366 /*
367  * Depending on the configuration selected above,
368  * it may be necessary to generate stub graph nodes.
369  * It is never OK to ignore "node 'x' refers to unknown node 'y'
370  * messages!
371  */
372
373 #if CLIB_DEBUG > 0
374
375 static clib_error_t *
376 test_crash_command_fn (vlib_main_t * vm,
377                        unformat_input_t * input, vlib_cli_command_t * cmd)
378 {
379   u64 *p = (u64 *) 0xdefec8ed;
380
381   ELOG_TYPE_DECLARE (e) =
382   {
383   .format = "deliberate crash: touching %x",.format_args = "i4",};
384
385   elog (&vm->elog_main, &e, 0xdefec8ed);
386
387   *p = 0xdeadbeef;
388
389   /* Not so much... */
390   return 0;
391 }
392
393 /* *INDENT-OFF* */
394 VLIB_CLI_COMMAND (test_crash_command, static) = {
395   .path = "test crash",
396   .short_help = "crash the bus!",
397   .function = test_crash_command_fn,
398 };
399 /* *INDENT-ON* */
400
401 #endif
402
403 /*
404  * fd.io coding-style-patch-verification: ON
405  *
406  * Local Variables:
407  * eval: (c-set-style "gnu")
408  * End:
409  */