xref: /spdk/module/accel/error/accel_error_rpc.c (revision f8abbede89d30584d2a4f8427b13896f8591b873)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2023 Intel Corporation. All rights reserved.
3  */
4 
5 #include "accel_error.h"
6 #include "spdk/accel.h"
7 #include "spdk/rpc.h"
8 #include "spdk/string.h"
9 #include "spdk/util.h"
10 
11 static int
12 rpc_accel_error_decode_opcode(const struct spdk_json_val *val, void *out)
13 {
14 	enum spdk_accel_opcode *opcode = out;
15 	char *opstr = NULL;
16 	int i, rc;
17 
18 	rc = spdk_json_decode_string(val, &opstr);
19 	if (rc != 0) {
20 		return rc;
21 	}
22 
23 	rc = -EINVAL;
24 	for (i = 0; i < SPDK_ACCEL_OPC_LAST; ++i) {
25 		if (strcmp(spdk_accel_get_opcode_name((enum spdk_accel_opcode)i), opstr) == 0) {
26 			*opcode = (enum spdk_accel_opcode)i;
27 			rc = 0;
28 			break;
29 		}
30 	}
31 
32 	free(opstr);
33 
34 	return rc;
35 }
36 
37 static int
38 rpc_accel_error_decode_type(const struct spdk_json_val *val, void *out)
39 {
40 	enum accel_error_inject_type *type = out;
41 	char *typestr = NULL;
42 	int i, rc;
43 
44 	rc = spdk_json_decode_string(val, &typestr);
45 	if (rc != 0) {
46 		return rc;
47 	}
48 
49 	rc = -EINVAL;
50 	for (i = 0; i < ACCEL_ERROR_INJECT_MAX; ++i) {
51 		if (strcmp(accel_error_get_type_name(i), typestr) == 0) {
52 			*type = (enum accel_error_inject_type)i;
53 			rc = 0;
54 			break;
55 		}
56 	}
57 
58 	free(typestr);
59 
60 	return rc;
61 }
62 
63 static const struct spdk_json_object_decoder rpc_accel_error_inject_error_decoders[] = {
64 	{"opcode", offsetof(struct accel_error_inject_opts, opcode), rpc_accel_error_decode_opcode},
65 	{"type", offsetof(struct accel_error_inject_opts, type), rpc_accel_error_decode_type},
66 	{"count", offsetof(struct accel_error_inject_opts, count), spdk_json_decode_uint64, true},
67 	{"interval", offsetof(struct accel_error_inject_opts, interval), spdk_json_decode_uint64, true},
68 	{"errcode", offsetof(struct accel_error_inject_opts, errcode), spdk_json_decode_int32, true},
69 };
70 
71 static void
72 rpc_accel_error_inject_error(struct spdk_jsonrpc_request *request,
73 			     const struct spdk_json_val *params)
74 {
75 	struct accel_error_inject_opts opts = {.count = UINT64_MAX};
76 	int rc;
77 
78 	rc = spdk_json_decode_object(params, rpc_accel_error_inject_error_decoders,
79 				     SPDK_COUNTOF(rpc_accel_error_inject_error_decoders), &opts);
80 	if (rc != 0) {
81 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
82 		return;
83 	}
84 
85 	rc = accel_error_inject_error(&opts);
86 	if (rc != 0) {
87 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
88 		return;
89 	}
90 
91 	spdk_jsonrpc_send_bool_response(request, true);
92 }
93 
94 SPDK_RPC_REGISTER("accel_error_inject_error", rpc_accel_error_inject_error, SPDK_RPC_RUNTIME)
95