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