c7b9349fc8bcc897f24cf70a1feae5e1f10bf299
[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
39 static uint64_t vhost_req_user_to_vdpa[] = {
40         [VHOST_USER_SET_OWNER] = VHOST_SET_OWNER,
41         [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
42         [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
43         [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
44         [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
45         [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
46         [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
47         [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
48         [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
49         [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
50         [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
51         [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
52         [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
53         [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
54 };
55
56 /* no alignment requirement */
57 struct vhost_iotlb_msg {
58         uint64_t iova;
59         uint64_t size;
60         uint64_t uaddr;
61 #define VHOST_ACCESS_RO      0x1
62 #define VHOST_ACCESS_WO      0x2
63 #define VHOST_ACCESS_RW      0x3
64         uint8_t perm;
65 #define VHOST_IOTLB_MISS           1
66 #define VHOST_IOTLB_UPDATE         2
67 #define VHOST_IOTLB_INVALIDATE     3
68 #define VHOST_IOTLB_ACCESS_FAIL    4
69         uint8_t type;
70 };
71
72 #define VHOST_IOTLB_MSG_V2 0x2
73
74 struct vhost_msg {
75         uint32_t type;
76         uint32_t reserved;
77         union {
78                 struct vhost_iotlb_msg iotlb;
79                 uint8_t padding[64];
80         };
81 };
82
83 static int
84 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
85                                   uint64_t iova, size_t len)
86 {
87         struct vhost_msg msg = {};
88
89         msg.type = VHOST_IOTLB_MSG_V2;
90         msg.iotlb.type = VHOST_IOTLB_UPDATE;
91         msg.iotlb.iova = iova;
92         msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
93         msg.iotlb.size = len;
94         msg.iotlb.perm = VHOST_ACCESS_RW;
95
96         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
97                 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
98                                 strerror(errno));
99                 return -1;
100         }
101
102         return 0;
103 }
104
105 static int
106 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
107                                   uint64_t iova, size_t len)
108 {
109         struct vhost_msg msg = {};
110
111         msg.type = VHOST_IOTLB_MSG_V2;
112         msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
113         msg.iotlb.iova = iova;
114         msg.iotlb.size = len;
115
116         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
117                 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
118                                 strerror(errno));
119                 return -1;
120         }
121
122         return 0;
123 }
124
125
126 static int
127 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
128                 const struct rte_memseg *ms, size_t len, void *arg)
129 {
130         struct virtio_user_dev *dev = arg;
131
132         if (msl->external)
133                 return 0;
134
135         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
136 }
137
138 static int
139 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
140                 void *arg)
141 {
142         struct virtio_user_dev *dev = arg;
143
144         /* skip external memory that isn't a heap */
145         if (msl->external && !msl->heap)
146                 return 0;
147
148         /* skip any segments with invalid IOVA addresses */
149         if (ms->iova == RTE_BAD_IOVA)
150                 return 0;
151
152         /* if IOVA mode is VA, we've already mapped the internal segments */
153         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
154                 return 0;
155
156         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
157 }
158
159 static int
160 vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
161 {
162         vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
163
164         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
165                 /* with IOVA as VA mode, we can get away with mapping contiguous
166                  * chunks rather than going page-by-page.
167                  */
168                 int ret = rte_memseg_contig_walk_thread_unsafe(
169                                 vhost_vdpa_map_contig, dev);
170                 if (ret)
171                         return ret;
172                 /* we have to continue the walk because we've skipped the
173                  * external segments during the config walk.
174                  */
175         }
176         return rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
177 }
178
179 /* with below features, vhost vdpa does not need to do the checksum and TSO,
180  * these info will be passed to virtio_user through virtio net header.
181  */
182 #define VHOST_VDPA_GUEST_OFFLOADS_MASK  \
183         ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
184          (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
185          (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
186          (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
187          (1ULL << VIRTIO_NET_F_GUEST_UFO))
188
189 #define VHOST_VDPA_HOST_OFFLOADS_MASK           \
190         ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
191          (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
192          (1ULL << VIRTIO_NET_F_CSUM))
193
194 static int
195 vhost_vdpa_ioctl(struct virtio_user_dev *dev,
196                    enum vhost_user_request req,
197                    void *arg)
198 {
199         int ret = -1;
200         uint64_t req_vdpa;
201
202         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
203
204         req_vdpa = vhost_req_user_to_vdpa[req];
205
206         if (req_vdpa == VHOST_SET_MEM_TABLE)
207                 return vhost_vdpa_dma_map_all(dev);
208
209         if (req_vdpa == VHOST_SET_FEATURES) {
210                 /* WORKAROUND */
211                 *(uint64_t *)arg |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
212
213                 /* Multiqueue not supported for now */
214                 *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
215         }
216
217         switch (req_vdpa) {
218         case VHOST_SET_VRING_NUM:
219         case VHOST_SET_VRING_ADDR:
220         case VHOST_SET_VRING_BASE:
221         case VHOST_GET_VRING_BASE:
222         case VHOST_SET_VRING_KICK:
223         case VHOST_SET_VRING_CALL:
224                 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
225                             dev->vhostfd, *(unsigned int *)arg);
226                 break;
227         default:
228                 break;
229         }
230
231         ret = ioctl(dev->vhostfd, req_vdpa, arg);
232         if (ret < 0)
233                 PMD_DRV_LOG(ERR, "%s failed: %s",
234                             vhost_msg_strings[req], strerror(errno));
235
236         return ret;
237 }
238
239 /**
240  * Set up environment to talk with a vhost vdpa backend.
241  *
242  * @return
243  *   - (-1) if fail to set up;
244  *   - (>=0) if successful.
245  */
246 static int
247 vhost_vdpa_setup(struct virtio_user_dev *dev)
248 {
249         uint32_t did = (uint32_t)-1;
250
251         dev->vhostfd = open(dev->path, O_RDWR);
252         if (dev->vhostfd < 0) {
253                 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
254                                 dev->path, strerror(errno));
255                 return -1;
256         }
257
258         if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
259                         did != VIRTIO_ID_NETWORK) {
260                 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
261                 return -1;
262         }
263
264         return 0;
265 }
266
267 static int
268 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
269                                uint16_t pair_idx,
270                                int enable)
271 {
272         int i;
273
274         if (dev->qp_enabled[pair_idx] == enable)
275                 return 0;
276
277         for (i = 0; i < 2; ++i) {
278                 struct vhost_vring_state state = {
279                         .index = pair_idx * 2 + i,
280                         .num   = enable,
281                 };
282
283                 if (vhost_vdpa_ioctl(dev, VHOST_USER_SET_VRING_ENABLE, &state))
284                         return -1;
285         }
286
287         dev->qp_enabled[pair_idx] = enable;
288
289         return 0;
290 }
291
292 struct virtio_user_backend_ops virtio_ops_vdpa = {
293         .setup = vhost_vdpa_setup,
294         .send_request = vhost_vdpa_ioctl,
295         .enable_qp = vhost_vdpa_enable_queue_pair,
296         .dma_map = vhost_vdpa_dma_map,
297         .dma_unmap = vhost_vdpa_dma_unmap,
298 };