xref: /spdk/lib/rpc/rpc.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. 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 <sys/file.h>
35 
36 #include "spdk/stdinc.h"
37 
38 #include "spdk/queue.h"
39 #include "spdk/rpc.h"
40 #include "spdk/env.h"
41 #include "spdk/log.h"
42 #include "spdk/string.h"
43 #include "spdk/util.h"
44 #include "spdk/version.h"
45 
46 static struct sockaddr_un g_rpc_listen_addr_unix = {};
47 static char g_rpc_lock_path[sizeof(g_rpc_listen_addr_unix.sun_path) + sizeof(".lock")];
48 static int g_rpc_lock_fd = -1;
49 
50 static struct spdk_jsonrpc_server *g_jsonrpc_server = NULL;
51 static uint32_t g_rpc_state;
52 static bool g_rpcs_correct = true;
53 
54 struct spdk_rpc_method {
55 	const char *name;
56 	spdk_rpc_method_handler func;
57 	SLIST_ENTRY(spdk_rpc_method) slist;
58 	uint32_t state_mask;
59 	bool is_deprecated;
60 	struct spdk_rpc_method *is_alias_of;
61 	bool deprecation_warning_printed;
62 };
63 
64 static SLIST_HEAD(, spdk_rpc_method) g_rpc_methods = SLIST_HEAD_INITIALIZER(g_rpc_methods);
65 
66 void
67 spdk_rpc_set_state(uint32_t state)
68 {
69 	g_rpc_state = state;
70 }
71 
72 uint32_t
73 spdk_rpc_get_state(void)
74 {
75 	return g_rpc_state;
76 }
77 
78 static struct spdk_rpc_method *
79 _get_rpc_method(const struct spdk_json_val *method)
80 {
81 	struct spdk_rpc_method *m;
82 
83 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
84 		if (spdk_json_strequal(method, m->name)) {
85 			return m;
86 		}
87 	}
88 
89 	return NULL;
90 }
91 
92 static struct spdk_rpc_method *
93 _get_rpc_method_raw(const char *method)
94 {
95 	struct spdk_json_val method_val;
96 
97 	method_val.type = SPDK_JSON_VAL_STRING;
98 	method_val.len = strlen(method);
99 	method_val.start = (char *)method;
100 
101 	return _get_rpc_method(&method_val);
102 }
103 
104 static void
105 jsonrpc_handler(struct spdk_jsonrpc_request *request,
106 		const struct spdk_json_val *method,
107 		const struct spdk_json_val *params)
108 {
109 	struct spdk_rpc_method *m;
110 
111 	assert(method != NULL);
112 
113 	m = _get_rpc_method(method);
114 	if (m == NULL) {
115 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_METHOD_NOT_FOUND, "Method not found");
116 		return;
117 	}
118 
119 	if (m->is_alias_of != NULL) {
120 		if (m->is_deprecated && !m->deprecation_warning_printed) {
121 			SPDK_WARNLOG("RPC method %s is deprecated.  Use %s instead.\n", m->name, m->is_alias_of->name);
122 			m->deprecation_warning_printed = true;
123 		}
124 		m = m->is_alias_of;
125 	}
126 
127 	if ((m->state_mask & g_rpc_state) == g_rpc_state) {
128 		m->func(request, params);
129 	} else {
130 		if (g_rpc_state == SPDK_RPC_STARTUP) {
131 			spdk_jsonrpc_send_error_response_fmt(request,
132 							     SPDK_JSONRPC_ERROR_INVALID_STATE,
133 							     "Method may only be called after "
134 							     "framework is initialized "
135 							     "using framework_start_init RPC.");
136 		} else {
137 			spdk_jsonrpc_send_error_response_fmt(request,
138 							     SPDK_JSONRPC_ERROR_INVALID_STATE,
139 							     "Method may only be called before "
140 							     "framework is initialized. "
141 							     "Use --wait-for-rpc command line "
142 							     "parameter and then issue this RPC "
143 							     "before the framework_start_init RPC.");
144 		}
145 	}
146 }
147 
148 int
149 spdk_rpc_listen(const char *listen_addr)
150 {
151 	int rc;
152 
153 	memset(&g_rpc_listen_addr_unix, 0, sizeof(g_rpc_listen_addr_unix));
154 
155 	g_rpc_listen_addr_unix.sun_family = AF_UNIX;
156 	rc = snprintf(g_rpc_listen_addr_unix.sun_path,
157 		      sizeof(g_rpc_listen_addr_unix.sun_path),
158 		      "%s", listen_addr);
159 	if (rc < 0 || (size_t)rc >= sizeof(g_rpc_listen_addr_unix.sun_path)) {
160 		SPDK_ERRLOG("RPC Listen address Unix socket path too long\n");
161 		g_rpc_listen_addr_unix.sun_path[0] = '\0';
162 		return -1;
163 	}
164 
165 	rc = snprintf(g_rpc_lock_path, sizeof(g_rpc_lock_path), "%s.lock",
166 		      g_rpc_listen_addr_unix.sun_path);
167 	if (rc < 0 || (size_t)rc >= sizeof(g_rpc_lock_path)) {
168 		SPDK_ERRLOG("RPC lock path too long\n");
169 		g_rpc_listen_addr_unix.sun_path[0] = '\0';
170 		g_rpc_lock_path[0] = '\0';
171 		return -1;
172 	}
173 
174 	g_rpc_lock_fd = open(g_rpc_lock_path, O_RDONLY | O_CREAT, 0600);
175 	if (g_rpc_lock_fd == -1) {
176 		SPDK_ERRLOG("Cannot open lock file %s: %s\n",
177 			    g_rpc_lock_path, spdk_strerror(errno));
178 		g_rpc_listen_addr_unix.sun_path[0] = '\0';
179 		g_rpc_lock_path[0] = '\0';
180 		return -1;
181 	}
182 
183 	rc = flock(g_rpc_lock_fd, LOCK_EX | LOCK_NB);
184 	if (rc != 0) {
185 		SPDK_ERRLOG("RPC Unix domain socket path %s in use. Specify another.\n",
186 			    g_rpc_listen_addr_unix.sun_path);
187 		g_rpc_listen_addr_unix.sun_path[0] = '\0';
188 		g_rpc_lock_path[0] = '\0';
189 		return -1;
190 	}
191 
192 	/*
193 	 * Since we acquired the lock, it is safe to delete the Unix socket file
194 	 * if it still exists from a previous process.
195 	 */
196 	unlink(g_rpc_listen_addr_unix.sun_path);
197 
198 	g_jsonrpc_server = spdk_jsonrpc_server_listen(AF_UNIX, 0,
199 			   (struct sockaddr *)&g_rpc_listen_addr_unix,
200 			   sizeof(g_rpc_listen_addr_unix),
201 			   jsonrpc_handler);
202 	if (g_jsonrpc_server == NULL) {
203 		SPDK_ERRLOG("spdk_jsonrpc_server_listen() failed\n");
204 		close(g_rpc_lock_fd);
205 		g_rpc_lock_fd = -1;
206 		unlink(g_rpc_lock_path);
207 		g_rpc_lock_path[0] = '\0';
208 		return -1;
209 	}
210 
211 	return 0;
212 }
213 
214 void
215 spdk_rpc_accept(void)
216 {
217 	spdk_jsonrpc_server_poll(g_jsonrpc_server);
218 }
219 
220 void
221 spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func, uint32_t state_mask)
222 {
223 	struct spdk_rpc_method *m;
224 
225 	m = _get_rpc_method_raw(method);
226 	if (m != NULL) {
227 		SPDK_ERRLOG("duplicate RPC %s registered...\n", method);
228 		g_rpcs_correct = false;
229 		return;
230 	}
231 
232 	m = calloc(1, sizeof(struct spdk_rpc_method));
233 	assert(m != NULL);
234 
235 	m->name = strdup(method);
236 	assert(m->name != NULL);
237 
238 	m->func = func;
239 	m->state_mask = state_mask;
240 
241 	/* TODO: use a hash table or sorted list */
242 	SLIST_INSERT_HEAD(&g_rpc_methods, m, slist);
243 }
244 
245 void
246 spdk_rpc_register_alias_deprecated(const char *method, const char *alias)
247 {
248 	struct spdk_rpc_method *m, *base;
249 
250 	base = _get_rpc_method_raw(method);
251 	if (base == NULL) {
252 		SPDK_ERRLOG("cannot create alias %s - method %s does not exist\n",
253 			    alias, method);
254 		g_rpcs_correct = false;
255 		return;
256 	}
257 
258 	if (base->is_alias_of != NULL) {
259 		SPDK_ERRLOG("cannot create alias %s of alias %s\n", alias, method);
260 		g_rpcs_correct = false;
261 		return;
262 	}
263 
264 	m = calloc(1, sizeof(struct spdk_rpc_method));
265 	assert(m != NULL);
266 
267 	m->name = strdup(alias);
268 	assert(m->name != NULL);
269 
270 	m->is_alias_of = base;
271 	m->is_deprecated = true;
272 	m->state_mask = base->state_mask;
273 
274 	/* TODO: use a hash table or sorted list */
275 	SLIST_INSERT_HEAD(&g_rpc_methods, m, slist);
276 }
277 
278 bool
279 spdk_rpc_verify_methods(void)
280 {
281 	return g_rpcs_correct;
282 }
283 
284 int
285 spdk_rpc_is_method_allowed(const char *method, uint32_t state_mask)
286 {
287 	struct spdk_rpc_method *m;
288 
289 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
290 		if (strcmp(m->name, method) != 0) {
291 			continue;
292 		}
293 
294 		if ((m->state_mask & state_mask) == state_mask) {
295 			return 0;
296 		} else {
297 			return -EPERM;
298 		}
299 	}
300 
301 	return -ENOENT;
302 }
303 
304 void
305 spdk_rpc_close(void)
306 {
307 	if (g_jsonrpc_server) {
308 		if (g_rpc_listen_addr_unix.sun_path[0]) {
309 			/* Delete the Unix socket file */
310 			unlink(g_rpc_listen_addr_unix.sun_path);
311 			g_rpc_listen_addr_unix.sun_path[0] = '\0';
312 		}
313 
314 		spdk_jsonrpc_server_shutdown(g_jsonrpc_server);
315 		g_jsonrpc_server = NULL;
316 
317 		if (g_rpc_lock_fd != -1) {
318 			close(g_rpc_lock_fd);
319 			g_rpc_lock_fd = -1;
320 		}
321 
322 		if (g_rpc_lock_path[0]) {
323 			unlink(g_rpc_lock_path);
324 			g_rpc_lock_path[0] = '\0';
325 		}
326 	}
327 }
328 
329 struct rpc_get_methods {
330 	bool current;
331 	bool include_aliases;
332 };
333 
334 static const struct spdk_json_object_decoder rpc_get_methods_decoders[] = {
335 	{"current", offsetof(struct rpc_get_methods, current), spdk_json_decode_bool, true},
336 	{"include_aliases", offsetof(struct rpc_get_methods, include_aliases), spdk_json_decode_bool, true},
337 };
338 
339 static void
340 rpc_get_methods(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
341 {
342 	struct rpc_get_methods req = {};
343 	struct spdk_json_write_ctx *w;
344 	struct spdk_rpc_method *m;
345 
346 	if (params != NULL) {
347 		if (spdk_json_decode_object(params, rpc_get_methods_decoders,
348 					    SPDK_COUNTOF(rpc_get_methods_decoders), &req)) {
349 			SPDK_ERRLOG("spdk_json_decode_object failed\n");
350 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
351 							 "Invalid parameters");
352 			return;
353 		}
354 	}
355 
356 	w = spdk_jsonrpc_begin_result(request);
357 	spdk_json_write_array_begin(w);
358 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
359 		if (m->is_alias_of != NULL && !req.include_aliases) {
360 			continue;
361 		}
362 		if (req.current && ((m->state_mask & g_rpc_state) != g_rpc_state)) {
363 			continue;
364 		}
365 		spdk_json_write_string(w, m->name);
366 	}
367 	spdk_json_write_array_end(w);
368 	spdk_jsonrpc_end_result(request, w);
369 }
370 SPDK_RPC_REGISTER("rpc_get_methods", rpc_get_methods, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
371 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(rpc_get_methods, get_rpc_methods)
372 
373 static void
374 rpc_spdk_get_version(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
375 {
376 	struct spdk_json_write_ctx *w;
377 
378 	if (params != NULL) {
379 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
380 						 "spdk_get_version method requires no parameters");
381 		return;
382 	}
383 
384 	w = spdk_jsonrpc_begin_result(request);
385 	spdk_json_write_object_begin(w);
386 
387 	spdk_json_write_named_string_fmt(w, "version", "%s", SPDK_VERSION_STRING);
388 	spdk_json_write_named_object_begin(w, "fields");
389 	spdk_json_write_named_uint32(w, "major", SPDK_VERSION_MAJOR);
390 	spdk_json_write_named_uint32(w, "minor", SPDK_VERSION_MINOR);
391 	spdk_json_write_named_uint32(w, "patch", SPDK_VERSION_PATCH);
392 	spdk_json_write_named_string_fmt(w, "suffix", "%s", SPDK_VERSION_SUFFIX);
393 #ifdef SPDK_GIT_COMMIT
394 	spdk_json_write_named_string_fmt(w, "commit", "%s", SPDK_GIT_COMMIT_STRING);
395 #endif
396 	spdk_json_write_object_end(w);
397 
398 	spdk_json_write_object_end(w);
399 	spdk_jsonrpc_end_result(request, w);
400 }
401 SPDK_RPC_REGISTER("spdk_get_version", rpc_spdk_get_version,
402 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
403 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(spdk_get_version, get_spdk_version)
404