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 if (access(g_rpc_listen_addr_unix.sun_path, F_OK) == 0) { 97 SPDK_ERRLOG("RPC Unix domain socket path already exists.\n"); 98 return -1; 99 } 100 101 g_jsonrpc_server = spdk_jsonrpc_server_listen(AF_UNIX, 0, 102 (struct sockaddr *)&g_rpc_listen_addr_unix, 103 sizeof(g_rpc_listen_addr_unix), 104 spdk_jsonrpc_handler); 105 } else { 106 char *tmp; 107 char *host, *port; 108 109 tmp = strdup(listen_addr); 110 if (!tmp) { 111 SPDK_ERRLOG("Out of memory\n"); 112 return -1; 113 } 114 115 if (spdk_parse_ip_addr(tmp, &host, &port) < 0) { 116 free(tmp); 117 SPDK_ERRLOG("Invalid listen address '%s'\n", listen_addr); 118 return -1; 119 } 120 121 if (port == NULL) { 122 port = RPC_DEFAULT_PORT; 123 } 124 125 memset(&hints, 0, sizeof(hints)); 126 hints.ai_family = AF_UNSPEC; 127 hints.ai_socktype = SOCK_STREAM; 128 hints.ai_protocol = IPPROTO_TCP; 129 130 if (getaddrinfo(host, port, &hints, &res) != 0) { 131 free(tmp); 132 SPDK_ERRLOG("Unable to look up RPC listen address '%s'\n", listen_addr); 133 return -1; 134 } 135 136 g_jsonrpc_server = spdk_jsonrpc_server_listen(res->ai_family, res->ai_protocol, 137 res->ai_addr, res->ai_addrlen, 138 spdk_jsonrpc_handler); 139 140 freeaddrinfo(res); 141 free(tmp); 142 } 143 144 if (g_jsonrpc_server == NULL) { 145 SPDK_ERRLOG("spdk_jsonrpc_server_listen() failed\n"); 146 return -1; 147 } 148 149 return 0; 150 } 151 152 void 153 spdk_rpc_accept(void) 154 { 155 spdk_jsonrpc_server_poll(g_jsonrpc_server); 156 } 157 158 void 159 spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func) 160 { 161 struct spdk_rpc_method *m; 162 163 m = calloc(1, sizeof(struct spdk_rpc_method)); 164 assert(m != NULL); 165 166 m->name = strdup(method); 167 assert(m->name != NULL); 168 169 m->func = func; 170 171 /* TODO: use a hash table or sorted list */ 172 SLIST_INSERT_HEAD(&g_rpc_methods, m, slist); 173 } 174 175 void 176 spdk_rpc_close(void) 177 { 178 if (g_jsonrpc_server) { 179 if (g_rpc_listen_addr_unix.sun_path[0]) { 180 /* Delete the Unix socket file */ 181 unlink(g_rpc_listen_addr_unix.sun_path); 182 } 183 184 spdk_jsonrpc_server_shutdown(g_jsonrpc_server); 185 g_jsonrpc_server = NULL; 186 } 187 } 188 189 190 static void 191 spdk_rpc_get_rpc_methods(struct spdk_jsonrpc_request *request, 192 const struct spdk_json_val *params) 193 { 194 struct spdk_json_write_ctx *w; 195 struct spdk_rpc_method *m; 196 197 if (params != NULL) { 198 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 199 "get_rpc_methods requires no parameters"); 200 return; 201 } 202 203 w = spdk_jsonrpc_begin_result(request); 204 if (w == NULL) { 205 return; 206 } 207 208 spdk_json_write_array_begin(w); 209 SLIST_FOREACH(m, &g_rpc_methods, slist) { 210 spdk_json_write_string(w, m->name); 211 } 212 spdk_json_write_array_end(w); 213 spdk_jsonrpc_end_result(request, w); 214 } 215 SPDK_RPC_REGISTER("get_rpc_methods", spdk_rpc_get_rpc_methods) 216