d9bc213e0dea6488b866b906937a06dff65af2d3
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Red Hat Inc.
3  */
4
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10
11 #include <rte_memory.h>
12
13 #include "vhost.h"
14 #include "virtio_user_dev.h"
15
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)
40
41 static uint64_t vhost_req_user_to_vdpa[] = {
42         [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
43         [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
44         [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
45         [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
46         [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
47         [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
48         [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
49         [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
50         [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
51         [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
52         [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
53         [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
54         [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
55         [VHOST_USER_GET_PROTOCOL_FEATURES] = VHOST_GET_BACKEND_FEATURES,
56         [VHOST_USER_SET_PROTOCOL_FEATURES] = VHOST_SET_BACKEND_FEATURES,
57 };
58
59 /* no alignment requirement */
60 struct vhost_iotlb_msg {
61         uint64_t iova;
62         uint64_t size;
63         uint64_t uaddr;
64 #define VHOST_ACCESS_RO      0x1
65 #define VHOST_ACCESS_WO      0x2
66 #define VHOST_ACCESS_RW      0x3
67         uint8_t perm;
68 #define VHOST_IOTLB_MISS           1
69 #define VHOST_IOTLB_UPDATE         2
70 #define VHOST_IOTLB_INVALIDATE     3
71 #define VHOST_IOTLB_ACCESS_FAIL    4
72 #define VHOST_IOTLB_BATCH_BEGIN    5
73 #define VHOST_IOTLB_BATCH_END      6
74         uint8_t type;
75 };
76
77 #define VHOST_IOTLB_MSG_V2 0x2
78
79 struct vhost_msg {
80         uint32_t type;
81         uint32_t reserved;
82         union {
83                 struct vhost_iotlb_msg iotlb;
84                 uint8_t padding[64];
85         };
86 };
87
88
89 static int
90 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
91 {
92         int ret;
93
94         ret = ioctl(fd, request, arg);
95         if (ret) {
96                 PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
97                                 request, strerror(errno));
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 static int
105 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
106 {
107         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
108 }
109
110 static int
111 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
112 {
113         struct vhost_msg msg = {};
114
115         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
116                 return 0;
117
118         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
119                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
120                 return -1;
121         }
122
123         msg.type = VHOST_IOTLB_MSG_V2;
124         msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
125
126         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
127                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
128                                 strerror(errno));
129                 return -1;
130         }
131
132         return 0;
133 }
134
135 static int
136 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
137 {
138         struct vhost_msg msg = {};
139
140         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
141                 return 0;
142
143         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
144                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
145                 return -1;
146         }
147
148         msg.type = VHOST_IOTLB_MSG_V2;
149         msg.iotlb.type = VHOST_IOTLB_BATCH_END;
150
151         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
152                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
153                                 strerror(errno));
154                 return -1;
155         }
156
157         return 0;
158 }
159
160 static int
161 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
162                                   uint64_t iova, size_t len)
163 {
164         struct vhost_msg msg = {};
165
166         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
167                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
168                 return -1;
169         }
170
171         msg.type = VHOST_IOTLB_MSG_V2;
172         msg.iotlb.type = VHOST_IOTLB_UPDATE;
173         msg.iotlb.iova = iova;
174         msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
175         msg.iotlb.size = len;
176         msg.iotlb.perm = VHOST_ACCESS_RW;
177
178         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
179                         __func__, iova, addr, len);
180
181         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
182                 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
183                                 strerror(errno));
184                 return -1;
185         }
186
187         return 0;
188 }
189
190 static int
191 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
192                                   uint64_t iova, size_t len)
193 {
194         struct vhost_msg msg = {};
195
196         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
197                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
198                 return -1;
199         }
200
201         msg.type = VHOST_IOTLB_MSG_V2;
202         msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
203         msg.iotlb.iova = iova;
204         msg.iotlb.size = len;
205
206         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
207                         __func__, iova, len);
208
209         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
210                 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
211                                 strerror(errno));
212                 return -1;
213         }
214
215         return 0;
216 }
217
218 static int
219 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
220                                   uint64_t iova, size_t len)
221 {
222         int ret;
223
224         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
225                 return -1;
226
227         ret = vhost_vdpa_dma_map(dev, addr, iova, len);
228
229         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
230                 return -1;
231
232         return ret;
233 }
234
235 static int
236 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
237                                   uint64_t iova, size_t len)
238 {
239         int ret;
240
241         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
242                 return -1;
243
244         ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
245
246         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
247                 return -1;
248
249         return ret;
250 }
251
252 static int
253 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
254                 const struct rte_memseg *ms, size_t len, void *arg)
255 {
256         struct virtio_user_dev *dev = arg;
257
258         if (msl->external)
259                 return 0;
260
261         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
262 }
263
264 static int
265 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
266                 void *arg)
267 {
268         struct virtio_user_dev *dev = arg;
269
270         /* skip external memory that isn't a heap */
271         if (msl->external && !msl->heap)
272                 return 0;
273
274         /* skip any segments with invalid IOVA addresses */
275         if (ms->iova == RTE_BAD_IOVA)
276                 return 0;
277
278         /* if IOVA mode is VA, we've already mapped the internal segments */
279         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
280                 return 0;
281
282         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
283 }
284
285 static int
286 vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
287 {
288         int ret;
289
290         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
291                 return -1;
292
293         vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
294
295         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
296                 /* with IOVA as VA mode, we can get away with mapping contiguous
297                  * chunks rather than going page-by-page.
298                  */
299                 ret = rte_memseg_contig_walk_thread_unsafe(
300                                 vhost_vdpa_map_contig, dev);
301                 if (ret)
302                         goto batch_end;
303                 /* we have to continue the walk because we've skipped the
304                  * external segments during the config walk.
305                  */
306         }
307         ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
308
309 batch_end:
310         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
311                 return -1;
312
313         return ret;
314 }
315
316 /* with below features, vhost vdpa does not need to do the checksum and TSO,
317  * these info will be passed to virtio_user through virtio net header.
318  */
319 #define VHOST_VDPA_GUEST_OFFLOADS_MASK  \
320         ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
321          (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
322          (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
323          (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
324          (1ULL << VIRTIO_NET_F_GUEST_UFO))
325
326 #define VHOST_VDPA_HOST_OFFLOADS_MASK           \
327         ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
328          (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
329          (1ULL << VIRTIO_NET_F_CSUM))
330
331 static int
332 vhost_vdpa_send_request(struct virtio_user_dev *dev,
333                    enum vhost_user_request req,
334                    void *arg)
335 {
336         int ret = -1;
337         uint64_t req_vdpa;
338
339         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
340
341         req_vdpa = vhost_req_user_to_vdpa[req];
342
343         if (req_vdpa == VHOST_SET_MEM_TABLE)
344                 return vhost_vdpa_dma_map_all(dev);
345
346         if (req_vdpa == VHOST_SET_FEATURES) {
347                 /* WORKAROUND */
348                 *(uint64_t *)arg |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
349
350                 /* Multiqueue not supported for now */
351                 *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
352         }
353
354         switch (req_vdpa) {
355         case VHOST_SET_VRING_NUM:
356         case VHOST_SET_VRING_ADDR:
357         case VHOST_SET_VRING_BASE:
358         case VHOST_GET_VRING_BASE:
359         case VHOST_SET_VRING_KICK:
360         case VHOST_SET_VRING_CALL:
361                 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
362                             dev->vhostfd, *(unsigned int *)arg);
363                 break;
364         default:
365                 break;
366         }
367
368         ret = ioctl(dev->vhostfd, req_vdpa, arg);
369         if (ret < 0)
370                 PMD_DRV_LOG(ERR, "%s failed: %s",
371                             vhost_msg_strings[req], strerror(errno));
372
373         return ret;
374 }
375
376 /**
377  * Set up environment to talk with a vhost vdpa backend.
378  *
379  * @return
380  *   - (-1) if fail to set up;
381  *   - (>=0) if successful.
382  */
383 static int
384 vhost_vdpa_setup(struct virtio_user_dev *dev)
385 {
386         uint32_t did = (uint32_t)-1;
387
388         dev->vhostfd = open(dev->path, O_RDWR);
389         if (dev->vhostfd < 0) {
390                 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
391                                 dev->path, strerror(errno));
392                 return -1;
393         }
394
395         if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
396                         did != VIRTIO_ID_NETWORK) {
397                 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
398                 return -1;
399         }
400
401         return 0;
402 }
403
404 static int
405 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
406                                uint16_t pair_idx,
407                                int enable)
408 {
409         int i;
410
411         if (dev->qp_enabled[pair_idx] == enable)
412                 return 0;
413
414         for (i = 0; i < 2; ++i) {
415                 struct vhost_vring_state state = {
416                         .index = pair_idx * 2 + i,
417                         .num   = enable,
418                 };
419
420                 if (vhost_vdpa_send_request(dev, VHOST_USER_SET_VRING_ENABLE, &state))
421                         return -1;
422         }
423
424         dev->qp_enabled[pair_idx] = enable;
425
426         return 0;
427 }
428
429 struct virtio_user_backend_ops virtio_ops_vdpa = {
430         .setup = vhost_vdpa_setup,
431         .set_owner = vhost_vdpa_set_owner,
432         .send_request = vhost_vdpa_send_request,
433         .enable_qp = vhost_vdpa_enable_queue_pair,
434         .dma_map = vhost_vdpa_dma_map_batch,
435         .dma_unmap = vhost_vdpa_dma_unmap_batch,
436 };