vhost: support inflight info sharing
[dpdk.git] / lib / librte_vhost / vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <unistd.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
16 #include <linux/if.h>
17
18 #include <rte_log.h>
19 #include <rte_ether.h>
20 #include <rte_rwlock.h>
21 #include <rte_malloc.h>
22
23 #include "rte_vhost.h"
24 #include "rte_vdpa.h"
25
26 /* Used to indicate that the device is running on a data core */
27 #define VIRTIO_DEV_RUNNING 1
28 /* Used to indicate that the device is ready to operate */
29 #define VIRTIO_DEV_READY 2
30 /* Used to indicate that the built-in vhost net device backend is enabled */
31 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
32 /* Used to indicate that the device has its own data path and configured */
33 #define VIRTIO_DEV_VDPA_CONFIGURED 8
34
35 /* Backend value set by guest. */
36 #define VIRTIO_DEV_STOPPED -1
37
38 #define BUF_VECTOR_MAX 256
39
40 #define VHOST_LOG_CACHE_NR 32
41
42 /**
43  * Structure contains buffer address, length and descriptor index
44  * from vring to do scatter RX.
45  */
46 struct buf_vector {
47         uint64_t buf_iova;
48         uint64_t buf_addr;
49         uint32_t buf_len;
50         uint32_t desc_idx;
51 };
52
53 /*
54  * A structure to hold some fields needed in zero copy code path,
55  * mainly for associating an mbuf with the right desc_idx.
56  */
57 struct zcopy_mbuf {
58         struct rte_mbuf *mbuf;
59         uint32_t desc_idx;
60         uint16_t desc_count;
61         uint16_t in_use;
62
63         TAILQ_ENTRY(zcopy_mbuf) next;
64 };
65 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
66
67 /*
68  * Structure contains the info for each batched memory copy.
69  */
70 struct batch_copy_elem {
71         void *dst;
72         void *src;
73         uint32_t len;
74         uint64_t log_addr;
75 };
76
77 /*
78  * Structure that contains the info for batched dirty logging.
79  */
80 struct log_cache_entry {
81         uint32_t offset;
82         unsigned long val;
83 };
84
85 struct vring_used_elem_packed {
86         uint16_t id;
87         uint32_t len;
88         uint32_t count;
89 };
90
91 /**
92  * Structure contains variables relevant to RX/TX virtqueues.
93  */
94 struct vhost_virtqueue {
95         union {
96                 struct vring_desc       *desc;
97                 struct vring_packed_desc   *desc_packed;
98         };
99         union {
100                 struct vring_avail      *avail;
101                 struct vring_packed_desc_event *driver_event;
102         };
103         union {
104                 struct vring_used       *used;
105                 struct vring_packed_desc_event *device_event;
106         };
107         uint32_t                size;
108
109         uint16_t                last_avail_idx;
110         uint16_t                last_used_idx;
111         /* Last used index we notify to front end. */
112         uint16_t                signalled_used;
113         bool                    signalled_used_valid;
114 #define VIRTIO_INVALID_EVENTFD          (-1)
115 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
116
117         /* Backend value to determine if device should started/stopped */
118         int                     backend;
119         int                     enabled;
120         int                     access_ok;
121         rte_spinlock_t          access_lock;
122
123         /* Used to notify the guest (trigger interrupt) */
124         int                     callfd;
125         /* Currently unused as polling mode is enabled */
126         int                     kickfd;
127
128         /* Physical address of used ring, for logging */
129         uint64_t                log_guest_addr;
130
131         uint16_t                nr_zmbuf;
132         uint16_t                zmbuf_size;
133         uint16_t                last_zmbuf_idx;
134         struct zcopy_mbuf       *zmbufs;
135         struct zcopy_mbuf_list  zmbuf_list;
136
137         union {
138                 struct vring_used_elem  *shadow_used_split;
139                 struct vring_used_elem_packed *shadow_used_packed;
140         };
141         uint16_t                shadow_used_idx;
142         struct vhost_vring_addr ring_addrs;
143
144         struct batch_copy_elem  *batch_copy_elems;
145         uint16_t                batch_copy_nb_elems;
146         bool                    used_wrap_counter;
147         bool                    avail_wrap_counter;
148
149         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
150         uint16_t log_cache_nb_elem;
151
152         rte_rwlock_t    iotlb_lock;
153         rte_rwlock_t    iotlb_pending_lock;
154         struct rte_mempool *iotlb_pool;
155         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
156         int                             iotlb_cache_nr;
157         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
158 } __rte_cache_aligned;
159
160 /* Old kernels have no such macros defined */
161 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
162  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
163 #endif
164
165 #ifndef VIRTIO_NET_F_MQ
166  #define VIRTIO_NET_F_MQ                22
167 #endif
168
169 #define VHOST_MAX_VRING                 0x100
170 #define VHOST_MAX_QUEUE_PAIRS           0x80
171
172 #ifndef VIRTIO_NET_F_MTU
173  #define VIRTIO_NET_F_MTU 3
174 #endif
175
176 #ifndef VIRTIO_F_ANY_LAYOUT
177  #define VIRTIO_F_ANY_LAYOUT            27
178 #endif
179
180 /* Declare IOMMU related bits for older kernels */
181 #ifndef VIRTIO_F_IOMMU_PLATFORM
182
183 #define VIRTIO_F_IOMMU_PLATFORM 33
184
185 struct vhost_iotlb_msg {
186         __u64 iova;
187         __u64 size;
188         __u64 uaddr;
189 #define VHOST_ACCESS_RO      0x1
190 #define VHOST_ACCESS_WO      0x2
191 #define VHOST_ACCESS_RW      0x3
192         __u8 perm;
193 #define VHOST_IOTLB_MISS           1
194 #define VHOST_IOTLB_UPDATE         2
195 #define VHOST_IOTLB_INVALIDATE     3
196 #define VHOST_IOTLB_ACCESS_FAIL    4
197         __u8 type;
198 };
199
200 #define VHOST_IOTLB_MSG 0x1
201
202 struct vhost_msg {
203         int type;
204         union {
205                 struct vhost_iotlb_msg iotlb;
206                 __u8 padding[64];
207         };
208 };
209 #endif
210
211 /*
212  * Define virtio 1.0 for older kernels
213  */
214 #ifndef VIRTIO_F_VERSION_1
215  #define VIRTIO_F_VERSION_1 32
216 #endif
217
218 /* Declare packed ring related bits for older kernels */
219 #ifndef VIRTIO_F_RING_PACKED
220
221 #define VIRTIO_F_RING_PACKED 34
222
223 struct vring_packed_desc {
224         uint64_t addr;
225         uint32_t len;
226         uint16_t id;
227         uint16_t flags;
228 };
229
230 struct vring_packed_desc_event {
231         uint16_t off_wrap;
232         uint16_t flags;
233 };
234 #endif
235
236 /*
237  * Declare below packed ring defines unconditionally
238  * as Kernel header might use different names.
239  */
240 #define VRING_DESC_F_AVAIL      (1ULL << 7)
241 #define VRING_DESC_F_USED       (1ULL << 15)
242
243 #define VRING_EVENT_F_ENABLE 0x0
244 #define VRING_EVENT_F_DISABLE 0x1
245 #define VRING_EVENT_F_DESC 0x2
246
247 /*
248  * Available and used descs are in same order
249  */
250 #ifndef VIRTIO_F_IN_ORDER
251 #define VIRTIO_F_IN_ORDER      35
252 #endif
253
254 /* Features supported by this builtin vhost-user net driver. */
255 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
256                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
257                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
258                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
259                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
260                                 (1ULL << VIRTIO_NET_F_MQ)      | \
261                                 (1ULL << VIRTIO_F_VERSION_1)   | \
262                                 (1ULL << VHOST_F_LOG_ALL)      | \
263                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
264                                 (1ULL << VIRTIO_NET_F_GSO) | \
265                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
266                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
267                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
268                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
269                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
270                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
271                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
272                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
273                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
274                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
275                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
276                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
277                                 (1ULL << VIRTIO_NET_F_MTU)  | \
278                                 (1ULL << VIRTIO_F_IN_ORDER) | \
279                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
280                                 (1ULL << VIRTIO_F_RING_PACKED))
281
282
283 struct guest_page {
284         uint64_t guest_phys_addr;
285         uint64_t host_phys_addr;
286         uint64_t size;
287 };
288
289 struct inflight_mem_info {
290         int             fd;
291         void            *addr;
292         uint64_t        size;
293 };
294
295 /**
296  * Device structure contains all configuration information relating
297  * to the device.
298  */
299 struct virtio_net {
300         /* Frontend (QEMU) memory and memory region information */
301         struct rte_vhost_memory *mem;
302         uint64_t                features;
303         uint64_t                protocol_features;
304         int                     vid;
305         uint32_t                flags;
306         uint16_t                vhost_hlen;
307         /* to tell if we need broadcast rarp packet */
308         rte_atomic16_t          broadcast_rarp;
309         uint32_t                nr_vring;
310         int                     dequeue_zero_copy;
311         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
312         struct inflight_mem_info *inflight_info;
313 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
314         char                    ifname[IF_NAME_SZ];
315         uint64_t                log_size;
316         uint64_t                log_base;
317         uint64_t                log_addr;
318         struct rte_ether_addr   mac;
319         uint16_t                mtu;
320
321         struct vhost_device_ops const *notify_ops;
322
323         uint32_t                nr_guest_pages;
324         uint32_t                max_guest_pages;
325         struct guest_page       *guest_pages;
326
327         int                     slave_req_fd;
328         rte_spinlock_t          slave_req_lock;
329
330         int                     postcopy_ufd;
331         int                     postcopy_listening;
332
333         /*
334          * Device id to identify a specific backend device.
335          * It's set to -1 for the default software implementation.
336          */
337         int                     vdpa_dev_id;
338
339         /* context data for the external message handlers */
340         void                    *extern_data;
341         /* pre and post vhost user message handlers for the device */
342         struct rte_vhost_user_extern_ops extern_ops;
343 } __rte_cache_aligned;
344
345 static __rte_always_inline bool
346 vq_is_packed(struct virtio_net *dev)
347 {
348         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
349 }
350
351 static inline bool
352 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
353 {
354         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
355
356         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
357                 wrap_counter != !!(flags & VRING_DESC_F_USED);
358 }
359
360 void __vhost_log_cache_write(struct virtio_net *dev,
361                 struct vhost_virtqueue *vq,
362                 uint64_t addr, uint64_t len);
363 void __vhost_log_cache_write_iova(struct virtio_net *dev,
364                 struct vhost_virtqueue *vq,
365                 uint64_t iova, uint64_t len);
366 void __vhost_log_cache_sync(struct virtio_net *dev,
367                 struct vhost_virtqueue *vq);
368 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
369 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
370                             uint64_t iova, uint64_t len);
371
372 static __rte_always_inline void
373 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
374 {
375         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
376                 __vhost_log_write(dev, addr, len);
377 }
378
379 static __rte_always_inline void
380 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
381 {
382         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
383                 __vhost_log_cache_sync(dev, vq);
384 }
385
386 static __rte_always_inline void
387 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
388                         uint64_t addr, uint64_t len)
389 {
390         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
391                 __vhost_log_cache_write(dev, vq, addr, len);
392 }
393
394 static __rte_always_inline void
395 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
396                         uint64_t offset, uint64_t len)
397 {
398         vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
399 }
400
401 static __rte_always_inline void
402 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
403                      uint64_t offset, uint64_t len)
404 {
405         vhost_log_write(dev, vq->log_guest_addr + offset, len);
406 }
407
408 static __rte_always_inline void
409 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
410                            uint64_t iova, uint64_t len)
411 {
412         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
413                 return;
414
415         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
416                 __vhost_log_cache_write_iova(dev, vq, iova, len);
417         else
418                 __vhost_log_cache_write(dev, vq, iova, len);
419 }
420
421 static __rte_always_inline void
422 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
423                            uint64_t iova, uint64_t len)
424 {
425         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
426                 return;
427
428         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
429                 __vhost_log_write_iova(dev, vq, iova, len);
430         else
431                 __vhost_log_write(dev, iova, len);
432 }
433
434 /* Macros for printing using RTE_LOG */
435 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
436 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
437
438 #ifdef RTE_LIBRTE_VHOST_DEBUG
439 #define VHOST_MAX_PRINT_BUFF 6072
440 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
441         RTE_LOG(DEBUG, log_type, fmt, ##args)
442 #define PRINT_PACKET(device, addr, size, header) do { \
443         char *pkt_addr = (char *)(addr); \
444         unsigned int index; \
445         char packet[VHOST_MAX_PRINT_BUFF]; \
446         \
447         if ((header)) \
448                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
449         else \
450                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
451         for (index = 0; index < (size); index++) { \
452                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
453                         "%02hhx ", pkt_addr[index]); \
454         } \
455         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
456         \
457         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
458 } while (0)
459 #else
460 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
461 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
462 #endif
463
464 extern uint64_t VHOST_FEATURES;
465 #define MAX_VHOST_DEVICE        1024
466 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
467
468 /* Convert guest physical address to host physical address */
469 static __rte_always_inline rte_iova_t
470 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
471 {
472         uint32_t i;
473         struct guest_page *page;
474
475         for (i = 0; i < dev->nr_guest_pages; i++) {
476                 page = &dev->guest_pages[i];
477
478                 if (gpa >= page->guest_phys_addr &&
479                     gpa + size < page->guest_phys_addr + page->size) {
480                         return gpa - page->guest_phys_addr +
481                                page->host_phys_addr;
482                 }
483         }
484
485         return 0;
486 }
487
488 static __rte_always_inline uint64_t
489 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
490 {
491         struct rte_vhost_mem_region *r;
492         uint32_t i;
493
494         if (unlikely(!dev || !dev->mem))
495                 return 0;
496
497         for (i = 0; i < dev->mem->nregions; i++) {
498                 r = &dev->mem->regions[i];
499
500                 if (vva >= r->host_user_addr &&
501                     vva + len <  r->host_user_addr + r->size) {
502                         return r->guest_phys_addr + vva - r->host_user_addr;
503                 }
504         }
505         return 0;
506 }
507
508 static __rte_always_inline struct virtio_net *
509 get_device(int vid)
510 {
511         struct virtio_net *dev = vhost_devices[vid];
512
513         if (unlikely(!dev)) {
514                 RTE_LOG(ERR, VHOST_CONFIG,
515                         "(%d) device not found.\n", vid);
516         }
517
518         return dev;
519 }
520
521 int vhost_new_device(void);
522 void cleanup_device(struct virtio_net *dev, int destroy);
523 void reset_device(struct virtio_net *dev);
524 void vhost_destroy_device(int);
525 void vhost_destroy_device_notify(struct virtio_net *dev);
526
527 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
528 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
529
530 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
531
532 void vhost_attach_vdpa_device(int vid, int did);
533
534 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
535 void vhost_enable_dequeue_zero_copy(int vid);
536 void vhost_set_builtin_virtio_net(int vid, bool enable);
537
538 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
539
540 /*
541  * Backend-specific cleanup.
542  *
543  * TODO: fix it; we have one backend now
544  */
545 void vhost_backend_cleanup(struct virtio_net *dev);
546
547 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
548                         uint64_t iova, uint64_t *len, uint8_t perm);
549 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
550                         struct vhost_virtqueue *vq,
551                         uint64_t desc_addr, uint64_t desc_len);
552 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
553 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
554
555 static __rte_always_inline uint64_t
556 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
557                         uint64_t iova, uint64_t *len, uint8_t perm)
558 {
559         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
560                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
561
562         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
563 }
564
565 #define vhost_avail_event(vr) \
566         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
567 #define vhost_used_event(vr) \
568         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
569
570 /*
571  * The following is used with VIRTIO_RING_F_EVENT_IDX.
572  * Assuming a given event_idx value from the other size, if we have
573  * just incremented index from old to new_idx, should we trigger an
574  * event?
575  */
576 static __rte_always_inline int
577 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
578 {
579         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
580 }
581
582 static __rte_always_inline void
583 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
584 {
585         /* Flush used->idx update before we read avail->flags. */
586         rte_smp_mb();
587
588         /* Don't kick guest if we don't reach index specified by guest. */
589         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
590                 uint16_t old = vq->signalled_used;
591                 uint16_t new = vq->last_used_idx;
592                 bool signalled_used_valid = vq->signalled_used_valid;
593
594                 vq->signalled_used = new;
595                 vq->signalled_used_valid = true;
596
597                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
598                         __func__,
599                         vhost_used_event(vq),
600                         old, new);
601
602                 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
603                                         (vq->callfd >= 0)) ||
604                                 unlikely(!signalled_used_valid)) {
605                         eventfd_write(vq->callfd, (eventfd_t) 1);
606                         if (dev->notify_ops->guest_notified)
607                                 dev->notify_ops->guest_notified(dev->vid);
608                 }
609         } else {
610                 /* Kick the guest if necessary. */
611                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
612                                 && (vq->callfd >= 0)) {
613                         eventfd_write(vq->callfd, (eventfd_t)1);
614                         if (dev->notify_ops->guest_notified)
615                                 dev->notify_ops->guest_notified(dev->vid);
616                 }
617         }
618 }
619
620 static __rte_always_inline void
621 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
622 {
623         uint16_t old, new, off, off_wrap;
624         bool signalled_used_valid, kick = false;
625
626         /* Flush used desc update. */
627         rte_smp_mb();
628
629         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
630                 if (vq->driver_event->flags !=
631                                 VRING_EVENT_F_DISABLE)
632                         kick = true;
633                 goto kick;
634         }
635
636         old = vq->signalled_used;
637         new = vq->last_used_idx;
638         vq->signalled_used = new;
639         signalled_used_valid = vq->signalled_used_valid;
640         vq->signalled_used_valid = true;
641
642         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
643                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
644                         kick = true;
645                 goto kick;
646         }
647
648         if (unlikely(!signalled_used_valid)) {
649                 kick = true;
650                 goto kick;
651         }
652
653         rte_smp_rmb();
654
655         off_wrap = vq->driver_event->off_wrap;
656         off = off_wrap & ~(1 << 15);
657
658         if (new <= old)
659                 old -= vq->size;
660
661         if (vq->used_wrap_counter != off_wrap >> 15)
662                 off -= vq->size;
663
664         if (vhost_need_event(off, new, old))
665                 kick = true;
666 kick:
667         if (kick) {
668                 eventfd_write(vq->callfd, (eventfd_t)1);
669                 if (dev->notify_ops->guest_notified)
670                         dev->notify_ops->guest_notified(dev->vid);
671         }
672 }
673
674 static __rte_always_inline void
675 free_ind_table(void *idesc)
676 {
677         rte_free(idesc);
678 }
679
680 static __rte_always_inline void
681 restore_mbuf(struct rte_mbuf *m)
682 {
683         uint32_t mbuf_size, priv_size;
684
685         while (m) {
686                 priv_size = rte_pktmbuf_priv_size(m->pool);
687                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
688                 /* start of buffer is after mbuf structure and priv data */
689
690                 m->buf_addr = (char *)m + mbuf_size;
691                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
692                 m = m->next;
693         }
694 }
695
696 static __rte_always_inline bool
697 mbuf_is_consumed(struct rte_mbuf *m)
698 {
699         while (m) {
700                 if (rte_mbuf_refcnt_read(m) > 1)
701                         return false;
702                 m = m->next;
703         }
704
705         return true;
706 }
707
708 static __rte_always_inline void
709 put_zmbuf(struct zcopy_mbuf *zmbuf)
710 {
711         zmbuf->in_use = 0;
712 }
713
714 #endif /* _VHOST_NET_CDEV_H_ */