1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Red Hat Inc.
11 #include <rte_memory.h>
14 #include "virtio_user_dev.h"
16 /* vhost kernel & vdpa ioctls */
17 #define VHOST_VIRTIO 0xAF
18 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
19 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
20 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
21 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
22 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, void *)
23 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
24 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
25 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
26 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
27 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
28 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
29 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
30 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
31 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
32 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
33 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
34 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
35 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
36 #define VHOST_VDPA_SET_VRING_ENABLE _IOW(VHOST_VIRTIO, 0x75, \
37 struct vhost_vring_state)
38 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
39 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
41 static uint64_t vhost_req_user_to_vdpa[] = {
42 [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
43 [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
44 [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
45 [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
46 [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
47 [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
48 [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
49 [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
50 [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
51 [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
52 [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
53 [VHOST_USER_GET_PROTOCOL_FEATURES] = VHOST_GET_BACKEND_FEATURES,
54 [VHOST_USER_SET_PROTOCOL_FEATURES] = VHOST_SET_BACKEND_FEATURES,
57 /* no alignment requirement */
58 struct vhost_iotlb_msg {
62 #define VHOST_ACCESS_RO 0x1
63 #define VHOST_ACCESS_WO 0x2
64 #define VHOST_ACCESS_RW 0x3
66 #define VHOST_IOTLB_MISS 1
67 #define VHOST_IOTLB_UPDATE 2
68 #define VHOST_IOTLB_INVALIDATE 3
69 #define VHOST_IOTLB_ACCESS_FAIL 4
70 #define VHOST_IOTLB_BATCH_BEGIN 5
71 #define VHOST_IOTLB_BATCH_END 6
75 #define VHOST_IOTLB_MSG_V2 0x2
81 struct vhost_iotlb_msg iotlb;
88 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
92 ret = ioctl(fd, request, arg);
94 PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
95 request, strerror(errno));
103 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
105 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
109 vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
113 ret = vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_FEATURES, features);
115 PMD_DRV_LOG(ERR, "Failed to get features");
119 /* Multiqueue not supported for now */
120 *features &= ~(1ULL << VIRTIO_NET_F_MQ);
126 vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
129 features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
131 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_FEATURES, &features);
135 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
137 struct vhost_msg msg = {};
139 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
142 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
143 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
147 msg.type = VHOST_IOTLB_MSG_V2;
148 msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
150 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
151 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
160 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
162 struct vhost_msg msg = {};
164 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
167 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
168 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
172 msg.type = VHOST_IOTLB_MSG_V2;
173 msg.iotlb.type = VHOST_IOTLB_BATCH_END;
175 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
176 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
185 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
186 uint64_t iova, size_t len)
188 struct vhost_msg msg = {};
190 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
191 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
195 msg.type = VHOST_IOTLB_MSG_V2;
196 msg.iotlb.type = VHOST_IOTLB_UPDATE;
197 msg.iotlb.iova = iova;
198 msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
199 msg.iotlb.size = len;
200 msg.iotlb.perm = VHOST_ACCESS_RW;
202 PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
203 __func__, iova, addr, len);
205 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
206 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
215 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
216 uint64_t iova, size_t len)
218 struct vhost_msg msg = {};
220 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
221 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
225 msg.type = VHOST_IOTLB_MSG_V2;
226 msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
227 msg.iotlb.iova = iova;
228 msg.iotlb.size = len;
230 PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
231 __func__, iova, len);
233 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
234 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
243 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
244 uint64_t iova, size_t len)
248 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
251 ret = vhost_vdpa_dma_map(dev, addr, iova, len);
253 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
260 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
261 uint64_t iova, size_t len)
265 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
268 ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
270 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
277 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
278 const struct rte_memseg *ms, size_t len, void *arg)
280 struct virtio_user_dev *dev = arg;
285 return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
289 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
292 struct virtio_user_dev *dev = arg;
294 /* skip external memory that isn't a heap */
295 if (msl->external && !msl->heap)
298 /* skip any segments with invalid IOVA addresses */
299 if (ms->iova == RTE_BAD_IOVA)
302 /* if IOVA mode is VA, we've already mapped the internal segments */
303 if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
306 return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
310 vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
314 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
317 vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
319 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
320 /* with IOVA as VA mode, we can get away with mapping contiguous
321 * chunks rather than going page-by-page.
323 ret = rte_memseg_contig_walk_thread_unsafe(
324 vhost_vdpa_map_contig, dev);
327 /* we have to continue the walk because we've skipped the
328 * external segments during the config walk.
331 ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
334 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
340 /* with below features, vhost vdpa does not need to do the checksum and TSO,
341 * these info will be passed to virtio_user through virtio net header.
343 #define VHOST_VDPA_GUEST_OFFLOADS_MASK \
344 ((1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
345 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
346 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
347 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
348 (1ULL << VIRTIO_NET_F_GUEST_UFO))
350 #define VHOST_VDPA_HOST_OFFLOADS_MASK \
351 ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
352 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
353 (1ULL << VIRTIO_NET_F_CSUM))
356 vhost_vdpa_send_request(struct virtio_user_dev *dev,
357 enum vhost_user_request req,
363 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
365 req_vdpa = vhost_req_user_to_vdpa[req];
367 if (req_vdpa == VHOST_SET_MEM_TABLE)
368 return vhost_vdpa_dma_map_all(dev);
371 case VHOST_SET_VRING_NUM:
372 case VHOST_SET_VRING_ADDR:
373 case VHOST_SET_VRING_BASE:
374 case VHOST_GET_VRING_BASE:
375 case VHOST_SET_VRING_KICK:
376 case VHOST_SET_VRING_CALL:
377 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
378 dev->vhostfd, *(unsigned int *)arg);
384 ret = ioctl(dev->vhostfd, req_vdpa, arg);
386 PMD_DRV_LOG(ERR, "%s failed: %s",
387 vhost_msg_strings[req], strerror(errno));
393 * Set up environment to talk with a vhost vdpa backend.
396 * - (-1) if fail to set up;
397 * - (>=0) if successful.
400 vhost_vdpa_setup(struct virtio_user_dev *dev)
402 uint32_t did = (uint32_t)-1;
404 dev->vhostfd = open(dev->path, O_RDWR);
405 if (dev->vhostfd < 0) {
406 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
407 dev->path, strerror(errno));
411 if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
412 did != VIRTIO_ID_NETWORK) {
413 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
421 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
427 if (dev->qp_enabled[pair_idx] == enable)
430 for (i = 0; i < 2; ++i) {
431 struct vhost_vring_state state = {
432 .index = pair_idx * 2 + i,
436 if (vhost_vdpa_send_request(dev, VHOST_USER_SET_VRING_ENABLE, &state))
440 dev->qp_enabled[pair_idx] = enable;
445 struct virtio_user_backend_ops virtio_ops_vdpa = {
446 .setup = vhost_vdpa_setup,
447 .set_owner = vhost_vdpa_set_owner,
448 .get_features = vhost_vdpa_get_features,
449 .set_features = vhost_vdpa_set_features,
450 .send_request = vhost_vdpa_send_request,
451 .enable_qp = vhost_vdpa_enable_queue_pair,
452 .dma_map = vhost_vdpa_dma_map_batch,
453 .dma_unmap = vhost_vdpa_dma_unmap_batch,