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 static void 188 hello_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 189 void *event_ctx) 190 { 191 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 192 } 193 194 /* 195 * Our initial event that kicks off everything from main(). 196 */ 197 static void 198 hello_start(void *arg1) 199 { 200 struct hello_context_t *hello_context = arg1; 201 uint32_t blk_size, buf_align; 202 int rc = 0; 203 hello_context->bdev = NULL; 204 hello_context->bdev_desc = NULL; 205 206 SPDK_NOTICELOG("Successfully started the application\n"); 207 208 /* 209 * There can be many bdevs configured, but this application will only use 210 * the one input by the user at runtime. 211 * 212 * Open the bdev by calling spdk_bdev_open_ext() with its name. 213 * The function will return a descriptor 214 */ 215 SPDK_NOTICELOG("Opening the bdev %s\n", hello_context->bdev_name); 216 rc = spdk_bdev_open_ext(hello_context->bdev_name, true, hello_bdev_event_cb, NULL, 217 &hello_context->bdev_desc); 218 if (rc) { 219 SPDK_ERRLOG("Could not open bdev: %s\n", hello_context->bdev_name); 220 spdk_app_stop(-1); 221 return; 222 } 223 224 /* A bdev pointer is valid while the bdev is opened. */ 225 hello_context->bdev = spdk_bdev_desc_get_bdev(hello_context->bdev_desc); 226 227 228 SPDK_NOTICELOG("Opening io channel\n"); 229 /* Open I/O channel */ 230 hello_context->bdev_io_channel = spdk_bdev_get_io_channel(hello_context->bdev_desc); 231 if (hello_context->bdev_io_channel == NULL) { 232 SPDK_ERRLOG("Could not create bdev I/O channel!!\n"); 233 spdk_bdev_close(hello_context->bdev_desc); 234 spdk_app_stop(-1); 235 return; 236 } 237 238 /* Allocate memory for the write buffer. 239 * Initialize the write buffer with the string "Hello World!" 240 */ 241 blk_size = spdk_bdev_get_block_size(hello_context->bdev); 242 buf_align = spdk_bdev_get_buf_align(hello_context->bdev); 243 hello_context->buff = spdk_dma_zmalloc(blk_size, buf_align, NULL); 244 if (!hello_context->buff) { 245 SPDK_ERRLOG("Failed to allocate buffer\n"); 246 spdk_put_io_channel(hello_context->bdev_io_channel); 247 spdk_bdev_close(hello_context->bdev_desc); 248 spdk_app_stop(-1); 249 return; 250 } 251 snprintf(hello_context->buff, blk_size, "%s", "Hello World!\n"); 252 253 hello_write(hello_context); 254 } 255 256 int 257 main(int argc, char **argv) 258 { 259 struct spdk_app_opts opts = {}; 260 int rc = 0; 261 struct hello_context_t hello_context = {}; 262 263 /* Set default values in opts structure. */ 264 spdk_app_opts_init(&opts, sizeof(opts)); 265 opts.name = "hello_bdev"; 266 267 /* 268 * Parse built-in SPDK command line parameters as well 269 * as our custom one(s). 270 */ 271 if ((rc = spdk_app_parse_args(argc, argv, &opts, "b:", NULL, hello_bdev_parse_arg, 272 hello_bdev_usage)) != SPDK_APP_PARSE_ARGS_SUCCESS) { 273 exit(rc); 274 } 275 hello_context.bdev_name = g_bdev_name; 276 277 /* 278 * spdk_app_start() will initialize the SPDK framework, call hello_start(), 279 * and then block until spdk_app_stop() is called (or if an initialization 280 * error occurs, spdk_app_start() will return with rc even without calling 281 * hello_start(). 282 */ 283 rc = spdk_app_start(&opts, hello_start, &hello_context); 284 if (rc) { 285 SPDK_ERRLOG("ERROR starting application\n"); 286 } 287 288 /* At this point either spdk_app_stop() was called, or spdk_app_start() 289 * failed because of internal error. 290 */ 291 292 /* When the app stops, free up memory that we allocated. */ 293 spdk_dma_free(hello_context.buff); 294 295 /* Gracefully close out all of the SPDK subsystems. */ 296 spdk_app_fini(); 297 return rc; 298 } 299