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