91026b3e13fe7f3556fcf53122d43ea045156663
[dpdk.git] / lib / librte_vhost / vhost.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
11 #include <numa.h>
12 #include <numaif.h>
13 #endif
14
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
17 #include <rte_log.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
23
24 #include "iotlb.h"
25 #include "vhost.h"
26 #include "vhost_user.h"
27
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29
30 /* Called with iotlb_lock read-locked */
31 uint64_t
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33                     uint64_t iova, uint64_t *size, uint8_t perm)
34 {
35         uint64_t vva, tmp_size;
36
37         if (unlikely(!*size))
38                 return 0;
39
40         tmp_size = *size;
41
42         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43         if (tmp_size == *size)
44                 return vva;
45
46         iova += tmp_size;
47
48         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
49                 /*
50                  * iotlb_lock is read-locked for a full burst,
51                  * but it only protects the iotlb cache.
52                  * In case of IOTLB miss, we might block on the socket,
53                  * which could cause a deadlock with QEMU if an IOTLB update
54                  * is being handled. We can safely unlock here to avoid it.
55                  */
56                 vhost_user_iotlb_rd_unlock(vq);
57
58                 vhost_user_iotlb_pending_insert(vq, iova, perm);
59                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
60                         RTE_LOG(ERR, VHOST_CONFIG,
61                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
62                                 iova);
63                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
64                 }
65
66                 vhost_user_iotlb_rd_lock(vq);
67         }
68
69         return 0;
70 }
71
72 void
73 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
74 {
75         if ((vq->callfd >= 0) && (destroy != 0))
76                 close(vq->callfd);
77         if (vq->kickfd >= 0)
78                 close(vq->kickfd);
79 }
80
81 /*
82  * Unmap any memory, close any file descriptors and
83  * free any memory owned by a device.
84  */
85 void
86 cleanup_device(struct virtio_net *dev, int destroy)
87 {
88         uint32_t i;
89
90         vhost_backend_cleanup(dev);
91
92         for (i = 0; i < dev->nr_vring; i++)
93                 cleanup_vq(dev->virtqueue[i], destroy);
94 }
95
96 void
97 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
98 {
99         if (vq_is_packed(dev))
100                 rte_free(vq->shadow_used_packed);
101         else
102                 rte_free(vq->shadow_used_split);
103         rte_free(vq->batch_copy_elems);
104         rte_mempool_free(vq->iotlb_pool);
105         rte_free(vq);
106 }
107
108 /*
109  * Release virtqueues and device memory.
110  */
111 static void
112 free_device(struct virtio_net *dev)
113 {
114         uint32_t i;
115
116         for (i = 0; i < dev->nr_vring; i++)
117                 free_vq(dev, dev->virtqueue[i]);
118
119         rte_free(dev);
120 }
121
122 static int
123 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
124 {
125         uint64_t req_size, size;
126
127         req_size = sizeof(struct vring_desc) * vq->size;
128         size = req_size;
129         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
130                                                 vq->ring_addrs.desc_user_addr,
131                                                 &size, VHOST_ACCESS_RW);
132         if (!vq->desc || size != req_size)
133                 return -1;
134
135         req_size = sizeof(struct vring_avail);
136         req_size += sizeof(uint16_t) * vq->size;
137         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
138                 req_size += sizeof(uint16_t);
139         size = req_size;
140         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
141                                                 vq->ring_addrs.avail_user_addr,
142                                                 &size, VHOST_ACCESS_RW);
143         if (!vq->avail || size != req_size)
144                 return -1;
145
146         req_size = sizeof(struct vring_used);
147         req_size += sizeof(struct vring_used_elem) * vq->size;
148         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
149                 req_size += sizeof(uint16_t);
150         size = req_size;
151         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
152                                                 vq->ring_addrs.used_user_addr,
153                                                 &size, VHOST_ACCESS_RW);
154         if (!vq->used || size != req_size)
155                 return -1;
156
157         return 0;
158 }
159
160 static int
161 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
162 {
163         uint64_t req_size, size;
164
165         req_size = sizeof(struct vring_packed_desc) * vq->size;
166         size = req_size;
167         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
168                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
169                                 &size, VHOST_ACCESS_RW);
170         if (!vq->desc_packed || size != req_size)
171                 return -1;
172
173         req_size = sizeof(struct vring_packed_desc_event);
174         size = req_size;
175         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
176                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
177                                 &size, VHOST_ACCESS_RW);
178         if (!vq->driver_event || size != req_size)
179                 return -1;
180
181         req_size = sizeof(struct vring_packed_desc_event);
182         size = req_size;
183         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
184                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
185                                 &size, VHOST_ACCESS_RW);
186         if (!vq->device_event || size != req_size)
187                 return -1;
188
189         return 0;
190 }
191
192 int
193 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
194 {
195
196         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
197                 goto out;
198
199         if (vq_is_packed(dev)) {
200                 if (vring_translate_packed(dev, vq) < 0)
201                         return -1;
202         } else {
203                 if (vring_translate_split(dev, vq) < 0)
204                         return -1;
205         }
206 out:
207         vq->access_ok = 1;
208
209         return 0;
210 }
211
212 void
213 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
214 {
215         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
216                 vhost_user_iotlb_wr_lock(vq);
217
218         vq->access_ok = 0;
219         vq->desc = NULL;
220         vq->avail = NULL;
221         vq->used = NULL;
222
223         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
224                 vhost_user_iotlb_wr_unlock(vq);
225 }
226
227 static void
228 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
229 {
230         struct vhost_virtqueue *vq;
231
232         if (vring_idx >= VHOST_MAX_VRING) {
233                 RTE_LOG(ERR, VHOST_CONFIG,
234                                 "Failed not init vring, out of bound (%d)\n",
235                                 vring_idx);
236                 return;
237         }
238
239         vq = dev->virtqueue[vring_idx];
240
241         memset(vq, 0, sizeof(struct vhost_virtqueue));
242
243         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
244         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
245
246         vhost_user_iotlb_init(dev, vring_idx);
247         /* Backends are set to -1 indicating an inactive device. */
248         vq->backend = -1;
249
250         TAILQ_INIT(&vq->zmbuf_list);
251 }
252
253 static void
254 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
255 {
256         struct vhost_virtqueue *vq;
257         int callfd;
258
259         if (vring_idx >= VHOST_MAX_VRING) {
260                 RTE_LOG(ERR, VHOST_CONFIG,
261                                 "Failed not init vring, out of bound (%d)\n",
262                                 vring_idx);
263                 return;
264         }
265
266         vq = dev->virtqueue[vring_idx];
267         callfd = vq->callfd;
268         init_vring_queue(dev, vring_idx);
269         vq->callfd = callfd;
270 }
271
272 int
273 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
274 {
275         struct vhost_virtqueue *vq;
276
277         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
278         if (vq == NULL) {
279                 RTE_LOG(ERR, VHOST_CONFIG,
280                         "Failed to allocate memory for vring:%u.\n", vring_idx);
281                 return -1;
282         }
283
284         dev->virtqueue[vring_idx] = vq;
285         init_vring_queue(dev, vring_idx);
286         rte_spinlock_init(&vq->access_lock);
287         vq->avail_wrap_counter = 1;
288         vq->used_wrap_counter = 1;
289         vq->signalled_used_valid = false;
290
291         dev->nr_vring += 1;
292
293         return 0;
294 }
295
296 /*
297  * Reset some variables in device structure, while keeping few
298  * others untouched, such as vid, ifname, nr_vring: they
299  * should be same unless the device is removed.
300  */
301 void
302 reset_device(struct virtio_net *dev)
303 {
304         uint32_t i;
305
306         dev->features = 0;
307         dev->protocol_features = 0;
308         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
309
310         for (i = 0; i < dev->nr_vring; i++)
311                 reset_vring_queue(dev, i);
312 }
313
314 /*
315  * Invoked when there is a new vhost-user connection established (when
316  * there is a new virtio device being attached).
317  */
318 int
319 vhost_new_device(void)
320 {
321         struct virtio_net *dev;
322         int i;
323
324         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
325                 if (vhost_devices[i] == NULL)
326                         break;
327         }
328
329         if (i == MAX_VHOST_DEVICE) {
330                 RTE_LOG(ERR, VHOST_CONFIG,
331                         "Failed to find a free slot for new device.\n");
332                 return -1;
333         }
334
335         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
336         if (dev == NULL) {
337                 RTE_LOG(ERR, VHOST_CONFIG,
338                         "Failed to allocate memory for new dev.\n");
339                 return -1;
340         }
341
342         vhost_devices[i] = dev;
343         dev->vid = i;
344         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
345         dev->slave_req_fd = -1;
346         dev->vdpa_dev_id = -1;
347         rte_spinlock_init(&dev->slave_req_lock);
348
349         return i;
350 }
351
352 void
353 vhost_destroy_device_notify(struct virtio_net *dev)
354 {
355         struct rte_vdpa_device *vdpa_dev;
356         int did;
357
358         if (dev->flags & VIRTIO_DEV_RUNNING) {
359                 did = dev->vdpa_dev_id;
360                 vdpa_dev = rte_vdpa_get_device(did);
361                 if (vdpa_dev && vdpa_dev->ops->dev_close)
362                         vdpa_dev->ops->dev_close(dev->vid);
363                 dev->flags &= ~VIRTIO_DEV_RUNNING;
364                 dev->notify_ops->destroy_device(dev->vid);
365         }
366 }
367
368 /*
369  * Invoked when there is the vhost-user connection is broken (when
370  * the virtio device is being detached).
371  */
372 void
373 vhost_destroy_device(int vid)
374 {
375         struct virtio_net *dev = get_device(vid);
376
377         if (dev == NULL)
378                 return;
379
380         vhost_destroy_device_notify(dev);
381
382         cleanup_device(dev, 1);
383         free_device(dev);
384
385         vhost_devices[vid] = NULL;
386 }
387
388 void
389 vhost_attach_vdpa_device(int vid, int did)
390 {
391         struct virtio_net *dev = get_device(vid);
392
393         if (dev == NULL)
394                 return;
395
396         if (rte_vdpa_get_device(did) == NULL)
397                 return;
398
399         dev->vdpa_dev_id = did;
400 }
401
402 void
403 vhost_detach_vdpa_device(int vid)
404 {
405         struct virtio_net *dev = get_device(vid);
406
407         if (dev == NULL)
408                 return;
409
410         vhost_user_host_notifier_ctrl(vid, false);
411
412         dev->vdpa_dev_id = -1;
413 }
414
415 void
416 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
417 {
418         struct virtio_net *dev;
419         unsigned int len;
420
421         dev = get_device(vid);
422         if (dev == NULL)
423                 return;
424
425         len = if_len > sizeof(dev->ifname) ?
426                 sizeof(dev->ifname) : if_len;
427
428         strncpy(dev->ifname, if_name, len);
429         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
430 }
431
432 void
433 vhost_enable_dequeue_zero_copy(int vid)
434 {
435         struct virtio_net *dev = get_device(vid);
436
437         if (dev == NULL)
438                 return;
439
440         dev->dequeue_zero_copy = 1;
441 }
442
443 void
444 vhost_set_builtin_virtio_net(int vid, bool enable)
445 {
446         struct virtio_net *dev = get_device(vid);
447
448         if (dev == NULL)
449                 return;
450
451         if (enable)
452                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
453         else
454                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
455 }
456
457 int
458 rte_vhost_get_mtu(int vid, uint16_t *mtu)
459 {
460         struct virtio_net *dev = get_device(vid);
461
462         if (!dev)
463                 return -ENODEV;
464
465         if (!(dev->flags & VIRTIO_DEV_READY))
466                 return -EAGAIN;
467
468         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
469                 return -ENOTSUP;
470
471         *mtu = dev->mtu;
472
473         return 0;
474 }
475
476 int
477 rte_vhost_get_numa_node(int vid)
478 {
479 #ifdef RTE_LIBRTE_VHOST_NUMA
480         struct virtio_net *dev = get_device(vid);
481         int numa_node;
482         int ret;
483
484         if (dev == NULL || numa_available() != 0)
485                 return -1;
486
487         ret = get_mempolicy(&numa_node, NULL, 0, dev,
488                             MPOL_F_NODE | MPOL_F_ADDR);
489         if (ret < 0) {
490                 RTE_LOG(ERR, VHOST_CONFIG,
491                         "(%d) failed to query numa node: %s\n",
492                         vid, rte_strerror(errno));
493                 return -1;
494         }
495
496         return numa_node;
497 #else
498         RTE_SET_USED(vid);
499         return -1;
500 #endif
501 }
502
503 uint32_t
504 rte_vhost_get_queue_num(int vid)
505 {
506         struct virtio_net *dev = get_device(vid);
507
508         if (dev == NULL)
509                 return 0;
510
511         return dev->nr_vring / 2;
512 }
513
514 uint16_t
515 rte_vhost_get_vring_num(int vid)
516 {
517         struct virtio_net *dev = get_device(vid);
518
519         if (dev == NULL)
520                 return 0;
521
522         return dev->nr_vring;
523 }
524
525 int
526 rte_vhost_get_ifname(int vid, char *buf, size_t len)
527 {
528         struct virtio_net *dev = get_device(vid);
529
530         if (dev == NULL)
531                 return -1;
532
533         len = RTE_MIN(len, sizeof(dev->ifname));
534
535         strncpy(buf, dev->ifname, len);
536         buf[len - 1] = '\0';
537
538         return 0;
539 }
540
541 int
542 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
543 {
544         struct virtio_net *dev;
545
546         dev = get_device(vid);
547         if (!dev)
548                 return -1;
549
550         *features = dev->features;
551         return 0;
552 }
553
554 int
555 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
556 {
557         struct virtio_net *dev;
558         struct rte_vhost_memory *m;
559         size_t size;
560
561         dev = get_device(vid);
562         if (!dev)
563                 return -1;
564
565         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
566         m = malloc(sizeof(struct rte_vhost_memory) + size);
567         if (!m)
568                 return -1;
569
570         m->nregions = dev->mem->nregions;
571         memcpy(m->regions, dev->mem->regions, size);
572         *mem = m;
573
574         return 0;
575 }
576
577 int
578 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
579                           struct rte_vhost_vring *vring)
580 {
581         struct virtio_net *dev;
582         struct vhost_virtqueue *vq;
583
584         dev = get_device(vid);
585         if (!dev)
586                 return -1;
587
588         if (vring_idx >= VHOST_MAX_VRING)
589                 return -1;
590
591         vq = dev->virtqueue[vring_idx];
592         if (!vq)
593                 return -1;
594
595         vring->desc  = vq->desc;
596         vring->avail = vq->avail;
597         vring->used  = vq->used;
598         vring->log_guest_addr  = vq->log_guest_addr;
599
600         vring->callfd  = vq->callfd;
601         vring->kickfd  = vq->kickfd;
602         vring->size    = vq->size;
603
604         return 0;
605 }
606
607 int
608 rte_vhost_vring_call(int vid, uint16_t vring_idx)
609 {
610         struct virtio_net *dev;
611         struct vhost_virtqueue *vq;
612
613         dev = get_device(vid);
614         if (!dev)
615                 return -1;
616
617         if (vring_idx >= VHOST_MAX_VRING)
618                 return -1;
619
620         vq = dev->virtqueue[vring_idx];
621         if (!vq)
622                 return -1;
623
624         if (vq_is_packed(dev))
625                 vhost_vring_call_packed(dev, vq);
626         else
627                 vhost_vring_call_split(dev, vq);
628
629         return 0;
630 }
631
632 uint16_t
633 rte_vhost_avail_entries(int vid, uint16_t queue_id)
634 {
635         struct virtio_net *dev;
636         struct vhost_virtqueue *vq;
637
638         dev = get_device(vid);
639         if (!dev)
640                 return 0;
641
642         vq = dev->virtqueue[queue_id];
643         if (!vq->enabled)
644                 return 0;
645
646         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
647 }
648
649 static inline void
650 vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
651 {
652         if (enable)
653                 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
654         else
655                 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
656 }
657
658 static inline void
659 vhost_enable_notify_packed(struct virtio_net *dev,
660                 struct vhost_virtqueue *vq, int enable)
661 {
662         uint16_t flags;
663
664         if (!enable)
665                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
666
667         flags = VRING_EVENT_F_ENABLE;
668         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
669                 flags = VRING_EVENT_F_DESC;
670                 vq->device_event->off_wrap = vq->last_avail_idx |
671                         vq->avail_wrap_counter << 15;
672         }
673
674         rte_smp_wmb();
675
676         vq->device_event->flags = flags;
677 }
678
679 int
680 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
681 {
682         struct virtio_net *dev = get_device(vid);
683         struct vhost_virtqueue *vq;
684
685         if (!dev)
686                 return -1;
687
688         vq = dev->virtqueue[queue_id];
689
690         if (vq_is_packed(dev))
691                 vhost_enable_notify_packed(dev, vq, enable);
692         else
693                 vhost_enable_notify_split(vq, enable);
694
695         return 0;
696 }
697
698 void
699 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
700 {
701         struct virtio_net *dev = get_device(vid);
702
703         if (dev == NULL)
704                 return;
705
706         vhost_log_write(dev, addr, len);
707 }
708
709 void
710 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
711                          uint64_t offset, uint64_t len)
712 {
713         struct virtio_net *dev;
714         struct vhost_virtqueue *vq;
715
716         dev = get_device(vid);
717         if (dev == NULL)
718                 return;
719
720         if (vring_idx >= VHOST_MAX_VRING)
721                 return;
722         vq = dev->virtqueue[vring_idx];
723         if (!vq)
724                 return;
725
726         vhost_log_used_vring(dev, vq, offset, len);
727 }
728
729 uint32_t
730 rte_vhost_rx_queue_count(int vid, uint16_t qid)
731 {
732         struct virtio_net *dev;
733         struct vhost_virtqueue *vq;
734
735         dev = get_device(vid);
736         if (dev == NULL)
737                 return 0;
738
739         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
740                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
741                         dev->vid, __func__, qid);
742                 return 0;
743         }
744
745         vq = dev->virtqueue[qid];
746         if (vq == NULL)
747                 return 0;
748
749         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
750                 return 0;
751
752         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
753 }
754
755 int rte_vhost_get_vdpa_device_id(int vid)
756 {
757         struct virtio_net *dev = get_device(vid);
758
759         if (dev == NULL)
760                 return -1;
761
762         return dev->vdpa_dev_id;
763 }
764
765 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
766                 uint64_t *log_size)
767 {
768         struct virtio_net *dev = get_device(vid);
769
770         if (!dev)
771                 return -1;
772
773         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
774                 RTE_LOG(ERR, VHOST_DATA,
775                         "(%d) %s: built-in vhost net backend is disabled.\n",
776                         dev->vid, __func__);
777                 return -1;
778         }
779
780         *log_base = dev->log_base;
781         *log_size = dev->log_size;
782
783         return 0;
784 }
785
786 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
787                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
788 {
789         struct virtio_net *dev = get_device(vid);
790
791         if (!dev)
792                 return -1;
793
794         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
795                 RTE_LOG(ERR, VHOST_DATA,
796                         "(%d) %s: built-in vhost net backend is disabled.\n",
797                         dev->vid, __func__);
798                 return -1;
799         }
800
801         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
802         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
803
804         return 0;
805 }
806
807 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
808                 uint16_t last_avail_idx, uint16_t last_used_idx)
809 {
810         struct virtio_net *dev = get_device(vid);
811
812         if (!dev)
813                 return -1;
814
815         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
816                 RTE_LOG(ERR, VHOST_DATA,
817                         "(%d) %s: built-in vhost net backend is disabled.\n",
818                         dev->vid, __func__);
819                 return -1;
820         }
821
822         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
823         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
824
825         return 0;
826 }