vhost: support virtio status message
[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 #include "rte_vdpa_dev.h"
26
27 #include "rte_vhost_async.h"
28
29 /* Used to indicate that the device is running on a data core */
30 #define VIRTIO_DEV_RUNNING 1
31 /* Used to indicate that the device is ready to operate */
32 #define VIRTIO_DEV_READY 2
33 /* Used to indicate that the built-in vhost net device backend is enabled */
34 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
35 /* Used to indicate that the device has its own data path and configured */
36 #define VIRTIO_DEV_VDPA_CONFIGURED 8
37 /* Used to indicate that the feature negotiation failed */
38 #define VIRTIO_DEV_FEATURES_FAILED 16
39
40 /* Backend value set by guest. */
41 #define VIRTIO_DEV_STOPPED -1
42
43 #define BUF_VECTOR_MAX 256
44
45 #define VHOST_LOG_CACHE_NR 32
46
47 #define MAX_PKT_BURST 32
48
49 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST * 2)
50 #define VHOST_MAX_ASYNC_VEC (BUF_VECTOR_MAX * 2)
51
52 #define PACKED_DESC_ENQUEUE_USED_FLAG(w)        \
53         ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
54                 VRING_DESC_F_WRITE)
55 #define PACKED_DESC_DEQUEUE_USED_FLAG(w)        \
56         ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
57 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
58                                          VRING_DESC_F_INDIRECT)
59
60 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
61                             sizeof(struct vring_packed_desc))
62 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
63
64 #ifdef VHOST_GCC_UNROLL_PRAGMA
65 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
66         for (iter = val; iter < size; iter++)
67 #endif
68
69 #ifdef VHOST_CLANG_UNROLL_PRAGMA
70 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
71         for (iter = val; iter < size; iter++)
72 #endif
73
74 #ifdef VHOST_ICC_UNROLL_PRAGMA
75 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
76         for (iter = val; iter < size; iter++)
77 #endif
78
79 #ifndef vhost_for_each_try_unroll
80 #define vhost_for_each_try_unroll(iter, val, num) \
81         for (iter = val; iter < num; iter++)
82 #endif
83
84 /**
85  * Structure contains buffer address, length and descriptor index
86  * from vring to do scatter RX.
87  */
88 struct buf_vector {
89         uint64_t buf_iova;
90         uint64_t buf_addr;
91         uint32_t buf_len;
92         uint32_t desc_idx;
93 };
94
95 /*
96  * A structure to hold some fields needed in zero copy code path,
97  * mainly for associating an mbuf with the right desc_idx.
98  */
99 struct zcopy_mbuf {
100         struct rte_mbuf *mbuf;
101         uint32_t desc_idx;
102         uint16_t desc_count;
103         uint16_t in_use;
104
105         TAILQ_ENTRY(zcopy_mbuf) next;
106 };
107 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
108
109 /*
110  * Structure contains the info for each batched memory copy.
111  */
112 struct batch_copy_elem {
113         void *dst;
114         void *src;
115         uint32_t len;
116         uint64_t log_addr;
117 };
118
119 /*
120  * Structure that contains the info for batched dirty logging.
121  */
122 struct log_cache_entry {
123         uint32_t offset;
124         unsigned long val;
125 };
126
127 struct vring_used_elem_packed {
128         uint16_t id;
129         uint16_t flags;
130         uint32_t len;
131         uint32_t count;
132 };
133
134 /**
135  * Structure contains variables relevant to RX/TX virtqueues.
136  */
137 struct vhost_virtqueue {
138         union {
139                 struct vring_desc       *desc;
140                 struct vring_packed_desc   *desc_packed;
141         };
142         union {
143                 struct vring_avail      *avail;
144                 struct vring_packed_desc_event *driver_event;
145         };
146         union {
147                 struct vring_used       *used;
148                 struct vring_packed_desc_event *device_event;
149         };
150         uint32_t                size;
151
152         uint16_t                last_avail_idx;
153         uint16_t                last_used_idx;
154         /* Last used index we notify to front end. */
155         uint16_t                signalled_used;
156         bool                    signalled_used_valid;
157 #define VIRTIO_INVALID_EVENTFD          (-1)
158 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
159
160         /* Backend value to determine if device should started/stopped */
161         int                     backend;
162         int                     enabled;
163         int                     access_ok;
164         int                     ready;
165         rte_spinlock_t          access_lock;
166
167         /* Used to notify the guest (trigger interrupt) */
168         int                     callfd;
169         /* Currently unused as polling mode is enabled */
170         int                     kickfd;
171
172         /* Physical address of used ring, for logging */
173         uint64_t                log_guest_addr;
174
175         /* inflight share memory info */
176         union {
177                 struct rte_vhost_inflight_info_split *inflight_split;
178                 struct rte_vhost_inflight_info_packed *inflight_packed;
179         };
180         struct rte_vhost_resubmit_info *resubmit_inflight;
181         uint64_t                global_counter;
182
183         uint16_t                nr_zmbuf;
184         uint16_t                zmbuf_size;
185         uint16_t                last_zmbuf_idx;
186         struct zcopy_mbuf       *zmbufs;
187         struct zcopy_mbuf_list  zmbuf_list;
188
189         union {
190                 struct vring_used_elem  *shadow_used_split;
191                 struct vring_used_elem_packed *shadow_used_packed;
192         };
193         uint16_t                shadow_used_idx;
194         /* Record packed ring enqueue latest desc cache aligned index */
195         uint16_t                shadow_aligned_idx;
196         /* Record packed ring first dequeue desc index */
197         uint16_t                shadow_last_used_idx;
198         struct vhost_vring_addr ring_addrs;
199
200         struct batch_copy_elem  *batch_copy_elems;
201         uint16_t                batch_copy_nb_elems;
202         bool                    used_wrap_counter;
203         bool                    avail_wrap_counter;
204
205         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
206         uint16_t log_cache_nb_elem;
207
208         rte_rwlock_t    iotlb_lock;
209         rte_rwlock_t    iotlb_pending_lock;
210         struct rte_mempool *iotlb_pool;
211         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
212         int                             iotlb_cache_nr;
213         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
214
215         /* operation callbacks for async dma */
216         struct rte_vhost_async_channel_ops      async_ops;
217
218         struct rte_vhost_iov_iter it_pool[VHOST_MAX_ASYNC_IT];
219         struct iovec vec_pool[VHOST_MAX_ASYNC_VEC];
220
221         /* async data transfer status */
222         uintptr_t       **async_pkts_pending;
223         #define         ASYNC_PENDING_INFO_N_MSK 0xFFFF
224         #define         ASYNC_PENDING_INFO_N_SFT 16
225         uint64_t        *async_pending_info;
226         uint16_t        async_pkts_idx;
227         uint16_t        async_pkts_inflight_n;
228
229         /* vq async features */
230         bool            async_inorder;
231         bool            async_registered;
232         uint16_t        async_threshold;
233 } __rte_cache_aligned;
234
235 /* Virtio device status as per Virtio specification */
236 #define VIRTIO_DEVICE_STATUS_ACK                0x01
237 #define VIRTIO_DEVICE_STATUS_DRIVER             0x02
238 #define VIRTIO_DEVICE_STATUS_DRIVER_OK          0x04
239 #define VIRTIO_DEVICE_STATUS_FEATURES_OK        0x08
240 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET     0x40
241 #define VIRTIO_DEVICE_STATUS_FAILED             0x80
242
243 #define VHOST_MAX_VRING                 0x100
244 #define VHOST_MAX_QUEUE_PAIRS           0x80
245
246 /* Declare IOMMU related bits for older kernels */
247 #ifndef VIRTIO_F_IOMMU_PLATFORM
248
249 #define VIRTIO_F_IOMMU_PLATFORM 33
250
251 struct vhost_iotlb_msg {
252         __u64 iova;
253         __u64 size;
254         __u64 uaddr;
255 #define VHOST_ACCESS_RO      0x1
256 #define VHOST_ACCESS_WO      0x2
257 #define VHOST_ACCESS_RW      0x3
258         __u8 perm;
259 #define VHOST_IOTLB_MISS           1
260 #define VHOST_IOTLB_UPDATE         2
261 #define VHOST_IOTLB_INVALIDATE     3
262 #define VHOST_IOTLB_ACCESS_FAIL    4
263         __u8 type;
264 };
265
266 #define VHOST_IOTLB_MSG 0x1
267
268 struct vhost_msg {
269         int type;
270         union {
271                 struct vhost_iotlb_msg iotlb;
272                 __u8 padding[64];
273         };
274 };
275 #endif
276
277 /*
278  * Define virtio 1.0 for older kernels
279  */
280 #ifndef VIRTIO_F_VERSION_1
281  #define VIRTIO_F_VERSION_1 32
282 #endif
283
284 /* Declare packed ring related bits for older kernels */
285 #ifndef VIRTIO_F_RING_PACKED
286
287 #define VIRTIO_F_RING_PACKED 34
288
289 struct vring_packed_desc {
290         uint64_t addr;
291         uint32_t len;
292         uint16_t id;
293         uint16_t flags;
294 };
295
296 struct vring_packed_desc_event {
297         uint16_t off_wrap;
298         uint16_t flags;
299 };
300 #endif
301
302 /*
303  * Declare below packed ring defines unconditionally
304  * as Kernel header might use different names.
305  */
306 #define VRING_DESC_F_AVAIL      (1ULL << 7)
307 #define VRING_DESC_F_USED       (1ULL << 15)
308
309 #define VRING_EVENT_F_ENABLE 0x0
310 #define VRING_EVENT_F_DISABLE 0x1
311 #define VRING_EVENT_F_DESC 0x2
312
313 /*
314  * Available and used descs are in same order
315  */
316 #ifndef VIRTIO_F_IN_ORDER
317 #define VIRTIO_F_IN_ORDER      35
318 #endif
319
320 /* Features supported by this builtin vhost-user net driver. */
321 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
322                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
323                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
324                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
325                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
326                                 (1ULL << VIRTIO_NET_F_MQ)      | \
327                                 (1ULL << VIRTIO_F_VERSION_1)   | \
328                                 (1ULL << VHOST_F_LOG_ALL)      | \
329                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
330                                 (1ULL << VIRTIO_NET_F_GSO) | \
331                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
332                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
333                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
334                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
335                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
336                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
337                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
338                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
339                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
340                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
341                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
342                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
343                                 (1ULL << VIRTIO_NET_F_MTU)  | \
344                                 (1ULL << VIRTIO_F_IN_ORDER) | \
345                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
346                                 (1ULL << VIRTIO_F_RING_PACKED))
347
348
349 struct guest_page {
350         uint64_t guest_phys_addr;
351         uint64_t host_phys_addr;
352         uint64_t size;
353 };
354
355 struct inflight_mem_info {
356         int             fd;
357         void            *addr;
358         uint64_t        size;
359 };
360
361 /**
362  * Device structure contains all configuration information relating
363  * to the device.
364  */
365 struct virtio_net {
366         /* Frontend (QEMU) memory and memory region information */
367         struct rte_vhost_memory *mem;
368         uint64_t                features;
369         uint64_t                protocol_features;
370         int                     vid;
371         uint32_t                flags;
372         uint16_t                vhost_hlen;
373         /* to tell if we need broadcast rarp packet */
374         int16_t                 broadcast_rarp;
375         uint32_t                nr_vring;
376         int                     dequeue_zero_copy;
377         int                     async_copy;
378         int                     extbuf;
379         int                     linearbuf;
380         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
381         struct inflight_mem_info *inflight_info;
382 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
383         char                    ifname[IF_NAME_SZ];
384         uint64_t                log_size;
385         uint64_t                log_base;
386         uint64_t                log_addr;
387         struct rte_ether_addr   mac;
388         uint16_t                mtu;
389         uint8_t                 status;
390
391         struct vhost_device_ops const *notify_ops;
392
393         uint32_t                nr_guest_pages;
394         uint32_t                max_guest_pages;
395         struct guest_page       *guest_pages;
396
397         int                     slave_req_fd;
398         rte_spinlock_t          slave_req_lock;
399
400         int                     postcopy_ufd;
401         int                     postcopy_listening;
402
403         struct rte_vdpa_device *vdpa_dev;
404
405         /* context data for the external message handlers */
406         void                    *extern_data;
407         /* pre and post vhost user message handlers for the device */
408         struct rte_vhost_user_extern_ops extern_ops;
409 } __rte_cache_aligned;
410
411 static __rte_always_inline bool
412 vq_is_packed(struct virtio_net *dev)
413 {
414         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
415 }
416
417 static inline bool
418 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
419 {
420         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
421
422         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
423                 wrap_counter != !!(flags & VRING_DESC_F_USED);
424 }
425
426 static inline void
427 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
428 {
429         vq->last_used_idx += num;
430         if (vq->last_used_idx >= vq->size) {
431                 vq->used_wrap_counter ^= 1;
432                 vq->last_used_idx -= vq->size;
433         }
434 }
435
436 static inline void
437 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
438 {
439         vq->last_avail_idx += num;
440         if (vq->last_avail_idx >= vq->size) {
441                 vq->avail_wrap_counter ^= 1;
442                 vq->last_avail_idx -= vq->size;
443         }
444 }
445
446 void __vhost_log_cache_write(struct virtio_net *dev,
447                 struct vhost_virtqueue *vq,
448                 uint64_t addr, uint64_t len);
449 void __vhost_log_cache_write_iova(struct virtio_net *dev,
450                 struct vhost_virtqueue *vq,
451                 uint64_t iova, uint64_t len);
452 void __vhost_log_cache_sync(struct virtio_net *dev,
453                 struct vhost_virtqueue *vq);
454 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
455 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
456                             uint64_t iova, uint64_t len);
457
458 static __rte_always_inline void
459 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
460 {
461         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
462                 __vhost_log_write(dev, addr, len);
463 }
464
465 static __rte_always_inline void
466 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
467 {
468         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
469                 __vhost_log_cache_sync(dev, vq);
470 }
471
472 static __rte_always_inline void
473 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
474                         uint64_t addr, uint64_t len)
475 {
476         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
477                 __vhost_log_cache_write(dev, vq, addr, len);
478 }
479
480 static __rte_always_inline void
481 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
482                         uint64_t offset, uint64_t len)
483 {
484         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
485                 if (unlikely(vq->log_guest_addr == 0))
486                         return;
487                 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
488                                         len);
489         }
490 }
491
492 static __rte_always_inline void
493 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
494                      uint64_t offset, uint64_t len)
495 {
496         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
497                 if (unlikely(vq->log_guest_addr == 0))
498                         return;
499                 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
500         }
501 }
502
503 static __rte_always_inline void
504 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
505                            uint64_t iova, uint64_t len)
506 {
507         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
508                 return;
509
510         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
511                 __vhost_log_cache_write_iova(dev, vq, iova, len);
512         else
513                 __vhost_log_cache_write(dev, vq, iova, len);
514 }
515
516 static __rte_always_inline void
517 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
518                            uint64_t iova, uint64_t len)
519 {
520         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
521                 return;
522
523         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
524                 __vhost_log_write_iova(dev, vq, iova, len);
525         else
526                 __vhost_log_write(dev, iova, len);
527 }
528
529 extern int vhost_config_log_level;
530 extern int vhost_data_log_level;
531
532 #define VHOST_LOG_CONFIG(level, fmt, args...)                   \
533         rte_log(RTE_LOG_ ## level, vhost_config_log_level,      \
534                 "VHOST_CONFIG: " fmt, ##args)
535
536 #define VHOST_LOG_DATA(level, fmt, args...) \
537         (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ?        \
538          rte_log(RTE_LOG_ ## level,  vhost_data_log_level,      \
539                 "VHOST_DATA : " fmt, ##args) :                  \
540          0)
541
542 #ifdef RTE_LIBRTE_VHOST_DEBUG
543 #define VHOST_MAX_PRINT_BUFF 6072
544 #define PRINT_PACKET(device, addr, size, header) do { \
545         char *pkt_addr = (char *)(addr); \
546         unsigned int index; \
547         char packet[VHOST_MAX_PRINT_BUFF]; \
548         \
549         if ((header)) \
550                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
551         else \
552                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
553         for (index = 0; index < (size); index++) { \
554                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
555                         "%02hhx ", pkt_addr[index]); \
556         } \
557         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
558         \
559         VHOST_LOG_DATA(DEBUG, "%s", packet); \
560 } while (0)
561 #else
562 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
563 #endif
564
565 #define MAX_VHOST_DEVICE        1024
566 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
567
568 #define VHOST_BINARY_SEARCH_THRESH 256
569
570 static __rte_always_inline int guest_page_addrcmp(const void *p1,
571                                                 const void *p2)
572 {
573         const struct guest_page *page1 = (const struct guest_page *)p1;
574         const struct guest_page *page2 = (const struct guest_page *)p2;
575
576         if (page1->guest_phys_addr > page2->guest_phys_addr)
577                 return 1;
578         if (page1->guest_phys_addr < page2->guest_phys_addr)
579                 return -1;
580
581         return 0;
582 }
583
584 /* Convert guest physical address to host physical address */
585 static __rte_always_inline rte_iova_t
586 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
587 {
588         uint32_t i;
589         struct guest_page *page;
590         struct guest_page key;
591
592         if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
593                 key.guest_phys_addr = gpa;
594                 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
595                                sizeof(struct guest_page), guest_page_addrcmp);
596                 if (page) {
597                         if (gpa + size < page->guest_phys_addr + page->size)
598                                 return gpa - page->guest_phys_addr +
599                                         page->host_phys_addr;
600                 }
601         } else {
602                 for (i = 0; i < dev->nr_guest_pages; i++) {
603                         page = &dev->guest_pages[i];
604
605                         if (gpa >= page->guest_phys_addr &&
606                             gpa + size < page->guest_phys_addr +
607                             page->size)
608                                 return gpa - page->guest_phys_addr +
609                                        page->host_phys_addr;
610                 }
611         }
612
613         return 0;
614 }
615
616 static __rte_always_inline uint64_t
617 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
618 {
619         struct rte_vhost_mem_region *r;
620         uint32_t i;
621
622         if (unlikely(!dev || !dev->mem))
623                 return 0;
624
625         for (i = 0; i < dev->mem->nregions; i++) {
626                 r = &dev->mem->regions[i];
627
628                 if (vva >= r->host_user_addr &&
629                     vva + len <  r->host_user_addr + r->size) {
630                         return r->guest_phys_addr + vva - r->host_user_addr;
631                 }
632         }
633         return 0;
634 }
635
636 static __rte_always_inline struct virtio_net *
637 get_device(int vid)
638 {
639         struct virtio_net *dev = vhost_devices[vid];
640
641         if (unlikely(!dev)) {
642                 VHOST_LOG_CONFIG(ERR,
643                         "(%d) device not found.\n", vid);
644         }
645
646         return dev;
647 }
648
649 int vhost_new_device(void);
650 void cleanup_device(struct virtio_net *dev, int destroy);
651 void reset_device(struct virtio_net *dev);
652 void vhost_destroy_device(int);
653 void vhost_destroy_device_notify(struct virtio_net *dev);
654
655 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
656 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
657 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
658
659 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
660
661 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
662
663 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
664 void vhost_enable_dequeue_zero_copy(int vid);
665 void vhost_set_builtin_virtio_net(int vid, bool enable);
666 void vhost_enable_extbuf(int vid);
667 void vhost_enable_linearbuf(int vid);
668
669 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
670
671 /*
672  * Backend-specific cleanup.
673  *
674  * TODO: fix it; we have one backend now
675  */
676 void vhost_backend_cleanup(struct virtio_net *dev);
677
678 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
679                         uint64_t iova, uint64_t *len, uint8_t perm);
680 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
681                         struct vhost_virtqueue *vq,
682                         uint64_t desc_addr, uint64_t desc_len);
683 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
684 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
685                 uint64_t log_addr);
686 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
687
688 static __rte_always_inline uint64_t
689 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
690                         uint64_t iova, uint64_t *len, uint8_t perm)
691 {
692         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
693                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
694
695         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
696 }
697
698 #define vhost_avail_event(vr) \
699         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
700 #define vhost_used_event(vr) \
701         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
702
703 /*
704  * The following is used with VIRTIO_RING_F_EVENT_IDX.
705  * Assuming a given event_idx value from the other size, if we have
706  * just incremented index from old to new_idx, should we trigger an
707  * event?
708  */
709 static __rte_always_inline int
710 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
711 {
712         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
713 }
714
715 static __rte_always_inline void
716 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
717 {
718         /* Flush used->idx update before we read avail->flags. */
719         rte_smp_mb();
720
721         /* Don't kick guest if we don't reach index specified by guest. */
722         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
723                 uint16_t old = vq->signalled_used;
724                 uint16_t new = vq->async_pkts_inflight_n ?
725                                         vq->used->idx:vq->last_used_idx;
726                 bool signalled_used_valid = vq->signalled_used_valid;
727
728                 vq->signalled_used = new;
729                 vq->signalled_used_valid = true;
730
731                 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
732                         __func__,
733                         vhost_used_event(vq),
734                         old, new);
735
736                 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
737                                         (vq->callfd >= 0)) ||
738                                 unlikely(!signalled_used_valid)) {
739                         eventfd_write(vq->callfd, (eventfd_t) 1);
740                         if (dev->notify_ops->guest_notified)
741                                 dev->notify_ops->guest_notified(dev->vid);
742                 }
743         } else {
744                 /* Kick the guest if necessary. */
745                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
746                                 && (vq->callfd >= 0)) {
747                         eventfd_write(vq->callfd, (eventfd_t)1);
748                         if (dev->notify_ops->guest_notified)
749                                 dev->notify_ops->guest_notified(dev->vid);
750                 }
751         }
752 }
753
754 static __rte_always_inline void
755 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
756 {
757         uint16_t old, new, off, off_wrap;
758         bool signalled_used_valid, kick = false;
759
760         /* Flush used desc update. */
761         rte_smp_mb();
762
763         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
764                 if (vq->driver_event->flags !=
765                                 VRING_EVENT_F_DISABLE)
766                         kick = true;
767                 goto kick;
768         }
769
770         old = vq->signalled_used;
771         new = vq->last_used_idx;
772         vq->signalled_used = new;
773         signalled_used_valid = vq->signalled_used_valid;
774         vq->signalled_used_valid = true;
775
776         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
777                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
778                         kick = true;
779                 goto kick;
780         }
781
782         if (unlikely(!signalled_used_valid)) {
783                 kick = true;
784                 goto kick;
785         }
786
787         rte_smp_rmb();
788
789         off_wrap = vq->driver_event->off_wrap;
790         off = off_wrap & ~(1 << 15);
791
792         if (new <= old)
793                 old -= vq->size;
794
795         if (vq->used_wrap_counter != off_wrap >> 15)
796                 off -= vq->size;
797
798         if (vhost_need_event(off, new, old))
799                 kick = true;
800 kick:
801         if (kick) {
802                 eventfd_write(vq->callfd, (eventfd_t)1);
803                 if (dev->notify_ops->guest_notified)
804                         dev->notify_ops->guest_notified(dev->vid);
805         }
806 }
807
808 static __rte_always_inline void
809 free_ind_table(void *idesc)
810 {
811         rte_free(idesc);
812 }
813
814 static __rte_always_inline void
815 restore_mbuf(struct rte_mbuf *m)
816 {
817         uint32_t mbuf_size, priv_size;
818
819         while (m) {
820                 priv_size = rte_pktmbuf_priv_size(m->pool);
821                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
822                 /* start of buffer is after mbuf structure and priv data */
823
824                 m->buf_addr = (char *)m + mbuf_size;
825                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
826                 m = m->next;
827         }
828 }
829
830 static __rte_always_inline bool
831 mbuf_is_consumed(struct rte_mbuf *m)
832 {
833         while (m) {
834                 if (rte_mbuf_refcnt_read(m) > 1)
835                         return false;
836                 m = m->next;
837         }
838
839         return true;
840 }
841
842 static __rte_always_inline void
843 put_zmbuf(struct zcopy_mbuf *zmbuf)
844 {
845         zmbuf->in_use = 0;
846 }
847
848 #endif /* _VHOST_NET_CDEV_H_ */