Trivial: vpp/vnet/main.c fix resource leak
[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         {
159           fclose (fp);
160           return 1;
161         }
162       arg = strndup (argv[0], 1024);
163       if (arg == NULL)
164         {
165           fclose (fp);
166           free (argv_);
167           return 1;
168         }
169       argv_[0] = arg;
170
171       while (1)
172         {
173           if (fgets (inbuf, 4096, fp) == 0)
174             break;
175           p = strtok (inbuf, " \t\n");
176           while (p != NULL)
177             {
178               if (*p == '#')
179                 break;
180               argc_++;
181               char **tmp = realloc (argv_, argc_ * sizeof (char *));
182               if (tmp == NULL)
183                 return 1;
184               argv_ = tmp;
185               arg = strndup (p, 1024);
186               if (arg == NULL)
187                 return 1;
188               argv_[argc_ - 1] = arg;
189               p = strtok (NULL, " \t\n");
190             }
191         }
192
193       fclose (fp);
194
195       char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
196       if (tmp == NULL)
197         return 1;
198       argv_ = tmp;
199       argv_[argc_] = NULL;
200
201       argc = argc_;
202       argv = argv_;
203     }
204
205   /*
206    * Look for and parse the "heapsize" config parameter.
207    * Manual since none of the clib infra has been bootstrapped yet.
208    *
209    * Format: heapsize <nn>[mM][gG]
210    */
211
212   for (i = 1; i < (argc - 1); i++)
213     {
214       if (!strncmp (argv[i], "plugin_path", 11))
215         {
216           if (i < (argc - 1))
217             vlib_plugin_path = argv[++i];
218         }
219       else if (!strncmp (argv[i], "heapsize", 8))
220         {
221           sizep = (u8 *) argv[i + 1];
222           size = 0;
223           while (*sizep >= '0' && *sizep <= '9')
224             {
225               size *= 10;
226               size += *sizep++ - '0';
227             }
228           if (size == 0)
229             {
230               fprintf
231                 (stderr,
232                  "warning: heapsize parse error '%s', use default %lld\n",
233                  argv[i], (long long int) main_heap_size);
234               goto defaulted;
235             }
236
237           main_heap_size = size;
238
239           if (*sizep == 'g' || *sizep == 'G')
240             main_heap_size <<= 30;
241           else if (*sizep == 'm' || *sizep == 'M')
242             main_heap_size <<= 20;
243         }
244       else if (!strncmp (argv[i], "main-core", 9))
245         {
246           if (i < (argc - 1))
247             {
248               errno = 0;
249               unsigned long x = strtol (argv[++i], 0, 0);
250               if (errno == 0)
251                 main_core = x;
252             }
253         }
254     }
255
256 defaulted:
257
258   /* set process affinity for main thread */
259   CPU_ZERO (&cpuset);
260   CPU_SET (main_core, &cpuset);
261   pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
262
263   /* Set up the plugin message ID allocator right now... */
264   vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
265
266   /* Allocate main heap */
267   if (clib_mem_init_thread_safe (0, main_heap_size))
268     {
269       vm->init_functions_called = hash_create (0, /* value bytes */ 0);
270       vpe_main_init (vm);
271       return vlib_unix_main (argc, argv);
272     }
273   else
274     {
275       {
276         int rv __attribute__ ((unused)) =
277           write (2, "Main heap allocation failure!\r\n", 31);
278       }
279       return 1;
280     }
281 }
282
283 static clib_error_t *
284 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
285 {
286   u32 junk;
287
288   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
289     {
290       if (unformat (input, "%dm", &junk)
291           || unformat (input, "%dM", &junk)
292           || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
293         return 0;
294       else
295         return clib_error_return (0, "unknown input '%U'",
296                                   format_unformat_error, input);
297     }
298   return 0;
299 }
300
301 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
302
303 static clib_error_t *
304 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
305 {
306   u8 *junk;
307
308   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309     {
310       if (unformat (input, "%s", &junk))
311         {
312           vec_free (junk);
313           return 0;
314         }
315       else
316         return clib_error_return (0, "unknown input '%U'",
317                                   format_unformat_error, input);
318     }
319   return 0;
320 }
321
322 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
323
324 void vl_msg_api_post_mortem_dump (void);
325 void elog_post_mortem_dump (void);
326
327 void
328 os_panic (void)
329 {
330   vl_msg_api_post_mortem_dump ();
331   elog_post_mortem_dump ();
332   abort ();
333 }
334
335 void vhost_user_unmap_all (void) __attribute__ ((weak));
336 void
337 vhost_user_unmap_all (void)
338 {
339 }
340
341 void
342 os_exit (int code)
343 {
344   static int recursion_block;
345
346   if (code)
347     {
348       if (recursion_block)
349         abort ();
350
351       recursion_block = 1;
352
353       vl_msg_api_post_mortem_dump ();
354       elog_post_mortem_dump ();
355       vhost_user_unmap_all ();
356       abort ();
357     }
358   exit (code);
359 }
360
361 #ifdef BARRIER_TRACING
362 void
363 vl_msg_api_barrier_trace_context (const char *context)
364 {
365   vlib_worker_threads[0].barrier_context = context;
366 }
367 #endif
368
369 void
370 vl_msg_api_barrier_sync (void)
371 {
372   vlib_worker_thread_barrier_sync (vlib_get_main ());
373 }
374
375 void
376 vl_msg_api_barrier_release (void)
377 {
378   vlib_worker_thread_barrier_release (vlib_get_main ());
379 }
380
381 /* This application needs 1 thread stack for the stats pthread */
382 u32
383 vlib_app_num_thread_stacks_needed (void)
384 {
385   return 1;
386 }
387
388 /*
389  * Depending on the configuration selected above,
390  * it may be necessary to generate stub graph nodes.
391  * It is never OK to ignore "node 'x' refers to unknown node 'y'
392  * messages!
393  */
394
395 /*
396  * fd.io coding-style-patch-verification: ON
397  *
398  * Local Variables:
399  * eval: (c-set-style "gnu")
400  * End:
401  */