Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2017 Intel Corporation.
3 : * All rights reserved.
4 : */
5 :
6 : #include "spdk/stdinc.h"
7 : #include "spdk/string.h"
8 : #include "spdk/rpc.h"
9 : #include "spdk/util.h"
10 : #include "spdk/log.h"
11 : #include "vbdev_error.h"
12 :
13 : static int
14 0 : rpc_error_bdev_decode_io_type(const struct spdk_json_val *val, void *out)
15 : {
16 0 : uint32_t *io_type = out;
17 :
18 0 : if (spdk_json_strequal(val, "read") == true) {
19 0 : *io_type = SPDK_BDEV_IO_TYPE_READ;
20 0 : } else if (spdk_json_strequal(val, "write") == true) {
21 0 : *io_type = SPDK_BDEV_IO_TYPE_WRITE;
22 0 : } else if (spdk_json_strequal(val, "flush") == true) {
23 0 : *io_type = SPDK_BDEV_IO_TYPE_FLUSH;
24 0 : } else if (spdk_json_strequal(val, "unmap") == true) {
25 0 : *io_type = SPDK_BDEV_IO_TYPE_UNMAP;
26 0 : } else if (spdk_json_strequal(val, "all") == true) {
27 0 : *io_type = 0xffffffff;
28 0 : } else if (spdk_json_strequal(val, "clear") == true) {
29 0 : *io_type = 0;
30 : } else {
31 0 : SPDK_NOTICELOG("Invalid parameter value: io_type\n");
32 0 : return -EINVAL;
33 : }
34 :
35 0 : return 0;
36 : }
37 :
38 : static int
39 0 : rpc_error_bdev_decode_error_type(const struct spdk_json_val *val, void *out)
40 : {
41 0 : uint32_t *error_type = out;
42 :
43 0 : if (spdk_json_strequal(val, "failure") == true) {
44 0 : *error_type = VBDEV_IO_FAILURE;
45 0 : } else if (spdk_json_strequal(val, "pending") == true) {
46 0 : *error_type = VBDEV_IO_PENDING;
47 0 : } else if (spdk_json_strequal(val, "corrupt_data") == true) {
48 0 : *error_type = VBDEV_IO_CORRUPT_DATA;
49 0 : } else if (spdk_json_strequal(val, "nomem") == true) {
50 0 : *error_type = VBDEV_IO_NOMEM;
51 : } else {
52 0 : SPDK_NOTICELOG("Invalid parameter value: error_type\n");
53 0 : return -EINVAL;
54 : }
55 :
56 0 : return 0;
57 : }
58 :
59 : struct rpc_bdev_error_create {
60 : char *base_name;
61 : struct spdk_uuid uuid;
62 : };
63 :
64 : static void
65 0 : free_rpc_bdev_error_create(struct rpc_bdev_error_create *req)
66 : {
67 0 : free(req->base_name);
68 0 : }
69 :
70 : static const struct spdk_json_object_decoder rpc_bdev_error_create_decoders[] = {
71 : {"base_name", offsetof(struct rpc_bdev_error_create, base_name), spdk_json_decode_string},
72 : {"uuid", offsetof(struct rpc_bdev_error_create, uuid), spdk_json_decode_uuid, true},
73 : };
74 :
75 : static void
76 0 : rpc_bdev_error_create(struct spdk_jsonrpc_request *request,
77 : const struct spdk_json_val *params)
78 : {
79 0 : struct rpc_bdev_error_create req = {};
80 0 : int rc = 0;
81 :
82 0 : if (spdk_json_decode_object(params, rpc_bdev_error_create_decoders,
83 : SPDK_COUNTOF(rpc_bdev_error_create_decoders),
84 : &req)) {
85 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
86 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
87 : "spdk_json_decode_object failed");
88 0 : goto cleanup;
89 : }
90 :
91 0 : rc = vbdev_error_create(req.base_name, &req.uuid);
92 0 : if (rc) {
93 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
94 0 : goto cleanup;
95 : }
96 :
97 0 : spdk_jsonrpc_send_bool_response(request, true);
98 :
99 0 : cleanup:
100 0 : free_rpc_bdev_error_create(&req);
101 0 : }
102 0 : SPDK_RPC_REGISTER("bdev_error_create", rpc_bdev_error_create, SPDK_RPC_RUNTIME)
103 :
104 : struct rpc_delete_error {
105 : char *name;
106 : };
107 :
108 : static void
109 0 : free_rpc_delete_error(struct rpc_delete_error *r)
110 : {
111 0 : free(r->name);
112 0 : }
113 :
114 : static const struct spdk_json_object_decoder rpc_delete_error_decoders[] = {
115 : {"name", offsetof(struct rpc_delete_error, name), spdk_json_decode_string},
116 : };
117 :
118 : static void
119 0 : rpc_bdev_error_delete_cb(void *cb_arg, int bdeverrno)
120 : {
121 0 : struct spdk_jsonrpc_request *request = cb_arg;
122 :
123 0 : if (bdeverrno == 0) {
124 0 : spdk_jsonrpc_send_bool_response(request, true);
125 : } else {
126 0 : spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
127 : }
128 0 : }
129 :
130 : static void
131 0 : rpc_bdev_error_delete(struct spdk_jsonrpc_request *request,
132 : const struct spdk_json_val *params)
133 : {
134 0 : struct rpc_delete_error req = {NULL};
135 :
136 0 : if (spdk_json_decode_object(params, rpc_delete_error_decoders,
137 : SPDK_COUNTOF(rpc_delete_error_decoders),
138 : &req)) {
139 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
140 : "spdk_json_decode_object failed");
141 0 : goto cleanup;
142 : }
143 :
144 0 : vbdev_error_delete(req.name, rpc_bdev_error_delete_cb, request);
145 :
146 0 : cleanup:
147 0 : free_rpc_delete_error(&req);
148 0 : }
149 0 : SPDK_RPC_REGISTER("bdev_error_delete", rpc_bdev_error_delete, SPDK_RPC_RUNTIME)
150 :
151 : struct rpc_error_information {
152 : char *name;
153 : struct vbdev_error_inject_opts opts;
154 : };
155 :
156 : static const struct spdk_json_object_decoder rpc_error_information_decoders[] = {
157 : {"name", offsetof(struct rpc_error_information, name), spdk_json_decode_string},
158 : {"io_type", offsetof(struct rpc_error_information, opts.io_type), rpc_error_bdev_decode_io_type},
159 : {"error_type", offsetof(struct rpc_error_information, opts.error_type), rpc_error_bdev_decode_error_type},
160 : {"num", offsetof(struct rpc_error_information, opts.error_num), spdk_json_decode_uint32, true},
161 : {"queue_depth", offsetof(struct rpc_error_information, opts.error_qd), spdk_json_decode_uint64, true},
162 : {"corrupt_offset", offsetof(struct rpc_error_information, opts.corrupt_offset), spdk_json_decode_uint64, true},
163 : {"corrupt_value", offsetof(struct rpc_error_information, opts.corrupt_value), spdk_json_decode_uint8, true},
164 : };
165 :
166 : static void
167 0 : free_rpc_error_information(struct rpc_error_information *p)
168 : {
169 0 : free(p->name);
170 0 : }
171 :
172 : static void
173 0 : rpc_bdev_error_inject_error(struct spdk_jsonrpc_request *request,
174 : const struct spdk_json_val *params)
175 : {
176 0 : struct rpc_error_information req = {.opts.error_num = 1};
177 0 : int rc = 0;
178 :
179 0 : if (spdk_json_decode_object(params, rpc_error_information_decoders,
180 : SPDK_COUNTOF(rpc_error_information_decoders),
181 : &req)) {
182 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
183 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
184 : "spdk_json_decode_object failed");
185 0 : goto cleanup;
186 : }
187 :
188 0 : rc = vbdev_error_inject_error(req.name, &req.opts);
189 0 : if (rc) {
190 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
191 0 : goto cleanup;
192 : }
193 :
194 0 : spdk_jsonrpc_send_bool_response(request, true);
195 :
196 0 : cleanup:
197 0 : free_rpc_error_information(&req);
198 0 : }
199 0 : SPDK_RPC_REGISTER("bdev_error_inject_error", rpc_bdev_error_inject_error, SPDK_RPC_RUNTIME)
|