xref: /spdk/lib/sock/sock_rpc.c (revision cc0d05b427038cc877539c12fb0f8bc9053300c0)
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_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_quickack", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_quickack),
113 		spdk_json_decode_bool, true
114 	},
115 	{
116 		"enable_placement_id", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_placement_id),
117 		spdk_json_decode_uint32, true
118 	},
119 	{
120 		"enable_zerocopy_send_server", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_server),
121 		spdk_json_decode_bool, true
122 	},
123 	{
124 		"enable_zerocopy_send_client", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_client),
125 		spdk_json_decode_bool, true
126 	}
127 };
128 
129 static void
130 rpc_sock_impl_set_options(struct spdk_jsonrpc_request *request,
131 			  const struct spdk_json_val *params)
132 {
133 	struct spdk_rpc_sock_impl_set_opts opts = {};
134 	size_t len;
135 	int rc;
136 
137 	/* Get type */
138 	if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
139 				    SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
140 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
141 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
142 						 "Invalid parameters");
143 		return;
144 	}
145 
146 	/* Retrieve default opts for requested socket implementation */
147 	len = sizeof(opts.sock_opts);
148 	rc = spdk_sock_impl_get_opts(opts.impl_name, &opts.sock_opts, &len);
149 	if (rc) {
150 		free(opts.impl_name);
151 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
152 						 "Invalid parameters");
153 		return;
154 	}
155 
156 	/* Decode opts */
157 	if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
158 				    SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
159 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
160 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
161 						 "Invalid parameters");
162 		return;
163 	}
164 
165 	rc = spdk_sock_impl_set_opts(opts.impl_name, &opts.sock_opts, sizeof(opts.sock_opts));
166 	if (rc != 0) {
167 		free(opts.impl_name);
168 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
169 						 "Invalid parameters");
170 		return;
171 	}
172 
173 	spdk_jsonrpc_send_bool_response(request, true);
174 	free(opts.impl_name);
175 }
176 SPDK_RPC_REGISTER("sock_impl_set_options", rpc_sock_impl_set_options, SPDK_RPC_STARTUP)
177 
178 static void
179 rpc_sock_set_default_impl(struct spdk_jsonrpc_request *request,
180 			  const struct spdk_json_val *params)
181 {
182 	char *impl_name = NULL;
183 	int rc;
184 
185 	/* Reuse get_opts decoder */
186 	if (spdk_json_decode_object(params, rpc_sock_impl_get_opts_decoders,
187 				    SPDK_COUNTOF(rpc_sock_impl_get_opts_decoders), &impl_name)) {
188 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
189 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
190 						 "Invalid parameters");
191 		return;
192 	}
193 
194 	rc = spdk_sock_set_default_impl(impl_name);
195 	if (rc) {
196 		free(impl_name);
197 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
198 						 "Invalid parameters");
199 		return;
200 	}
201 
202 	spdk_jsonrpc_send_bool_response(request, true);
203 	free(impl_name);
204 }
205 SPDK_RPC_REGISTER("sock_set_default_impl", rpc_sock_set_default_impl, SPDK_RPC_STARTUP)
206