xref: /dpdk/examples/vhost_blk/vhost_blk.h (revision 7e06c0de1952d3109a5b0c4779d7e7d8059c9d78)
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 vring;
34 	struct rte_vhost_ring_inflight inflight_ring;
35 
36 	uint16_t last_avail_idx;
37 	uint16_t last_used_idx;
38 	uint16_t id;
39 
40 	bool avail_wrap_counter;
41 	bool used_wrap_counter;
42 	bool packed_ring;
43 
44 	struct vhost_blk_task *tasks;
45 };
46 
47 #define NUM_OF_BLK_QUEUES 1
48 
49 struct vhost_block_dev {
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 __rte_cache_aligned vhost_blk_ctrlr {
70 	uint8_t started;
71 	/** ID for vhost library. */
72 	int vid;
73 	/** Queues for the block device */
74 	struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
75 	/** Only support 1 LUN for the example */
76 	struct vhost_block_dev *bdev;
77 	/** VM memory region */
78 	struct rte_vhost_memory *mem;
79 };
80 
81 #define VHOST_BLK_MAX_IOVS 128
82 
83 enum blk_data_dir {
84 	BLK_DIR_NONE = 0,
85 	BLK_DIR_TO_DEV = 1,
86 	BLK_DIR_FROM_DEV = 2,
87 };
88 
89 struct vhost_blk_task {
90 	uint8_t req_idx;
91 	uint16_t chain_num;
92 	uint16_t inflight_idx;
93 	uint16_t buffer_id;
94 	uint32_t dxfer_dir;
95 	uint32_t data_len;
96 
97 	struct virtio_blk_outhdr *req;
98 	volatile uint8_t *status;
99 	struct iovec iovs[VHOST_BLK_MAX_IOVS];
100 	uint32_t iovs_cnt;
101 
102 	struct vhost_blk_queue *vq;
103 	struct vhost_blk_ctrlr *ctrlr;
104 };
105 
106 extern struct vhost_blk_ctrlr *g_vhost_ctrlr;
107 extern struct rte_vhost_device_ops vhost_blk_device_ops;
108 
109 int vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
110 				     struct vhost_blk_task *task);
111 
112 void vhost_session_install_rte_compat_hooks(uint32_t vid);
113 
114 void vhost_dev_install_rte_compat_hooks(const char *path);
115 
116 struct vhost_blk_ctrlr *vhost_blk_ctrlr_find(const char *ctrlr_name);
117 
118 #endif /* _VHOST_blk_H_ */
119