FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
main.c
Go to the documentation of this file.
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 <unistd.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <linux/vfio.h>
21 #include <sys/ioctl.h>
22 
23 #include <vnet/vnet.h>
24 #include <vnet/plugin/plugin.h>
25 #include <dpdk/device/dpdk.h>
26 #include <vpp/app/version.h>
27 
28 /*
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.
33  */
34 static int
35 rte_delay_us_override (unsigned us)
36 {
37  vlib_main_t *vm;
38 
39  /* Don't bother intercepting for short delays */
40  if (us < 10)
41  return 0;
42 
43  /*
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).
48  */
49  if (vlib_get_thread_index () == 0)
50  {
51  /*
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.
54  */
55  vm = vlib_get_main ();
56  if (vlib_in_process_context (vm))
57  {
58  /* Only suspend for the admin_down_process */
60  if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
62  return 0;
63 
64  f64 delay = 1e-6 * us;
65  vlib_process_suspend (vm, delay);
66  return 1;
67  }
68  }
69  return 0; // no override
70 }
71 
72 static void
74 {
75  if (rte_delay_us_override (us) == 0)
76  rte_delay_us_block (us);
77 }
78 
80 {
81  dpdk_main_t * dm = &dpdk_main;
82  clib_error_t * error = 0;
83 
84  dm->vlib_main = vm;
85  dm->vnet_main = vnet_get_main ();
86 
87  if ((error = vlib_call_init_function (vm, dpdk_init)))
88  return error;
89 
90  /* register custom delay function */
91  rte_delay_us_callback_register (rte_delay_us_override_cb);
92 
93  return error;
94 }
95 
97 
98 
101 {
102  int fd = -1;
103  u64 *pt = 0;
104  clib_error_t *err = 0;
105  clib_mem_vm_alloc_t alloc = { 0 };
106 
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);
111  if (pt && pt[0])
112  goto check_hugetlb;
113 
114  if ((fd = open ("/dev/vfio/vfio", O_RDWR)) == -1)
115  goto error;
116 
117  if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
118  goto error;
119 
120  /* if we have type 1 IOMMU page map is not needed */
121  if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) != 1)
122  goto error;
123 
124 check_hugetlb:
126  alloc.size = 1;
127 
128  if ((err = clib_mem_vm_ext_alloc (&alloc)))
129  {
130  clib_error_free (err);
131  goto error;
132  }
133  else
134  clib_mem_vm_free (alloc.addr, 1 << alloc.log2_page_size);
135 
136  goto done;
137 
138 error:
139  err = clib_error_return (0, "access to physical devices is not allowed");
140 
141 done:
142  if (fd != -1)
143  close (fd);
144  vec_free (pt);
145  return err;
146 }
147 
148 /* *INDENT-OFF* */
150  .version = VPP_BUILD_VER,
151  .description = "Data Plane Development Kit (DPDK)",
152  .early_init = "dpdk_early_init",
153 };
154 /* *INDENT-ON* */
#define CLIB_MEM_VM_F_HUGETLB
Definition: mem.h:327
vlib_node_runtime_t node_runtime
Definition: node.h:531
dpdk_main_t dpdk_main
Definition: init.c:42
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
clib_error_t * dpdk_early_init(vlib_main_t *vm)
Definition: main.c:100
static clib_error_t * dpdk_main_init(vlib_main_t *vm)
Definition: main.c:79
unsigned long u64
Definition: types.h:89
void * addr
Pointer to allocated memory, set on successful allocation.
Definition: mem.h:346
VLIB_PLUGIN_REGISTER()
static uword min_log2(uword x)
Definition: clib.h:138
double f64
Definition: types.h:142
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:448
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
Definition: mem.c:80
vlib_node_function_t * function
Node function to call.
Definition: node.h:449
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
#define clib_error_return(e, args...)
Definition: error.h:99
uword size
Allocation size, set by caller.
Definition: mem.h:344
#define vlib_call_init_function(vm, x)
Definition: init.h:227
#define VLIB_PROCESS_IS_RUNNING
Definition: node.h:551
static clib_error_t * dpdk_init(vlib_main_t *vm)
Definition: init.c:1729
static void rte_delay_us_override_cb(unsigned us)
Definition: main.c:73
#define CLIB_MEM_VM_F_SHARED
Definition: mem.h:326
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:417
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
u32 flags
vm allocation flags: CLIB_MEM_VM_F_SHARED: request shared memory, file descriptor will be provided ...
Definition: mem.h:332
#define CLIB_MEM_VM_F_HUGETLB_PREALLOC
Definition: mem.h:330
static int rte_delay_us_override(unsigned us)
Definition: main.c:35
uword admin_up_down_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: device.c:700
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static void clib_mem_vm_free(void *addr, uword size)
Definition: mem.h:289
u64 * clib_mem_vm_get_paddr(void *mem, int log2_page_size, int n_pages)
Definition: mem.c:256
#define clib_error_free(e)
Definition: error.h:86
u16 flags
Definition: node.h:544
static uword vlib_in_process_context(vlib_main_t *vm)
Definition: node_funcs.h:411
int log2_page_size
Definition: mem.h:348
vnet_main_t * vnet_main
Definition: dpdk.h:439
vlib_main_t * vlib_main
Definition: dpdk.h:438