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