]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/nvme/err_injection/err_injection.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / test / nvme / err_injection / err_injection.c
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/log.h"
37 #include "spdk/nvme.h"
38 #include "spdk/env.h"
39
40 #define MAX_DEVS 64
41
42 struct dev {
43 bool error_expected;
44 struct spdk_nvme_ctrlr *ctrlr;
45 struct spdk_nvme_ns *ns;
46 struct spdk_nvme_qpair *qpair;
47 void *data;
48 char name[SPDK_NVMF_TRADDR_MAX_LEN + 1];
49 };
50
51 static struct dev devs[MAX_DEVS];
52 static int num_devs = 0;
53
54 #define foreach_dev(iter) \
55 for (iter = devs; iter - devs < num_devs; iter++)
56
57 static int outstanding_commands = 0;
58 static int failed = 0;
59
60 static bool
61 probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
62 struct spdk_nvme_ctrlr_opts *opts)
63 {
64 printf("Attaching to %s\n", trid->traddr);
65
66 return true;
67 }
68
69 static void
70 attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
71 struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
72 {
73 struct dev *dev;
74 uint32_t nsid;
75
76 /* add to dev list */
77 dev = &devs[num_devs++];
78 if (num_devs >= MAX_DEVS) {
79 return;
80 }
81
82 dev->ctrlr = ctrlr;
83 nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
84 dev->ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
85 if (dev->ns == NULL) {
86 failed = 1;
87 return;
88 }
89 dev->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ctrlr, NULL, 0);
90 if (dev->qpair == NULL) {
91 failed = 1;
92 return;
93 }
94
95 snprintf(dev->name, sizeof(dev->name), "%s",
96 trid->traddr);
97
98 printf("Attached to %s\n", dev->name);
99 }
100
101 static void
102 get_feature_test_cb(void *cb_arg, const struct spdk_nvme_cpl *cpl)
103 {
104 struct dev *dev = cb_arg;
105
106 outstanding_commands--;
107
108 if (spdk_nvme_cpl_is_error(cpl) && dev->error_expected) {
109 if (cpl->status.sct != SPDK_NVME_SCT_GENERIC ||
110 cpl->status.sc != SPDK_NVME_SC_INVALID_FIELD) {
111 failed = 1;
112 }
113 printf("%s: get features failed as expected\n", dev->name);
114 return;
115 }
116
117 if (!spdk_nvme_cpl_is_error(cpl) && !dev->error_expected) {
118 printf("%s: get features successfully as expected\n", dev->name);
119 return;
120 }
121
122 failed = 1;
123 }
124
125 static void
126 get_feature_test(bool error_expected)
127 {
128 struct dev *dev;
129 struct spdk_nvme_cmd cmd;
130
131 memset(&cmd, 0, sizeof(cmd));
132 cmd.opc = SPDK_NVME_OPC_GET_FEATURES;
133 cmd.cdw10 = SPDK_NVME_FEAT_NUMBER_OF_QUEUES;
134
135 foreach_dev(dev) {
136 dev->error_expected = error_expected;
137 if (spdk_nvme_ctrlr_cmd_admin_raw(dev->ctrlr, &cmd, NULL, 0,
138 get_feature_test_cb, dev) != 0) {
139 printf("Error: failed to send Get Features command for dev=%p\n", dev);
140 failed = 1;
141 goto cleanup;
142 }
143 outstanding_commands++;
144 }
145
146 cleanup:
147
148 while (outstanding_commands) {
149 foreach_dev(dev) {
150 spdk_nvme_ctrlr_process_admin_completions(dev->ctrlr);
151 }
152 }
153 }
154
155 static void
156 read_test_cb(void *cb_arg, const struct spdk_nvme_cpl *cpl)
157 {
158 struct dev *dev = cb_arg;
159
160 outstanding_commands--;
161 spdk_dma_free(dev->data);
162
163 if (spdk_nvme_cpl_is_error(cpl) && dev->error_expected) {
164 if (cpl->status.sct != SPDK_NVME_SCT_MEDIA_ERROR ||
165 cpl->status.sc != SPDK_NVME_SC_UNRECOVERED_READ_ERROR) {
166 failed = 1;
167 }
168 printf("%s: read failed as expected\n", dev->name);
169 return;
170 }
171
172 if (!spdk_nvme_cpl_is_error(cpl) && !dev->error_expected) {
173 printf("%s: read successfully as expected\n", dev->name);
174 return;
175 }
176
177 failed = 1;
178 }
179
180 static void
181 read_test(bool error_expected)
182 {
183 struct dev *dev;
184
185 foreach_dev(dev) {
186 dev->error_expected = error_expected;
187 dev->data = spdk_dma_zmalloc(0x1000, 0x1000, NULL);
188 if (!dev->data) {
189 failed = 1;
190 goto cleanup;
191 }
192
193 if (spdk_nvme_ns_cmd_read(dev->ns, dev->qpair, dev->data,
194 0, 1, read_test_cb, dev, 0) != 0) {
195 printf("Error: failed to send Read command for dev=%p\n", dev);
196 failed = 1;
197 goto cleanup;
198 }
199
200 outstanding_commands++;
201 }
202
203 cleanup:
204
205 while (outstanding_commands) {
206 foreach_dev(dev) {
207 spdk_nvme_qpair_process_completions(dev->qpair, 0);
208 }
209 }
210 }
211
212 int main(int argc, char **argv)
213 {
214 struct dev *dev;
215 int i;
216 struct spdk_env_opts opts;
217 int rc;
218
219 spdk_env_opts_init(&opts);
220 opts.name = "err_injection";
221 opts.core_mask = "0x1";
222 opts.shm_id = 0;
223 if (spdk_env_init(&opts) < 0) {
224 fprintf(stderr, "Unable to initialize SPDK env\n");
225 return 1;
226 }
227
228 printf("NVMe Error Injection test\n");
229
230 if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
231 fprintf(stderr, "spdk_nvme_probe() failed\n");
232 return 1;
233 }
234
235 if (failed) {
236 goto exit;
237 }
238
239 if (!num_devs) {
240 printf("No NVMe controller found, %s exiting\n", argv[0]);
241 return 1;
242 }
243
244 foreach_dev(dev) {
245 /* Admin error injection at submission path */
246 rc = spdk_nvme_qpair_add_cmd_error_injection(dev->ctrlr, NULL,
247 SPDK_NVME_OPC_GET_FEATURES, true, 5000, 1,
248 SPDK_NVME_SCT_GENERIC, SPDK_NVME_SC_INVALID_FIELD);
249 failed += rc;
250 /* IO error injection at completion path */
251 rc = spdk_nvme_qpair_add_cmd_error_injection(dev->ctrlr, dev->qpair,
252 SPDK_NVME_OPC_READ, false, 0, 1,
253 SPDK_NVME_SCT_MEDIA_ERROR, SPDK_NVME_SC_UNRECOVERED_READ_ERROR);
254 failed += rc;
255 }
256
257 if (failed) {
258 goto exit;
259 }
260
261 /* Admin Get Feature, expect error return */
262 get_feature_test(true);
263 /* Admin Get Feature, expect successful return */
264 get_feature_test(false);
265 /* Read, expect error return */
266 read_test(true);
267 /* Read, expect successful return */
268 read_test(false);
269
270 exit:
271 printf("Cleaning up...\n");
272 for (i = 0; i < num_devs; i++) {
273 struct dev *dev = &devs[i];
274 spdk_nvme_detach(dev->ctrlr);
275 }
276
277 return failed;
278 }