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