xref: /spdk/test/unit/lib/blob/bs_dev_common.c (revision 552e21cce6cccbf833ed9109827e08337377d7ce)
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 #include "spdk/thread.h"
35 #include "bs_scheduler.c"
36 
37 
38 #define DEV_BUFFER_SIZE (64 * 1024 * 1024)
39 #define DEV_BUFFER_BLOCKLEN (4096)
40 #define DEV_BUFFER_BLOCKCNT (DEV_BUFFER_SIZE / DEV_BUFFER_BLOCKLEN)
41 uint8_t *g_dev_buffer;
42 uint64_t g_dev_write_bytes;
43 uint64_t g_dev_read_bytes;
44 
45 /* Define here for UT only. */
46 struct spdk_io_channel g_io_channel;
47 
48 static struct spdk_io_channel *
49 dev_create_channel(struct spdk_bs_dev *dev)
50 {
51 	return &g_io_channel;
52 }
53 
54 static void
55 dev_destroy_channel(struct spdk_bs_dev *dev, struct spdk_io_channel *channel)
56 {
57 }
58 
59 static void
60 dev_destroy(struct spdk_bs_dev *dev)
61 {
62 	free(dev);
63 }
64 
65 
66 static void
67 dev_complete_cb(void *arg)
68 {
69 	struct spdk_bs_dev_cb_args *cb_args = arg;
70 
71 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
72 }
73 
74 static void
75 dev_complete(void *arg)
76 {
77 	_bs_send_msg(dev_complete_cb, arg, NULL);
78 }
79 
80 static void
81 dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
82 	 uint64_t lba, uint32_t lba_count,
83 	 struct spdk_bs_dev_cb_args *cb_args)
84 {
85 	uint64_t offset, length;
86 
87 	offset = lba * dev->blocklen;
88 	length = lba_count * dev->blocklen;
89 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
90 	memcpy(payload, &g_dev_buffer[offset], length);
91 	g_dev_read_bytes += length;
92 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
93 }
94 
95 static void
96 dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
97 	  uint64_t lba, uint32_t lba_count,
98 	  struct spdk_bs_dev_cb_args *cb_args)
99 {
100 	uint64_t offset, length;
101 
102 	offset = lba * dev->blocklen;
103 	length = lba_count * dev->blocklen;
104 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
105 	memcpy(&g_dev_buffer[offset], payload, length);
106 	g_dev_write_bytes += length;
107 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
108 }
109 
110 static void
111 __check_iov(struct iovec *iov, int iovcnt, uint64_t length)
112 {
113 	int i;
114 
115 	for (i = 0; i < iovcnt; i++) {
116 		length -= iov[i].iov_len;
117 	}
118 
119 	CU_ASSERT(length == 0);
120 }
121 
122 static void
123 dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
124 	  struct iovec *iov, int iovcnt,
125 	  uint64_t lba, uint32_t lba_count,
126 	  struct spdk_bs_dev_cb_args *cb_args)
127 {
128 	uint64_t offset, length;
129 	int i;
130 
131 	offset = lba * dev->blocklen;
132 	length = lba_count * dev->blocklen;
133 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
134 	__check_iov(iov, iovcnt, length);
135 
136 	for (i = 0; i < iovcnt; i++) {
137 		memcpy(iov[i].iov_base, &g_dev_buffer[offset], iov[i].iov_len);
138 		offset += iov[i].iov_len;
139 	}
140 
141 	g_dev_read_bytes += length;
142 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
143 }
144 
145 static void
146 dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
147 	   struct iovec *iov, int iovcnt,
148 	   uint64_t lba, uint32_t lba_count,
149 	   struct spdk_bs_dev_cb_args *cb_args)
150 {
151 	uint64_t offset, length;
152 	int i;
153 
154 	offset = lba * dev->blocklen;
155 	length = lba_count * dev->blocklen;
156 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
157 	__check_iov(iov, iovcnt, length);
158 
159 	for (i = 0; i < iovcnt; i++) {
160 		memcpy(&g_dev_buffer[offset], iov[i].iov_base, iov[i].iov_len);
161 		offset += iov[i].iov_len;
162 	}
163 
164 	g_dev_write_bytes += length;
165 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
166 }
167 
168 static void
169 dev_flush(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
170 	  struct spdk_bs_dev_cb_args *cb_args)
171 {
172 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
173 }
174 
175 static void
176 dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
177 	  uint64_t lba, uint32_t lba_count,
178 	  struct spdk_bs_dev_cb_args *cb_args)
179 {
180 	uint64_t offset, length;
181 
182 	offset = lba * dev->blocklen;
183 	length = lba_count * dev->blocklen;
184 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
185 	memset(&g_dev_buffer[offset], 0, length);
186 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
187 }
188 
189 static void
190 dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
191 		 uint64_t lba, uint32_t lba_count,
192 		 struct spdk_bs_dev_cb_args *cb_args)
193 {
194 	uint64_t offset, length;
195 
196 	offset = lba * dev->blocklen;
197 	length = lba_count * dev->blocklen;
198 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
199 	memset(&g_dev_buffer[offset], 0, length);
200 	g_dev_write_bytes += length;
201 	spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args);
202 }
203 
204 static struct spdk_bs_dev *
205 init_dev(void)
206 {
207 	struct spdk_bs_dev *dev = calloc(1, sizeof(*dev));
208 
209 	SPDK_CU_ASSERT_FATAL(dev != NULL);
210 
211 	dev->create_channel = dev_create_channel;
212 	dev->destroy_channel = dev_destroy_channel;
213 	dev->destroy = dev_destroy;
214 	dev->read = dev_read;
215 	dev->write = dev_write;
216 	dev->readv = dev_readv;
217 	dev->writev = dev_writev;
218 	dev->flush = dev_flush;
219 	dev->unmap = dev_unmap;
220 	dev->write_zeroes = dev_write_zeroes;
221 	dev->blockcnt = DEV_BUFFER_BLOCKCNT;
222 	dev->blocklen = DEV_BUFFER_BLOCKLEN;
223 
224 	return dev;
225 }
226