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