xref: /spdk/lib/rpc/rpc.c (revision d27b24c94b3e506868d5eaa7b93fddc8abfe250f)
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 <assert.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <arpa/inet.h>
43 #include <netdb.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <string.h>
47 
48 #include "spdk/queue.h"
49 #include "spdk/rpc.h"
50 #include "spdk/env.h"
51 #include "spdk/event.h"
52 #include "spdk/conf.h"
53 #include "spdk/log.h"
54 
55 #define RPC_SELECT_INTERVAL	4000 /* 4ms */
56 
57 static struct spdk_poller *g_rpc_poller = NULL;
58 
59 static struct spdk_jsonrpc_server *g_jsonrpc_server = NULL;
60 
61 struct spdk_rpc_method {
62 	const char *name;
63 	spdk_rpc_method_handler func;
64 	SLIST_ENTRY(spdk_rpc_method) slist;
65 };
66 
67 static SLIST_HEAD(, spdk_rpc_method) g_rpc_methods = SLIST_HEAD_INITIALIZER(g_rpc_methods);
68 
69 static void
70 spdk_rpc_server_do_work(void *arg)
71 {
72 	spdk_jsonrpc_server_poll(g_jsonrpc_server);
73 }
74 
75 static int
76 enable_rpc(void)
77 {
78 	struct spdk_conf_section	*sp;
79 	char				*val;
80 
81 	sp = spdk_conf_find_section(NULL, "Rpc");
82 	if (sp == NULL) {
83 		return 0;
84 	}
85 
86 	val = spdk_conf_section_get_val(sp, "Enable");
87 	if (val == NULL) {
88 		return 0;
89 	}
90 
91 	if (!strcmp(val, "Yes")) {
92 		return 1;
93 	}
94 
95 	return 0;
96 }
97 
98 void
99 spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func)
100 {
101 	struct spdk_rpc_method *m;
102 
103 	m = calloc(1, sizeof(struct spdk_rpc_method));
104 	assert(m != NULL);
105 
106 	m->name = strdup(method);
107 	assert(m->name != NULL);
108 
109 	m->func = func;
110 
111 	/* TODO: use a hash table or sorted list */
112 	SLIST_INSERT_HEAD(&g_rpc_methods, m, slist);
113 }
114 
115 static void
116 spdk_jsonrpc_handler(
117 	struct spdk_jsonrpc_server_conn *conn,
118 	const struct spdk_json_val *method,
119 	const struct spdk_json_val *params,
120 	const struct spdk_json_val *id)
121 {
122 	struct spdk_rpc_method *m;
123 
124 	assert(method != NULL);
125 
126 	SLIST_FOREACH(m, &g_rpc_methods, slist) {
127 		if (spdk_json_strequal(method, m->name)) {
128 			m->func(conn, params, id);
129 			return;
130 		}
131 	}
132 
133 	spdk_jsonrpc_send_error_response(conn, id, SPDK_JSONRPC_ERROR_METHOD_NOT_FOUND, "Method not found");
134 }
135 
136 static void
137 spdk_rpc_setup(void *arg)
138 {
139 	struct sockaddr_in	serv_addr;
140 	uint16_t		port;
141 
142 	/* Unregister the one-shot setup poller */
143 	spdk_poller_unregister(&g_rpc_poller, NULL);
144 
145 	if (!enable_rpc()) {
146 		return;
147 	}
148 
149 	port = SPDK_JSONRPC_PORT_BASE + spdk_app_get_instance_id();
150 
151 	memset(&serv_addr, 0, sizeof(serv_addr));
152 	serv_addr.sin_family = AF_INET;
153 	serv_addr.sin_addr.s_addr = INADDR_ANY;
154 	serv_addr.sin_port = htons(port);
155 
156 	g_jsonrpc_server = spdk_jsonrpc_server_listen((struct sockaddr *)&serv_addr, sizeof(serv_addr),
157 			   spdk_jsonrpc_handler);
158 	if (g_jsonrpc_server == NULL) {
159 		SPDK_ERRLOG("spdk_jsonrpc_server_listen() failed\n");
160 		return;
161 	}
162 
163 	/* Register the periodic rpc_server_do_work */
164 	spdk_poller_register(&g_rpc_poller, spdk_rpc_server_do_work, NULL, spdk_app_get_current_core(),
165 			     NULL, RPC_SELECT_INTERVAL);
166 }
167 
168 static int
169 spdk_rpc_initialize(void)
170 {
171 	/*
172 	 * Defer setup of the RPC service until the reactor has started.  This
173 	 *  allows us to detect the RPC listen socket as a suitable proxy for determining
174 	 *  when the SPDK application has finished initialization and ready for logins
175 	 *  or RPC commands.
176 	 */
177 	spdk_poller_register(&g_rpc_poller, spdk_rpc_setup, NULL, spdk_app_get_current_core(),
178 			     NULL, 0);
179 	return 0;
180 }
181 
182 static void
183 spdk_rpc_finish_cleanup(struct spdk_event *event)
184 {
185 	if (g_jsonrpc_server) {
186 		spdk_jsonrpc_server_shutdown(g_jsonrpc_server);
187 	}
188 }
189 
190 static int
191 spdk_rpc_finish(void)
192 {
193 	struct spdk_event *complete;
194 
195 	complete = spdk_event_allocate(spdk_app_get_current_core(), spdk_rpc_finish_cleanup,
196 				       NULL, NULL, NULL);
197 	spdk_poller_unregister(&g_rpc_poller, complete);
198 	return 0;
199 }
200 
201 static void
202 spdk_rpc_config_text(FILE *fp)
203 {
204 	fprintf(fp,
205 		"\n"
206 		"[Rpc]\n"
207 		"  # Defines whether to enable configuration via RPC.\n"
208 		"  # Default is disabled.  Note that the RPC interface is not\n"
209 		"  # authenticated, so users should be careful about enabling\n"
210 		"  # RPC in non-trusted environments.\n"
211 		"  Enable %s\n",
212 		enable_rpc() ? "Yes" : "No");
213 }
214 
215 SPDK_SUBSYSTEM_REGISTER(spdk_rpc, spdk_rpc_initialize, spdk_rpc_finish, spdk_rpc_config_text)
216