17258d284b74595591c3adb23cf3163acd063ccf
[dpdk.git] / examples / vhost_blk / vhost_blk.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _VHOST_BLK_H_
6 #define _VHOST_BLK_H_
7
8 #include <stdio.h>
9 #include <sys/uio.h>
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include <linux/virtio_blk.h>
13 #include <linux/virtio_ring.h>
14
15 #include <rte_vhost.h>
16
17 #ifndef VIRTIO_F_RING_PACKED
18 #define VIRTIO_F_RING_PACKED 34
19
20 struct vring_packed_desc {
21         /* Buffer Address. */
22         __le64 addr;
23         /* Buffer Length. */
24         __le32 len;
25         /* Buffer ID. */
26         __le16 id;
27         /* The flags depending on descriptor type. */
28         __le16 flags;
29 };
30 #endif
31
32 struct vhost_blk_queue {
33         struct rte_vhost_vring vq;
34         struct rte_vhost_ring_inflight inflight_vq;
35         uint16_t last_avail_idx;
36         uint16_t last_used_idx;
37         bool avail_wrap_counter;
38         bool used_wrap_counter;
39 };
40
41 #define NUM_OF_BLK_QUEUES 1
42
43 #define min(a, b) (((a) < (b)) ? (a) : (b))
44
45 struct vhost_block_dev {
46         /** ID for vhost library. */
47         int vid;
48         /** Queues for the block device */
49         struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
50         /** Unique name for this block device. */
51         char name[64];
52
53         /** Unique product name for this kind of block device. */
54         char product_name[256];
55
56         /** Size in bytes of a logical block for the backend */
57         uint32_t blocklen;
58
59         /** Number of blocks */
60         uint64_t blockcnt;
61
62         /** write cache enabled, not used at the moment */
63         int write_cache;
64
65         /** use memory as disk storage space */
66         uint8_t *data;
67 };
68
69 struct vhost_blk_ctrlr {
70         uint8_t started;
71         uint8_t packed_ring;
72         uint8_t need_restart;
73         /** Only support 1 LUN for the example */
74         struct vhost_block_dev *bdev;
75         /** VM memory region */
76         struct rte_vhost_memory *mem;
77 } __rte_cache_aligned;
78
79 #define VHOST_BLK_MAX_IOVS 128
80
81 enum blk_data_dir {
82         BLK_DIR_NONE = 0,
83         BLK_DIR_TO_DEV = 1,
84         BLK_DIR_FROM_DEV = 2,
85 };
86
87 struct vhost_blk_task {
88         uint8_t readtype;
89         uint8_t req_idx;
90         uint16_t head_idx;
91         uint16_t last_idx;
92         uint16_t inflight_idx;
93         uint16_t buffer_id;
94         uint32_t dxfer_dir;
95         uint32_t data_len;
96         struct virtio_blk_outhdr *req;
97
98         volatile uint8_t *status;
99
100         struct iovec iovs[VHOST_BLK_MAX_IOVS];
101         uint32_t iovs_cnt;
102         struct vring_packed_desc *desc_packed;
103         struct vring_desc *desc_split;
104         struct rte_vhost_vring *vq;
105         struct vhost_block_dev *bdev;
106         struct vhost_blk_ctrlr *ctrlr;
107 };
108
109 struct inflight_blk_task {
110         struct vhost_blk_task blk_task;
111         struct rte_vhost_inflight_desc_packed *inflight_desc;
112         struct rte_vhost_inflight_info_packed *inflight_packed;
113 };
114
115 extern struct vhost_blk_ctrlr *g_vhost_ctrlr;
116 extern struct vhost_device_ops vhost_blk_device_ops;
117
118 int vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
119                                      struct vhost_blk_task *task);
120
121 void vhost_session_install_rte_compat_hooks(uint32_t vid);
122
123 void vhost_dev_install_rte_compat_hooks(const char *path);
124
125 struct vhost_blk_ctrlr *vhost_blk_ctrlr_find(const char *ctrlr_name);
126
127 #endif /* _VHOST_blk_H_ */