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