xref: /spdk/test/unit/lib/blob/bs_dev_common.c (revision 2baeea7dd43483689a430ab2f03091373a626a7b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #define DEV_BUFFER_SIZE (64 * 1024 * 1024)
35 #define DEV_BUFFER_BLOCKLEN (4096)
36 #define DEV_BUFFER_BLOCKCNT (DEV_BUFFER_SIZE / DEV_BUFFER_BLOCKLEN)
37 uint8_t *g_dev_buffer;
38 
39 static struct spdk_io_channel *
40 dev_create_channel(struct spdk_bs_dev *dev)
41 {
42 	return NULL;
43 }
44 
45 static void
46 dev_destroy_channel(struct spdk_bs_dev *dev, struct spdk_io_channel *channel)
47 {
48 }
49 
50 static void
51 dev_destroy(struct spdk_bs_dev *dev)
52 {
53 	free(dev);
54 }
55 
56 static void
57 dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
58 	 uint64_t lba, uint32_t lba_count,
59 	 struct spdk_bs_dev_cb_args *cb_args)
60 {
61 	uint64_t offset, length;
62 
63 	offset = lba * DEV_BUFFER_BLOCKLEN;
64 	length = lba_count * DEV_BUFFER_BLOCKLEN;
65 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
66 	memcpy(payload, &g_dev_buffer[offset], length);
67 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
68 }
69 
70 static void
71 dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
72 	  uint64_t lba, uint32_t lba_count,
73 	  struct spdk_bs_dev_cb_args *cb_args)
74 {
75 	uint64_t offset, length;
76 
77 	offset = lba * DEV_BUFFER_BLOCKLEN;
78 	length = lba_count * DEV_BUFFER_BLOCKLEN;
79 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
80 	memcpy(&g_dev_buffer[offset], payload, length);
81 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
82 }
83 
84 static void
85 __check_iov(struct iovec *iov, int iovcnt, uint64_t length)
86 {
87 	int i;
88 
89 	for (i = 0; i < iovcnt; i++) {
90 		length -= iov[i].iov_len;
91 	}
92 
93 	CU_ASSERT(length == 0);
94 }
95 
96 static void
97 dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
98 	  struct iovec *iov, int iovcnt,
99 	  uint64_t lba, uint32_t lba_count,
100 	  struct spdk_bs_dev_cb_args *cb_args)
101 {
102 	uint64_t offset, length;
103 	int i;
104 
105 	offset = lba * DEV_BUFFER_BLOCKLEN;
106 	length = lba_count * DEV_BUFFER_BLOCKLEN;
107 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
108 	__check_iov(iov, iovcnt, length);
109 
110 	for (i = 0; i < iovcnt; i++) {
111 		memcpy(iov[i].iov_base, &g_dev_buffer[offset], iov[i].iov_len);
112 		offset += iov[i].iov_len;
113 	}
114 
115 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
116 }
117 
118 static void
119 dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
120 	   struct iovec *iov, int iovcnt,
121 	   uint64_t lba, uint32_t lba_count,
122 	   struct spdk_bs_dev_cb_args *cb_args)
123 {
124 	uint64_t offset, length;
125 	int i;
126 
127 	offset = lba * DEV_BUFFER_BLOCKLEN;
128 	length = lba_count * DEV_BUFFER_BLOCKLEN;
129 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
130 	__check_iov(iov, iovcnt, length);
131 
132 	for (i = 0; i < iovcnt; i++) {
133 		memcpy(&g_dev_buffer[offset], iov[i].iov_base, iov[i].iov_len);
134 		offset += iov[i].iov_len;
135 	}
136 
137 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
138 }
139 
140 static void
141 dev_flush(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
142 	  struct spdk_bs_dev_cb_args *cb_args)
143 {
144 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
145 }
146 
147 static void
148 dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
149 	  uint64_t lba, uint32_t lba_count,
150 	  struct spdk_bs_dev_cb_args *cb_args)
151 {
152 	uint64_t offset, length;
153 
154 	offset = lba * DEV_BUFFER_BLOCKLEN;
155 	length = lba_count * DEV_BUFFER_BLOCKLEN;
156 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
157 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
158 }
159 
160 static void
161 dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
162 		 uint64_t lba, uint32_t lba_count,
163 		 struct spdk_bs_dev_cb_args *cb_args)
164 {
165 	uint64_t offset, length;
166 
167 	offset = lba * DEV_BUFFER_BLOCKLEN;
168 	length = lba_count * DEV_BUFFER_BLOCKLEN;
169 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
170 	memset(&g_dev_buffer[offset], 0, length);
171 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
172 }
173 
174 static struct spdk_bs_dev *
175 init_dev(void)
176 {
177 	struct spdk_bs_dev *dev = calloc(1, sizeof(*dev));
178 
179 	SPDK_CU_ASSERT_FATAL(dev != NULL);
180 
181 	dev->create_channel = dev_create_channel;
182 	dev->destroy_channel = dev_destroy_channel;
183 	dev->destroy = dev_destroy;
184 	dev->read = dev_read;
185 	dev->write = dev_write;
186 	dev->readv = dev_readv;
187 	dev->writev = dev_writev;
188 	dev->flush = dev_flush;
189 	dev->unmap = dev_unmap;
190 	dev->write_zeroes = dev_write_zeroes;
191 	dev->blockcnt = DEV_BUFFER_BLOCKCNT;
192 	dev->blocklen = DEV_BUFFER_BLOCKLEN;
193 
194 	return dev;
195 }
196