Line data Source code
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 0 : rpc_accel_error_decode_opcode(const struct spdk_json_val *val, void *out)
13 : {
14 0 : enum spdk_accel_opcode *opcode = out;
15 0 : char *opstr = NULL;
16 : int i, rc;
17 :
18 0 : rc = spdk_json_decode_string(val, &opstr);
19 0 : if (rc != 0) {
20 0 : return rc;
21 : }
22 :
23 0 : rc = -EINVAL;
24 0 : for (i = 0; i < SPDK_ACCEL_OPC_LAST; ++i) {
25 0 : if (strcmp(spdk_accel_get_opcode_name((enum spdk_accel_opcode)i), opstr) == 0) {
26 0 : *opcode = (enum spdk_accel_opcode)i;
27 0 : rc = 0;
28 0 : break;
29 : }
30 : }
31 :
32 0 : free(opstr);
33 :
34 0 : return rc;
35 : }
36 :
37 : static int
38 0 : rpc_accel_error_decode_type(const struct spdk_json_val *val, void *out)
39 : {
40 0 : enum accel_error_inject_type *type = out;
41 0 : char *typestr = NULL;
42 : int i, rc;
43 :
44 0 : rc = spdk_json_decode_string(val, &typestr);
45 0 : if (rc != 0) {
46 0 : return rc;
47 : }
48 :
49 0 : rc = -EINVAL;
50 0 : for (i = 0; i < ACCEL_ERROR_INJECT_MAX; ++i) {
51 0 : if (strcmp(accel_error_get_type_name(i), typestr) == 0) {
52 0 : *type = (enum accel_error_inject_type)i;
53 0 : rc = 0;
54 0 : break;
55 : }
56 : }
57 :
58 0 : free(typestr);
59 :
60 0 : 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 0 : rpc_accel_error_inject_error(struct spdk_jsonrpc_request *request,
73 : const struct spdk_json_val *params)
74 : {
75 0 : struct accel_error_inject_opts opts = {.count = UINT64_MAX};
76 : int rc;
77 :
78 0 : rc = spdk_json_decode_object(params, rpc_accel_error_inject_error_decoders,
79 : SPDK_COUNTOF(rpc_accel_error_inject_error_decoders), &opts);
80 0 : if (rc != 0) {
81 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
82 0 : return;
83 : }
84 :
85 0 : rc = accel_error_inject_error(&opts);
86 0 : if (rc != 0) {
87 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
88 0 : return;
89 : }
90 :
91 0 : spdk_jsonrpc_send_bool_response(request, true);
92 : }
93 :
94 0 : SPDK_RPC_REGISTER("accel_error_inject_error", rpc_accel_error_inject_error, SPDK_RPC_RUNTIME)
|