dpdk: fix rte_delay_us callback issue
[vpp.git] / 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
22 #include <vpp-api/vpe_msg_enum.h>
23
24 #if DPDK
25 #include <vnet/devices/dpdk/dpdk.h>
26
27 /*
28  * Called by the dpdk driver's rte_delay_us() function.
29  * Return 0 to have the dpdk do a regular delay loop.
30  * Return 1 if to skip the delay loop because we are suspending
31  * the calling vlib process instead.
32  */
33 int
34 rte_delay_us_override (unsigned us)
35 {
36   vlib_main_t *vm;
37
38   /* Don't bother intercepting for short delays */
39   if (us < 10)
40     return 0;
41
42   /*
43    * Only intercept if we are in a vlib process.
44    * If we are called from a vlib worker thread or the vlib main
45    * thread then do not intercept. (Must not be called from an
46    * independent pthread).
47    */
48   if (os_get_cpu_number () == 0)
49     {
50       /*
51        * We're in the vlib main thread or a vlib process. Make sure
52        * the process is running and we're not still initializing.
53        */
54       vm = vlib_get_main ();
55       if (vlib_in_process_context (vm))
56         {
57           /* Only suspend for the admin_down_process */
58           vlib_process_t *proc = vlib_get_current_process (vm);
59           if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
60               (proc->node_runtime.function != admin_up_down_process))
61             return 0;
62
63           f64 delay = 1e-6 * us;
64           vlib_process_suspend (vm, delay);
65           return 1;
66         }
67     }
68   return 0;                     // no override
69 }
70
71 static void
72 rte_delay_us_override_cb (unsigned us)
73 {
74   if (rte_delay_us_override (us) == 0)
75     rte_delay_us_block (us);
76 }
77 #endif
78
79 static void
80 vpe_main_init (vlib_main_t * vm)
81 {
82   if (CLIB_DEBUG > 0)
83     vlib_unix_cli_set_prompt ("DBGvpp# ");
84   else
85     vlib_unix_cli_set_prompt ("vpp# ");
86
87   /* Turn off network stack components which we don't want */
88   vlib_mark_init_function_complete (vm, srp_init);
89
90 #if DPDK
91 #if RTE_VERSION >= RTE_VERSION_NUM(16, 11, 0, 0)
92   /* register custom delay function */
93   rte_delay_us_callback_register (rte_delay_us_override_cb);
94 #endif
95 #endif
96 }
97
98 /*
99  * Load plugins from /usr/lib/vpp_plugins by default
100  */
101 char *vlib_plugin_path = "/usr/lib/vpp_plugins";
102
103 void *
104 vnet_get_handoff_structure (void)
105 {
106   static vnet_plugin_handoff_t _rv, *rv = &_rv;
107
108   rv->vnet_main = vnet_get_main ();
109   rv->ethernet_main = &ethernet_main;
110   return (void *) rv;
111 }
112
113 int
114 main (int argc, char *argv[])
115 {
116   int i;
117   vlib_main_t *vm = &vlib_global_main;
118   void vl_msg_api_set_first_available_msg_id (u16);
119   uword main_heap_size = (1ULL << 30);
120   u8 *sizep;
121   u32 size;
122   void vlib_set_get_handoff_structure_cb (void *cb);
123
124 #if __x86_64__
125   const char *msg = "ERROR: This binary requires CPU with %s extensions.\n";
126 #define _(a,b)                                  \
127     if (!clib_cpu_supports_ ## a ())            \
128       {                                         \
129         fprintf(stderr, msg, b);                \
130         exit(1);                                \
131       }
132
133 #if __AVX2__
134   _(avx2, "AVX2")
135 #endif
136 #if __AVX__
137     _(avx, "AVX")
138 #endif
139 #if __SSE4_2__
140     _(sse42, "SSE4.2")
141 #endif
142 #if __SSE4_1__
143     _(sse41, "SSE4.1")
144 #endif
145 #if __SSSE3__
146     _(ssse3, "SSSE3")
147 #endif
148 #if __SSE3__
149     _(sse3, "SSE3")
150 #endif
151 #undef _
152 #endif
153     /*
154      * Load startup config from file.
155      * usage: vpp -c /etc/vpp/startup.conf
156      */
157     if ((argc == 3) && !strncmp (argv[1], "-c", 2))
158     {
159       FILE *fp;
160       char inbuf[4096];
161       int argc_ = 1;
162       char **argv_ = NULL;
163       char *arg = NULL;
164       char *p;
165
166       fp = fopen (argv[2], "r");
167       if (fp == NULL)
168         {
169           fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
170           return 1;
171         }
172       argv_ = calloc (1, sizeof (char *));
173       if (argv_ == NULL)
174         return 1;
175       arg = strndup (argv[0], 1024);
176       if (arg == NULL)
177         return 1;
178       argv_[0] = arg;
179
180       while (1)
181         {
182           if (fgets (inbuf, 4096, fp) == 0)
183             break;
184           p = strtok (inbuf, " \t\n");
185           while (p != NULL)
186             {
187               if (*p == '#')
188                 break;
189               argc_++;
190               char **tmp = realloc (argv_, argc_ * sizeof (char *));
191               if (tmp == NULL)
192                 return 1;
193               argv_ = tmp;
194               arg = strndup (p, 1024);
195               if (arg == NULL)
196                 return 1;
197               argv_[argc_ - 1] = arg;
198               p = strtok (NULL, " \t\n");
199             }
200         }
201
202       fclose (fp);
203
204       char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
205       if (tmp == NULL)
206         return 1;
207       argv_ = tmp;
208       argv_[argc_] = NULL;
209
210       argc = argc_;
211       argv = argv_;
212     }
213
214   /*
215    * Look for and parse the "heapsize" config parameter.
216    * Manual since none of the clib infra has been bootstrapped yet.
217    *
218    * Format: heapsize <nn>[mM][gG]
219    */
220
221   for (i = 1; i < (argc - 1); i++)
222     {
223       if (!strncmp (argv[i], "plugin_path", 11))
224         {
225           if (i < (argc - 1))
226             vlib_plugin_path = argv[++i];
227         }
228       else if (!strncmp (argv[i], "heapsize", 8))
229         {
230           sizep = (u8 *) argv[i + 1];
231           size = 0;
232           while (*sizep >= '0' && *sizep <= '9')
233             {
234               size *= 10;
235               size += *sizep++ - '0';
236             }
237           if (size == 0)
238             {
239               fprintf
240                 (stderr,
241                  "warning: heapsize parse error '%s', use default %lld\n",
242                  argv[i], (long long int) main_heap_size);
243               goto defaulted;
244             }
245
246           main_heap_size = size;
247
248           if (*sizep == 'g' || *sizep == 'G')
249             main_heap_size <<= 30;
250           else if (*sizep == 'm' || *sizep == 'M')
251             main_heap_size <<= 20;
252         }
253     }
254
255 defaulted:
256
257   /* Set up the plugin message ID allocator right now... */
258   vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
259
260   /* Allocate main heap */
261   if (clib_mem_init (0, main_heap_size))
262     {
263       vm->init_functions_called = hash_create (0, /* value bytes */ 0);
264       vpe_main_init (vm);
265 #if DPDK == 0
266       unix_physmem_init (vm, 0 /* fail_if_physical_memory_not_present */ );
267 #endif
268       vlib_set_get_handoff_structure_cb (&vnet_get_handoff_structure);
269       return vlib_unix_main (argc, argv);
270     }
271   else
272     {
273       {
274         int rv __attribute__ ((unused)) =
275           write (2, "Main heap allocation failure!\r\n", 31);
276       }
277       return 1;
278     }
279 }
280
281 static clib_error_t *
282 heapsize_config (vlib_main_t * vm, unformat_input_t * input)
283 {
284   u32 junk;
285
286   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287     {
288       if (unformat (input, "%dm", &junk)
289           || unformat (input, "%dM", &junk)
290           || unformat (input, "%dg", &junk) || unformat (input, "%dG", &junk))
291         return 0;
292       else
293         return clib_error_return (0, "unknown input '%U'",
294                                   format_unformat_error, input);
295     }
296   return 0;
297 }
298
299 VLIB_CONFIG_FUNCTION (heapsize_config, "heapsize");
300
301 static clib_error_t *
302 plugin_path_config (vlib_main_t * vm, unformat_input_t * input)
303 {
304   u8 *junk;
305
306   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
307     {
308       if (unformat (input, "%s", &junk))
309         {
310           vec_free (junk);
311           return 0;
312         }
313       else
314         return clib_error_return (0, "unknown input '%U'",
315                                   format_unformat_error, input);
316     }
317   return 0;
318 }
319
320 VLIB_CONFIG_FUNCTION (plugin_path_config, "plugin_path");
321
322 void vl_msg_api_post_mortem_dump (void);
323
324 void
325 os_panic (void)
326 {
327   vl_msg_api_post_mortem_dump ();
328   abort ();
329 }
330
331 void vhost_user_unmap_all (void) __attribute__ ((weak));
332 void
333 vhost_user_unmap_all (void)
334 {
335 }
336
337 void
338 os_exit (int code)
339 {
340   static int recursion_block;
341
342   if (code)
343     {
344       if (recursion_block)
345         abort ();
346
347       recursion_block = 1;
348
349       vl_msg_api_post_mortem_dump ();
350       vhost_user_unmap_all ();
351       abort ();
352     }
353   exit (code);
354 }
355
356 void
357 vl_msg_api_barrier_sync (void)
358 {
359   vlib_worker_thread_barrier_sync (vlib_get_main ());
360 }
361
362 void
363 vl_msg_api_barrier_release (void)
364 {
365   vlib_worker_thread_barrier_release (vlib_get_main ());
366 }
367
368 /* This application needs 1 thread stack for the stats pthread */
369 u32
370 vlib_app_num_thread_stacks_needed (void)
371 {
372   return 1;
373 }
374
375 /*
376  * Depending on the configuration selected above,
377  * it may be necessary to generate stub graph nodes.
378  * It is never OK to ignore "node 'x' refers to unknown node 'y'
379  * messages!
380  */
381
382 #if CLIB_DEBUG > 0
383
384 static clib_error_t *
385 test_crash_command_fn (vlib_main_t * vm,
386                        unformat_input_t * input, vlib_cli_command_t * cmd)
387 {
388   u64 *p = (u64 *) 0xdefec8ed;
389
390   *p = 0xdeadbeef;
391
392   /* Not so much... */
393   return 0;
394 }
395
396 /* *INDENT-OFF* */
397 VLIB_CLI_COMMAND (test_crash_command, static) = {
398   .path = "test crash",
399   .short_help = "crash the bus!",
400   .function = test_crash_command_fn,
401 };
402 /* *INDENT-ON* */
403
404 #endif
405
406 /*
407  * fd.io coding-style-patch-verification: ON
408  *
409  * Local Variables:
410  * eval: (c-set-style "gnu")
411  * End:
412  */