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