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 36 #include "spdk/event.h" 37 #include "spdk/nvme.h" 38 #include "spdk/string.h" 39 #include "spdk/thread.h" 40 41 static char g_path[256]; 42 static struct spdk_poller *g_poller; 43 44 struct ctrlr_entry { 45 struct spdk_nvme_ctrlr *ctrlr; 46 TAILQ_ENTRY(ctrlr_entry) link; 47 }; 48 49 static TAILQ_HEAD(, ctrlr_entry) g_controllers = TAILQ_HEAD_INITIALIZER(g_controllers); 50 51 static void 52 cleanup(void) 53 { 54 struct ctrlr_entry *ctrlr_entry, *tmp; 55 struct spdk_nvme_detach_ctx *detach_ctx = NULL; 56 57 TAILQ_FOREACH_SAFE(ctrlr_entry, &g_controllers, link, tmp) { 58 TAILQ_REMOVE(&g_controllers, ctrlr_entry, link); 59 spdk_nvme_detach_async(ctrlr_entry->ctrlr, &detach_ctx); 60 free(ctrlr_entry); 61 } 62 63 while (detach_ctx && spdk_nvme_detach_poll_async(detach_ctx) == -EAGAIN) { 64 ; 65 } 66 } 67 68 static void 69 usage(char *executable_name) 70 { 71 printf("%s [options]\n", executable_name); 72 printf("options:\n"); 73 printf(" -i shared memory ID [required]\n"); 74 printf(" -m mask core mask for DPDK\n"); 75 printf(" -n channel number of memory channels used for DPDK\n"); 76 printf(" -p core main (primary) core for DPDK\n"); 77 printf(" -s size memory size in MB for DPDK\n"); 78 printf(" -H show this usage\n"); 79 } 80 81 static bool 82 probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, 83 struct spdk_nvme_ctrlr_opts *opts) 84 { 85 /* 86 * Set the io_queue_size to UINT16_MAX to initialize 87 * the controller with the possible largest queue size. 88 */ 89 opts->io_queue_size = UINT16_MAX; 90 return true; 91 } 92 93 static void 94 attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, 95 struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts) 96 { 97 struct ctrlr_entry *entry; 98 99 entry = malloc(sizeof(struct ctrlr_entry)); 100 if (entry == NULL) { 101 fprintf(stderr, "Malloc error\n"); 102 exit(1); 103 } 104 105 entry->ctrlr = ctrlr; 106 TAILQ_INSERT_TAIL(&g_controllers, entry, link); 107 } 108 109 static int 110 stub_sleep(void *arg) 111 { 112 usleep(1000 * 1000); 113 return 0; 114 } 115 116 static void 117 stub_start(void *arg1) 118 { 119 int shm_id = (intptr_t)arg1; 120 121 spdk_unaffinitize_thread(); 122 123 if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) { 124 fprintf(stderr, "spdk_nvme_probe() failed\n"); 125 exit(1); 126 } 127 128 snprintf(g_path, sizeof(g_path), "/var/run/spdk_stub%d", shm_id); 129 if (mknod(g_path, S_IFREG, 0) != 0) { 130 fprintf(stderr, "could not create sentinel file %s\n", g_path); 131 exit(1); 132 } 133 134 g_poller = SPDK_POLLER_REGISTER(stub_sleep, NULL, 0); 135 } 136 137 static void 138 stub_shutdown(void) 139 { 140 spdk_poller_unregister(&g_poller); 141 unlink(g_path); 142 spdk_app_stop(0); 143 } 144 145 int 146 main(int argc, char **argv) 147 { 148 int ch; 149 struct spdk_app_opts opts = {}; 150 long int val; 151 152 /* default value in opts structure */ 153 spdk_app_opts_init(&opts, sizeof(opts)); 154 155 opts.name = "stub"; 156 opts.rpc_addr = NULL; 157 158 while ((ch = getopt(argc, argv, "i:m:n:p:s:H")) != -1) { 159 if (ch == 'm') { 160 opts.reactor_mask = optarg; 161 } else if (ch == '?' || ch == 'H') { 162 usage(argv[0]); 163 exit(EXIT_SUCCESS); 164 } else { 165 val = spdk_strtol(optarg, 10); 166 if (val < 0) { 167 fprintf(stderr, "Converting a string to integer failed\n"); 168 exit(1); 169 } 170 switch (ch) { 171 case 'i': 172 opts.shm_id = val; 173 break; 174 case 'n': 175 opts.mem_channel = val; 176 break; 177 case 'p': 178 opts.main_core = val; 179 break; 180 case 's': 181 opts.mem_size = val; 182 break; 183 default: 184 usage(argv[0]); 185 exit(EXIT_FAILURE); 186 } 187 } 188 } 189 190 if (opts.shm_id < 0) { 191 fprintf(stderr, "%s: -i shared memory ID must be specified\n", argv[0]); 192 usage(argv[0]); 193 exit(1); 194 } 195 196 opts.shutdown_cb = stub_shutdown; 197 198 ch = spdk_app_start(&opts, stub_start, (void *)(intptr_t)opts.shm_id); 199 200 cleanup(); 201 spdk_app_fini(); 202 203 return ch; 204 } 205