xref: /spdk/examples/bdev/hello_world/hello_bdev.c (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
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/stdinc.h"
35 #include "spdk/thread.h"
36 #include "spdk/bdev.h"
37 #include "spdk/env.h"
38 #include "spdk/event.h"
39 #include "spdk/log.h"
40 #include "spdk/string.h"
41 #include "spdk/bdev_module.h"
42 
43 static char *g_bdev_name = "Malloc0";
44 
45 /*
46  * We'll use this struct to gather housekeeping hello_context to pass between
47  * our events and callbacks.
48  */
49 struct hello_context_t {
50 	struct spdk_bdev *bdev;
51 	struct spdk_bdev_desc *bdev_desc;
52 	struct spdk_io_channel *bdev_io_channel;
53 	char *buff;
54 	char *bdev_name;
55 	struct spdk_bdev_io_wait_entry bdev_io_wait;
56 };
57 
58 /*
59  * Usage function for printing parameters that are specific to this application
60  */
61 static void
62 hello_bdev_usage(void)
63 {
64 	printf(" -b <bdev>                 name of the bdev to use\n");
65 }
66 
67 /*
68  * This function is called to parse the parameters that are specific to this application
69  */
70 static int hello_bdev_parse_arg(int ch, char *arg)
71 {
72 	switch (ch) {
73 	case 'b':
74 		g_bdev_name = arg;
75 		break;
76 	default:
77 		return -EINVAL;
78 	}
79 	return 0;
80 }
81 
82 /*
83  * Callback function for read io completion.
84  */
85 static void
86 read_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
87 {
88 	struct hello_context_t *hello_context = cb_arg;
89 
90 	if (success) {
91 		SPDK_NOTICELOG("Read string from bdev : %s\n", hello_context->buff);
92 	} else {
93 		SPDK_ERRLOG("bdev io read error\n");
94 	}
95 
96 	/* Complete the bdev io and close the channel */
97 	spdk_bdev_free_io(bdev_io);
98 	spdk_put_io_channel(hello_context->bdev_io_channel);
99 	spdk_bdev_close(hello_context->bdev_desc);
100 	SPDK_NOTICELOG("Stopping app\n");
101 	spdk_app_stop(success ? 0 : -1);
102 }
103 
104 static void
105 hello_read(void *arg)
106 {
107 	struct hello_context_t *hello_context = arg;
108 	int rc = 0;
109 	uint32_t length = spdk_bdev_get_block_size(hello_context->bdev);
110 
111 	SPDK_NOTICELOG("Reading io\n");
112 	rc = spdk_bdev_read(hello_context->bdev_desc, hello_context->bdev_io_channel,
113 			    hello_context->buff, 0, length, read_complete, hello_context);
114 
115 	if (rc == -ENOMEM) {
116 		SPDK_NOTICELOG("Queueing io\n");
117 		/* In case we cannot perform I/O now, queue I/O */
118 		hello_context->bdev_io_wait.bdev = hello_context->bdev;
119 		hello_context->bdev_io_wait.cb_fn = hello_read;
120 		hello_context->bdev_io_wait.cb_arg = hello_context;
121 		spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel,
122 					&hello_context->bdev_io_wait);
123 	} else if (rc) {
124 		SPDK_ERRLOG("%s error while reading from bdev: %d\n", spdk_strerror(-rc), rc);
125 		spdk_put_io_channel(hello_context->bdev_io_channel);
126 		spdk_bdev_close(hello_context->bdev_desc);
127 		spdk_app_stop(-1);
128 	}
129 }
130 
131 /*
132  * Callback function for write io completion.
133  */
134 static void
135 write_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
136 {
137 	struct hello_context_t *hello_context = cb_arg;
138 	uint32_t length;
139 
140 	/* Complete the I/O */
141 	spdk_bdev_free_io(bdev_io);
142 
143 	if (success) {
144 		SPDK_NOTICELOG("bdev io write completed successfully\n");
145 	} else {
146 		SPDK_ERRLOG("bdev io write error: %d\n", EIO);
147 		spdk_put_io_channel(hello_context->bdev_io_channel);
148 		spdk_bdev_close(hello_context->bdev_desc);
149 		spdk_app_stop(-1);
150 		return;
151 	}
152 
153 	/* Zero the buffer so that we can use it for reading */
154 	length = spdk_bdev_get_block_size(hello_context->bdev);
155 	memset(hello_context->buff, 0, length);
156 
157 	hello_read(hello_context);
158 }
159 
160 static void
161 hello_write(void *arg)
162 {
163 	struct hello_context_t *hello_context = arg;
164 	int rc = 0;
165 	uint32_t length = spdk_bdev_get_block_size(hello_context->bdev);
166 
167 	SPDK_NOTICELOG("Writing to the bdev\n");
168 	rc = spdk_bdev_write(hello_context->bdev_desc, hello_context->bdev_io_channel,
169 			     hello_context->buff, 0, length, write_complete, hello_context);
170 
171 	if (rc == -ENOMEM) {
172 		SPDK_NOTICELOG("Queueing io\n");
173 		/* In case we cannot perform I/O now, queue I/O */
174 		hello_context->bdev_io_wait.bdev = hello_context->bdev;
175 		hello_context->bdev_io_wait.cb_fn = hello_write;
176 		hello_context->bdev_io_wait.cb_arg = hello_context;
177 		spdk_bdev_queue_io_wait(hello_context->bdev, hello_context->bdev_io_channel,
178 					&hello_context->bdev_io_wait);
179 	} else if (rc) {
180 		SPDK_ERRLOG("%s error while writing to bdev: %d\n", spdk_strerror(-rc), rc);
181 		spdk_put_io_channel(hello_context->bdev_io_channel);
182 		spdk_bdev_close(hello_context->bdev_desc);
183 		spdk_app_stop(-1);
184 	}
185 }
186 
187 /*
188  * Our initial event that kicks off everything from main().
189  */
190 static void
191 hello_start(void *arg1)
192 {
193 	struct hello_context_t *hello_context = arg1;
194 	uint32_t blk_size, buf_align;
195 	int rc = 0;
196 	hello_context->bdev = NULL;
197 	hello_context->bdev_desc = NULL;
198 
199 	SPDK_NOTICELOG("Successfully started the application\n");
200 
201 	/*
202 	 * Get the bdev. There can be many bdevs configured, but this
203 	 * application will only use the one input by the user at runtime so
204 	 * we get it via its name.
205 	 */
206 	hello_context->bdev = spdk_bdev_get_by_name(hello_context->bdev_name);
207 	if (hello_context->bdev == NULL) {
208 		SPDK_ERRLOG("Could not find the bdev: %s\n", hello_context->bdev_name);
209 		spdk_app_stop(-1);
210 		return;
211 	}
212 
213 	/*
214 	 * Open the bdev by calling spdk_bdev_open()
215 	 * The function will return a descriptor
216 	 */
217 	SPDK_NOTICELOG("Opening the bdev %s\n", hello_context->bdev_name);
218 	rc = spdk_bdev_open(hello_context->bdev, true, NULL, NULL, &hello_context->bdev_desc);
219 	if (rc) {
220 		SPDK_ERRLOG("Could not open bdev: %s\n", hello_context->bdev_name);
221 		spdk_app_stop(-1);
222 		return;
223 	}
224 
225 	SPDK_NOTICELOG("Opening io channel\n");
226 	/* Open I/O channel */
227 	hello_context->bdev_io_channel = spdk_bdev_get_io_channel(hello_context->bdev_desc);
228 	if (hello_context->bdev_io_channel == NULL) {
229 		SPDK_ERRLOG("Could not create bdev I/O channel!!\n");
230 		spdk_bdev_close(hello_context->bdev_desc);
231 		spdk_app_stop(-1);
232 		return;
233 	}
234 
235 	/* Allocate memory for the write buffer.
236 	 * Initialize the write buffer with the string "Hello World!"
237 	 */
238 	blk_size = spdk_bdev_get_block_size(hello_context->bdev);
239 	buf_align = spdk_bdev_get_buf_align(hello_context->bdev);
240 	hello_context->buff = spdk_dma_zmalloc(blk_size, buf_align, NULL);
241 	if (!hello_context->buff) {
242 		SPDK_ERRLOG("Failed to allocate buffer\n");
243 		spdk_put_io_channel(hello_context->bdev_io_channel);
244 		spdk_bdev_close(hello_context->bdev_desc);
245 		spdk_app_stop(-1);
246 		return;
247 	}
248 	snprintf(hello_context->buff, blk_size, "%s", "Hello World!\n");
249 
250 	hello_write(hello_context);
251 }
252 
253 int
254 main(int argc, char **argv)
255 {
256 	struct spdk_app_opts opts = {};
257 	int rc = 0;
258 	struct hello_context_t hello_context = {};
259 
260 	/* Set default values in opts structure. */
261 	spdk_app_opts_init(&opts);
262 	opts.name = "hello_bdev";
263 
264 	/*
265 	 * Parse built-in SPDK command line parameters as well
266 	 * as our custom one(s).
267 	 */
268 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "b:", NULL, hello_bdev_parse_arg,
269 				      hello_bdev_usage)) != SPDK_APP_PARSE_ARGS_SUCCESS) {
270 		exit(rc);
271 	}
272 	hello_context.bdev_name = g_bdev_name;
273 
274 	/*
275 	 * spdk_app_start() will initialize the SPDK framework, call hello_start(),
276 	 * and then block until spdk_app_stop() is called (or if an initialization
277 	 * error occurs, spdk_app_start() will return with rc even without calling
278 	 * hello_start().
279 	 */
280 	rc = spdk_app_start(&opts, hello_start, &hello_context);
281 	if (rc) {
282 		SPDK_ERRLOG("ERROR starting application\n");
283 	}
284 
285 	/* At this point either spdk_app_stop() was called, or spdk_app_start()
286 	 * failed because of internal error.
287 	 */
288 
289 	/* When the app stops, free up memory that we allocated. */
290 	spdk_dma_free(hello_context.buff);
291 
292 	/* Gracefully close out all of the SPDK subsystems. */
293 	spdk_app_fini();
294 	return rc;
295 }
296