xref: /spdk/lib/sock/sock_rpc.c (revision 441431d22872ae4e05a1bf8b78e9aeff1eba1eb3)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2020, 2021 Mellanox Technologies LTD. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "spdk/sock.h"
34 
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 #include "spdk/string.h"
38 
39 #include "spdk/log.h"
40 
41 
42 static const struct spdk_json_object_decoder rpc_sock_impl_get_opts_decoders[] = {
43 	{ "impl_name", 0, spdk_json_decode_string, false },
44 };
45 
46 static void
47 rpc_sock_impl_get_options(struct spdk_jsonrpc_request *request,
48 			  const struct spdk_json_val *params)
49 {
50 	char *impl_name = NULL;
51 	struct spdk_sock_impl_opts sock_opts = {};
52 	struct spdk_json_write_ctx *w;
53 	size_t len;
54 	int rc;
55 
56 	if (spdk_json_decode_object(params, rpc_sock_impl_get_opts_decoders,
57 				    SPDK_COUNTOF(rpc_sock_impl_get_opts_decoders), &impl_name)) {
58 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
59 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
60 						 "Invalid parameters");
61 		return;
62 	}
63 
64 	len = sizeof(sock_opts);
65 	rc = spdk_sock_impl_get_opts(impl_name, &sock_opts, &len);
66 	if (rc) {
67 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
68 						 "Invalid parameters");
69 		return;
70 	}
71 
72 	w = spdk_jsonrpc_begin_result(request);
73 	spdk_json_write_object_begin(w);
74 	spdk_json_write_named_uint32(w, "recv_buf_size", sock_opts.recv_buf_size);
75 	spdk_json_write_named_uint32(w, "send_buf_size", sock_opts.send_buf_size);
76 	spdk_json_write_named_bool(w, "enable_recv_pipe", sock_opts.enable_recv_pipe);
77 	spdk_json_write_named_bool(w, "enable_zerocopy_send", sock_opts.enable_zerocopy_send);
78 	spdk_json_write_named_bool(w, "enable_quickack", sock_opts.enable_quickack);
79 	spdk_json_write_named_uint32(w, "enable_placement_id", sock_opts.enable_placement_id);
80 	spdk_json_write_named_bool(w, "enable_zerocopy_send_server", sock_opts.enable_zerocopy_send_server);
81 	spdk_json_write_named_bool(w, "enable_zerocopy_send_client", sock_opts.enable_zerocopy_send_client);
82 	spdk_json_write_object_end(w);
83 	spdk_jsonrpc_end_result(request, w);
84 	free(impl_name);
85 }
86 SPDK_RPC_REGISTER("sock_impl_get_options", rpc_sock_impl_get_options,
87 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
88 
89 struct spdk_rpc_sock_impl_set_opts {
90 	char *impl_name;
91 	struct spdk_sock_impl_opts sock_opts;
92 };
93 
94 static const struct spdk_json_object_decoder rpc_sock_impl_set_opts_decoders[] = {
95 	{
96 		"impl_name", offsetof(struct spdk_rpc_sock_impl_set_opts, impl_name),
97 		spdk_json_decode_string, false
98 	},
99 	{
100 		"recv_buf_size", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.recv_buf_size),
101 		spdk_json_decode_uint32, true
102 	},
103 	{
104 		"send_buf_size", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.send_buf_size),
105 		spdk_json_decode_uint32, true
106 	},
107 	{
108 		"enable_recv_pipe", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_recv_pipe),
109 		spdk_json_decode_bool, true
110 	},
111 	{
112 		"enable_zerocopy_send", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send),
113 		spdk_json_decode_bool, true
114 	},
115 	{
116 		"enable_quickack", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_quickack),
117 		spdk_json_decode_bool, true
118 	},
119 	{
120 		"enable_placement_id", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_placement_id),
121 		spdk_json_decode_uint32, true
122 	},
123 	{
124 		"enable_zerocopy_send_server", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_server),
125 		spdk_json_decode_bool, true
126 	},
127 	{
128 		"enable_zerocopy_send_client", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_client),
129 		spdk_json_decode_bool, true
130 	}
131 };
132 
133 static void
134 rpc_sock_impl_set_options(struct spdk_jsonrpc_request *request,
135 			  const struct spdk_json_val *params)
136 {
137 	struct spdk_rpc_sock_impl_set_opts opts = {};
138 	size_t len;
139 	int rc;
140 
141 	/* Get type */
142 	if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
143 				    SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
144 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
145 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
146 						 "Invalid parameters");
147 		return;
148 	}
149 
150 	/* Retrieve default opts for requested socket implementation */
151 	len = sizeof(opts.sock_opts);
152 	rc = spdk_sock_impl_get_opts(opts.impl_name, &opts.sock_opts, &len);
153 	if (rc) {
154 		free(opts.impl_name);
155 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
156 						 "Invalid parameters");
157 		return;
158 	}
159 
160 	/* Decode opts */
161 	if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
162 				    SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
163 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
164 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
165 						 "Invalid parameters");
166 		return;
167 	}
168 
169 	rc = spdk_sock_impl_set_opts(opts.impl_name, &opts.sock_opts, sizeof(opts.sock_opts));
170 	if (rc != 0) {
171 		free(opts.impl_name);
172 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
173 						 "Invalid parameters");
174 		return;
175 	}
176 
177 	spdk_jsonrpc_send_bool_response(request, true);
178 	free(opts.impl_name);
179 }
180 SPDK_RPC_REGISTER("sock_impl_set_options", rpc_sock_impl_set_options, SPDK_RPC_STARTUP)
181 
182 static void
183 rpc_sock_set_default_impl(struct spdk_jsonrpc_request *request,
184 			  const struct spdk_json_val *params)
185 {
186 	char *impl_name = NULL;
187 	int rc;
188 
189 	/* Reuse get_opts decoder */
190 	if (spdk_json_decode_object(params, rpc_sock_impl_get_opts_decoders,
191 				    SPDK_COUNTOF(rpc_sock_impl_get_opts_decoders), &impl_name)) {
192 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
193 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
194 						 "Invalid parameters");
195 		return;
196 	}
197 
198 	rc = spdk_sock_set_default_impl(impl_name);
199 	if (rc) {
200 		free(impl_name);
201 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
202 						 "Invalid parameters");
203 		return;
204 	}
205 
206 	spdk_jsonrpc_send_bool_response(request, true);
207 	free(impl_name);
208 }
209 SPDK_RPC_REGISTER("sock_set_default_impl", rpc_sock_set_default_impl, SPDK_RPC_STARTUP)
210