vhost: support interrupt mode
[dpdk.git] / lib / librte_vhost / vhost.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
11 #include <numaif.h>
12 #endif
13
14 #include <rte_errno.h>
15 #include <rte_ethdev.h>
16 #include <rte_log.h>
17 #include <rte_string_fns.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_vhost.h>
21 #include <rte_rwlock.h>
22
23 #include "iotlb.h"
24 #include "vhost.h"
25 #include "vhost_user.h"
26
27 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
28
29 /* Called with iotlb_lock read-locked */
30 uint64_t
31 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
32                     uint64_t iova, uint64_t size, uint8_t perm)
33 {
34         uint64_t vva, tmp_size;
35
36         if (unlikely(!size))
37                 return 0;
38
39         tmp_size = size;
40
41         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
42         if (tmp_size == size)
43                 return vva;
44
45         iova += tmp_size;
46
47         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
48                 /*
49                  * iotlb_lock is read-locked for a full burst,
50                  * but it only protects the iotlb cache.
51                  * In case of IOTLB miss, we might block on the socket,
52                  * which could cause a deadlock with QEMU if an IOTLB update
53                  * is being handled. We can safely unlock here to avoid it.
54                  */
55                 vhost_user_iotlb_rd_unlock(vq);
56
57                 vhost_user_iotlb_pending_insert(vq, iova, perm);
58                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
59                         RTE_LOG(ERR, VHOST_CONFIG,
60                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
61                                 iova);
62                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
63                 }
64
65                 vhost_user_iotlb_rd_lock(vq);
66         }
67
68         return 0;
69 }
70
71 void
72 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
73 {
74         if ((vq->callfd >= 0) && (destroy != 0))
75                 close(vq->callfd);
76         if (vq->kickfd >= 0)
77                 close(vq->kickfd);
78 }
79
80 /*
81  * Unmap any memory, close any file descriptors and
82  * free any memory owned by a device.
83  */
84 void
85 cleanup_device(struct virtio_net *dev, int destroy)
86 {
87         uint32_t i;
88
89         vhost_backend_cleanup(dev);
90
91         for (i = 0; i < dev->nr_vring; i++)
92                 cleanup_vq(dev->virtqueue[i], destroy);
93 }
94
95 void
96 free_vq(struct vhost_virtqueue *vq)
97 {
98         rte_free(vq->shadow_used_ring);
99         rte_free(vq->batch_copy_elems);
100         rte_mempool_free(vq->iotlb_pool);
101         rte_free(vq);
102 }
103
104 /*
105  * Release virtqueues and device memory.
106  */
107 static void
108 free_device(struct virtio_net *dev)
109 {
110         uint32_t i;
111
112         for (i = 0; i < dev->nr_vring; i++)
113                 free_vq(dev->virtqueue[i]);
114
115         rte_free(dev);
116 }
117
118 int
119 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
120 {
121         uint64_t size;
122
123         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
124                 goto out;
125
126         size = sizeof(struct vring_desc) * vq->size;
127         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
128                                                 vq->ring_addrs.desc_user_addr,
129                                                 size, VHOST_ACCESS_RW);
130         if (!vq->desc)
131                 return -1;
132
133         size = sizeof(struct vring_avail);
134         size += sizeof(uint16_t) * vq->size;
135         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
136                                                 vq->ring_addrs.avail_user_addr,
137                                                 size, VHOST_ACCESS_RW);
138         if (!vq->avail)
139                 return -1;
140
141         size = sizeof(struct vring_used);
142         size += sizeof(struct vring_used_elem) * vq->size;
143         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
144                                                 vq->ring_addrs.used_user_addr,
145                                                 size, VHOST_ACCESS_RW);
146         if (!vq->used)
147                 return -1;
148
149 out:
150         vq->access_ok = 1;
151
152         return 0;
153 }
154
155 void
156 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
157 {
158         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
159                 vhost_user_iotlb_wr_lock(vq);
160
161         vq->access_ok = 0;
162         vq->desc = NULL;
163         vq->avail = NULL;
164         vq->used = NULL;
165
166         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
167                 vhost_user_iotlb_wr_unlock(vq);
168 }
169
170 static void
171 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
172 {
173         struct vhost_virtqueue *vq;
174
175         if (vring_idx >= VHOST_MAX_VRING) {
176                 RTE_LOG(ERR, VHOST_CONFIG,
177                                 "Failed not init vring, out of bound (%d)\n",
178                                 vring_idx);
179                 return;
180         }
181
182         vq = dev->virtqueue[vring_idx];
183
184         memset(vq, 0, sizeof(struct vhost_virtqueue));
185
186         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
187         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
188
189         vhost_user_iotlb_init(dev, vring_idx);
190         /* Backends are set to -1 indicating an inactive device. */
191         vq->backend = -1;
192
193         TAILQ_INIT(&vq->zmbuf_list);
194 }
195
196 static void
197 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
198 {
199         struct vhost_virtqueue *vq;
200         int callfd;
201
202         if (vring_idx >= VHOST_MAX_VRING) {
203                 RTE_LOG(ERR, VHOST_CONFIG,
204                                 "Failed not init vring, out of bound (%d)\n",
205                                 vring_idx);
206                 return;
207         }
208
209         vq = dev->virtqueue[vring_idx];
210         callfd = vq->callfd;
211         init_vring_queue(dev, vring_idx);
212         vq->callfd = callfd;
213 }
214
215 int
216 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
217 {
218         struct vhost_virtqueue *vq;
219
220         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
221         if (vq == NULL) {
222                 RTE_LOG(ERR, VHOST_CONFIG,
223                         "Failed to allocate memory for vring:%u.\n", vring_idx);
224                 return -1;
225         }
226
227         dev->virtqueue[vring_idx] = vq;
228         init_vring_queue(dev, vring_idx);
229         rte_spinlock_init(&vq->access_lock);
230
231         dev->nr_vring += 1;
232
233         return 0;
234 }
235
236 /*
237  * Reset some variables in device structure, while keeping few
238  * others untouched, such as vid, ifname, nr_vring: they
239  * should be same unless the device is removed.
240  */
241 void
242 reset_device(struct virtio_net *dev)
243 {
244         uint32_t i;
245
246         dev->features = 0;
247         dev->protocol_features = 0;
248         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
249
250         for (i = 0; i < dev->nr_vring; i++)
251                 reset_vring_queue(dev, i);
252 }
253
254 /*
255  * Invoked when there is a new vhost-user connection established (when
256  * there is a new virtio device being attached).
257  */
258 int
259 vhost_new_device(void)
260 {
261         struct virtio_net *dev;
262         int i;
263
264         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
265         if (dev == NULL) {
266                 RTE_LOG(ERR, VHOST_CONFIG,
267                         "Failed to allocate memory for new dev.\n");
268                 return -1;
269         }
270
271         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
272                 if (vhost_devices[i] == NULL)
273                         break;
274         }
275         if (i == MAX_VHOST_DEVICE) {
276                 RTE_LOG(ERR, VHOST_CONFIG,
277                         "Failed to find a free slot for new device.\n");
278                 rte_free(dev);
279                 return -1;
280         }
281
282         vhost_devices[i] = dev;
283         dev->vid = i;
284         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
285         dev->slave_req_fd = -1;
286         dev->vdpa_dev_id = -1;
287
288         return i;
289 }
290
291 /*
292  * Invoked when there is the vhost-user connection is broken (when
293  * the virtio device is being detached).
294  */
295 void
296 vhost_destroy_device(int vid)
297 {
298         struct virtio_net *dev = get_device(vid);
299         struct rte_vdpa_device *vdpa_dev;
300         int did = -1;
301
302         if (dev == NULL)
303                 return;
304
305         if (dev->flags & VIRTIO_DEV_RUNNING) {
306                 did = dev->vdpa_dev_id;
307                 vdpa_dev = rte_vdpa_get_device(did);
308                 if (vdpa_dev && vdpa_dev->ops->dev_close)
309                         vdpa_dev->ops->dev_close(dev->vid);
310                 dev->flags &= ~VIRTIO_DEV_RUNNING;
311                 dev->notify_ops->destroy_device(vid);
312         }
313
314         cleanup_device(dev, 1);
315         free_device(dev);
316
317         vhost_devices[vid] = NULL;
318 }
319
320 void
321 vhost_attach_vdpa_device(int vid, int did)
322 {
323         struct virtio_net *dev = get_device(vid);
324
325         if (dev == NULL)
326                 return;
327
328         if (rte_vdpa_get_device(did) == NULL)
329                 return;
330
331         dev->vdpa_dev_id = did;
332 }
333
334 void
335 vhost_detach_vdpa_device(int vid)
336 {
337         struct virtio_net *dev = get_device(vid);
338
339         if (dev == NULL)
340                 return;
341
342         dev->vdpa_dev_id = -1;
343 }
344
345 void
346 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
347 {
348         struct virtio_net *dev;
349         unsigned int len;
350
351         dev = get_device(vid);
352         if (dev == NULL)
353                 return;
354
355         len = if_len > sizeof(dev->ifname) ?
356                 sizeof(dev->ifname) : if_len;
357
358         strncpy(dev->ifname, if_name, len);
359         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
360 }
361
362 void
363 vhost_enable_dequeue_zero_copy(int vid)
364 {
365         struct virtio_net *dev = get_device(vid);
366
367         if (dev == NULL)
368                 return;
369
370         dev->dequeue_zero_copy = 1;
371 }
372
373 void
374 vhost_set_builtin_virtio_net(int vid, bool enable)
375 {
376         struct virtio_net *dev = get_device(vid);
377
378         if (dev == NULL)
379                 return;
380
381         if (enable)
382                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
383         else
384                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
385 }
386
387 int
388 rte_vhost_get_mtu(int vid, uint16_t *mtu)
389 {
390         struct virtio_net *dev = get_device(vid);
391
392         if (!dev)
393                 return -ENODEV;
394
395         if (!(dev->flags & VIRTIO_DEV_READY))
396                 return -EAGAIN;
397
398         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
399                 return -ENOTSUP;
400
401         *mtu = dev->mtu;
402
403         return 0;
404 }
405
406 int
407 rte_vhost_get_numa_node(int vid)
408 {
409 #ifdef RTE_LIBRTE_VHOST_NUMA
410         struct virtio_net *dev = get_device(vid);
411         int numa_node;
412         int ret;
413
414         if (dev == NULL)
415                 return -1;
416
417         ret = get_mempolicy(&numa_node, NULL, 0, dev,
418                             MPOL_F_NODE | MPOL_F_ADDR);
419         if (ret < 0) {
420                 RTE_LOG(ERR, VHOST_CONFIG,
421                         "(%d) failed to query numa node: %s\n",
422                         vid, rte_strerror(errno));
423                 return -1;
424         }
425
426         return numa_node;
427 #else
428         RTE_SET_USED(vid);
429         return -1;
430 #endif
431 }
432
433 uint32_t
434 rte_vhost_get_queue_num(int vid)
435 {
436         struct virtio_net *dev = get_device(vid);
437
438         if (dev == NULL)
439                 return 0;
440
441         return dev->nr_vring / 2;
442 }
443
444 uint16_t
445 rte_vhost_get_vring_num(int vid)
446 {
447         struct virtio_net *dev = get_device(vid);
448
449         if (dev == NULL)
450                 return 0;
451
452         return dev->nr_vring;
453 }
454
455 int
456 rte_vhost_get_ifname(int vid, char *buf, size_t len)
457 {
458         struct virtio_net *dev = get_device(vid);
459
460         if (dev == NULL)
461                 return -1;
462
463         len = RTE_MIN(len, sizeof(dev->ifname));
464
465         strncpy(buf, dev->ifname, len);
466         buf[len - 1] = '\0';
467
468         return 0;
469 }
470
471 int
472 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
473 {
474         struct virtio_net *dev;
475
476         dev = get_device(vid);
477         if (!dev)
478                 return -1;
479
480         *features = dev->features;
481         return 0;
482 }
483
484 int
485 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
486 {
487         struct virtio_net *dev;
488         struct rte_vhost_memory *m;
489         size_t size;
490
491         dev = get_device(vid);
492         if (!dev)
493                 return -1;
494
495         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
496         m = malloc(sizeof(struct rte_vhost_memory) + size);
497         if (!m)
498                 return -1;
499
500         m->nregions = dev->mem->nregions;
501         memcpy(m->regions, dev->mem->regions, size);
502         *mem = m;
503
504         return 0;
505 }
506
507 int
508 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
509                           struct rte_vhost_vring *vring)
510 {
511         struct virtio_net *dev;
512         struct vhost_virtqueue *vq;
513
514         dev = get_device(vid);
515         if (!dev)
516                 return -1;
517
518         if (vring_idx >= VHOST_MAX_VRING)
519                 return -1;
520
521         vq = dev->virtqueue[vring_idx];
522         if (!vq)
523                 return -1;
524
525         vring->desc  = vq->desc;
526         vring->avail = vq->avail;
527         vring->used  = vq->used;
528         vring->log_guest_addr  = vq->log_guest_addr;
529
530         vring->callfd  = vq->callfd;
531         vring->kickfd  = vq->kickfd;
532         vring->size    = vq->size;
533
534         return 0;
535 }
536
537 int
538 rte_vhost_vring_call(int vid, uint16_t vring_idx)
539 {
540         struct virtio_net *dev;
541         struct vhost_virtqueue *vq;
542
543         dev = get_device(vid);
544         if (!dev)
545                 return -1;
546
547         if (vring_idx >= VHOST_MAX_VRING)
548                 return -1;
549
550         vq = dev->virtqueue[vring_idx];
551         if (!vq)
552                 return -1;
553
554         vhost_vring_call(dev, vq);
555         return 0;
556 }
557
558 uint16_t
559 rte_vhost_avail_entries(int vid, uint16_t queue_id)
560 {
561         struct virtio_net *dev;
562         struct vhost_virtqueue *vq;
563
564         dev = get_device(vid);
565         if (!dev)
566                 return 0;
567
568         vq = dev->virtqueue[queue_id];
569         if (!vq->enabled)
570                 return 0;
571
572         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
573 }
574
575 int
576 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
577 {
578         struct virtio_net *dev = get_device(vid);
579
580         if (!dev)
581                 return -1;
582
583         if (enable)
584                 dev->virtqueue[queue_id]->used->flags &=
585                         ~VRING_USED_F_NO_NOTIFY;
586         else
587                 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
588         return 0;
589 }
590
591 void
592 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
593 {
594         struct virtio_net *dev = get_device(vid);
595
596         if (dev == NULL)
597                 return;
598
599         vhost_log_write(dev, addr, len);
600 }
601
602 void
603 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
604                          uint64_t offset, uint64_t len)
605 {
606         struct virtio_net *dev;
607         struct vhost_virtqueue *vq;
608
609         dev = get_device(vid);
610         if (dev == NULL)
611                 return;
612
613         if (vring_idx >= VHOST_MAX_VRING)
614                 return;
615         vq = dev->virtqueue[vring_idx];
616         if (!vq)
617                 return;
618
619         vhost_log_used_vring(dev, vq, offset, len);
620 }
621
622 uint32_t
623 rte_vhost_rx_queue_count(int vid, uint16_t qid)
624 {
625         struct virtio_net *dev;
626         struct vhost_virtqueue *vq;
627
628         dev = get_device(vid);
629         if (dev == NULL)
630                 return 0;
631
632         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
633                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
634                         dev->vid, __func__, qid);
635                 return 0;
636         }
637
638         vq = dev->virtqueue[qid];
639         if (vq == NULL)
640                 return 0;
641
642         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
643                 return 0;
644
645         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
646 }
647
648 int rte_vhost_get_vdpa_device_id(int vid)
649 {
650         struct virtio_net *dev = get_device(vid);
651
652         if (dev == NULL)
653                 return -1;
654
655         return dev->vdpa_dev_id;
656 }
657
658 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
659                 uint64_t *log_size)
660 {
661         struct virtio_net *dev = get_device(vid);
662
663         if (!dev)
664                 return -1;
665
666         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
667                 RTE_LOG(ERR, VHOST_DATA,
668                         "(%d) %s: built-in vhost net backend is disabled.\n",
669                         dev->vid, __func__);
670                 return -1;
671         }
672
673         *log_base = dev->log_base;
674         *log_size = dev->log_size;
675
676         return 0;
677 }
678
679 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
680                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
681 {
682         struct virtio_net *dev = get_device(vid);
683
684         if (!dev)
685                 return -1;
686
687         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
688                 RTE_LOG(ERR, VHOST_DATA,
689                         "(%d) %s: built-in vhost net backend is disabled.\n",
690                         dev->vid, __func__);
691                 return -1;
692         }
693
694         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
695         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
696
697         return 0;
698 }
699
700 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
701                 uint16_t last_avail_idx, uint16_t last_used_idx)
702 {
703         struct virtio_net *dev = get_device(vid);
704
705         if (!dev)
706                 return -1;
707
708         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
709                 RTE_LOG(ERR, VHOST_DATA,
710                         "(%d) %s: built-in vhost net backend is disabled.\n",
711                         dev->vid, __func__);
712                 return -1;
713         }
714
715         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
716         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
717
718         return 0;
719 }