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