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