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/string.h" 36 #include "spdk/rpc.h" 37 #include "spdk/util.h" 38 #include "spdk/string.h" 39 #include "spdk/log.h" 40 #include "vbdev_error.h" 41 42 #define ERROR_BDEV_IO_TYPE_INVALID (SPDK_BDEV_IO_TYPE_RESET + 1) 43 #define ERROR_BDEV_ERROR_TYPE_INVALID (VBDEV_IO_PENDING + 1) 44 45 static uint32_t 46 rpc_error_bdev_io_type_parse(char *name) 47 { 48 if (strcmp(name, "read") == 0) { 49 return SPDK_BDEV_IO_TYPE_READ; 50 } else if (strcmp(name, "write") == 0) { 51 return SPDK_BDEV_IO_TYPE_WRITE; 52 } else if (strcmp(name, "flush") == 0) { 53 return SPDK_BDEV_IO_TYPE_FLUSH; 54 } else if (strcmp(name, "unmap") == 0) { 55 return SPDK_BDEV_IO_TYPE_UNMAP; 56 } else if (strcmp(name, "all") == 0) { 57 return 0xffffffff; 58 } else if (strcmp(name, "clear") == 0) { 59 return 0; 60 } 61 return ERROR_BDEV_IO_TYPE_INVALID; 62 } 63 64 static uint32_t 65 rpc_error_bdev_error_type_parse(char *name) 66 { 67 if (strcmp(name, "failure") == 0) { 68 return VBDEV_IO_FAILURE; 69 } else if (strcmp(name, "pending") == 0) { 70 return VBDEV_IO_PENDING; 71 } 72 return ERROR_BDEV_ERROR_TYPE_INVALID; 73 } 74 75 struct rpc_bdev_error_create { 76 char *base_name; 77 }; 78 79 static void 80 free_rpc_bdev_error_create(struct rpc_bdev_error_create *req) 81 { 82 free(req->base_name); 83 } 84 85 static const struct spdk_json_object_decoder rpc_bdev_error_create_decoders[] = { 86 {"base_name", offsetof(struct rpc_bdev_error_create, base_name), spdk_json_decode_string}, 87 }; 88 89 static void 90 rpc_bdev_error_create(struct spdk_jsonrpc_request *request, 91 const struct spdk_json_val *params) 92 { 93 struct rpc_bdev_error_create req = {}; 94 int rc = 0; 95 96 if (spdk_json_decode_object(params, rpc_bdev_error_create_decoders, 97 SPDK_COUNTOF(rpc_bdev_error_create_decoders), 98 &req)) { 99 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 100 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 101 "spdk_json_decode_object failed"); 102 goto cleanup; 103 } 104 105 rc = vbdev_error_create(req.base_name); 106 if (rc) { 107 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 108 goto cleanup; 109 } 110 111 spdk_jsonrpc_send_bool_response(request, true); 112 113 cleanup: 114 free_rpc_bdev_error_create(&req); 115 } 116 SPDK_RPC_REGISTER("bdev_error_create", rpc_bdev_error_create, SPDK_RPC_RUNTIME) 117 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_error_create, construct_error_bdev) 118 119 struct rpc_delete_error { 120 char *name; 121 }; 122 123 static void 124 free_rpc_delete_error(struct rpc_delete_error *r) 125 { 126 free(r->name); 127 } 128 129 static const struct spdk_json_object_decoder rpc_delete_error_decoders[] = { 130 {"name", offsetof(struct rpc_delete_error, name), spdk_json_decode_string}, 131 }; 132 133 static void 134 rpc_bdev_error_delete_cb(void *cb_arg, int bdeverrno) 135 { 136 struct spdk_jsonrpc_request *request = cb_arg; 137 138 if (bdeverrno == 0) { 139 spdk_jsonrpc_send_bool_response(request, true); 140 } else { 141 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno)); 142 } 143 } 144 145 static void 146 rpc_bdev_error_delete(struct spdk_jsonrpc_request *request, 147 const struct spdk_json_val *params) 148 { 149 struct rpc_delete_error req = {NULL}; 150 151 if (spdk_json_decode_object(params, rpc_delete_error_decoders, 152 SPDK_COUNTOF(rpc_delete_error_decoders), 153 &req)) { 154 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 155 "spdk_json_decode_object failed"); 156 goto cleanup; 157 } 158 159 vbdev_error_delete(req.name, rpc_bdev_error_delete_cb, request); 160 161 cleanup: 162 free_rpc_delete_error(&req); 163 } 164 SPDK_RPC_REGISTER("bdev_error_delete", rpc_bdev_error_delete, SPDK_RPC_RUNTIME) 165 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_error_delete, delete_error_bdev) 166 167 struct rpc_error_information { 168 char *name; 169 char *io_type; 170 char *error_type; 171 uint32_t num; 172 }; 173 174 static const struct spdk_json_object_decoder rpc_error_information_decoders[] = { 175 {"name", offsetof(struct rpc_error_information, name), spdk_json_decode_string}, 176 {"io_type", offsetof(struct rpc_error_information, io_type), spdk_json_decode_string}, 177 {"error_type", offsetof(struct rpc_error_information, error_type), spdk_json_decode_string}, 178 {"num", offsetof(struct rpc_error_information, num), spdk_json_decode_uint32, true}, 179 }; 180 181 static void 182 free_rpc_error_information(struct rpc_error_information *p) 183 { 184 free(p->name); 185 free(p->io_type); 186 free(p->error_type); 187 } 188 189 static void 190 rpc_bdev_error_inject_error(struct spdk_jsonrpc_request *request, 191 const struct spdk_json_val *params) 192 { 193 struct rpc_error_information req = {.num = 1}; 194 uint32_t io_type; 195 uint32_t error_type; 196 int rc = 0; 197 198 if (spdk_json_decode_object(params, rpc_error_information_decoders, 199 SPDK_COUNTOF(rpc_error_information_decoders), 200 &req)) { 201 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 202 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 203 "spdk_json_decode_object failed"); 204 goto cleanup; 205 } 206 207 io_type = rpc_error_bdev_io_type_parse(req.io_type); 208 if (io_type == ERROR_BDEV_IO_TYPE_INVALID) { 209 spdk_jsonrpc_send_error_response(request, -EINVAL, 210 "Unexpected io_type value"); 211 goto cleanup; 212 } 213 214 error_type = rpc_error_bdev_error_type_parse(req.error_type); 215 if (error_type == ERROR_BDEV_ERROR_TYPE_INVALID) { 216 spdk_jsonrpc_send_error_response(request, -EINVAL, 217 "Unexpected error_type value"); 218 goto cleanup; 219 } 220 221 rc = vbdev_error_inject_error(req.name, io_type, error_type, req.num); 222 if (rc) { 223 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 224 goto cleanup; 225 } 226 227 spdk_jsonrpc_send_bool_response(request, true); 228 229 cleanup: 230 free_rpc_error_information(&req); 231 } 232 SPDK_RPC_REGISTER("bdev_error_inject_error", rpc_bdev_error_inject_error, SPDK_RPC_RUNTIME) 233 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_error_inject_error, bdev_inject_error) 234