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