vppinfra: fix issue when copying 16 bytes with clib_memcpy
[vpp.git] / src / vnet / devices / dpdk / main.c
1 /*
2  * Copyright (c) 2017 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 <vnet/vnet.h>
17 #include <vnet/devices/dpdk/dpdk.h>
18
19
20 /*
21  * Called by the dpdk driver's rte_delay_us() function.
22  * Return 0 to have the dpdk do a regular delay loop.
23  * Return 1 if to skip the delay loop because we are suspending
24  * the calling vlib process instead.
25  */
26 int
27 rte_delay_us_override (unsigned us)
28 {
29   vlib_main_t *vm;
30
31   /* Don't bother intercepting for short delays */
32   if (us < 10)
33     return 0;
34
35   /*
36    * Only intercept if we are in a vlib process.
37    * If we are called from a vlib worker thread or the vlib main
38    * thread then do not intercept. (Must not be called from an
39    * independent pthread).
40    */
41   if (os_get_cpu_number () == 0)
42     {
43       /*
44        * We're in the vlib main thread or a vlib process. Make sure
45        * the process is running and we're not still initializing.
46        */
47       vm = vlib_get_main ();
48       if (vlib_in_process_context (vm))
49         {
50           /* Only suspend for the admin_down_process */
51           vlib_process_t *proc = vlib_get_current_process (vm);
52           if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
53               (proc->node_runtime.function != admin_up_down_process))
54             return 0;
55
56           f64 delay = 1e-6 * us;
57           vlib_process_suspend (vm, delay);
58           return 1;
59         }
60     }
61   return 0;                     // no override
62 }
63
64 static void
65 rte_delay_us_override_cb (unsigned us)
66 {
67   if (rte_delay_us_override (us) == 0)
68     rte_delay_us_block (us);
69 }
70
71 static clib_error_t * dpdk_main_init (vlib_main_t * vm)
72 {
73   clib_error_t * error = 0;
74
75   if ((error = vlib_call_init_function (vm, dpdk_init)))
76     return error;
77
78   /* register custom delay function */
79   rte_delay_us_callback_register (rte_delay_us_override_cb);
80
81   return error;
82 }
83
84 VLIB_INIT_FUNCTION (dpdk_main_init);
85