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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
20 #include <linux/vfio.h>
21 #include <sys/ioctl.h>
23 #include <vnet/vnet.h>
24 #include <vnet/plugin/plugin.h>
25 #include <dpdk/device/dpdk.h>
26 #include <vpp/app/version.h>
29 * Called by the dpdk driver's rte_delay_us() function.
30 * Return 0 to have the dpdk do a regular delay loop.
31 * Return 1 if to skip the delay loop because we are suspending
32 * the calling vlib process instead.
35 rte_delay_us_override (unsigned us)
39 /* Don't bother intercepting for short delays */
44 * Only intercept if we are in a vlib process.
45 * If we are called from a vlib worker thread or the vlib main
46 * thread then do not intercept. (Must not be called from an
47 * independent pthread).
49 if (vlib_get_thread_index () == 0)
52 * We're in the vlib main thread or a vlib process. Make sure
53 * the process is running and we're not still initializing.
55 vm = vlib_get_main ();
56 if (vlib_in_process_context (vm))
58 /* Only suspend for the admin_down_process */
59 vlib_process_t *proc = vlib_get_current_process (vm);
60 if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
61 (proc->node_runtime.function != admin_up_down_process))
64 f64 delay = 1e-6 * us;
65 vlib_process_suspend (vm, delay);
69 return 0; // no override
73 rte_delay_us_override_cb (unsigned us)
75 if (rte_delay_us_override (us) == 0)
76 rte_delay_us_block (us);
79 static clib_error_t * dpdk_main_init (vlib_main_t * vm)
81 dpdk_main_t * dm = &dpdk_main;
82 clib_error_t * error = 0;
85 dm->vnet_main = vnet_get_main ();
87 if ((error = vlib_call_init_function (vm, dpdk_init)))
90 /* register custom delay function */
91 rte_delay_us_callback_register (rte_delay_us_override_cb);
96 VLIB_INIT_FUNCTION (dpdk_main_init);
100 dpdk_early_init (vlib_main_t *vm)
104 clib_error_t *err = 0;
105 clib_mem_vm_alloc_t alloc = { 0 };
107 /* check if pagemap is accessible - if we get zero result
108 dpdk will not be able to get physical memory address and game is over
109 unless we have IOMMU */
110 pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
114 if ((fd = open ("/dev/vfio/vfio", O_RDWR)) == -1)
117 if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
120 /* if we have type 1 IOMMU page map is not needed */
121 if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) != 1)
125 alloc.flags = CLIB_MEM_VM_F_SHARED | CLIB_MEM_VM_F_HUGETLB | CLIB_MEM_VM_F_HUGETLB_PREALLOC;
128 if ((err = clib_mem_vm_ext_alloc (&alloc)))
130 clib_error_free (err);
134 clib_mem_vm_free (alloc.addr, 1 << alloc.log2_page_size);
139 err = clib_error_return (0, "access to physical devices is not allowed");
149 VLIB_PLUGIN_REGISTER () = {
150 .version = VPP_BUILD_VER,
151 .description = "Data Plane Development Kit (DPDK)",
152 .early_init = "dpdk_early_init",