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 rpc_error_bdev_decode_io_type(const struct spdk_json_val *val, void *out) 15 { 16 uint32_t *io_type = out; 17 18 if (spdk_json_strequal(val, "read") == true) { 19 *io_type = SPDK_BDEV_IO_TYPE_READ; 20 } else if (spdk_json_strequal(val, "write") == true) { 21 *io_type = SPDK_BDEV_IO_TYPE_WRITE; 22 } else if (spdk_json_strequal(val, "flush") == true) { 23 *io_type = SPDK_BDEV_IO_TYPE_FLUSH; 24 } else if (spdk_json_strequal(val, "unmap") == true) { 25 *io_type = SPDK_BDEV_IO_TYPE_UNMAP; 26 } else if (spdk_json_strequal(val, "all") == true) { 27 *io_type = 0xffffffff; 28 } else if (spdk_json_strequal(val, "clear") == true) { 29 *io_type = 0; 30 } else { 31 SPDK_NOTICELOG("Invalid parameter value: io_type\n"); 32 return -EINVAL; 33 } 34 35 return 0; 36 } 37 38 static int 39 rpc_error_bdev_decode_error_type(const struct spdk_json_val *val, void *out) 40 { 41 uint32_t *error_type = out; 42 43 if (spdk_json_strequal(val, "failure") == true) { 44 *error_type = VBDEV_IO_FAILURE; 45 } else if (spdk_json_strequal(val, "pending") == true) { 46 *error_type = VBDEV_IO_PENDING; 47 } else if (spdk_json_strequal(val, "corrupt_data") == true) { 48 *error_type = VBDEV_IO_CORRUPT_DATA; 49 } else if (spdk_json_strequal(val, "nomem") == true) { 50 *error_type = VBDEV_IO_NOMEM; 51 } else { 52 SPDK_NOTICELOG("Invalid parameter value: error_type\n"); 53 return -EINVAL; 54 } 55 56 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 free_rpc_bdev_error_create(struct rpc_bdev_error_create *req) 66 { 67 free(req->base_name); 68 } 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 rpc_bdev_error_create(struct spdk_jsonrpc_request *request, 77 const struct spdk_json_val *params) 78 { 79 struct rpc_bdev_error_create req = {}; 80 int rc = 0; 81 82 if (spdk_json_decode_object(params, rpc_bdev_error_create_decoders, 83 SPDK_COUNTOF(rpc_bdev_error_create_decoders), 84 &req)) { 85 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 86 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 87 "spdk_json_decode_object failed"); 88 goto cleanup; 89 } 90 91 rc = vbdev_error_create(req.base_name, &req.uuid); 92 if (rc) { 93 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 94 goto cleanup; 95 } 96 97 spdk_jsonrpc_send_bool_response(request, true); 98 99 cleanup: 100 free_rpc_bdev_error_create(&req); 101 } 102 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 free_rpc_delete_error(struct rpc_delete_error *r) 110 { 111 free(r->name); 112 } 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 rpc_bdev_error_delete_cb(void *cb_arg, int bdeverrno) 120 { 121 struct spdk_jsonrpc_request *request = cb_arg; 122 123 if (bdeverrno == 0) { 124 spdk_jsonrpc_send_bool_response(request, true); 125 } else { 126 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno)); 127 } 128 } 129 130 static void 131 rpc_bdev_error_delete(struct spdk_jsonrpc_request *request, 132 const struct spdk_json_val *params) 133 { 134 struct rpc_delete_error req = {NULL}; 135 136 if (spdk_json_decode_object(params, rpc_delete_error_decoders, 137 SPDK_COUNTOF(rpc_delete_error_decoders), 138 &req)) { 139 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 140 "spdk_json_decode_object failed"); 141 goto cleanup; 142 } 143 144 vbdev_error_delete(req.name, rpc_bdev_error_delete_cb, request); 145 146 cleanup: 147 free_rpc_delete_error(&req); 148 } 149 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 free_rpc_error_information(struct rpc_error_information *p) 168 { 169 free(p->name); 170 } 171 172 static void 173 rpc_bdev_error_inject_error(struct spdk_jsonrpc_request *request, 174 const struct spdk_json_val *params) 175 { 176 struct rpc_error_information req = {.opts.error_num = 1}; 177 int rc = 0; 178 179 if (spdk_json_decode_object(params, rpc_error_information_decoders, 180 SPDK_COUNTOF(rpc_error_information_decoders), 181 &req)) { 182 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 183 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 184 "spdk_json_decode_object failed"); 185 goto cleanup; 186 } 187 188 rc = vbdev_error_inject_error(req.name, &req.opts); 189 if (rc) { 190 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 191 goto cleanup; 192 } 193 194 spdk_jsonrpc_send_bool_response(request, true); 195 196 cleanup: 197 free_rpc_error_information(&req); 198 } 199 SPDK_RPC_REGISTER("bdev_error_inject_error", rpc_bdev_error_inject_error, SPDK_RPC_RUNTIME) 200