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