vhost: fix guest notification setting
[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                         VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_DATA(ERR,
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                 VHOST_LOG_DATA(ERR,
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                 if (vq->async_pkts_pending)
335                         rte_free(vq->async_pkts_pending);
336                 if (vq->async_pending_info)
337                         rte_free(vq->async_pending_info);
338         }
339         rte_free(vq->batch_copy_elems);
340         rte_mempool_free(vq->iotlb_pool);
341         rte_free(vq);
342 }
343
344 /*
345  * Release virtqueues and device memory.
346  */
347 static void
348 free_device(struct virtio_net *dev)
349 {
350         uint32_t i;
351
352         for (i = 0; i < dev->nr_vring; i++)
353                 free_vq(dev, dev->virtqueue[i]);
354
355         rte_free(dev);
356 }
357
358 static __rte_always_inline int
359 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
360 {
361         if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
362                 return 0;
363
364         vq->log_guest_addr = translate_log_addr(dev, vq,
365                                                 vq->ring_addrs.log_guest_addr);
366         if (vq->log_guest_addr == 0)
367                 return -1;
368
369         return 0;
370 }
371
372 /*
373  * Converts vring log address to GPA
374  * If IOMMU is enabled, the log address is IOVA
375  * If IOMMU not enabled, the log address is already GPA
376  *
377  * Caller should have iotlb_lock read-locked
378  */
379 uint64_t
380 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
381                 uint64_t log_addr)
382 {
383         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
384                 const uint64_t exp_size = sizeof(uint64_t);
385                 uint64_t hva, gpa;
386                 uint64_t size = exp_size;
387
388                 hva = vhost_iova_to_vva(dev, vq, log_addr,
389                                         &size, VHOST_ACCESS_RW);
390
391                 if (size != exp_size)
392                         return 0;
393
394                 gpa = hva_to_gpa(dev, hva, exp_size);
395                 if (!gpa) {
396                         VHOST_LOG_CONFIG(ERR,
397                                 "VQ: Failed to find GPA for log_addr: 0x%"
398                                 PRIx64 " hva: 0x%" PRIx64 "\n",
399                                 log_addr, hva);
400                         return 0;
401                 }
402                 return gpa;
403
404         } else
405                 return log_addr;
406 }
407
408 /* Caller should have iotlb_lock read-locked */
409 static int
410 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
411 {
412         uint64_t req_size, size;
413
414         req_size = sizeof(struct vring_desc) * vq->size;
415         size = req_size;
416         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
417                                                 vq->ring_addrs.desc_user_addr,
418                                                 &size, VHOST_ACCESS_RW);
419         if (!vq->desc || size != req_size)
420                 return -1;
421
422         req_size = sizeof(struct vring_avail);
423         req_size += sizeof(uint16_t) * vq->size;
424         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
425                 req_size += sizeof(uint16_t);
426         size = req_size;
427         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
428                                                 vq->ring_addrs.avail_user_addr,
429                                                 &size, VHOST_ACCESS_RW);
430         if (!vq->avail || size != req_size)
431                 return -1;
432
433         req_size = sizeof(struct vring_used);
434         req_size += sizeof(struct vring_used_elem) * vq->size;
435         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
436                 req_size += sizeof(uint16_t);
437         size = req_size;
438         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
439                                                 vq->ring_addrs.used_user_addr,
440                                                 &size, VHOST_ACCESS_RW);
441         if (!vq->used || size != req_size)
442                 return -1;
443
444         return 0;
445 }
446
447 /* Caller should have iotlb_lock read-locked */
448 static int
449 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
450 {
451         uint64_t req_size, size;
452
453         req_size = sizeof(struct vring_packed_desc) * vq->size;
454         size = req_size;
455         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
456                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
457                                 &size, VHOST_ACCESS_RW);
458         if (!vq->desc_packed || size != req_size)
459                 return -1;
460
461         req_size = sizeof(struct vring_packed_desc_event);
462         size = req_size;
463         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
464                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
465                                 &size, VHOST_ACCESS_RW);
466         if (!vq->driver_event || size != req_size)
467                 return -1;
468
469         req_size = sizeof(struct vring_packed_desc_event);
470         size = req_size;
471         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
472                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
473                                 &size, VHOST_ACCESS_RW);
474         if (!vq->device_event || size != req_size)
475                 return -1;
476
477         return 0;
478 }
479
480 int
481 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
482 {
483
484         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
485                 return -1;
486
487         if (vq_is_packed(dev)) {
488                 if (vring_translate_packed(dev, vq) < 0)
489                         return -1;
490         } else {
491                 if (vring_translate_split(dev, vq) < 0)
492                         return -1;
493         }
494
495         if (log_translate(dev, vq) < 0)
496                 return -1;
497
498         vq->access_ok = 1;
499
500         return 0;
501 }
502
503 void
504 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
505 {
506         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
507                 vhost_user_iotlb_wr_lock(vq);
508
509         vq->access_ok = 0;
510         vq->desc = NULL;
511         vq->avail = NULL;
512         vq->used = NULL;
513         vq->log_guest_addr = 0;
514
515         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
516                 vhost_user_iotlb_wr_unlock(vq);
517 }
518
519 static void
520 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
521 {
522         struct vhost_virtqueue *vq;
523
524         if (vring_idx >= VHOST_MAX_VRING) {
525                 VHOST_LOG_CONFIG(ERR,
526                                 "Failed not init vring, out of bound (%d)\n",
527                                 vring_idx);
528                 return;
529         }
530
531         vq = dev->virtqueue[vring_idx];
532
533         memset(vq, 0, sizeof(struct vhost_virtqueue));
534
535         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
536         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
537         vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
538
539         vhost_user_iotlb_init(dev, vring_idx);
540         /* Backends are set to -1 indicating an inactive device. */
541         vq->backend = -1;
542
543         TAILQ_INIT(&vq->zmbuf_list);
544 }
545
546 static void
547 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
548 {
549         struct vhost_virtqueue *vq;
550         int callfd;
551
552         if (vring_idx >= VHOST_MAX_VRING) {
553                 VHOST_LOG_CONFIG(ERR,
554                                 "Failed not init vring, out of bound (%d)\n",
555                                 vring_idx);
556                 return;
557         }
558
559         vq = dev->virtqueue[vring_idx];
560         callfd = vq->callfd;
561         init_vring_queue(dev, vring_idx);
562         vq->callfd = callfd;
563 }
564
565 int
566 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
567 {
568         struct vhost_virtqueue *vq;
569
570         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
571         if (vq == NULL) {
572                 VHOST_LOG_CONFIG(ERR,
573                         "Failed to allocate memory for vring:%u.\n", vring_idx);
574                 return -1;
575         }
576
577         dev->virtqueue[vring_idx] = vq;
578         init_vring_queue(dev, vring_idx);
579         rte_spinlock_init(&vq->access_lock);
580         vq->avail_wrap_counter = 1;
581         vq->used_wrap_counter = 1;
582         vq->signalled_used_valid = false;
583
584         dev->nr_vring += 1;
585
586         return 0;
587 }
588
589 /*
590  * Reset some variables in device structure, while keeping few
591  * others untouched, such as vid, ifname, nr_vring: they
592  * should be same unless the device is removed.
593  */
594 void
595 reset_device(struct virtio_net *dev)
596 {
597         uint32_t i;
598
599         dev->features = 0;
600         dev->protocol_features = 0;
601         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
602
603         for (i = 0; i < dev->nr_vring; i++)
604                 reset_vring_queue(dev, i);
605 }
606
607 /*
608  * Invoked when there is a new vhost-user connection established (when
609  * there is a new virtio device being attached).
610  */
611 int
612 vhost_new_device(void)
613 {
614         struct virtio_net *dev;
615         int i;
616
617         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
618                 if (vhost_devices[i] == NULL)
619                         break;
620         }
621
622         if (i == MAX_VHOST_DEVICE) {
623                 VHOST_LOG_CONFIG(ERR,
624                         "Failed to find a free slot for new device.\n");
625                 return -1;
626         }
627
628         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
629         if (dev == NULL) {
630                 VHOST_LOG_CONFIG(ERR,
631                         "Failed to allocate memory for new dev.\n");
632                 return -1;
633         }
634
635         vhost_devices[i] = dev;
636         dev->vid = i;
637         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
638         dev->slave_req_fd = -1;
639         dev->postcopy_ufd = -1;
640         rte_spinlock_init(&dev->slave_req_lock);
641
642         return i;
643 }
644
645 void
646 vhost_destroy_device_notify(struct virtio_net *dev)
647 {
648         struct rte_vdpa_device *vdpa_dev;
649
650         if (dev->flags & VIRTIO_DEV_RUNNING) {
651                 vdpa_dev = dev->vdpa_dev;
652                 if (vdpa_dev)
653                         vdpa_dev->ops->dev_close(dev->vid);
654                 dev->flags &= ~VIRTIO_DEV_RUNNING;
655                 dev->notify_ops->destroy_device(dev->vid);
656         }
657 }
658
659 /*
660  * Invoked when there is the vhost-user connection is broken (when
661  * the virtio device is being detached).
662  */
663 void
664 vhost_destroy_device(int vid)
665 {
666         struct virtio_net *dev = get_device(vid);
667
668         if (dev == NULL)
669                 return;
670
671         vhost_destroy_device_notify(dev);
672
673         cleanup_device(dev, 1);
674         free_device(dev);
675
676         vhost_devices[vid] = NULL;
677 }
678
679 void
680 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
681 {
682         struct virtio_net *dev = get_device(vid);
683
684         if (dev == NULL)
685                 return;
686
687         dev->vdpa_dev = vdpa_dev;
688 }
689
690 void
691 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
692 {
693         struct virtio_net *dev;
694         unsigned int len;
695
696         dev = get_device(vid);
697         if (dev == NULL)
698                 return;
699
700         len = if_len > sizeof(dev->ifname) ?
701                 sizeof(dev->ifname) : if_len;
702
703         strncpy(dev->ifname, if_name, len);
704         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
705 }
706
707 void
708 vhost_enable_dequeue_zero_copy(int vid)
709 {
710         struct virtio_net *dev = get_device(vid);
711
712         if (dev == NULL)
713                 return;
714
715         dev->dequeue_zero_copy = 1;
716         VHOST_LOG_CONFIG(INFO, "dequeue zero copy is enabled\n");
717 }
718
719 void
720 vhost_set_builtin_virtio_net(int vid, bool enable)
721 {
722         struct virtio_net *dev = get_device(vid);
723
724         if (dev == NULL)
725                 return;
726
727         if (enable)
728                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
729         else
730                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
731 }
732
733 void
734 vhost_enable_extbuf(int vid)
735 {
736         struct virtio_net *dev = get_device(vid);
737
738         if (dev == NULL)
739                 return;
740
741         dev->extbuf = 1;
742 }
743
744 void
745 vhost_enable_linearbuf(int vid)
746 {
747         struct virtio_net *dev = get_device(vid);
748
749         if (dev == NULL)
750                 return;
751
752         dev->linearbuf = 1;
753 }
754
755 int
756 rte_vhost_get_mtu(int vid, uint16_t *mtu)
757 {
758         struct virtio_net *dev = get_device(vid);
759
760         if (dev == NULL || mtu == NULL)
761                 return -ENODEV;
762
763         if (!(dev->flags & VIRTIO_DEV_READY))
764                 return -EAGAIN;
765
766         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
767                 return -ENOTSUP;
768
769         *mtu = dev->mtu;
770
771         return 0;
772 }
773
774 int
775 rte_vhost_get_numa_node(int vid)
776 {
777 #ifdef RTE_LIBRTE_VHOST_NUMA
778         struct virtio_net *dev = get_device(vid);
779         int numa_node;
780         int ret;
781
782         if (dev == NULL || numa_available() != 0)
783                 return -1;
784
785         ret = get_mempolicy(&numa_node, NULL, 0, dev,
786                             MPOL_F_NODE | MPOL_F_ADDR);
787         if (ret < 0) {
788                 VHOST_LOG_CONFIG(ERR,
789                         "(%d) failed to query numa node: %s\n",
790                         vid, rte_strerror(errno));
791                 return -1;
792         }
793
794         return numa_node;
795 #else
796         RTE_SET_USED(vid);
797         return -1;
798 #endif
799 }
800
801 uint32_t
802 rte_vhost_get_queue_num(int vid)
803 {
804         struct virtio_net *dev = get_device(vid);
805
806         if (dev == NULL)
807                 return 0;
808
809         return dev->nr_vring / 2;
810 }
811
812 uint16_t
813 rte_vhost_get_vring_num(int vid)
814 {
815         struct virtio_net *dev = get_device(vid);
816
817         if (dev == NULL)
818                 return 0;
819
820         return dev->nr_vring;
821 }
822
823 int
824 rte_vhost_get_ifname(int vid, char *buf, size_t len)
825 {
826         struct virtio_net *dev = get_device(vid);
827
828         if (dev == NULL || buf == NULL)
829                 return -1;
830
831         len = RTE_MIN(len, sizeof(dev->ifname));
832
833         strncpy(buf, dev->ifname, len);
834         buf[len - 1] = '\0';
835
836         return 0;
837 }
838
839 int
840 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
841 {
842         struct virtio_net *dev;
843
844         dev = get_device(vid);
845         if (dev == NULL || features == NULL)
846                 return -1;
847
848         *features = dev->features;
849         return 0;
850 }
851
852 int
853 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
854 {
855         struct virtio_net *dev;
856         struct rte_vhost_memory *m;
857         size_t size;
858
859         dev = get_device(vid);
860         if (dev == NULL || mem == NULL)
861                 return -1;
862
863         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
864         m = malloc(sizeof(struct rte_vhost_memory) + size);
865         if (!m)
866                 return -1;
867
868         m->nregions = dev->mem->nregions;
869         memcpy(m->regions, dev->mem->regions, size);
870         *mem = m;
871
872         return 0;
873 }
874
875 int
876 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
877                           struct rte_vhost_vring *vring)
878 {
879         struct virtio_net *dev;
880         struct vhost_virtqueue *vq;
881
882         dev = get_device(vid);
883         if (dev == NULL || vring == NULL)
884                 return -1;
885
886         if (vring_idx >= VHOST_MAX_VRING)
887                 return -1;
888
889         vq = dev->virtqueue[vring_idx];
890         if (!vq)
891                 return -1;
892
893         if (vq_is_packed(dev)) {
894                 vring->desc_packed = vq->desc_packed;
895                 vring->driver_event = vq->driver_event;
896                 vring->device_event = vq->device_event;
897         } else {
898                 vring->desc = vq->desc;
899                 vring->avail = vq->avail;
900                 vring->used = vq->used;
901         }
902         vring->log_guest_addr  = vq->log_guest_addr;
903
904         vring->callfd  = vq->callfd;
905         vring->kickfd  = vq->kickfd;
906         vring->size    = vq->size;
907
908         return 0;
909 }
910
911 int
912 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
913                                   struct rte_vhost_ring_inflight *vring)
914 {
915         struct virtio_net *dev;
916         struct vhost_virtqueue *vq;
917
918         dev = get_device(vid);
919         if (unlikely(!dev))
920                 return -1;
921
922         if (vring_idx >= VHOST_MAX_VRING)
923                 return -1;
924
925         vq = dev->virtqueue[vring_idx];
926         if (unlikely(!vq))
927                 return -1;
928
929         if (vq_is_packed(dev)) {
930                 if (unlikely(!vq->inflight_packed))
931                         return -1;
932
933                 vring->inflight_packed = vq->inflight_packed;
934         } else {
935                 if (unlikely(!vq->inflight_split))
936                         return -1;
937
938                 vring->inflight_split = vq->inflight_split;
939         }
940
941         vring->resubmit_inflight = vq->resubmit_inflight;
942
943         return 0;
944 }
945
946 int
947 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
948                                   uint16_t idx)
949 {
950         struct vhost_virtqueue *vq;
951         struct virtio_net *dev;
952
953         dev = get_device(vid);
954         if (unlikely(!dev))
955                 return -1;
956
957         if (unlikely(!(dev->protocol_features &
958             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
959                 return 0;
960
961         if (unlikely(vq_is_packed(dev)))
962                 return -1;
963
964         if (unlikely(vring_idx >= VHOST_MAX_VRING))
965                 return -1;
966
967         vq = dev->virtqueue[vring_idx];
968         if (unlikely(!vq))
969                 return -1;
970
971         if (unlikely(!vq->inflight_split))
972                 return -1;
973
974         if (unlikely(idx >= vq->size))
975                 return -1;
976
977         vq->inflight_split->desc[idx].counter = vq->global_counter++;
978         vq->inflight_split->desc[idx].inflight = 1;
979         return 0;
980 }
981
982 int
983 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
984                                    uint16_t head, uint16_t last,
985                                    uint16_t *inflight_entry)
986 {
987         struct rte_vhost_inflight_info_packed *inflight_info;
988         struct virtio_net *dev;
989         struct vhost_virtqueue *vq;
990         struct vring_packed_desc *desc;
991         uint16_t old_free_head, free_head;
992
993         dev = get_device(vid);
994         if (unlikely(!dev))
995                 return -1;
996
997         if (unlikely(!(dev->protocol_features &
998             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
999                 return 0;
1000
1001         if (unlikely(!vq_is_packed(dev)))
1002                 return -1;
1003
1004         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1005                 return -1;
1006
1007         vq = dev->virtqueue[vring_idx];
1008         if (unlikely(!vq))
1009                 return -1;
1010
1011         inflight_info = vq->inflight_packed;
1012         if (unlikely(!inflight_info))
1013                 return -1;
1014
1015         if (unlikely(head >= vq->size))
1016                 return -1;
1017
1018         desc = vq->desc_packed;
1019         old_free_head = inflight_info->old_free_head;
1020         if (unlikely(old_free_head >= vq->size))
1021                 return -1;
1022
1023         free_head = old_free_head;
1024
1025         /* init header descriptor */
1026         inflight_info->desc[old_free_head].num = 0;
1027         inflight_info->desc[old_free_head].counter = vq->global_counter++;
1028         inflight_info->desc[old_free_head].inflight = 1;
1029
1030         /* save desc entry in flight entry */
1031         while (head != ((last + 1) % vq->size)) {
1032                 inflight_info->desc[old_free_head].num++;
1033                 inflight_info->desc[free_head].addr = desc[head].addr;
1034                 inflight_info->desc[free_head].len = desc[head].len;
1035                 inflight_info->desc[free_head].flags = desc[head].flags;
1036                 inflight_info->desc[free_head].id = desc[head].id;
1037
1038                 inflight_info->desc[old_free_head].last = free_head;
1039                 free_head = inflight_info->desc[free_head].next;
1040                 inflight_info->free_head = free_head;
1041                 head = (head + 1) % vq->size;
1042         }
1043
1044         inflight_info->old_free_head = free_head;
1045         *inflight_entry = old_free_head;
1046
1047         return 0;
1048 }
1049
1050 int
1051 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1052                                   uint16_t last_used_idx, uint16_t idx)
1053 {
1054         struct virtio_net *dev;
1055         struct vhost_virtqueue *vq;
1056
1057         dev = get_device(vid);
1058         if (unlikely(!dev))
1059                 return -1;
1060
1061         if (unlikely(!(dev->protocol_features &
1062             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1063                 return 0;
1064
1065         if (unlikely(vq_is_packed(dev)))
1066                 return -1;
1067
1068         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1069                 return -1;
1070
1071         vq = dev->virtqueue[vring_idx];
1072         if (unlikely(!vq))
1073                 return -1;
1074
1075         if (unlikely(!vq->inflight_split))
1076                 return -1;
1077
1078         if (unlikely(idx >= vq->size))
1079                 return -1;
1080
1081         rte_smp_mb();
1082
1083         vq->inflight_split->desc[idx].inflight = 0;
1084
1085         rte_smp_mb();
1086
1087         vq->inflight_split->used_idx = last_used_idx;
1088         return 0;
1089 }
1090
1091 int
1092 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1093                                    uint16_t head)
1094 {
1095         struct rte_vhost_inflight_info_packed *inflight_info;
1096         struct virtio_net *dev;
1097         struct vhost_virtqueue *vq;
1098
1099         dev = get_device(vid);
1100         if (unlikely(!dev))
1101                 return -1;
1102
1103         if (unlikely(!(dev->protocol_features &
1104             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1105                 return 0;
1106
1107         if (unlikely(!vq_is_packed(dev)))
1108                 return -1;
1109
1110         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1111                 return -1;
1112
1113         vq = dev->virtqueue[vring_idx];
1114         if (unlikely(!vq))
1115                 return -1;
1116
1117         inflight_info = vq->inflight_packed;
1118         if (unlikely(!inflight_info))
1119                 return -1;
1120
1121         if (unlikely(head >= vq->size))
1122                 return -1;
1123
1124         rte_smp_mb();
1125
1126         inflight_info->desc[head].inflight = 0;
1127
1128         rte_smp_mb();
1129
1130         inflight_info->old_free_head = inflight_info->free_head;
1131         inflight_info->old_used_idx = inflight_info->used_idx;
1132         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1133
1134         return 0;
1135 }
1136
1137 int
1138 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1139                                      uint16_t idx)
1140 {
1141         struct virtio_net *dev;
1142         struct vhost_virtqueue *vq;
1143
1144         dev = get_device(vid);
1145         if (unlikely(!dev))
1146                 return -1;
1147
1148         if (unlikely(!(dev->protocol_features &
1149             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1150                 return 0;
1151
1152         if (unlikely(vq_is_packed(dev)))
1153                 return -1;
1154
1155         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1156                 return -1;
1157
1158         vq = dev->virtqueue[vring_idx];
1159         if (unlikely(!vq))
1160                 return -1;
1161
1162         if (unlikely(!vq->inflight_split))
1163                 return -1;
1164
1165         vq->inflight_split->last_inflight_io = idx;
1166         return 0;
1167 }
1168
1169 int
1170 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1171                                       uint16_t head)
1172 {
1173         struct rte_vhost_inflight_info_packed *inflight_info;
1174         struct virtio_net *dev;
1175         struct vhost_virtqueue *vq;
1176         uint16_t last;
1177
1178         dev = get_device(vid);
1179         if (unlikely(!dev))
1180                 return -1;
1181
1182         if (unlikely(!(dev->protocol_features &
1183             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1184                 return 0;
1185
1186         if (unlikely(!vq_is_packed(dev)))
1187                 return -1;
1188
1189         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1190                 return -1;
1191
1192         vq = dev->virtqueue[vring_idx];
1193         if (unlikely(!vq))
1194                 return -1;
1195
1196         inflight_info = vq->inflight_packed;
1197         if (unlikely(!inflight_info))
1198                 return -1;
1199
1200         if (unlikely(head >= vq->size))
1201                 return -1;
1202
1203         last = inflight_info->desc[head].last;
1204         if (unlikely(last >= vq->size))
1205                 return -1;
1206
1207         inflight_info->desc[last].next = inflight_info->free_head;
1208         inflight_info->free_head = head;
1209         inflight_info->used_idx += inflight_info->desc[head].num;
1210         if (inflight_info->used_idx >= inflight_info->desc_num) {
1211                 inflight_info->used_idx -= inflight_info->desc_num;
1212                 inflight_info->used_wrap_counter =
1213                         !inflight_info->used_wrap_counter;
1214         }
1215
1216         return 0;
1217 }
1218
1219 int
1220 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1221 {
1222         struct virtio_net *dev;
1223         struct vhost_virtqueue *vq;
1224
1225         dev = get_device(vid);
1226         if (!dev)
1227                 return -1;
1228
1229         if (vring_idx >= VHOST_MAX_VRING)
1230                 return -1;
1231
1232         vq = dev->virtqueue[vring_idx];
1233         if (!vq)
1234                 return -1;
1235
1236         if (vq_is_packed(dev))
1237                 vhost_vring_call_packed(dev, vq);
1238         else
1239                 vhost_vring_call_split(dev, vq);
1240
1241         return 0;
1242 }
1243
1244 uint16_t
1245 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1246 {
1247         struct virtio_net *dev;
1248         struct vhost_virtqueue *vq;
1249         uint16_t ret = 0;
1250
1251         dev = get_device(vid);
1252         if (!dev)
1253                 return 0;
1254
1255         vq = dev->virtqueue[queue_id];
1256
1257         rte_spinlock_lock(&vq->access_lock);
1258
1259         if (unlikely(!vq->enabled || vq->avail == NULL))
1260                 goto out;
1261
1262         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1263
1264 out:
1265         rte_spinlock_unlock(&vq->access_lock);
1266         return ret;
1267 }
1268
1269 static inline int
1270 vhost_enable_notify_split(struct virtio_net *dev,
1271                 struct vhost_virtqueue *vq, int enable)
1272 {
1273         if (vq->used == NULL)
1274                 return -1;
1275
1276         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1277                 if (enable)
1278                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1279                 else
1280                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1281         } else {
1282                 if (enable)
1283                         vhost_avail_event(vq) = vq->last_avail_idx;
1284         }
1285         return 0;
1286 }
1287
1288 static inline int
1289 vhost_enable_notify_packed(struct virtio_net *dev,
1290                 struct vhost_virtqueue *vq, int enable)
1291 {
1292         uint16_t flags;
1293
1294         if (vq->device_event == NULL)
1295                 return -1;
1296
1297         if (!enable) {
1298                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1299                 return 0;
1300         }
1301
1302         flags = VRING_EVENT_F_ENABLE;
1303         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1304                 flags = VRING_EVENT_F_DESC;
1305                 vq->device_event->off_wrap = vq->last_avail_idx |
1306                         vq->avail_wrap_counter << 15;
1307         }
1308
1309         rte_smp_wmb();
1310
1311         vq->device_event->flags = flags;
1312         return 0;
1313 }
1314
1315 int
1316 vhost_enable_guest_notification(struct virtio_net *dev,
1317                 struct vhost_virtqueue *vq, int enable)
1318 {
1319         /*
1320          * If the virtqueue is not ready yet, it will be applied
1321          * when it will become ready.
1322          */
1323         if (!vq->ready)
1324                 return 0;
1325
1326         if (vq_is_packed(dev))
1327                 return vhost_enable_notify_packed(dev, vq, enable);
1328         else
1329                 return vhost_enable_notify_split(dev, vq, enable);
1330 }
1331
1332 int
1333 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1334 {
1335         struct virtio_net *dev = get_device(vid);
1336         struct vhost_virtqueue *vq;
1337         int ret;
1338
1339         if (!dev)
1340                 return -1;
1341
1342         vq = dev->virtqueue[queue_id];
1343
1344         rte_spinlock_lock(&vq->access_lock);
1345
1346         vq->notif_enable = enable;
1347         ret = vhost_enable_guest_notification(dev, vq, enable);
1348
1349         rte_spinlock_unlock(&vq->access_lock);
1350
1351         return ret;
1352 }
1353
1354 void
1355 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1356 {
1357         struct virtio_net *dev = get_device(vid);
1358
1359         if (dev == NULL)
1360                 return;
1361
1362         vhost_log_write(dev, addr, len);
1363 }
1364
1365 void
1366 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1367                          uint64_t offset, uint64_t len)
1368 {
1369         struct virtio_net *dev;
1370         struct vhost_virtqueue *vq;
1371
1372         dev = get_device(vid);
1373         if (dev == NULL)
1374                 return;
1375
1376         if (vring_idx >= VHOST_MAX_VRING)
1377                 return;
1378         vq = dev->virtqueue[vring_idx];
1379         if (!vq)
1380                 return;
1381
1382         vhost_log_used_vring(dev, vq, offset, len);
1383 }
1384
1385 uint32_t
1386 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1387 {
1388         struct virtio_net *dev;
1389         struct vhost_virtqueue *vq;
1390         uint32_t ret = 0;
1391
1392         dev = get_device(vid);
1393         if (dev == NULL)
1394                 return 0;
1395
1396         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1397                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1398                         dev->vid, __func__, qid);
1399                 return 0;
1400         }
1401
1402         vq = dev->virtqueue[qid];
1403         if (vq == NULL)
1404                 return 0;
1405
1406         rte_spinlock_lock(&vq->access_lock);
1407
1408         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1409                 goto out;
1410
1411         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1412
1413 out:
1414         rte_spinlock_unlock(&vq->access_lock);
1415         return ret;
1416 }
1417
1418 struct rte_vdpa_device *
1419 rte_vhost_get_vdpa_device(int vid)
1420 {
1421         struct virtio_net *dev = get_device(vid);
1422
1423         if (dev == NULL)
1424                 return NULL;
1425
1426         return dev->vdpa_dev;
1427 }
1428
1429 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1430                 uint64_t *log_size)
1431 {
1432         struct virtio_net *dev = get_device(vid);
1433
1434         if (dev == NULL || log_base == NULL || log_size == NULL)
1435                 return -1;
1436
1437         *log_base = dev->log_base;
1438         *log_size = dev->log_size;
1439
1440         return 0;
1441 }
1442
1443 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1444                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1445 {
1446         struct vhost_virtqueue *vq;
1447         struct virtio_net *dev = get_device(vid);
1448
1449         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1450                 return -1;
1451
1452         vq = dev->virtqueue[queue_id];
1453         if (!vq)
1454                 return -1;
1455
1456         if (vq_is_packed(dev)) {
1457                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1458                                   vq->last_avail_idx;
1459                 *last_used_idx = (vq->used_wrap_counter << 15) |
1460                                  vq->last_used_idx;
1461         } else {
1462                 *last_avail_idx = vq->last_avail_idx;
1463                 *last_used_idx = vq->last_used_idx;
1464         }
1465
1466         return 0;
1467 }
1468
1469 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1470                 uint16_t last_avail_idx, uint16_t last_used_idx)
1471 {
1472         struct vhost_virtqueue *vq;
1473         struct virtio_net *dev = get_device(vid);
1474
1475         if (!dev)
1476                 return -1;
1477
1478         vq = dev->virtqueue[queue_id];
1479         if (!vq)
1480                 return -1;
1481
1482         if (vq_is_packed(dev)) {
1483                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1484                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1485                 vq->last_used_idx = last_used_idx & 0x7fff;
1486                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1487         } else {
1488                 vq->last_avail_idx = last_avail_idx;
1489                 vq->last_used_idx = last_used_idx;
1490         }
1491
1492         return 0;
1493 }
1494
1495 int
1496 rte_vhost_get_vring_base_from_inflight(int vid,
1497                                        uint16_t queue_id,
1498                                        uint16_t *last_avail_idx,
1499                                        uint16_t *last_used_idx)
1500 {
1501         struct rte_vhost_inflight_info_packed *inflight_info;
1502         struct virtio_net *dev = get_device(vid);
1503
1504         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1505                 return -1;
1506
1507         if (!vq_is_packed(dev))
1508                 return -1;
1509
1510         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1511         if (!inflight_info)
1512                 return -1;
1513
1514         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1515                           inflight_info->old_used_idx;
1516         *last_used_idx = *last_avail_idx;
1517
1518         return 0;
1519 }
1520
1521 int rte_vhost_extern_callback_register(int vid,
1522                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1523 {
1524         struct virtio_net *dev = get_device(vid);
1525
1526         if (dev == NULL || ops == NULL)
1527                 return -1;
1528
1529         dev->extern_ops = *ops;
1530         dev->extern_data = ctx;
1531         return 0;
1532 }
1533
1534 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1535                                         uint32_t features,
1536                                         struct rte_vhost_async_channel_ops *ops)
1537 {
1538         struct vhost_virtqueue *vq;
1539         struct virtio_net *dev = get_device(vid);
1540         struct rte_vhost_async_features f;
1541
1542         if (dev == NULL || ops == NULL)
1543                 return -1;
1544
1545         f.intval = features;
1546
1547         vq = dev->virtqueue[queue_id];
1548
1549         if (unlikely(vq == NULL || !dev->async_copy))
1550                 return -1;
1551
1552         /* packed queue is not supported */
1553         if (unlikely(vq_is_packed(dev) || !f.async_inorder)) {
1554                 VHOST_LOG_CONFIG(ERR,
1555                         "async copy is not supported on packed queue or non-inorder mode "
1556                         "(vid %d, qid: %d)\n", vid, queue_id);
1557                 return -1;
1558         }
1559
1560         if (unlikely(ops->check_completed_copies == NULL ||
1561                 ops->transfer_data == NULL))
1562                 return -1;
1563
1564         rte_spinlock_lock(&vq->access_lock);
1565
1566         if (unlikely(vq->async_registered)) {
1567                 VHOST_LOG_CONFIG(ERR,
1568                         "async register failed: channel already registered "
1569                         "(vid %d, qid: %d)\n", vid, queue_id);
1570                 goto reg_out;
1571         }
1572
1573         vq->async_pkts_pending = rte_malloc(NULL,
1574                         vq->size * sizeof(uintptr_t),
1575                         RTE_CACHE_LINE_SIZE);
1576         vq->async_pending_info = rte_malloc(NULL,
1577                         vq->size * sizeof(uint64_t),
1578                         RTE_CACHE_LINE_SIZE);
1579         if (!vq->async_pkts_pending || !vq->async_pending_info) {
1580                 if (vq->async_pkts_pending)
1581                         rte_free(vq->async_pkts_pending);
1582
1583                 if (vq->async_pending_info)
1584                         rte_free(vq->async_pending_info);
1585
1586                 VHOST_LOG_CONFIG(ERR,
1587                                 "async register failed: cannot allocate memory for vq data "
1588                                 "(vid %d, qid: %d)\n", vid, queue_id);
1589                 goto reg_out;
1590         }
1591
1592         vq->async_ops.check_completed_copies = ops->check_completed_copies;
1593         vq->async_ops.transfer_data = ops->transfer_data;
1594
1595         vq->async_inorder = f.async_inorder;
1596         vq->async_threshold = f.async_threshold;
1597
1598         vq->async_registered = true;
1599
1600 reg_out:
1601         rte_spinlock_unlock(&vq->access_lock);
1602
1603         return 0;
1604 }
1605
1606 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1607 {
1608         struct vhost_virtqueue *vq;
1609         struct virtio_net *dev = get_device(vid);
1610         int ret = -1;
1611
1612         if (dev == NULL)
1613                 return ret;
1614
1615         vq = dev->virtqueue[queue_id];
1616
1617         if (vq == NULL)
1618                 return ret;
1619
1620         ret = 0;
1621         rte_spinlock_lock(&vq->access_lock);
1622
1623         if (!vq->async_registered)
1624                 goto out;
1625
1626         if (vq->async_pkts_inflight_n) {
1627                 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1628                         "async inflight packets must be completed before unregistration.\n");
1629                 ret = -1;
1630                 goto out;
1631         }
1632
1633         if (vq->async_pkts_pending) {
1634                 rte_free(vq->async_pkts_pending);
1635                 vq->async_pkts_pending = NULL;
1636         }
1637
1638         if (vq->async_pending_info) {
1639                 rte_free(vq->async_pending_info);
1640                 vq->async_pending_info = NULL;
1641         }
1642
1643         vq->async_ops.transfer_data = NULL;
1644         vq->async_ops.check_completed_copies = NULL;
1645         vq->async_registered = false;
1646
1647 out:
1648         rte_spinlock_unlock(&vq->access_lock);
1649
1650         return ret;
1651 }
1652
1653 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1654 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);