vhost: batch used descs chains write-back with packed ring
[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 /* The possible results of a message handling function */
290 enum vh_result {
291         /* Message handling failed */
292         VH_RESULT_ERR   = -1,
293         /* Message handling successful */
294         VH_RESULT_OK    =  0,
295         /* Message handling successful and reply prepared */
296         VH_RESULT_REPLY =  1,
297 };
298
299 /**
300  * function prototype for the vhost backend to handler specific vhost user
301  * messages prior to the master message handling
302  *
303  * @param vid
304  *  vhost device id
305  * @param msg
306  *  Message pointer.
307  * @param skip_master
308  *  If the handler requires skipping the master message handling, this variable
309  *  shall be written 1, otherwise 0.
310  * @return
311  *  VH_RESULT_OK on success, VH_RESULT_REPLY on success with reply,
312  *  VH_RESULT_ERR on failure
313  */
314 typedef enum vh_result (*vhost_msg_pre_handle)(int vid, void *msg,
315                 uint32_t *skip_master);
316
317 /**
318  * function prototype for the vhost backend to handler specific vhost user
319  * messages after the master message handling is done
320  *
321  * @param vid
322  *  vhost device id
323  * @param msg
324  *  Message pointer.
325  * @return
326  *  VH_RESULT_OK on success, VH_RESULT_REPLY on success with reply,
327  *  VH_RESULT_ERR on failure
328  */
329 typedef enum vh_result (*vhost_msg_post_handle)(int vid, void *msg);
330
331 /**
332  * pre and post vhost user message handlers
333  */
334 struct vhost_user_extern_ops {
335         vhost_msg_pre_handle pre_msg_handle;
336         vhost_msg_post_handle post_msg_handle;
337 };
338
339 /**
340  * Device structure contains all configuration information relating
341  * to the device.
342  */
343 struct virtio_net {
344         /* Frontend (QEMU) memory and memory region information */
345         struct rte_vhost_memory *mem;
346         uint64_t                features;
347         uint64_t                protocol_features;
348         int                     vid;
349         uint32_t                flags;
350         uint16_t                vhost_hlen;
351         /* to tell if we need broadcast rarp packet */
352         rte_atomic16_t          broadcast_rarp;
353         uint32_t                nr_vring;
354         int                     dequeue_zero_copy;
355         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
356 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
357         char                    ifname[IF_NAME_SZ];
358         uint64_t                log_size;
359         uint64_t                log_base;
360         uint64_t                log_addr;
361         struct ether_addr       mac;
362         uint16_t                mtu;
363
364         struct vhost_device_ops const *notify_ops;
365
366         uint32_t                nr_guest_pages;
367         uint32_t                max_guest_pages;
368         struct guest_page       *guest_pages;
369
370         int                     slave_req_fd;
371         rte_spinlock_t          slave_req_lock;
372
373         int                     postcopy_ufd;
374         int                     postcopy_listening;
375
376         /*
377          * Device id to identify a specific backend device.
378          * It's set to -1 for the default software implementation.
379          */
380         int                     vdpa_dev_id;
381
382         /* private data for virtio device */
383         void                    *extern_data;
384         /* pre and post vhost user message handlers for the device */
385         struct vhost_user_extern_ops extern_ops;
386 } __rte_cache_aligned;
387
388 static __rte_always_inline bool
389 vq_is_packed(struct virtio_net *dev)
390 {
391         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
392 }
393
394 static inline bool
395 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
396 {
397         uint16_t flags = *((volatile uint16_t *) &desc->flags);
398
399         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
400                 wrap_counter != !!(flags & VRING_DESC_F_USED);
401 }
402
403 #define VHOST_LOG_PAGE  4096
404
405 /*
406  * Atomically set a bit in memory.
407  */
408 static __rte_always_inline void
409 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
410 {
411 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
412         /*
413          * __sync_ built-ins are deprecated, but __atomic_ ones
414          * are sub-optimized in older GCC versions.
415          */
416         __sync_fetch_and_or_1(addr, (1U << nr));
417 #else
418         __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
419 #endif
420 }
421
422 static __rte_always_inline void
423 vhost_log_page(uint8_t *log_base, uint64_t page)
424 {
425         vhost_set_bit(page % 8, &log_base[page / 8]);
426 }
427
428 static __rte_always_inline void
429 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
430 {
431         uint64_t page;
432
433         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
434                    !dev->log_base || !len))
435                 return;
436
437         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
438                 return;
439
440         /* To make sure guest memory updates are committed before logging */
441         rte_smp_wmb();
442
443         page = addr / VHOST_LOG_PAGE;
444         while (page * VHOST_LOG_PAGE < addr + len) {
445                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
446                 page += 1;
447         }
448 }
449
450 static __rte_always_inline void
451 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
452 {
453         unsigned long *log_base;
454         int i;
455
456         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
457                    !dev->log_base))
458                 return;
459
460         rte_smp_wmb();
461
462         log_base = (unsigned long *)(uintptr_t)dev->log_base;
463
464         for (i = 0; i < vq->log_cache_nb_elem; i++) {
465                 struct log_cache_entry *elem = vq->log_cache + i;
466
467 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
468                 /*
469                  * '__sync' builtins are deprecated, but '__atomic' ones
470                  * are sub-optimized in older GCC versions.
471                  */
472                 __sync_fetch_and_or(log_base + elem->offset, elem->val);
473 #else
474                 __atomic_fetch_or(log_base + elem->offset, elem->val,
475                                 __ATOMIC_RELAXED);
476 #endif
477         }
478
479         rte_smp_wmb();
480
481         vq->log_cache_nb_elem = 0;
482 }
483
484 static __rte_always_inline void
485 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
486                         uint64_t page)
487 {
488         uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
489         uint32_t offset = page / (sizeof(unsigned long) << 3);
490         int i;
491
492         for (i = 0; i < vq->log_cache_nb_elem; i++) {
493                 struct log_cache_entry *elem = vq->log_cache + i;
494
495                 if (elem->offset == offset) {
496                         elem->val |= (1UL << bit_nr);
497                         return;
498                 }
499         }
500
501         if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
502                 /*
503                  * No more room for a new log cache entry,
504                  * so write the dirty log map directly.
505                  */
506                 rte_smp_wmb();
507                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
508
509                 return;
510         }
511
512         vq->log_cache[i].offset = offset;
513         vq->log_cache[i].val = (1UL << bit_nr);
514         vq->log_cache_nb_elem++;
515 }
516
517 static __rte_always_inline void
518 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
519                         uint64_t addr, uint64_t len)
520 {
521         uint64_t page;
522
523         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
524                    !dev->log_base || !len))
525                 return;
526
527         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
528                 return;
529
530         page = addr / VHOST_LOG_PAGE;
531         while (page * VHOST_LOG_PAGE < addr + len) {
532                 vhost_log_cache_page(dev, vq, page);
533                 page += 1;
534         }
535 }
536
537 static __rte_always_inline void
538 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
539                         uint64_t offset, uint64_t len)
540 {
541         vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
542 }
543
544 static __rte_always_inline void
545 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
546                      uint64_t offset, uint64_t len)
547 {
548         vhost_log_write(dev, vq->log_guest_addr + offset, len);
549 }
550
551 /* Macros for printing using RTE_LOG */
552 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
553 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
554
555 #ifdef RTE_LIBRTE_VHOST_DEBUG
556 #define VHOST_MAX_PRINT_BUFF 6072
557 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
558         RTE_LOG(DEBUG, log_type, fmt, ##args)
559 #define PRINT_PACKET(device, addr, size, header) do { \
560         char *pkt_addr = (char *)(addr); \
561         unsigned int index; \
562         char packet[VHOST_MAX_PRINT_BUFF]; \
563         \
564         if ((header)) \
565                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
566         else \
567                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
568         for (index = 0; index < (size); index++) { \
569                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
570                         "%02hhx ", pkt_addr[index]); \
571         } \
572         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
573         \
574         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
575 } while (0)
576 #else
577 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
578 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
579 #endif
580
581 extern uint64_t VHOST_FEATURES;
582 #define MAX_VHOST_DEVICE        1024
583 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
584
585 /* Convert guest physical address to host physical address */
586 static __rte_always_inline rte_iova_t
587 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
588 {
589         uint32_t i;
590         struct guest_page *page;
591
592         for (i = 0; i < dev->nr_guest_pages; i++) {
593                 page = &dev->guest_pages[i];
594
595                 if (gpa >= page->guest_phys_addr &&
596                     gpa + size < page->guest_phys_addr + page->size) {
597                         return gpa - page->guest_phys_addr +
598                                page->host_phys_addr;
599                 }
600         }
601
602         return 0;
603 }
604
605 static __rte_always_inline struct virtio_net *
606 get_device(int vid)
607 {
608         struct virtio_net *dev = vhost_devices[vid];
609
610         if (unlikely(!dev)) {
611                 RTE_LOG(ERR, VHOST_CONFIG,
612                         "(%d) device not found.\n", vid);
613         }
614
615         return dev;
616 }
617
618 int vhost_new_device(void);
619 void cleanup_device(struct virtio_net *dev, int destroy);
620 void reset_device(struct virtio_net *dev);
621 void vhost_destroy_device(int);
622 void vhost_destroy_device_notify(struct virtio_net *dev);
623
624 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
625 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
626
627 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
628
629 void vhost_attach_vdpa_device(int vid, int did);
630
631 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
632 void vhost_enable_dequeue_zero_copy(int vid);
633 void vhost_set_builtin_virtio_net(int vid, bool enable);
634
635 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
636
637 /*
638  * Backend-specific cleanup.
639  *
640  * TODO: fix it; we have one backend now
641  */
642 void vhost_backend_cleanup(struct virtio_net *dev);
643
644 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
645                         uint64_t iova, uint64_t *len, uint8_t perm);
646 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
647 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
648
649 static __rte_always_inline uint64_t
650 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
651                         uint64_t iova, uint64_t *len, uint8_t perm)
652 {
653         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
654                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
655
656         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
657 }
658
659 #define vhost_avail_event(vr) \
660         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
661 #define vhost_used_event(vr) \
662         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
663
664 /*
665  * The following is used with VIRTIO_RING_F_EVENT_IDX.
666  * Assuming a given event_idx value from the other size, if we have
667  * just incremented index from old to new_idx, should we trigger an
668  * event?
669  */
670 static __rte_always_inline int
671 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
672 {
673         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
674 }
675
676 static __rte_always_inline void
677 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
678 {
679         /* Flush used->idx update before we read avail->flags. */
680         rte_smp_mb();
681
682         /* Don't kick guest if we don't reach index specified by guest. */
683         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
684                 uint16_t old = vq->signalled_used;
685                 uint16_t new = vq->last_used_idx;
686
687                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
688                         __func__,
689                         vhost_used_event(vq),
690                         old, new);
691                 if (vhost_need_event(vhost_used_event(vq), new, old)
692                         && (vq->callfd >= 0)) {
693                         vq->signalled_used = vq->last_used_idx;
694                         eventfd_write(vq->callfd, (eventfd_t) 1);
695                 }
696         } else {
697                 /* Kick the guest if necessary. */
698                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
699                                 && (vq->callfd >= 0))
700                         eventfd_write(vq->callfd, (eventfd_t)1);
701         }
702 }
703
704 static __rte_always_inline void
705 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
706 {
707         uint16_t old, new, off, off_wrap;
708         bool signalled_used_valid, kick = false;
709
710         /* Flush used desc update. */
711         rte_smp_mb();
712
713         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
714                 if (vq->driver_event->flags !=
715                                 VRING_EVENT_F_DISABLE)
716                         kick = true;
717                 goto kick;
718         }
719
720         old = vq->signalled_used;
721         new = vq->last_used_idx;
722         vq->signalled_used = new;
723         signalled_used_valid = vq->signalled_used_valid;
724         vq->signalled_used_valid = true;
725
726         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
727                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
728                         kick = true;
729                 goto kick;
730         }
731
732         if (unlikely(!signalled_used_valid)) {
733                 kick = true;
734                 goto kick;
735         }
736
737         rte_smp_rmb();
738
739         off_wrap = vq->driver_event->off_wrap;
740         off = off_wrap & ~(1 << 15);
741
742         if (new <= old)
743                 old -= vq->size;
744
745         if (vq->used_wrap_counter != off_wrap >> 15)
746                 off -= vq->size;
747
748         if (vhost_need_event(off, new, old))
749                 kick = true;
750 kick:
751         if (kick)
752                 eventfd_write(vq->callfd, (eventfd_t)1);
753 }
754
755 static __rte_always_inline void *
756 alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
757                 uint64_t desc_addr, uint64_t desc_len)
758 {
759         void *idesc;
760         uint64_t src, dst;
761         uint64_t len, remain = desc_len;
762
763         idesc = rte_malloc(__func__, desc_len, 0);
764         if (unlikely(!idesc))
765                 return 0;
766
767         dst = (uint64_t)(uintptr_t)idesc;
768
769         while (remain) {
770                 len = remain;
771                 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
772                                 VHOST_ACCESS_RO);
773                 if (unlikely(!src || !len)) {
774                         rte_free(idesc);
775                         return 0;
776                 }
777
778                 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
779
780                 remain -= len;
781                 dst += len;
782                 desc_addr += len;
783         }
784
785         return idesc;
786 }
787
788 static __rte_always_inline void
789 free_ind_table(void *idesc)
790 {
791         rte_free(idesc);
792 }
793
794 #endif /* _VHOST_NET_CDEV_H_ */