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