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