vhost: improve performance by supporting large buffer
[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 <numa.h>
12 #include <numaif.h>
13 #endif
14
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
17 #include <rte_log.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
23
24 #include "iotlb.h"
25 #include "vhost.h"
26 #include "vhost_user.h"
27
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29
30 /* Called with iotlb_lock read-locked */
31 uint64_t
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33                     uint64_t iova, uint64_t *size, uint8_t perm)
34 {
35         uint64_t vva, tmp_size;
36
37         if (unlikely(!*size))
38                 return 0;
39
40         tmp_size = *size;
41
42         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43         if (tmp_size == *size)
44                 return vva;
45
46         iova += tmp_size;
47
48         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
49                 /*
50                  * iotlb_lock is read-locked for a full burst,
51                  * but it only protects the iotlb cache.
52                  * In case of IOTLB miss, we might block on the socket,
53                  * which could cause a deadlock with QEMU if an IOTLB update
54                  * is being handled. We can safely unlock here to avoid it.
55                  */
56                 vhost_user_iotlb_rd_unlock(vq);
57
58                 vhost_user_iotlb_pending_insert(vq, iova, perm);
59                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
60                         RTE_LOG(ERR, VHOST_CONFIG,
61                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
62                                 iova);
63                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
64                 }
65
66                 vhost_user_iotlb_rd_lock(vq);
67         }
68
69         return 0;
70 }
71
72 #define VHOST_LOG_PAGE  4096
73
74 /*
75  * Atomically set a bit in memory.
76  */
77 static __rte_always_inline void
78 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
79 {
80 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
81         /*
82          * __sync_ built-ins are deprecated, but __atomic_ ones
83          * are sub-optimized in older GCC versions.
84          */
85         __sync_fetch_and_or_1(addr, (1U << nr));
86 #else
87         __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
88 #endif
89 }
90
91 static __rte_always_inline void
92 vhost_log_page(uint8_t *log_base, uint64_t page)
93 {
94         vhost_set_bit(page % 8, &log_base[page / 8]);
95 }
96
97 void
98 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
99 {
100         uint64_t page;
101
102         if (unlikely(!dev->log_base || !len))
103                 return;
104
105         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
106                 return;
107
108         /* To make sure guest memory updates are committed before logging */
109         rte_smp_wmb();
110
111         page = addr / VHOST_LOG_PAGE;
112         while (page * VHOST_LOG_PAGE < addr + len) {
113                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
114                 page += 1;
115         }
116 }
117
118 void
119 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
120                              uint64_t iova, uint64_t len)
121 {
122         uint64_t hva, gpa, map_len;
123         map_len = len;
124
125         hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
126         if (map_len != len) {
127                 RTE_LOG(ERR, VHOST_CONFIG,
128                         "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
129                         iova);
130                 return;
131         }
132
133         gpa = hva_to_gpa(dev, hva, len);
134         if (gpa)
135                 __vhost_log_write(dev, gpa, len);
136 }
137
138 void
139 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
140 {
141         unsigned long *log_base;
142         int i;
143
144         if (unlikely(!dev->log_base))
145                 return;
146
147         rte_smp_wmb();
148
149         log_base = (unsigned long *)(uintptr_t)dev->log_base;
150
151         for (i = 0; i < vq->log_cache_nb_elem; i++) {
152                 struct log_cache_entry *elem = vq->log_cache + i;
153
154 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
155                 /*
156                  * '__sync' builtins are deprecated, but '__atomic' ones
157                  * are sub-optimized in older GCC versions.
158                  */
159                 __sync_fetch_and_or(log_base + elem->offset, elem->val);
160 #else
161                 __atomic_fetch_or(log_base + elem->offset, elem->val,
162                                 __ATOMIC_RELAXED);
163 #endif
164         }
165
166         rte_smp_wmb();
167
168         vq->log_cache_nb_elem = 0;
169 }
170
171 static __rte_always_inline void
172 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
173                         uint64_t page)
174 {
175         uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
176         uint32_t offset = page / (sizeof(unsigned long) << 3);
177         int i;
178
179         for (i = 0; i < vq->log_cache_nb_elem; i++) {
180                 struct log_cache_entry *elem = vq->log_cache + i;
181
182                 if (elem->offset == offset) {
183                         elem->val |= (1UL << bit_nr);
184                         return;
185                 }
186         }
187
188         if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
189                 /*
190                  * No more room for a new log cache entry,
191                  * so write the dirty log map directly.
192                  */
193                 rte_smp_wmb();
194                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
195
196                 return;
197         }
198
199         vq->log_cache[i].offset = offset;
200         vq->log_cache[i].val = (1UL << bit_nr);
201         vq->log_cache_nb_elem++;
202 }
203
204 void
205 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
206                         uint64_t addr, uint64_t len)
207 {
208         uint64_t page;
209
210         if (unlikely(!dev->log_base || !len))
211                 return;
212
213         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
214                 return;
215
216         page = addr / VHOST_LOG_PAGE;
217         while (page * VHOST_LOG_PAGE < addr + len) {
218                 vhost_log_cache_page(dev, vq, page);
219                 page += 1;
220         }
221 }
222
223 void
224 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
225                              uint64_t iova, uint64_t len)
226 {
227         uint64_t hva, gpa, map_len;
228         map_len = len;
229
230         hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
231         if (map_len != len) {
232                 RTE_LOG(ERR, VHOST_CONFIG,
233                         "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
234                         iova);
235                 return;
236         }
237
238         gpa = hva_to_gpa(dev, hva, len);
239         if (gpa)
240                 __vhost_log_cache_write(dev, vq, gpa, len);
241 }
242
243 void *
244 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
245                 uint64_t desc_addr, uint64_t desc_len)
246 {
247         void *idesc;
248         uint64_t src, dst;
249         uint64_t len, remain = desc_len;
250
251         idesc = rte_malloc(__func__, desc_len, 0);
252         if (unlikely(!idesc))
253                 return NULL;
254
255         dst = (uint64_t)(uintptr_t)idesc;
256
257         while (remain) {
258                 len = remain;
259                 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
260                                 VHOST_ACCESS_RO);
261                 if (unlikely(!src || !len)) {
262                         rte_free(idesc);
263                         return NULL;
264                 }
265
266                 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
267
268                 remain -= len;
269                 dst += len;
270                 desc_addr += len;
271         }
272
273         return idesc;
274 }
275
276 void
277 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
278 {
279         if ((vq->callfd >= 0) && (destroy != 0))
280                 close(vq->callfd);
281         if (vq->kickfd >= 0)
282                 close(vq->kickfd);
283 }
284
285 void
286 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
287 {
288         if (!(dev->protocol_features &
289             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
290                 return;
291
292         if (vq_is_packed(dev)) {
293                 if (vq->inflight_packed)
294                         vq->inflight_packed = NULL;
295         } else {
296                 if (vq->inflight_split)
297                         vq->inflight_split = NULL;
298         }
299
300         if (vq->resubmit_inflight) {
301                 if (vq->resubmit_inflight->resubmit_list) {
302                         free(vq->resubmit_inflight->resubmit_list);
303                         vq->resubmit_inflight->resubmit_list = NULL;
304                 }
305                 free(vq->resubmit_inflight);
306                 vq->resubmit_inflight = NULL;
307         }
308 }
309
310 /*
311  * Unmap any memory, close any file descriptors and
312  * free any memory owned by a device.
313  */
314 void
315 cleanup_device(struct virtio_net *dev, int destroy)
316 {
317         uint32_t i;
318
319         vhost_backend_cleanup(dev);
320
321         for (i = 0; i < dev->nr_vring; i++) {
322                 cleanup_vq(dev->virtqueue[i], destroy);
323                 cleanup_vq_inflight(dev, dev->virtqueue[i]);
324         }
325 }
326
327 void
328 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
329 {
330         if (vq_is_packed(dev))
331                 rte_free(vq->shadow_used_packed);
332         else
333                 rte_free(vq->shadow_used_split);
334         rte_free(vq->batch_copy_elems);
335         rte_mempool_free(vq->iotlb_pool);
336         rte_free(vq);
337 }
338
339 /*
340  * Release virtqueues and device memory.
341  */
342 static void
343 free_device(struct virtio_net *dev)
344 {
345         uint32_t i;
346
347         for (i = 0; i < dev->nr_vring; i++)
348                 free_vq(dev, dev->virtqueue[i]);
349
350         rte_free(dev);
351 }
352
353 static int
354 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
355 {
356         uint64_t req_size, size;
357
358         req_size = sizeof(struct vring_desc) * vq->size;
359         size = req_size;
360         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
361                                                 vq->ring_addrs.desc_user_addr,
362                                                 &size, VHOST_ACCESS_RW);
363         if (!vq->desc || size != req_size)
364                 return -1;
365
366         req_size = sizeof(struct vring_avail);
367         req_size += sizeof(uint16_t) * vq->size;
368         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
369                 req_size += sizeof(uint16_t);
370         size = req_size;
371         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
372                                                 vq->ring_addrs.avail_user_addr,
373                                                 &size, VHOST_ACCESS_RW);
374         if (!vq->avail || size != req_size)
375                 return -1;
376
377         req_size = sizeof(struct vring_used);
378         req_size += sizeof(struct vring_used_elem) * vq->size;
379         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
380                 req_size += sizeof(uint16_t);
381         size = req_size;
382         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
383                                                 vq->ring_addrs.used_user_addr,
384                                                 &size, VHOST_ACCESS_RW);
385         if (!vq->used || size != req_size)
386                 return -1;
387
388         return 0;
389 }
390
391 static int
392 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
393 {
394         uint64_t req_size, size;
395
396         req_size = sizeof(struct vring_packed_desc) * vq->size;
397         size = req_size;
398         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
399                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
400                                 &size, VHOST_ACCESS_RW);
401         if (!vq->desc_packed || size != req_size)
402                 return -1;
403
404         req_size = sizeof(struct vring_packed_desc_event);
405         size = req_size;
406         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
407                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
408                                 &size, VHOST_ACCESS_RW);
409         if (!vq->driver_event || size != req_size)
410                 return -1;
411
412         req_size = sizeof(struct vring_packed_desc_event);
413         size = req_size;
414         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
415                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
416                                 &size, VHOST_ACCESS_RW);
417         if (!vq->device_event || size != req_size)
418                 return -1;
419
420         return 0;
421 }
422
423 int
424 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
425 {
426
427         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
428                 return -1;
429
430         if (vq_is_packed(dev)) {
431                 if (vring_translate_packed(dev, vq) < 0)
432                         return -1;
433         } else {
434                 if (vring_translate_split(dev, vq) < 0)
435                         return -1;
436         }
437         vq->access_ok = 1;
438
439         return 0;
440 }
441
442 void
443 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
444 {
445         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
446                 vhost_user_iotlb_wr_lock(vq);
447
448         vq->access_ok = 0;
449         vq->desc = NULL;
450         vq->avail = NULL;
451         vq->used = NULL;
452         vq->log_guest_addr = 0;
453
454         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
455                 vhost_user_iotlb_wr_unlock(vq);
456 }
457
458 static void
459 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
460 {
461         struct vhost_virtqueue *vq;
462
463         if (vring_idx >= VHOST_MAX_VRING) {
464                 RTE_LOG(ERR, VHOST_CONFIG,
465                                 "Failed not init vring, out of bound (%d)\n",
466                                 vring_idx);
467                 return;
468         }
469
470         vq = dev->virtqueue[vring_idx];
471
472         memset(vq, 0, sizeof(struct vhost_virtqueue));
473
474         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
475         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
476
477         vhost_user_iotlb_init(dev, vring_idx);
478         /* Backends are set to -1 indicating an inactive device. */
479         vq->backend = -1;
480
481         TAILQ_INIT(&vq->zmbuf_list);
482 }
483
484 static void
485 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
486 {
487         struct vhost_virtqueue *vq;
488         int callfd;
489
490         if (vring_idx >= VHOST_MAX_VRING) {
491                 RTE_LOG(ERR, VHOST_CONFIG,
492                                 "Failed not init vring, out of bound (%d)\n",
493                                 vring_idx);
494                 return;
495         }
496
497         vq = dev->virtqueue[vring_idx];
498         callfd = vq->callfd;
499         init_vring_queue(dev, vring_idx);
500         vq->callfd = callfd;
501 }
502
503 int
504 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
505 {
506         struct vhost_virtqueue *vq;
507
508         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
509         if (vq == NULL) {
510                 RTE_LOG(ERR, VHOST_CONFIG,
511                         "Failed to allocate memory for vring:%u.\n", vring_idx);
512                 return -1;
513         }
514
515         dev->virtqueue[vring_idx] = vq;
516         init_vring_queue(dev, vring_idx);
517         rte_spinlock_init(&vq->access_lock);
518         vq->avail_wrap_counter = 1;
519         vq->used_wrap_counter = 1;
520         vq->signalled_used_valid = false;
521
522         dev->nr_vring += 1;
523
524         return 0;
525 }
526
527 /*
528  * Reset some variables in device structure, while keeping few
529  * others untouched, such as vid, ifname, nr_vring: they
530  * should be same unless the device is removed.
531  */
532 void
533 reset_device(struct virtio_net *dev)
534 {
535         uint32_t i;
536
537         dev->features = 0;
538         dev->protocol_features = 0;
539         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
540
541         for (i = 0; i < dev->nr_vring; i++)
542                 reset_vring_queue(dev, i);
543 }
544
545 /*
546  * Invoked when there is a new vhost-user connection established (when
547  * there is a new virtio device being attached).
548  */
549 int
550 vhost_new_device(void)
551 {
552         struct virtio_net *dev;
553         int i;
554
555         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
556                 if (vhost_devices[i] == NULL)
557                         break;
558         }
559
560         if (i == MAX_VHOST_DEVICE) {
561                 RTE_LOG(ERR, VHOST_CONFIG,
562                         "Failed to find a free slot for new device.\n");
563                 return -1;
564         }
565
566         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
567         if (dev == NULL) {
568                 RTE_LOG(ERR, VHOST_CONFIG,
569                         "Failed to allocate memory for new dev.\n");
570                 return -1;
571         }
572
573         vhost_devices[i] = dev;
574         dev->vid = i;
575         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
576         dev->slave_req_fd = -1;
577         dev->vdpa_dev_id = -1;
578         dev->postcopy_ufd = -1;
579         rte_spinlock_init(&dev->slave_req_lock);
580
581         return i;
582 }
583
584 void
585 vhost_destroy_device_notify(struct virtio_net *dev)
586 {
587         struct rte_vdpa_device *vdpa_dev;
588         int did;
589
590         if (dev->flags & VIRTIO_DEV_RUNNING) {
591                 did = dev->vdpa_dev_id;
592                 vdpa_dev = rte_vdpa_get_device(did);
593                 if (vdpa_dev && vdpa_dev->ops->dev_close)
594                         vdpa_dev->ops->dev_close(dev->vid);
595                 dev->flags &= ~VIRTIO_DEV_RUNNING;
596                 dev->notify_ops->destroy_device(dev->vid);
597         }
598 }
599
600 /*
601  * Invoked when there is the vhost-user connection is broken (when
602  * the virtio device is being detached).
603  */
604 void
605 vhost_destroy_device(int vid)
606 {
607         struct virtio_net *dev = get_device(vid);
608
609         if (dev == NULL)
610                 return;
611
612         vhost_destroy_device_notify(dev);
613
614         cleanup_device(dev, 1);
615         free_device(dev);
616
617         vhost_devices[vid] = NULL;
618 }
619
620 void
621 vhost_attach_vdpa_device(int vid, int did)
622 {
623         struct virtio_net *dev = get_device(vid);
624
625         if (dev == NULL)
626                 return;
627
628         if (rte_vdpa_get_device(did) == NULL)
629                 return;
630
631         dev->vdpa_dev_id = did;
632 }
633
634 void
635 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
636 {
637         struct virtio_net *dev;
638         unsigned int len;
639
640         dev = get_device(vid);
641         if (dev == NULL)
642                 return;
643
644         len = if_len > sizeof(dev->ifname) ?
645                 sizeof(dev->ifname) : if_len;
646
647         strncpy(dev->ifname, if_name, len);
648         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
649 }
650
651 void
652 vhost_enable_dequeue_zero_copy(int vid)
653 {
654         struct virtio_net *dev = get_device(vid);
655
656         if (dev == NULL)
657                 return;
658
659         dev->dequeue_zero_copy = 1;
660 }
661
662 void
663 vhost_set_builtin_virtio_net(int vid, bool enable)
664 {
665         struct virtio_net *dev = get_device(vid);
666
667         if (dev == NULL)
668                 return;
669
670         if (enable)
671                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
672         else
673                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
674 }
675
676 void
677 vhost_enable_extbuf(int vid)
678 {
679         struct virtio_net *dev = get_device(vid);
680
681         if (dev == NULL)
682                 return;
683
684         dev->extbuf = 1;
685 }
686
687 void
688 vhost_enable_linearbuf(int vid)
689 {
690         struct virtio_net *dev = get_device(vid);
691
692         if (dev == NULL)
693                 return;
694
695         dev->linearbuf = 1;
696 }
697
698 int
699 rte_vhost_get_mtu(int vid, uint16_t *mtu)
700 {
701         struct virtio_net *dev = get_device(vid);
702
703         if (dev == NULL || mtu == NULL)
704                 return -ENODEV;
705
706         if (!(dev->flags & VIRTIO_DEV_READY))
707                 return -EAGAIN;
708
709         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
710                 return -ENOTSUP;
711
712         *mtu = dev->mtu;
713
714         return 0;
715 }
716
717 int
718 rte_vhost_get_numa_node(int vid)
719 {
720 #ifdef RTE_LIBRTE_VHOST_NUMA
721         struct virtio_net *dev = get_device(vid);
722         int numa_node;
723         int ret;
724
725         if (dev == NULL || numa_available() != 0)
726                 return -1;
727
728         ret = get_mempolicy(&numa_node, NULL, 0, dev,
729                             MPOL_F_NODE | MPOL_F_ADDR);
730         if (ret < 0) {
731                 RTE_LOG(ERR, VHOST_CONFIG,
732                         "(%d) failed to query numa node: %s\n",
733                         vid, rte_strerror(errno));
734                 return -1;
735         }
736
737         return numa_node;
738 #else
739         RTE_SET_USED(vid);
740         return -1;
741 #endif
742 }
743
744 uint32_t
745 rte_vhost_get_queue_num(int vid)
746 {
747         struct virtio_net *dev = get_device(vid);
748
749         if (dev == NULL)
750                 return 0;
751
752         return dev->nr_vring / 2;
753 }
754
755 uint16_t
756 rte_vhost_get_vring_num(int vid)
757 {
758         struct virtio_net *dev = get_device(vid);
759
760         if (dev == NULL)
761                 return 0;
762
763         return dev->nr_vring;
764 }
765
766 int
767 rte_vhost_get_ifname(int vid, char *buf, size_t len)
768 {
769         struct virtio_net *dev = get_device(vid);
770
771         if (dev == NULL || buf == NULL)
772                 return -1;
773
774         len = RTE_MIN(len, sizeof(dev->ifname));
775
776         strncpy(buf, dev->ifname, len);
777         buf[len - 1] = '\0';
778
779         return 0;
780 }
781
782 int
783 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
784 {
785         struct virtio_net *dev;
786
787         dev = get_device(vid);
788         if (dev == NULL || features == NULL)
789                 return -1;
790
791         *features = dev->features;
792         return 0;
793 }
794
795 int
796 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
797 {
798         struct virtio_net *dev;
799         struct rte_vhost_memory *m;
800         size_t size;
801
802         dev = get_device(vid);
803         if (dev == NULL || mem == NULL)
804                 return -1;
805
806         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
807         m = malloc(sizeof(struct rte_vhost_memory) + size);
808         if (!m)
809                 return -1;
810
811         m->nregions = dev->mem->nregions;
812         memcpy(m->regions, dev->mem->regions, size);
813         *mem = m;
814
815         return 0;
816 }
817
818 int
819 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
820                           struct rte_vhost_vring *vring)
821 {
822         struct virtio_net *dev;
823         struct vhost_virtqueue *vq;
824
825         dev = get_device(vid);
826         if (dev == NULL || vring == NULL)
827                 return -1;
828
829         if (vring_idx >= VHOST_MAX_VRING)
830                 return -1;
831
832         vq = dev->virtqueue[vring_idx];
833         if (!vq)
834                 return -1;
835
836         if (vq_is_packed(dev)) {
837                 vring->desc_packed = vq->desc_packed;
838                 vring->driver_event = vq->driver_event;
839                 vring->device_event = vq->device_event;
840         } else {
841                 vring->desc = vq->desc;
842                 vring->avail = vq->avail;
843                 vring->used = vq->used;
844         }
845         vring->log_guest_addr  = vq->log_guest_addr;
846
847         vring->callfd  = vq->callfd;
848         vring->kickfd  = vq->kickfd;
849         vring->size    = vq->size;
850
851         return 0;
852 }
853
854 int
855 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
856                                   struct rte_vhost_ring_inflight *vring)
857 {
858         struct virtio_net *dev;
859         struct vhost_virtqueue *vq;
860
861         dev = get_device(vid);
862         if (unlikely(!dev))
863                 return -1;
864
865         if (vring_idx >= VHOST_MAX_VRING)
866                 return -1;
867
868         vq = dev->virtqueue[vring_idx];
869         if (unlikely(!vq))
870                 return -1;
871
872         if (vq_is_packed(dev)) {
873                 if (unlikely(!vq->inflight_packed))
874                         return -1;
875
876                 vring->inflight_packed = vq->inflight_packed;
877         } else {
878                 if (unlikely(!vq->inflight_split))
879                         return -1;
880
881                 vring->inflight_split = vq->inflight_split;
882         }
883
884         vring->resubmit_inflight = vq->resubmit_inflight;
885
886         return 0;
887 }
888
889 int
890 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
891                                   uint16_t idx)
892 {
893         struct vhost_virtqueue *vq;
894         struct virtio_net *dev;
895
896         dev = get_device(vid);
897         if (unlikely(!dev))
898                 return -1;
899
900         if (unlikely(!(dev->protocol_features &
901             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
902                 return 0;
903
904         if (unlikely(vq_is_packed(dev)))
905                 return -1;
906
907         if (unlikely(vring_idx >= VHOST_MAX_VRING))
908                 return -1;
909
910         vq = dev->virtqueue[vring_idx];
911         if (unlikely(!vq))
912                 return -1;
913
914         if (unlikely(!vq->inflight_split))
915                 return -1;
916
917         if (unlikely(idx >= vq->size))
918                 return -1;
919
920         vq->inflight_split->desc[idx].counter = vq->global_counter++;
921         vq->inflight_split->desc[idx].inflight = 1;
922         return 0;
923 }
924
925 int
926 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
927                                    uint16_t head, uint16_t last,
928                                    uint16_t *inflight_entry)
929 {
930         struct rte_vhost_inflight_info_packed *inflight_info;
931         struct virtio_net *dev;
932         struct vhost_virtqueue *vq;
933         struct vring_packed_desc *desc;
934         uint16_t old_free_head, free_head;
935
936         dev = get_device(vid);
937         if (unlikely(!dev))
938                 return -1;
939
940         if (unlikely(!(dev->protocol_features &
941             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
942                 return 0;
943
944         if (unlikely(!vq_is_packed(dev)))
945                 return -1;
946
947         if (unlikely(vring_idx >= VHOST_MAX_VRING))
948                 return -1;
949
950         vq = dev->virtqueue[vring_idx];
951         if (unlikely(!vq))
952                 return -1;
953
954         inflight_info = vq->inflight_packed;
955         if (unlikely(!inflight_info))
956                 return -1;
957
958         if (unlikely(head >= vq->size))
959                 return -1;
960
961         desc = vq->desc_packed;
962         old_free_head = inflight_info->old_free_head;
963         if (unlikely(old_free_head >= vq->size))
964                 return -1;
965
966         free_head = old_free_head;
967
968         /* init header descriptor */
969         inflight_info->desc[old_free_head].num = 0;
970         inflight_info->desc[old_free_head].counter = vq->global_counter++;
971         inflight_info->desc[old_free_head].inflight = 1;
972
973         /* save desc entry in flight entry */
974         while (head != ((last + 1) % vq->size)) {
975                 inflight_info->desc[old_free_head].num++;
976                 inflight_info->desc[free_head].addr = desc[head].addr;
977                 inflight_info->desc[free_head].len = desc[head].len;
978                 inflight_info->desc[free_head].flags = desc[head].flags;
979                 inflight_info->desc[free_head].id = desc[head].id;
980
981                 inflight_info->desc[old_free_head].last = free_head;
982                 free_head = inflight_info->desc[free_head].next;
983                 inflight_info->free_head = free_head;
984                 head = (head + 1) % vq->size;
985         }
986
987         inflight_info->old_free_head = free_head;
988         *inflight_entry = old_free_head;
989
990         return 0;
991 }
992
993 int
994 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
995                                   uint16_t last_used_idx, uint16_t idx)
996 {
997         struct virtio_net *dev;
998         struct vhost_virtqueue *vq;
999
1000         dev = get_device(vid);
1001         if (unlikely(!dev))
1002                 return -1;
1003
1004         if (unlikely(!(dev->protocol_features &
1005             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1006                 return 0;
1007
1008         if (unlikely(vq_is_packed(dev)))
1009                 return -1;
1010
1011         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1012                 return -1;
1013
1014         vq = dev->virtqueue[vring_idx];
1015         if (unlikely(!vq))
1016                 return -1;
1017
1018         if (unlikely(!vq->inflight_split))
1019                 return -1;
1020
1021         if (unlikely(idx >= vq->size))
1022                 return -1;
1023
1024         rte_smp_mb();
1025
1026         vq->inflight_split->desc[idx].inflight = 0;
1027
1028         rte_smp_mb();
1029
1030         vq->inflight_split->used_idx = last_used_idx;
1031         return 0;
1032 }
1033
1034 int
1035 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1036                                    uint16_t head)
1037 {
1038         struct rte_vhost_inflight_info_packed *inflight_info;
1039         struct virtio_net *dev;
1040         struct vhost_virtqueue *vq;
1041
1042         dev = get_device(vid);
1043         if (unlikely(!dev))
1044                 return -1;
1045
1046         if (unlikely(!(dev->protocol_features &
1047             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1048                 return 0;
1049
1050         if (unlikely(!vq_is_packed(dev)))
1051                 return -1;
1052
1053         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1054                 return -1;
1055
1056         vq = dev->virtqueue[vring_idx];
1057         if (unlikely(!vq))
1058                 return -1;
1059
1060         inflight_info = vq->inflight_packed;
1061         if (unlikely(!inflight_info))
1062                 return -1;
1063
1064         if (unlikely(head >= vq->size))
1065                 return -1;
1066
1067         rte_smp_mb();
1068
1069         inflight_info->desc[head].inflight = 0;
1070
1071         rte_smp_mb();
1072
1073         inflight_info->old_free_head = inflight_info->free_head;
1074         inflight_info->old_used_idx = inflight_info->used_idx;
1075         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1076
1077         return 0;
1078 }
1079
1080 int
1081 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1082                                      uint16_t idx)
1083 {
1084         struct virtio_net *dev;
1085         struct vhost_virtqueue *vq;
1086
1087         dev = get_device(vid);
1088         if (unlikely(!dev))
1089                 return -1;
1090
1091         if (unlikely(!(dev->protocol_features &
1092             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1093                 return 0;
1094
1095         if (unlikely(vq_is_packed(dev)))
1096                 return -1;
1097
1098         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1099                 return -1;
1100
1101         vq = dev->virtqueue[vring_idx];
1102         if (unlikely(!vq))
1103                 return -1;
1104
1105         if (unlikely(!vq->inflight_split))
1106                 return -1;
1107
1108         vq->inflight_split->last_inflight_io = idx;
1109         return 0;
1110 }
1111
1112 int
1113 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1114                                       uint16_t head)
1115 {
1116         struct rte_vhost_inflight_info_packed *inflight_info;
1117         struct virtio_net *dev;
1118         struct vhost_virtqueue *vq;
1119         uint16_t last;
1120
1121         dev = get_device(vid);
1122         if (unlikely(!dev))
1123                 return -1;
1124
1125         if (unlikely(!(dev->protocol_features &
1126             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1127                 return 0;
1128
1129         if (unlikely(!vq_is_packed(dev)))
1130                 return -1;
1131
1132         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1133                 return -1;
1134
1135         vq = dev->virtqueue[vring_idx];
1136         if (unlikely(!vq))
1137                 return -1;
1138
1139         inflight_info = vq->inflight_packed;
1140         if (unlikely(!inflight_info))
1141                 return -1;
1142
1143         if (unlikely(head >= vq->size))
1144                 return -1;
1145
1146         last = inflight_info->desc[head].last;
1147         if (unlikely(last >= vq->size))
1148                 return -1;
1149
1150         inflight_info->desc[last].next = inflight_info->free_head;
1151         inflight_info->free_head = head;
1152         inflight_info->used_idx += inflight_info->desc[head].num;
1153         if (inflight_info->used_idx >= inflight_info->desc_num) {
1154                 inflight_info->used_idx -= inflight_info->desc_num;
1155                 inflight_info->used_wrap_counter =
1156                         !inflight_info->used_wrap_counter;
1157         }
1158
1159         return 0;
1160 }
1161
1162 int
1163 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1164 {
1165         struct virtio_net *dev;
1166         struct vhost_virtqueue *vq;
1167
1168         dev = get_device(vid);
1169         if (!dev)
1170                 return -1;
1171
1172         if (vring_idx >= VHOST_MAX_VRING)
1173                 return -1;
1174
1175         vq = dev->virtqueue[vring_idx];
1176         if (!vq)
1177                 return -1;
1178
1179         if (vq_is_packed(dev))
1180                 vhost_vring_call_packed(dev, vq);
1181         else
1182                 vhost_vring_call_split(dev, vq);
1183
1184         return 0;
1185 }
1186
1187 uint16_t
1188 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1189 {
1190         struct virtio_net *dev;
1191         struct vhost_virtqueue *vq;
1192         uint16_t ret = 0;
1193
1194         dev = get_device(vid);
1195         if (!dev)
1196                 return 0;
1197
1198         vq = dev->virtqueue[queue_id];
1199
1200         rte_spinlock_lock(&vq->access_lock);
1201
1202         if (unlikely(!vq->enabled || vq->avail == NULL))
1203                 goto out;
1204
1205         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1206
1207 out:
1208         rte_spinlock_unlock(&vq->access_lock);
1209         return ret;
1210 }
1211
1212 static inline int
1213 vhost_enable_notify_split(struct virtio_net *dev,
1214                 struct vhost_virtqueue *vq, int enable)
1215 {
1216         if (vq->used == NULL)
1217                 return -1;
1218
1219         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1220                 if (enable)
1221                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1222                 else
1223                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1224         } else {
1225                 if (enable)
1226                         vhost_avail_event(vq) = vq->last_avail_idx;
1227         }
1228         return 0;
1229 }
1230
1231 static inline int
1232 vhost_enable_notify_packed(struct virtio_net *dev,
1233                 struct vhost_virtqueue *vq, int enable)
1234 {
1235         uint16_t flags;
1236
1237         if (vq->device_event == NULL)
1238                 return -1;
1239
1240         if (!enable) {
1241                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1242                 return 0;
1243         }
1244
1245         flags = VRING_EVENT_F_ENABLE;
1246         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1247                 flags = VRING_EVENT_F_DESC;
1248                 vq->device_event->off_wrap = vq->last_avail_idx |
1249                         vq->avail_wrap_counter << 15;
1250         }
1251
1252         rte_smp_wmb();
1253
1254         vq->device_event->flags = flags;
1255         return 0;
1256 }
1257
1258 int
1259 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1260 {
1261         struct virtio_net *dev = get_device(vid);
1262         struct vhost_virtqueue *vq;
1263         int ret;
1264
1265         if (!dev)
1266                 return -1;
1267
1268         vq = dev->virtqueue[queue_id];
1269
1270         rte_spinlock_lock(&vq->access_lock);
1271
1272         if (vq_is_packed(dev))
1273                 ret = vhost_enable_notify_packed(dev, vq, enable);
1274         else
1275                 ret = vhost_enable_notify_split(dev, vq, enable);
1276
1277         rte_spinlock_unlock(&vq->access_lock);
1278
1279         return ret;
1280 }
1281
1282 void
1283 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1284 {
1285         struct virtio_net *dev = get_device(vid);
1286
1287         if (dev == NULL)
1288                 return;
1289
1290         vhost_log_write(dev, addr, len);
1291 }
1292
1293 void
1294 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1295                          uint64_t offset, uint64_t len)
1296 {
1297         struct virtio_net *dev;
1298         struct vhost_virtqueue *vq;
1299
1300         dev = get_device(vid);
1301         if (dev == NULL)
1302                 return;
1303
1304         if (vring_idx >= VHOST_MAX_VRING)
1305                 return;
1306         vq = dev->virtqueue[vring_idx];
1307         if (!vq)
1308                 return;
1309
1310         vhost_log_used_vring(dev, vq, offset, len);
1311 }
1312
1313 uint32_t
1314 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1315 {
1316         struct virtio_net *dev;
1317         struct vhost_virtqueue *vq;
1318         uint32_t ret = 0;
1319
1320         dev = get_device(vid);
1321         if (dev == NULL)
1322                 return 0;
1323
1324         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1325                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1326                         dev->vid, __func__, qid);
1327                 return 0;
1328         }
1329
1330         vq = dev->virtqueue[qid];
1331         if (vq == NULL)
1332                 return 0;
1333
1334         rte_spinlock_lock(&vq->access_lock);
1335
1336         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1337                 goto out;
1338
1339         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1340
1341 out:
1342         rte_spinlock_unlock(&vq->access_lock);
1343         return ret;
1344 }
1345
1346 int rte_vhost_get_vdpa_device_id(int vid)
1347 {
1348         struct virtio_net *dev = get_device(vid);
1349
1350         if (dev == NULL)
1351                 return -1;
1352
1353         return dev->vdpa_dev_id;
1354 }
1355
1356 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1357                 uint64_t *log_size)
1358 {
1359         struct virtio_net *dev = get_device(vid);
1360
1361         if (dev == NULL || log_base == NULL || log_size == NULL)
1362                 return -1;
1363
1364         *log_base = dev->log_base;
1365         *log_size = dev->log_size;
1366
1367         return 0;
1368 }
1369
1370 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1371                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1372 {
1373         struct vhost_virtqueue *vq;
1374         struct virtio_net *dev = get_device(vid);
1375
1376         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1377                 return -1;
1378
1379         vq = dev->virtqueue[queue_id];
1380         if (!vq)
1381                 return -1;
1382
1383         if (vq_is_packed(dev)) {
1384                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1385                                   vq->last_avail_idx;
1386                 *last_used_idx = (vq->used_wrap_counter << 15) |
1387                                  vq->last_used_idx;
1388         } else {
1389                 *last_avail_idx = vq->last_avail_idx;
1390                 *last_used_idx = vq->last_used_idx;
1391         }
1392
1393         return 0;
1394 }
1395
1396 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1397                 uint16_t last_avail_idx, uint16_t last_used_idx)
1398 {
1399         struct vhost_virtqueue *vq;
1400         struct virtio_net *dev = get_device(vid);
1401
1402         if (!dev)
1403                 return -1;
1404
1405         vq = dev->virtqueue[queue_id];
1406         if (!vq)
1407                 return -1;
1408
1409         if (vq_is_packed(dev)) {
1410                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1411                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1412                 vq->last_used_idx = last_used_idx & 0x7fff;
1413                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1414         } else {
1415                 vq->last_avail_idx = last_avail_idx;
1416                 vq->last_used_idx = last_used_idx;
1417         }
1418
1419         return 0;
1420 }
1421
1422 int
1423 rte_vhost_get_vring_base_from_inflight(int vid,
1424                                        uint16_t queue_id,
1425                                        uint16_t *last_avail_idx,
1426                                        uint16_t *last_used_idx)
1427 {
1428         struct rte_vhost_inflight_info_packed *inflight_info;
1429         struct virtio_net *dev = get_device(vid);
1430
1431         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1432                 return -1;
1433
1434         if (!vq_is_packed(dev))
1435                 return -1;
1436
1437         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1438         if (!inflight_info)
1439                 return -1;
1440
1441         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1442                           inflight_info->old_used_idx;
1443         *last_used_idx = *last_avail_idx;
1444
1445         return 0;
1446 }
1447
1448 int rte_vhost_extern_callback_register(int vid,
1449                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1450 {
1451         struct virtio_net *dev = get_device(vid);
1452
1453         if (dev == NULL || ops == NULL)
1454                 return -1;
1455
1456         dev->extern_ops = *ops;
1457         dev->extern_data = ctx;
1458         return 0;
1459 }