Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2020 Intel Corporation.
3 : : * Copyright (c) 2020, 2021 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/sock.h"
8 : :
9 : : #include "spdk/rpc.h"
10 : : #include "spdk/util.h"
11 : : #include "spdk/string.h"
12 : :
13 : : #include "spdk/log.h"
14 : :
15 : :
16 : : static const struct spdk_json_object_decoder rpc_sock_impl_get_opts_decoders[] = {
17 : : { "impl_name", 0, spdk_json_decode_string, false },
18 : : };
19 : :
20 : : static void
21 : 18 : rpc_sock_impl_get_options(struct spdk_jsonrpc_request *request,
22 : : const struct spdk_json_val *params)
23 : : {
24 : 18 : char *impl_name = NULL;
25 : 18 : struct spdk_sock_impl_opts sock_opts = {};
26 : : struct spdk_json_write_ctx *w;
27 : 0 : size_t len;
28 : : int rc;
29 : :
30 [ - + ]: 18 : if (spdk_json_decode_object(params, rpc_sock_impl_get_opts_decoders,
31 : : SPDK_COUNTOF(rpc_sock_impl_get_opts_decoders), &impl_name)) {
32 : 0 : SPDK_ERRLOG("spdk_json_decode_object() failed\n");
33 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
34 : : "Invalid parameters");
35 : 0 : return;
36 : : }
37 : :
38 : 18 : len = sizeof(sock_opts);
39 : 18 : rc = spdk_sock_impl_get_opts(impl_name, &sock_opts, &len);
40 [ - + ]: 18 : if (rc) {
41 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
42 : : "Invalid parameters");
43 : 0 : return;
44 : : }
45 : :
46 : 18 : w = spdk_jsonrpc_begin_result(request);
47 : 18 : spdk_json_write_object_begin(w);
48 : 18 : spdk_json_write_named_uint32(w, "recv_buf_size", sock_opts.recv_buf_size);
49 : 18 : spdk_json_write_named_uint32(w, "send_buf_size", sock_opts.send_buf_size);
50 [ - + ]: 18 : spdk_json_write_named_bool(w, "enable_recv_pipe", sock_opts.enable_recv_pipe);
51 [ - + ]: 18 : spdk_json_write_named_bool(w, "enable_quickack", sock_opts.enable_quickack);
52 : 18 : spdk_json_write_named_uint32(w, "enable_placement_id", sock_opts.enable_placement_id);
53 [ - + ]: 18 : spdk_json_write_named_bool(w, "enable_zerocopy_send_server", sock_opts.enable_zerocopy_send_server);
54 [ - + ]: 18 : spdk_json_write_named_bool(w, "enable_zerocopy_send_client", sock_opts.enable_zerocopy_send_client);
55 : 18 : spdk_json_write_named_uint32(w, "zerocopy_threshold", sock_opts.zerocopy_threshold);
56 : 18 : spdk_json_write_named_uint32(w, "tls_version", sock_opts.tls_version);
57 [ - + ]: 18 : spdk_json_write_named_bool(w, "enable_ktls", sock_opts.enable_ktls);
58 : 18 : spdk_json_write_object_end(w);
59 : 18 : spdk_jsonrpc_end_result(request, w);
60 : 18 : free(impl_name);
61 : : }
62 : 2696 : SPDK_RPC_REGISTER("sock_impl_get_options", rpc_sock_impl_get_options,
63 : : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
64 : :
65 : : struct spdk_rpc_sock_impl_set_opts {
66 : : char *impl_name;
67 : : struct spdk_sock_impl_opts sock_opts;
68 : : };
69 : :
70 : : static const struct spdk_json_object_decoder rpc_sock_impl_set_opts_decoders[] = {
71 : : {
72 : : "impl_name", offsetof(struct spdk_rpc_sock_impl_set_opts, impl_name),
73 : : spdk_json_decode_string, false
74 : : },
75 : : {
76 : : "recv_buf_size", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.recv_buf_size),
77 : : spdk_json_decode_uint32, true
78 : : },
79 : : {
80 : : "send_buf_size", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.send_buf_size),
81 : : spdk_json_decode_uint32, true
82 : : },
83 : : {
84 : : "enable_recv_pipe", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_recv_pipe),
85 : : spdk_json_decode_bool, true
86 : : },
87 : : {
88 : : "enable_quickack", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_quickack),
89 : : spdk_json_decode_bool, true
90 : : },
91 : : {
92 : : "enable_placement_id", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_placement_id),
93 : : spdk_json_decode_uint32, true
94 : : },
95 : : {
96 : : "enable_zerocopy_send_server", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_server),
97 : : spdk_json_decode_bool, true
98 : : },
99 : : {
100 : : "enable_zerocopy_send_client", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_zerocopy_send_client),
101 : : spdk_json_decode_bool, true
102 : : },
103 : : {
104 : : "zerocopy_threshold", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.zerocopy_threshold),
105 : : spdk_json_decode_uint32, true
106 : : },
107 : : {
108 : : "tls_version", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.tls_version),
109 : : spdk_json_decode_uint32, true
110 : : },
111 : : {
112 : : "enable_ktls", offsetof(struct spdk_rpc_sock_impl_set_opts, sock_opts.enable_ktls),
113 : : spdk_json_decode_bool, true
114 : : }
115 : : };
116 : :
117 : : static void
118 : 132 : rpc_sock_impl_set_options(struct spdk_jsonrpc_request *request,
119 : : const struct spdk_json_val *params)
120 : : {
121 : 132 : struct spdk_rpc_sock_impl_set_opts opts = {};
122 : 40 : size_t len;
123 : : int rc;
124 : :
125 : : /* Get type */
126 [ - + ]: 132 : if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
127 : : SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
128 : 0 : SPDK_ERRLOG("spdk_json_decode_object() failed\n");
129 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
130 : : "Invalid parameters");
131 : 0 : return;
132 : : }
133 : :
134 : : /* Retrieve default opts for requested socket implementation */
135 : 132 : len = sizeof(opts.sock_opts);
136 : 132 : rc = spdk_sock_impl_get_opts(opts.impl_name, &opts.sock_opts, &len);
137 [ - + ]: 132 : if (rc) {
138 : 0 : free(opts.impl_name);
139 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
140 : : "Invalid parameters");
141 : 0 : return;
142 : : }
143 : :
144 : : /* Decode opts */
145 [ - + ]: 132 : if (spdk_json_decode_object(params, rpc_sock_impl_set_opts_decoders,
146 : : SPDK_COUNTOF(rpc_sock_impl_set_opts_decoders), &opts)) {
147 : 0 : SPDK_ERRLOG("spdk_json_decode_object() failed\n");
148 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
149 : : "Invalid parameters");
150 : 0 : return;
151 : : }
152 : :
153 : 132 : rc = spdk_sock_impl_set_opts(opts.impl_name, &opts.sock_opts, sizeof(opts.sock_opts));
154 [ - + ]: 132 : if (rc != 0) {
155 : 0 : free(opts.impl_name);
156 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
157 : : "Invalid parameters");
158 : 0 : return;
159 : : }
160 : :
161 : 132 : spdk_jsonrpc_send_bool_response(request, true);
162 : 132 : free(opts.impl_name);
163 : : }
164 : 2696 : SPDK_RPC_REGISTER("sock_impl_set_options", rpc_sock_impl_set_options, SPDK_RPC_STARTUP)
165 : :
166 : : static void
167 : 56 : rpc_sock_set_default_impl(struct spdk_jsonrpc_request *request,
168 : : const struct spdk_json_val *params)
169 : : {
170 : 56 : char *impl_name = NULL;
171 : : int rc;
172 : :
173 : : /* Reuse get_opts decoder */
174 [ - + ]: 56 : if (spdk_json_decode_object(params, rpc_sock_impl_get_opts_decoders,
175 : : SPDK_COUNTOF(rpc_sock_impl_get_opts_decoders), &impl_name)) {
176 : 0 : SPDK_ERRLOG("spdk_json_decode_object() failed\n");
177 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
178 : : "Invalid parameters");
179 : 0 : return;
180 : : }
181 : :
182 : 56 : rc = spdk_sock_set_default_impl(impl_name);
183 [ - + ]: 56 : if (rc) {
184 : 0 : free(impl_name);
185 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
186 : : "Invalid parameters");
187 : 0 : return;
188 : : }
189 : :
190 : 56 : spdk_jsonrpc_send_bool_response(request, true);
191 : 56 : free(impl_name);
192 : : }
193 : 2696 : SPDK_RPC_REGISTER("sock_set_default_impl", rpc_sock_set_default_impl, SPDK_RPC_STARTUP)
194 : :
195 : : static void
196 : 2 : rpc_sock_get_default_impl(struct spdk_jsonrpc_request *request,
197 : : const struct spdk_json_val *params)
198 : : {
199 : 2 : const char *impl_name = spdk_sock_get_default_impl();
200 : : struct spdk_json_write_ctx *w;
201 : :
202 [ - + ]: 2 : if (params) {
203 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
204 : : "sock_get_default_impl requires no parameters");
205 : 0 : return;
206 : : }
207 : :
208 [ - + ]: 2 : if (!impl_name) {
209 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
210 : : "No registered socket implementations found");
211 : 0 : return;
212 : : }
213 : :
214 : 2 : w = spdk_jsonrpc_begin_result(request);
215 : 2 : spdk_json_write_object_begin(w);
216 : 2 : spdk_json_write_named_string(w, "impl_name", impl_name);
217 : 2 : spdk_json_write_object_end(w);
218 : 2 : spdk_jsonrpc_end_result(request, w);
219 : : }
220 : 2696 : SPDK_RPC_REGISTER("sock_get_default_impl", rpc_sock_get_default_impl,
221 : : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
|