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