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