1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
10 #include <rte_memory.h>
11 #include <rte_eal_memconfig.h>
14 #include "virtio_user_dev.h"
15 #include "vhost_kernel_tap.h"
17 struct vhost_memory_kernel
{
20 struct vhost_memory_region regions
[0];
23 /* vhost kernel ioctls */
24 #define VHOST_VIRTIO 0xAF
25 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
26 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
27 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
28 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
29 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
30 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
31 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
32 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
33 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
34 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
35 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
36 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
37 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
38 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
39 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
41 static uint64_t max_regions
= 64;
44 get_vhost_kernel_max_regions(void)
47 char buf
[20] = {'\0'};
49 fd
= open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY
);
53 if (read(fd
, buf
, sizeof(buf
) - 1) > 0)
54 max_regions
= strtoull(buf
, NULL
, 10);
59 static uint64_t vhost_req_user_to_kernel
[] = {
60 [VHOST_USER_SET_OWNER
] = VHOST_SET_OWNER
,
61 [VHOST_USER_RESET_OWNER
] = VHOST_RESET_OWNER
,
62 [VHOST_USER_SET_FEATURES
] = VHOST_SET_FEATURES
,
63 [VHOST_USER_GET_FEATURES
] = VHOST_GET_FEATURES
,
64 [VHOST_USER_SET_VRING_CALL
] = VHOST_SET_VRING_CALL
,
65 [VHOST_USER_SET_VRING_NUM
] = VHOST_SET_VRING_NUM
,
66 [VHOST_USER_SET_VRING_BASE
] = VHOST_SET_VRING_BASE
,
67 [VHOST_USER_GET_VRING_BASE
] = VHOST_GET_VRING_BASE
,
68 [VHOST_USER_SET_VRING_ADDR
] = VHOST_SET_VRING_ADDR
,
69 [VHOST_USER_SET_VRING_KICK
] = VHOST_SET_VRING_KICK
,
70 [VHOST_USER_SET_MEM_TABLE
] = VHOST_SET_MEM_TABLE
,
74 add_memseg_list(const struct rte_memseg_list
*msl
, void *arg
)
76 struct vhost_memory_kernel
*vm
= arg
;
77 struct vhost_memory_region
*mr
;
84 if (vm
->nregions
>= max_regions
)
87 start_addr
= msl
->base_va
;
88 len
= msl
->page_sz
* msl
->memseg_arr
.len
;
90 mr
= &vm
->regions
[vm
->nregions
++];
92 mr
->guest_phys_addr
= (uint64_t)(uintptr_t)start_addr
;
93 mr
->userspace_addr
= (uint64_t)(uintptr_t)start_addr
;
94 mr
->memory_size
= len
;
95 mr
->mmap_offset
= 0; /* flags_padding */
97 PMD_DRV_LOG(DEBUG
, "index=%u addr=%p len=%" PRIu64
,
98 vm
->nregions
- 1, start_addr
, len
);
103 /* By default, vhost kernel module allows 64 regions, but DPDK may
104 * have much more memory regions. Below function will treat each
105 * contiguous memory space reserved by DPDK as one region.
107 static struct vhost_memory_kernel
*
108 prepare_vhost_memory_kernel(void)
110 struct vhost_memory_kernel
*vm
;
112 vm
= malloc(sizeof(struct vhost_memory_kernel
) +
114 sizeof(struct vhost_memory_region
));
122 * The memory lock has already been taken by memory subsystem
123 * or virtio_user_start_device().
125 if (rte_memseg_list_walk_thread_unsafe(add_memseg_list
, vm
) < 0) {
133 /* with below features, vhost kernel does not need to do the checksum and TSO,
134 * these info will be passed to virtio_user through virtio net header.
136 #define VHOST_KERNEL_GUEST_OFFLOADS_MASK \
137 ((1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
138 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
139 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
140 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
141 (1ULL << VIRTIO_NET_F_GUEST_UFO))
143 /* with below features, when flows from virtio_user to vhost kernel
144 * (1) if flows goes up through the kernel networking stack, it does not need
145 * to verify checksum, which can save CPU cycles;
146 * (2) if flows goes through a Linux bridge and outside from an interface
147 * (kernel driver), checksum and TSO will be done by GSO in kernel or even
148 * offloaded into real physical device.
150 #define VHOST_KERNEL_HOST_OFFLOADS_MASK \
151 ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
152 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
153 (1ULL << VIRTIO_NET_F_CSUM))
156 tap_support_features(void)
159 unsigned int tap_features
;
161 tapfd
= open(PATH_NET_TUN
, O_RDWR
);
163 PMD_DRV_LOG(ERR
, "fail to open %s: %s",
164 PATH_NET_TUN
, strerror(errno
));
168 if (ioctl(tapfd
, TUNGETFEATURES
, &tap_features
) == -1) {
169 PMD_DRV_LOG(ERR
, "TUNGETFEATURES failed: %s", strerror(errno
));
179 vhost_kernel_ioctl(struct virtio_user_dev
*dev
,
180 enum vhost_user_request req
,
186 struct vhost_memory_kernel
*vm
= NULL
;
188 unsigned int queue_sel
;
189 unsigned int features
;
191 PMD_DRV_LOG(INFO
, "%s", vhost_msg_strings
[req
]);
193 req_kernel
= vhost_req_user_to_kernel
[req
];
195 if (req_kernel
== VHOST_SET_MEM_TABLE
) {
196 vm
= prepare_vhost_memory_kernel();
202 if (req_kernel
== VHOST_SET_FEATURES
) {
203 /* We don't need memory protection here */
204 *(uint64_t *)arg
&= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM
);
206 /* VHOST kernel does not know about below flags */
207 *(uint64_t *)arg
&= ~VHOST_KERNEL_GUEST_OFFLOADS_MASK
;
208 *(uint64_t *)arg
&= ~VHOST_KERNEL_HOST_OFFLOADS_MASK
;
210 *(uint64_t *)arg
&= ~(1ULL << VIRTIO_NET_F_MQ
);
213 switch (req_kernel
) {
214 case VHOST_SET_VRING_NUM
:
215 case VHOST_SET_VRING_ADDR
:
216 case VHOST_SET_VRING_BASE
:
217 case VHOST_GET_VRING_BASE
:
218 case VHOST_SET_VRING_KICK
:
219 case VHOST_SET_VRING_CALL
:
220 queue_sel
= *(unsigned int *)arg
;
221 vhostfd
= dev
->vhostfds
[queue_sel
/ 2];
222 *(unsigned int *)arg
= queue_sel
% 2;
223 PMD_DRV_LOG(DEBUG
, "vhostfd=%d, index=%u",
224 vhostfd
, *(unsigned int *)arg
);
230 for (i
= 0; i
< dev
->max_queue_pairs
; ++i
) {
231 if (dev
->vhostfds
[i
] < 0)
234 ret
= ioctl(dev
->vhostfds
[i
], req_kernel
, arg
);
239 ret
= ioctl(vhostfd
, req_kernel
, arg
);
242 if (!ret
&& req_kernel
== VHOST_GET_FEATURES
) {
243 features
= tap_support_features();
244 /* with tap as the backend, all these features are supported
245 * but not claimed by vhost-net, so we add them back when
246 * reporting to upper layer.
248 if (features
& IFF_VNET_HDR
) {
249 *((uint64_t *)arg
) |= VHOST_KERNEL_GUEST_OFFLOADS_MASK
;
250 *((uint64_t *)arg
) |= VHOST_KERNEL_HOST_OFFLOADS_MASK
;
253 /* vhost_kernel will not declare this feature, but it does
254 * support multi-queue.
256 if (features
& IFF_MULTI_QUEUE
)
257 *(uint64_t *)arg
|= (1ull << VIRTIO_NET_F_MQ
);
264 PMD_DRV_LOG(ERR
, "%s failed: %s",
265 vhost_msg_strings
[req
], strerror(errno
));
271 * Set up environment to talk with a vhost kernel backend.
274 * - (-1) if fail to set up;
275 * - (>=0) if successful.
278 vhost_kernel_setup(struct virtio_user_dev
*dev
)
283 get_vhost_kernel_max_regions();
285 for (i
= 0; i
< dev
->max_queue_pairs
; ++i
) {
286 vhostfd
= open(dev
->path
, O_RDWR
);
288 PMD_DRV_LOG(ERR
, "fail to open %s, %s",
289 dev
->path
, strerror(errno
));
293 dev
->vhostfds
[i
] = vhostfd
;
300 vhost_kernel_set_backend(int vhostfd
, int tapfd
)
302 struct vhost_vring_file f
;
306 if (ioctl(vhostfd
, VHOST_NET_SET_BACKEND
, &f
) < 0) {
307 PMD_DRV_LOG(ERR
, "VHOST_NET_SET_BACKEND fails, %s",
313 if (ioctl(vhostfd
, VHOST_NET_SET_BACKEND
, &f
) < 0) {
314 PMD_DRV_LOG(ERR
, "VHOST_NET_SET_BACKEND fails, %s",
323 vhost_kernel_enable_queue_pair(struct virtio_user_dev
*dev
,
330 int req_mq
= (dev
->max_queue_pairs
> 1);
332 vhostfd
= dev
->vhostfds
[pair_idx
];
335 if (dev
->tapfds
[pair_idx
] >= 0) {
336 close(dev
->tapfds
[pair_idx
]);
337 dev
->tapfds
[pair_idx
] = -1;
339 return vhost_kernel_set_backend(vhostfd
, -1);
340 } else if (dev
->tapfds
[pair_idx
] >= 0) {
344 if ((dev
->features
& (1ULL << VIRTIO_NET_F_MRG_RXBUF
)) ||
345 (dev
->features
& (1ULL << VIRTIO_F_VERSION_1
)))
346 hdr_size
= sizeof(struct virtio_net_hdr_mrg_rxbuf
);
348 hdr_size
= sizeof(struct virtio_net_hdr
);
350 tapfd
= vhost_kernel_open_tap(&dev
->ifname
, hdr_size
, req_mq
,
351 (char *)dev
->mac_addr
, dev
->features
);
353 PMD_DRV_LOG(ERR
, "fail to open tap for vhost kernel");
357 if (vhost_kernel_set_backend(vhostfd
, tapfd
) < 0) {
358 PMD_DRV_LOG(ERR
, "fail to set backend for vhost kernel");
363 dev
->tapfds
[pair_idx
] = tapfd
;
367 struct virtio_user_backend_ops virtio_ops_kernel
= {
368 .setup
= vhost_kernel_setup
,
369 .send_request
= vhost_kernel_ioctl
,
370 .enable_qp
= vhost_kernel_enable_queue_pair