net/virtio: add virtio-user vring file ops
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_kernel.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
10
11 #include <rte_memory.h>
12
13 #include "vhost.h"
14 #include "virtio_user_dev.h"
15 #include "vhost_kernel_tap.h"
16
17 struct vhost_memory_kernel {
18         uint32_t nregions;
19         uint32_t padding;
20         struct vhost_memory_region regions[0];
21 };
22
23 /* vhost kernel ioctls */
24 #define VHOST_VIRTIO 0xAF
25 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
26 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
27 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
28 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
29 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
30 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
31 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
32 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
33 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
34 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
35 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
36 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
37 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
38 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
39 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
40
41 /* with below features, vhost kernel does not need to do the checksum and TSO,
42  * these info will be passed to virtio_user through virtio net header.
43  */
44 #define VHOST_KERNEL_GUEST_OFFLOADS_MASK        \
45         ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
46          (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
47          (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
48          (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
49          (1ULL << VIRTIO_NET_F_GUEST_UFO))
50
51 /* with below features, when flows from virtio_user to vhost kernel
52  * (1) if flows goes up through the kernel networking stack, it does not need
53  * to verify checksum, which can save CPU cycles;
54  * (2) if flows goes through a Linux bridge and outside from an interface
55  * (kernel driver), checksum and TSO will be done by GSO in kernel or even
56  * offloaded into real physical device.
57  */
58 #define VHOST_KERNEL_HOST_OFFLOADS_MASK         \
59         ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
60          (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
61          (1ULL << VIRTIO_NET_F_CSUM))
62
63 static uint64_t max_regions = 64;
64
65 static void
66 get_vhost_kernel_max_regions(void)
67 {
68         int fd;
69         char buf[20] = {'\0'};
70
71         fd = open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY);
72         if (fd < 0)
73                 return;
74
75         if (read(fd, buf, sizeof(buf) - 1) > 0)
76                 max_regions = strtoull(buf, NULL, 10);
77
78         close(fd);
79 }
80
81 static int
82 vhost_kernel_ioctl(int fd, uint64_t request, void *arg)
83 {
84         int ret;
85
86         ret = ioctl(fd, request, arg);
87         if (ret) {
88                 PMD_DRV_LOG(ERR, "Vhost-kernel ioctl %"PRIu64" failed (%s)",
89                                 request, strerror(errno));
90                 return -1;
91         }
92
93         return 0;
94 }
95
96 static int
97 vhost_kernel_set_owner(struct virtio_user_dev *dev)
98 {
99         return vhost_kernel_ioctl(dev->vhostfds[0], VHOST_SET_OWNER, NULL);
100 }
101
102 static int
103 vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
104 {
105         int ret;
106         unsigned int tap_features;
107
108         ret = vhost_kernel_ioctl(dev->vhostfds[0], VHOST_GET_FEATURES, features);
109         if (ret < 0) {
110                 PMD_DRV_LOG(ERR, "Failed to get features");
111                 return -1;
112         }
113
114         ret = tap_support_features(&tap_features);
115         if (ret < 0) {
116                 PMD_DRV_LOG(ERR, "Failed to get TAP features");
117                 return -1;
118         }
119
120         /* with tap as the backend, all these features are supported
121          * but not claimed by vhost-net, so we add them back when
122          * reporting to upper layer.
123          */
124         if (tap_features & IFF_VNET_HDR) {
125                 *features |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
126                 *features |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
127         }
128
129         /* vhost_kernel will not declare this feature, but it does
130          * support multi-queue.
131          */
132         if (tap_features & IFF_MULTI_QUEUE)
133                 *features |= (1ull << VIRTIO_NET_F_MQ);
134
135         return 0;
136 }
137
138 static int
139 vhost_kernel_set_features(struct virtio_user_dev *dev, uint64_t features)
140 {
141         /* We don't need memory protection here */
142         features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
143         /* VHOST kernel does not know about below flags */
144         features &= ~VHOST_KERNEL_GUEST_OFFLOADS_MASK;
145         features &= ~VHOST_KERNEL_HOST_OFFLOADS_MASK;
146         features &= ~(1ULL << VIRTIO_NET_F_MQ);
147
148         return vhost_kernel_ioctl(dev->vhostfds[0], VHOST_SET_FEATURES, &features);
149 }
150
151 static int
152 add_memseg_list(const struct rte_memseg_list *msl, void *arg)
153 {
154         struct vhost_memory_kernel *vm = arg;
155         struct vhost_memory_region *mr;
156         void *start_addr;
157         uint64_t len;
158
159         if (msl->external)
160                 return 0;
161
162         if (vm->nregions >= max_regions)
163                 return -1;
164
165         start_addr = msl->base_va;
166         len = msl->page_sz * msl->memseg_arr.len;
167
168         mr = &vm->regions[vm->nregions++];
169
170         mr->guest_phys_addr = (uint64_t)(uintptr_t)start_addr;
171         mr->userspace_addr = (uint64_t)(uintptr_t)start_addr;
172         mr->memory_size = len;
173         mr->mmap_offset = 0; /* flags_padding */
174
175         PMD_DRV_LOG(DEBUG, "index=%u addr=%p len=%" PRIu64,
176                         vm->nregions - 1, start_addr, len);
177
178         return 0;
179 }
180
181 /* By default, vhost kernel module allows 64 regions, but DPDK may
182  * have much more memory regions. Below function will treat each
183  * contiguous memory space reserved by DPDK as one region.
184  */
185 static int
186 vhost_kernel_set_memory_table(struct virtio_user_dev *dev)
187 {
188         struct vhost_memory_kernel *vm;
189         int ret;
190
191         vm = malloc(sizeof(struct vhost_memory_kernel) +
192                         max_regions *
193                         sizeof(struct vhost_memory_region));
194         if (!vm)
195                 goto err;
196
197         vm->nregions = 0;
198         vm->padding = 0;
199
200         /*
201          * The memory lock has already been taken by memory subsystem
202          * or virtio_user_start_device().
203          */
204         ret = rte_memseg_list_walk_thread_unsafe(add_memseg_list, vm);
205         if (ret < 0)
206                 goto err_free;
207
208         ret = vhost_kernel_ioctl(dev->vhostfds[0], VHOST_SET_MEM_TABLE, vm);
209         if (ret < 0)
210                 goto err_free;
211
212         free(vm);
213
214         return 0;
215 err_free:
216         free(vm);
217 err:
218         PMD_DRV_LOG(ERR, "Failed to set memory table");
219         return -1;
220 }
221
222 static int
223 vhost_kernel_set_vring(struct virtio_user_dev *dev, uint64_t req, struct vhost_vring_state *state)
224 {
225         int ret, fd;
226         unsigned int index = state->index;
227
228         /* Convert from queue index to queue-pair & offset */
229         fd = dev->vhostfds[state->index / 2];
230         state->index %= 2;
231
232         ret = vhost_kernel_ioctl(fd, req, state);
233         if (ret < 0) {
234                 PMD_DRV_LOG(ERR, "Failed to set vring (request %" PRIu64 ")", req);
235                 return -1;
236         }
237
238         /* restore index back to queue index */
239         state->index = index;
240
241         return 0;
242 }
243
244 static int
245 vhost_kernel_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
246 {
247         return vhost_kernel_set_vring(dev, VHOST_SET_VRING_NUM, state);
248 }
249
250 static int
251 vhost_kernel_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
252 {
253         return vhost_kernel_set_vring(dev, VHOST_SET_VRING_BASE, state);
254 }
255
256 static int
257 vhost_kernel_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
258 {
259         return vhost_kernel_set_vring(dev, VHOST_GET_VRING_BASE, state);
260 }
261
262 static int
263 vhost_kernel_set_vring_file(struct virtio_user_dev *dev, uint64_t req,
264                 struct vhost_vring_file *file)
265 {
266         int ret, fd;
267         unsigned int index = file->index;
268
269         /* Convert from queue index to queue-pair & offset */
270         fd = dev->vhostfds[file->index / 2];
271         file->index %= 2;
272
273         ret = vhost_kernel_ioctl(fd, req, file);
274         if (ret < 0) {
275                 PMD_DRV_LOG(ERR, "Failed to set vring file (request %" PRIu64 ")", req);
276                 return -1;
277         }
278
279         /* restore index back to queue index */
280         file->index = index;
281
282         return 0;
283 }
284
285 static int
286 vhost_kernel_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
287 {
288         return vhost_kernel_set_vring_file(dev, VHOST_SET_VRING_KICK, file);
289 }
290
291 static int
292 vhost_kernel_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
293 {
294         return vhost_kernel_set_vring_file(dev, VHOST_SET_VRING_CALL, file);
295 }
296
297 static uint64_t vhost_req_user_to_kernel[] = {
298         [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
299         [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
300 };
301
302 static int
303 vhost_kernel_send_request(struct virtio_user_dev *dev,
304                    enum vhost_user_request req,
305                    void *arg)
306 {
307         int ret = -1;
308         unsigned int i;
309         uint64_t req_kernel;
310         int vhostfd;
311         unsigned int queue_sel;
312
313         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
314
315         req_kernel = vhost_req_user_to_kernel[req];
316
317         switch (req_kernel) {
318         case VHOST_SET_VRING_ADDR:
319                 queue_sel = *(unsigned int *)arg;
320                 vhostfd = dev->vhostfds[queue_sel / 2];
321                 *(unsigned int *)arg = queue_sel % 2;
322                 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
323                             vhostfd, *(unsigned int *)arg);
324                 break;
325         default:
326                 vhostfd = -1;
327         }
328         if (vhostfd == -1) {
329                 for (i = 0; i < dev->max_queue_pairs; ++i) {
330                         if (dev->vhostfds[i] < 0)
331                                 continue;
332
333                         ret = ioctl(dev->vhostfds[i], req_kernel, arg);
334                         if (ret < 0)
335                                 break;
336                 }
337         } else {
338                 ret = ioctl(vhostfd, req_kernel, arg);
339         }
340
341         if (ret < 0)
342                 PMD_DRV_LOG(ERR, "%s failed: %s",
343                             vhost_msg_strings[req], strerror(errno));
344
345         return ret;
346 }
347
348 /**
349  * Set up environment to talk with a vhost kernel backend.
350  *
351  * @return
352  *   - (-1) if fail to set up;
353  *   - (>=0) if successful.
354  */
355 static int
356 vhost_kernel_setup(struct virtio_user_dev *dev)
357 {
358         int vhostfd;
359         uint32_t i;
360
361         get_vhost_kernel_max_regions();
362
363         for (i = 0; i < dev->max_queue_pairs; ++i) {
364                 vhostfd = open(dev->path, O_RDWR);
365                 if (vhostfd < 0) {
366                         PMD_DRV_LOG(ERR, "fail to open %s, %s",
367                                     dev->path, strerror(errno));
368                         return -1;
369                 }
370
371                 dev->vhostfds[i] = vhostfd;
372         }
373
374         return 0;
375 }
376
377 static int
378 vhost_kernel_set_backend(int vhostfd, int tapfd)
379 {
380         struct vhost_vring_file f;
381
382         f.fd = tapfd;
383         f.index = 0;
384         if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
385                 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
386                                 strerror(errno));
387                 return -1;
388         }
389
390         f.index = 1;
391         if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
392                 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
393                                 strerror(errno));
394                 return -1;
395         }
396
397         return 0;
398 }
399
400 static int
401 vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
402                                uint16_t pair_idx,
403                                int enable)
404 {
405         int hdr_size;
406         int vhostfd;
407         int tapfd;
408         int req_mq = (dev->max_queue_pairs > 1);
409
410         vhostfd = dev->vhostfds[pair_idx];
411
412         if (dev->qp_enabled[pair_idx] == enable)
413                 return 0;
414
415         if (!enable) {
416                 tapfd = dev->tapfds[pair_idx];
417                 if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
418                         PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
419                         return -1;
420                 }
421                 if (req_mq && vhost_kernel_tap_set_queue(tapfd, false) < 0) {
422                         PMD_DRV_LOG(ERR, "fail to disable tap for vhost kernel");
423                         return -1;
424                 }
425                 dev->qp_enabled[pair_idx] = false;
426                 return 0;
427         }
428
429         if (dev->tapfds[pair_idx] >= 0) {
430                 tapfd = dev->tapfds[pair_idx];
431                 if (vhost_kernel_tap_set_offload(tapfd, dev->features) == -1)
432                         return -1;
433                 if (req_mq && vhost_kernel_tap_set_queue(tapfd, true) < 0) {
434                         PMD_DRV_LOG(ERR, "fail to enable tap for vhost kernel");
435                         return -1;
436                 }
437                 goto set_backend;
438         }
439
440         if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
441             (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
442                 hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
443         else
444                 hdr_size = sizeof(struct virtio_net_hdr);
445
446         tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
447                          (char *)dev->mac_addr, dev->features);
448         if (tapfd < 0) {
449                 PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
450                 return -1;
451         }
452
453         dev->tapfds[pair_idx] = tapfd;
454
455 set_backend:
456         if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
457                 PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
458                 return -1;
459         }
460
461         dev->qp_enabled[pair_idx] = true;
462         return 0;
463 }
464
465 struct virtio_user_backend_ops virtio_ops_kernel = {
466         .setup = vhost_kernel_setup,
467         .set_owner = vhost_kernel_set_owner,
468         .get_features = vhost_kernel_get_features,
469         .set_features = vhost_kernel_set_features,
470         .set_memory_table = vhost_kernel_set_memory_table,
471         .set_vring_num = vhost_kernel_set_vring_num,
472         .set_vring_base = vhost_kernel_set_vring_base,
473         .get_vring_base = vhost_kernel_get_vring_base,
474         .set_vring_call = vhost_kernel_set_vring_call,
475         .set_vring_kick = vhost_kernel_set_vring_kick,
476         .send_request = vhost_kernel_send_request,
477         .enable_qp = vhost_kernel_enable_queue_pair
478 };