xref: /spdk/lib/accel/accel_rpc.c (revision fecffda6ecf8853b82edccde429b68252f0a62c5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2020 Intel Corporation.
3  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES.
4  *   All rights reserved.
5  */
6 
7 #include "accel_internal.h"
8 
9 #include "spdk/rpc.h"
10 #include "spdk/util.h"
11 #include "spdk/event.h"
12 #include "spdk/stdinc.h"
13 #include "spdk/env.h"
14 
15 static void
16 rpc_accel_get_opc_assignments(struct spdk_jsonrpc_request *request,
17 			      const struct spdk_json_val *params)
18 {
19 	struct spdk_json_write_ctx *w;
20 	enum accel_opcode opcode;
21 	const char *name, *module_name = NULL;
22 	int rc;
23 
24 	if (params != NULL) {
25 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
26 						 "accel_get_opc_assignments requires no parameters");
27 		return;
28 	}
29 
30 	w = spdk_jsonrpc_begin_result(request);
31 
32 	spdk_json_write_object_begin(w);
33 	for (opcode = 0; opcode < ACCEL_OPC_LAST; opcode++) {
34 		rc = _accel_get_opc_name(opcode, &name);
35 		if (rc == 0) {
36 			rc = spdk_accel_get_opc_module_name(opcode, &module_name);
37 			if (rc == 0) {
38 				spdk_json_write_named_string(w, name, module_name);
39 			} else {
40 				/* This isn't fatal but throw an informational message if we
41 				 * cant get an module name right now */
42 				SPDK_NOTICELOG("FYI error (%d) getting module name.\n", rc);
43 			}
44 		} else {
45 			/* this should never happen */
46 			SPDK_ERRLOG("Invalid opcode (%d)).\n", opcode);
47 			assert(0);
48 		}
49 	}
50 	spdk_json_write_object_end(w);
51 
52 	spdk_jsonrpc_end_result(request, w);
53 }
54 SPDK_RPC_REGISTER("accel_get_opc_assignments", rpc_accel_get_opc_assignments, SPDK_RPC_RUNTIME)
55 
56 static void
57 rpc_dump_module_info(struct module_info *info)
58 {
59 	struct spdk_json_write_ctx *w = info->w;
60 	const char *name;
61 	uint32_t i;
62 	int rc;
63 
64 	spdk_json_write_object_begin(w);
65 
66 	spdk_json_write_named_string(w, "module", info->name);
67 	spdk_json_write_named_array_begin(w, "supported ops");
68 
69 	for (i = 0; i < info->num_ops; i++) {
70 		rc = _accel_get_opc_name(i, &name);
71 		if (rc == 0) {
72 			spdk_json_write_string(w, name);
73 		} else {
74 			/* this should never happen */
75 			SPDK_ERRLOG("Invalid opcode (%d)).\n", i);
76 			assert(0);
77 		}
78 	}
79 
80 	spdk_json_write_array_end(w);
81 	spdk_json_write_object_end(w);
82 }
83 
84 static void
85 rpc_accel_get_module_info(struct spdk_jsonrpc_request *request,
86 			  const struct spdk_json_val *params)
87 {
88 	struct module_info info;
89 
90 	if (params != NULL) {
91 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
92 						 "accel_get_module_info requires no parameters");
93 		return;
94 	}
95 
96 	info.w = spdk_jsonrpc_begin_result(request);
97 	spdk_json_write_array_begin(info.w);
98 
99 	_accel_for_each_module(&info, rpc_dump_module_info);
100 
101 	spdk_json_write_array_end(info.w);
102 	spdk_jsonrpc_end_result(request, info.w);
103 }
104 SPDK_RPC_REGISTER("accel_get_module_info", rpc_accel_get_module_info, SPDK_RPC_RUNTIME)
105 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(accel_get_module_info, accel_get_engine_info)
106 
107 struct rpc_accel_assign_opc {
108 	char *opname;
109 	char *module;
110 };
111 
112 static const struct spdk_json_object_decoder rpc_accel_assign_opc_decoders[] = {
113 	{"opname", offsetof(struct rpc_accel_assign_opc, opname), spdk_json_decode_string},
114 	{"module", offsetof(struct rpc_accel_assign_opc, module), spdk_json_decode_string},
115 };
116 
117 static void
118 free_accel_assign_opc(struct rpc_accel_assign_opc *r)
119 {
120 	free(r->opname);
121 	free(r->module);
122 }
123 
124 static void
125 rpc_accel_assign_opc(struct spdk_jsonrpc_request *request,
126 		     const struct spdk_json_val *params)
127 {
128 	struct rpc_accel_assign_opc req = {};
129 	const char *opcode_str;
130 	enum accel_opcode opcode;
131 	bool found = false;
132 	int rc;
133 
134 	if (spdk_json_decode_object(params, rpc_accel_assign_opc_decoders,
135 				    SPDK_COUNTOF(rpc_accel_assign_opc_decoders),
136 				    &req)) {
137 		SPDK_DEBUGLOG(accel, "spdk_json_decode_object failed\n");
138 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
139 						 "spdk_json_decode_object failed");
140 		goto cleanup;
141 	}
142 
143 	for (opcode = 0; opcode < ACCEL_OPC_LAST; opcode++) {
144 		rc = _accel_get_opc_name(opcode, &opcode_str);
145 		assert(!rc);
146 		if (strcmp(opcode_str, req.opname) == 0) {
147 			found = true;
148 			break;
149 		}
150 	}
151 
152 	if (found == false) {
153 		SPDK_DEBUGLOG(accel, "Invalid operation name\n");
154 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
155 						 "spdk_json_decode_object failed");
156 		goto cleanup;
157 	}
158 
159 	rc = spdk_accel_assign_opc(opcode, req.module);
160 	if (rc) {
161 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
162 						 "error assigning opcode");
163 		goto cleanup;
164 	}
165 
166 	SPDK_NOTICELOG("Operation %s will be assigned to module %s\n", req.opname, req.module);
167 	spdk_jsonrpc_send_bool_response(request, true);
168 
169 cleanup:
170 	free_accel_assign_opc(&req);
171 
172 }
173 SPDK_RPC_REGISTER("accel_assign_opc", rpc_accel_assign_opc, SPDK_RPC_STARTUP)
174