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 m->state_mask = base->state_mask; 295 296 /* TODO: use a hash table or sorted list */ 297 SLIST_INSERT_HEAD(&g_rpc_methods, m, slist); 298 } 299 300 int 301 spdk_rpc_is_method_allowed(const char *method, uint32_t state_mask) 302 { 303 struct spdk_rpc_method *m; 304 305 SLIST_FOREACH(m, &g_rpc_methods, slist) { 306 if (strcmp(m->name, method) != 0) { 307 continue; 308 } 309 310 if ((m->state_mask & state_mask) == state_mask) { 311 return 0; 312 } else { 313 return -EPERM; 314 } 315 } 316 317 return -ENOENT; 318 } 319 320 void 321 spdk_rpc_close(void) 322 { 323 if (g_jsonrpc_server) { 324 if (g_rpc_listen_addr_unix.sun_path[0]) { 325 /* Delete the Unix socket file */ 326 unlink(g_rpc_listen_addr_unix.sun_path); 327 } 328 329 spdk_jsonrpc_server_shutdown(g_jsonrpc_server); 330 g_jsonrpc_server = NULL; 331 332 if (g_rpc_lock_fd != -1) { 333 close(g_rpc_lock_fd); 334 g_rpc_lock_fd = -1; 335 } 336 337 if (g_rpc_lock_path[0]) { 338 unlink(g_rpc_lock_path); 339 g_rpc_lock_path[0] = '\0'; 340 } 341 } 342 } 343 344 struct rpc_get_methods { 345 bool current; 346 bool include_aliases; 347 }; 348 349 static const struct spdk_json_object_decoder rpc_get_methods_decoders[] = { 350 {"current", offsetof(struct rpc_get_methods, current), spdk_json_decode_bool, true}, 351 {"include_aliases", offsetof(struct rpc_get_methods, include_aliases), spdk_json_decode_bool, true}, 352 }; 353 354 static void 355 spdk_rpc_get_methods(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) 356 { 357 struct rpc_get_methods req = {}; 358 struct spdk_json_write_ctx *w; 359 struct spdk_rpc_method *m; 360 361 if (params != NULL) { 362 if (spdk_json_decode_object(params, rpc_get_methods_decoders, 363 SPDK_COUNTOF(rpc_get_methods_decoders), &req)) { 364 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 365 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 366 "Invalid parameters"); 367 return; 368 } 369 } 370 371 w = spdk_jsonrpc_begin_result(request); 372 spdk_json_write_array_begin(w); 373 SLIST_FOREACH(m, &g_rpc_methods, slist) { 374 if (m->is_alias_of != NULL && !req.include_aliases) { 375 continue; 376 } 377 if (req.current && ((m->state_mask & g_rpc_state) != g_rpc_state)) { 378 continue; 379 } 380 spdk_json_write_string(w, m->name); 381 } 382 spdk_json_write_array_end(w); 383 spdk_jsonrpc_end_result(request, w); 384 } 385 SPDK_RPC_REGISTER("rpc_get_methods", spdk_rpc_get_methods, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 386 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(rpc_get_methods, get_rpc_methods) 387 388 static void 389 spdk_rpc_spdk_get_version(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) 390 { 391 struct spdk_json_write_ctx *w; 392 393 if (params != NULL) { 394 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 395 "spdk_get_version method requires no parameters"); 396 return; 397 } 398 399 w = spdk_jsonrpc_begin_result(request); 400 spdk_json_write_object_begin(w); 401 402 spdk_json_write_named_string_fmt(w, "version", "%s", SPDK_VERSION_STRING); 403 spdk_json_write_named_object_begin(w, "fields"); 404 spdk_json_write_named_uint32(w, "major", SPDK_VERSION_MAJOR); 405 spdk_json_write_named_uint32(w, "minor", SPDK_VERSION_MINOR); 406 spdk_json_write_named_uint32(w, "patch", SPDK_VERSION_PATCH); 407 spdk_json_write_named_string_fmt(w, "suffix", "%s", SPDK_VERSION_SUFFIX); 408 spdk_json_write_object_end(w); 409 410 spdk_json_write_object_end(w); 411 spdk_jsonrpc_end_result(request, w); 412 } 413 SPDK_RPC_REGISTER("spdk_get_version", spdk_rpc_spdk_get_version, 414 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 415 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(spdk_get_version, get_spdk_version) 416