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