xref: /spdk/lib/rpc/rpc.c (revision 4d43844f4d50a7abd5b6029ff3f2baff329890a8)
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 "spdk/stdinc.h"
35 
36 #include "spdk/queue.h"
37 #include "spdk/rpc.h"
38 #include "spdk/env.h"
39 #include "spdk/log.h"
40 #include "spdk/string.h"
41 
42 #define RPC_DEFAULT_PORT	"5260"
43 
44 static struct sockaddr_un g_rpc_listen_addr_unix = {};
45 
46 static struct spdk_jsonrpc_server *g_jsonrpc_server = NULL;
47 
48 struct spdk_rpc_method {
49 	const char *name;
50 	spdk_rpc_method_handler func;
51 	SLIST_ENTRY(spdk_rpc_method) slist;
52 };
53 
54 static SLIST_HEAD(, spdk_rpc_method) g_rpc_methods = SLIST_HEAD_INITIALIZER(g_rpc_methods);
55 
56 static void
57 spdk_jsonrpc_handler(struct spdk_jsonrpc_request *request,
58 		     const struct spdk_json_val *method,
59 		     const struct spdk_json_val *params)
60 {
61 	struct spdk_rpc_method *m;
62 
63 	assert(method != NULL);
64 
65 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
66 		if (spdk_json_strequal(method, m->name)) {
67 			m->func(request, params);
68 			return;
69 		}
70 	}
71 
72 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_METHOD_NOT_FOUND, "Method not found");
73 }
74 
75 int
76 spdk_rpc_listen(const char *listen_addr)
77 {
78 	struct addrinfo		hints;
79 	struct addrinfo		*res;
80 
81 	memset(&g_rpc_listen_addr_unix, 0, sizeof(g_rpc_listen_addr_unix));
82 
83 	if (listen_addr[0] == '/') {
84 		int rc;
85 
86 		g_rpc_listen_addr_unix.sun_family = AF_UNIX;
87 		rc = snprintf(g_rpc_listen_addr_unix.sun_path,
88 			      sizeof(g_rpc_listen_addr_unix.sun_path),
89 			      "%s", listen_addr);
90 		if (rc < 0 || (size_t)rc >= sizeof(g_rpc_listen_addr_unix.sun_path)) {
91 			SPDK_ERRLOG("RPC Listen address Unix socket path too long\n");
92 			g_rpc_listen_addr_unix.sun_path[0] = '\0';
93 			return -1;
94 		}
95 
96 		unlink(g_rpc_listen_addr_unix.sun_path);
97 
98 		g_jsonrpc_server = spdk_jsonrpc_server_listen(AF_UNIX, 0,
99 				   (struct sockaddr *)&g_rpc_listen_addr_unix,
100 				   sizeof(g_rpc_listen_addr_unix),
101 				   spdk_jsonrpc_handler);
102 	} else {
103 		char *tmp;
104 		char *host, *port;
105 
106 		tmp = strdup(listen_addr);
107 		if (!tmp) {
108 			SPDK_ERRLOG("Out of memory\n");
109 			return -1;
110 		}
111 
112 		if (spdk_parse_ip_addr(tmp, &host, &port) < 0) {
113 			free(tmp);
114 			SPDK_ERRLOG("Invalid listen address '%s'\n", listen_addr);
115 			return -1;
116 		}
117 
118 		if (port == NULL) {
119 			port = RPC_DEFAULT_PORT;
120 		}
121 
122 		memset(&hints, 0, sizeof(hints));
123 		hints.ai_family = AF_UNSPEC;
124 		hints.ai_socktype = SOCK_STREAM;
125 		hints.ai_protocol = IPPROTO_TCP;
126 
127 		if (getaddrinfo(host, port, &hints, &res) != 0) {
128 			free(tmp);
129 			SPDK_ERRLOG("Unable to look up RPC listen address '%s'\n", listen_addr);
130 			return -1;
131 		}
132 
133 		g_jsonrpc_server = spdk_jsonrpc_server_listen(res->ai_family, res->ai_protocol,
134 				   res->ai_addr, res->ai_addrlen,
135 				   spdk_jsonrpc_handler);
136 
137 		freeaddrinfo(res);
138 		free(tmp);
139 	}
140 
141 	if (g_jsonrpc_server == NULL) {
142 		SPDK_ERRLOG("spdk_jsonrpc_server_listen() failed\n");
143 		return -1;
144 	}
145 
146 	return 0;
147 }
148 
149 void
150 spdk_rpc_accept(void)
151 {
152 	spdk_jsonrpc_server_poll(g_jsonrpc_server);
153 }
154 
155 void
156 spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func)
157 {
158 	struct spdk_rpc_method *m;
159 
160 	m = calloc(1, sizeof(struct spdk_rpc_method));
161 	assert(m != NULL);
162 
163 	m->name = strdup(method);
164 	assert(m->name != NULL);
165 
166 	m->func = func;
167 
168 	/* TODO: use a hash table or sorted list */
169 	SLIST_INSERT_HEAD(&g_rpc_methods, m, slist);
170 }
171 
172 void
173 spdk_rpc_close(void)
174 {
175 	if (g_rpc_listen_addr_unix.sun_path[0]) {
176 		/* Delete the Unix socket file */
177 		unlink(g_rpc_listen_addr_unix.sun_path);
178 	}
179 
180 	if (g_jsonrpc_server) {
181 		spdk_jsonrpc_server_shutdown(g_jsonrpc_server);
182 	}
183 }
184 
185 
186 static void
187 spdk_rpc_get_rpc_methods(struct spdk_jsonrpc_request *request,
188 			 const struct spdk_json_val *params)
189 {
190 	struct spdk_json_write_ctx *w;
191 	struct spdk_rpc_method *m;
192 
193 	if (params != NULL) {
194 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
195 						 "get_rpc_methods requires no parameters");
196 		return;
197 	}
198 
199 	w = spdk_jsonrpc_begin_result(request);
200 	if (w == NULL) {
201 		return;
202 	}
203 
204 	spdk_json_write_array_begin(w);
205 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
206 		spdk_json_write_string(w, m->name);
207 	}
208 	spdk_json_write_array_end(w);
209 	spdk_jsonrpc_end_result(request, w);
210 }
211 SPDK_RPC_REGISTER("get_rpc_methods", spdk_rpc_get_rpc_methods)
212