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 #include "spdk/event.h" 36 #include "spdk/jsonrpc.h" 37 #include "spdk/util.h" 38 #include "spdk/rpc.h" 39 40 41 #define RPC_MAX_METHODS 200 42 #define JOIN_TIMEOUT_S 1 43 44 static const char *g_rpcsock_addr = SPDK_DEFAULT_RPC_ADDR; 45 static int g_addr_family = AF_UNIX; 46 47 #define RPC_MAX_METHODS 200 48 49 struct get_jsonrpc_methods_resp { 50 char *method_names[RPC_MAX_METHODS]; 51 size_t method_num; 52 }; 53 54 static int 55 _rpc_client_wait_for_response(struct spdk_jsonrpc_client *client) 56 { 57 int rc; 58 59 do { 60 rc = spdk_jsonrpc_client_poll(client, 1); 61 } while (rc == 0 || rc == -ENOTCONN); 62 63 if (rc <= 0) { 64 SPDK_ERRLOG("Failed to get response: %d\n", rc); 65 } 66 67 return rc; 68 } 69 70 static int 71 get_jsonrpc_method_json_parser(struct get_jsonrpc_methods_resp *resp, 72 const struct spdk_json_val *result) 73 { 74 return spdk_json_decode_array(result, spdk_json_decode_string, resp->method_names, 75 RPC_MAX_METHODS, &resp->method_num, sizeof(char *)); 76 } 77 78 static int 79 spdk_jsonrpc_client_check_rpc_method(struct spdk_jsonrpc_client *client, char *method_name) 80 { 81 int rc, i; 82 struct spdk_jsonrpc_client_response *json_resp = NULL; 83 struct get_jsonrpc_methods_resp resp = {}; 84 struct spdk_json_write_ctx *w; 85 struct spdk_jsonrpc_client_request *request; 86 87 request = spdk_jsonrpc_client_create_request(); 88 if (request == NULL) { 89 return -ENOMEM; 90 } 91 92 w = spdk_jsonrpc_begin_request(request, 1, "rpc_get_methods"); 93 spdk_jsonrpc_end_request(request, w); 94 spdk_jsonrpc_client_send_request(client, request); 95 96 rc = _rpc_client_wait_for_response(client); 97 if (rc <= 0) { 98 goto out; 99 } 100 101 json_resp = spdk_jsonrpc_client_get_response(client); 102 if (json_resp == NULL) { 103 SPDK_ERRLOG("spdk_jsonrpc_client_get_response() failed\n"); 104 rc = -1; 105 goto out; 106 107 } 108 109 /* Check for error response */ 110 if (json_resp->error != NULL) { 111 SPDK_ERRLOG("Unexpected error response\n"); 112 rc = -1; 113 goto out; 114 } 115 116 assert(json_resp->result); 117 118 rc = get_jsonrpc_method_json_parser(&resp, json_resp->result); 119 if (rc) { 120 SPDK_ERRLOG("get_jsonrpc_method_json_parser() failed\n"); 121 goto out; 122 } 123 124 for (i = 0; i < (int)resp.method_num; i++) { 125 if (strcmp(method_name, resp.method_names[i]) == 0) { 126 rc = 0; 127 goto out; 128 } 129 } 130 131 rc = -1; 132 SPDK_ERRLOG("Method '%s' not found in response\n", method_name); 133 134 out: 135 for (i = 0; i < (int)resp.method_num; i++) { 136 SPDK_NOTICELOG("%s\n", resp.method_names[i]); 137 free(resp.method_names[i]); 138 } 139 140 spdk_jsonrpc_client_free_response(json_resp); 141 return rc; 142 } 143 144 static int 145 spdk_jsonrpc_client_check_null_params_method(struct spdk_jsonrpc_client *client) 146 { 147 int rc; 148 bool res = false; 149 struct spdk_jsonrpc_client_response *json_resp = NULL; 150 struct spdk_json_write_ctx *w; 151 struct spdk_jsonrpc_client_request *request; 152 153 request = spdk_jsonrpc_client_create_request(); 154 if (request == NULL) { 155 return -ENOMEM; 156 } 157 158 w = spdk_jsonrpc_begin_request(request, 1, "test_null_params"); 159 spdk_json_write_name(w, "params"); 160 spdk_json_write_null(w); 161 spdk_jsonrpc_end_request(request, w); 162 spdk_jsonrpc_client_send_request(client, request); 163 164 rc = _rpc_client_wait_for_response(client); 165 if (rc <= 0) { 166 goto out; 167 } 168 169 json_resp = spdk_jsonrpc_client_get_response(client); 170 if (json_resp == NULL) { 171 SPDK_ERRLOG("spdk_jsonrpc_client_get_response() failed\n"); 172 rc = -1; 173 goto out; 174 175 } 176 177 /* Check for error response */ 178 if (json_resp->error != NULL) { 179 SPDK_ERRLOG("Unexpected error response\n"); 180 rc = -1; 181 goto out; 182 } 183 184 assert(json_resp->result); 185 186 if (spdk_json_decode_bool(json_resp->result, &res) != 0 || res != true) { 187 SPDK_ERRLOG("Response is not a boolean or it is not 'true'\n"); 188 rc = -EINVAL; 189 goto out; 190 } else { 191 rc = 0; 192 } 193 194 out: 195 spdk_jsonrpc_client_free_response(json_resp); 196 return rc; 197 } 198 199 static void 200 rpc_test_method_startup(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) 201 { 202 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 203 "rpc_test_method_startup(): Method body not implemented"); 204 } 205 SPDK_RPC_REGISTER("test_method_startup", rpc_test_method_startup, SPDK_RPC_STARTUP) 206 207 static void 208 rpc_test_method_runtime(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) 209 { 210 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 211 "rpc_test_method_runtime(): Method body not implemented"); 212 } 213 SPDK_RPC_REGISTER("test_method_runtime", rpc_test_method_runtime, SPDK_RPC_RUNTIME) 214 215 static void 216 rpc_test_method_null_params(struct spdk_jsonrpc_request *request, 217 const struct spdk_json_val *params) 218 { 219 struct spdk_json_write_ctx *w; 220 221 if (params != NULL) { 222 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 223 "rpc_test_method_null_params(): Parameters are not NULL"); 224 return; 225 } 226 w = spdk_jsonrpc_begin_result(request); 227 assert(w != NULL); 228 spdk_json_write_bool(w, true); 229 spdk_jsonrpc_end_result(request, w); 230 } 231 SPDK_RPC_REGISTER("test_null_params", rpc_test_method_null_params, SPDK_RPC_RUNTIME) 232 233 static bool g_conn_close_detected; 234 235 static void 236 rpc_test_conn_close_cb(struct spdk_jsonrpc_server_conn *conn, void *ctx) 237 { 238 assert((intptr_t)ctx == 42); 239 g_conn_close_detected = true; 240 } 241 242 static void 243 rpc_hook_conn_close(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) 244 { 245 struct spdk_jsonrpc_server_conn *conn = spdk_jsonrpc_get_conn(request); 246 struct spdk_json_write_ctx *w; 247 int rc; 248 249 rc = spdk_jsonrpc_conn_add_close_cb(conn, rpc_test_conn_close_cb, (void *)(intptr_t)(42)); 250 if (rc != 0) { 251 252 rc = spdk_jsonrpc_conn_add_close_cb(conn, rpc_test_conn_close_cb, (void *)(intptr_t)(42)); 253 assert(rc == -ENOSPC); 254 } 255 256 rc = spdk_jsonrpc_conn_add_close_cb(conn, rpc_test_conn_close_cb, (void *)(intptr_t)(42)); 257 if (rc != -EEXIST) { 258 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 259 "rpc_test_method_conn_close_detect(): rc != -EEXIST"); 260 return; 261 } 262 263 rc = spdk_jsonrpc_conn_add_close_cb(conn, rpc_test_conn_close_cb, (void *)(intptr_t)(43)); 264 if (rc != -ENOSPC) { 265 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 266 "rpc_test_method_conn_close_detect(): rc != -ENOSPC"); 267 return; 268 } 269 270 w = spdk_jsonrpc_begin_result(request); 271 assert(w != NULL); 272 spdk_json_write_bool(w, true); 273 spdk_jsonrpc_end_result(request, w); 274 275 } 276 SPDK_RPC_REGISTER("hook_conn_close", rpc_hook_conn_close, SPDK_RPC_RUNTIME | SPDK_RPC_STARTUP) 277 278 static int 279 spdk_jsonrpc_client_hook_conn_close(struct spdk_jsonrpc_client *client) 280 { 281 int rc; 282 bool res = false; 283 struct spdk_jsonrpc_client_response *json_resp = NULL; 284 struct spdk_json_write_ctx *w; 285 struct spdk_jsonrpc_client_request *request; 286 287 request = spdk_jsonrpc_client_create_request(); 288 if (request == NULL) { 289 return -ENOMEM; 290 } 291 292 w = spdk_jsonrpc_begin_request(request, 1, "hook_conn_close"); 293 spdk_jsonrpc_end_request(request, w); 294 spdk_jsonrpc_client_send_request(client, request); 295 296 rc = _rpc_client_wait_for_response(client); 297 if (rc <= 0) { 298 goto out; 299 } 300 301 json_resp = spdk_jsonrpc_client_get_response(client); 302 if (json_resp == NULL) { 303 SPDK_ERRLOG("spdk_jsonrpc_client_get_response() failed\n"); 304 rc = -errno; 305 goto out; 306 307 } 308 309 /* Check for error response */ 310 if (json_resp->error != NULL) { 311 SPDK_ERRLOG("Unexpected error response: %*s\n", json_resp->error->len, 312 (char *)json_resp->error->start); 313 rc = -EIO; 314 goto out; 315 } 316 317 assert(json_resp->result); 318 if (spdk_json_decode_bool(json_resp->result, &res) != 0 || res != true) { 319 SPDK_ERRLOG("Response is not and boolean or if not 'true'\n"); 320 rc = -EINVAL; 321 goto out; 322 } 323 324 rc = 0; 325 out: 326 spdk_jsonrpc_client_free_response(json_resp); 327 return rc; 328 } 329 330 /* Helper function */ 331 static int 332 _sem_timedwait(sem_t *sem, __time_t sec) 333 { 334 struct timespec timeout; 335 336 clock_gettime(CLOCK_REALTIME, &timeout); 337 timeout.tv_sec += sec; 338 339 return sem_timedwait(sem, &timeout); 340 } 341 342 volatile int g_rpc_server_th_stop; 343 static sem_t g_rpc_server_th_listening; 344 345 static void * 346 rpc_server_th(void *arg) 347 { 348 int rc; 349 350 rc = spdk_rpc_listen(g_rpcsock_addr); 351 if (rc) { 352 fprintf(stderr, "spdk_rpc_listen() failed: %d\n", rc); 353 goto out; 354 } 355 356 sem_post(&g_rpc_server_th_listening); 357 358 while (!g_rpc_server_th_stop) { 359 spdk_rpc_accept(); 360 usleep(50); 361 } 362 363 spdk_rpc_close(); 364 out: 365 return (void *)(intptr_t)rc; 366 } 367 368 static sem_t g_rpc_client_th_done; 369 370 static void * 371 rpc_client_th(void *arg) 372 { 373 struct spdk_jsonrpc_client *client = NULL; 374 char *method_name = "rpc_get_methods"; 375 int rc; 376 377 378 rc = _sem_timedwait(&g_rpc_server_th_listening, 2); 379 if (rc == -1) { 380 fprintf(stderr, "Timeout waiting for server thread to start listening: rc=%d errno=%d\n", rc, 381 errno); 382 goto out; 383 } 384 385 client = spdk_jsonrpc_client_connect(g_rpcsock_addr, g_addr_family); 386 if (!client) { 387 fprintf(stderr, "spdk_jsonrpc_client_connect() failed: %d\n", errno); 388 rc = -1; 389 goto out; 390 } 391 392 rc = spdk_jsonrpc_client_check_rpc_method(client, method_name); 393 if (rc) { 394 fprintf(stderr, "spdk_jsonrpc_client_check_rpc_method() failed: rc=%d errno=%d\n", rc, errno); 395 goto out; 396 } 397 398 rc = spdk_jsonrpc_client_check_null_params_method(client); 399 if (rc) { 400 fprintf(stderr, "spdk_jsonrpc_client_null_params_method() failed: rc=%d errno=%d\n", rc, errno); 401 goto out; 402 } 403 404 rc = spdk_jsonrpc_client_hook_conn_close(client); 405 if (rc) { 406 fprintf(stderr, "spdk_jsonrpc_client_hook_conn_close() failed: rc=%d errno=%d\n", rc, errno); 407 goto out; 408 } 409 410 out: 411 if (client) { 412 spdk_jsonrpc_client_close(client); 413 } 414 415 sem_post(&g_rpc_client_th_done); 416 return (void *)(intptr_t)rc; 417 } 418 419 int main(int argc, char **argv) 420 { 421 pthread_t srv_tid, client_tid; 422 int srv_tid_valid; 423 int client_tid_valid = -1; 424 intptr_t th_rc = INTPTR_MIN; 425 int rc = 0, err_cnt = 0; 426 427 sem_init(&g_rpc_server_th_listening, 0, 0); 428 sem_init(&g_rpc_client_th_done, 0, 0); 429 430 srv_tid_valid = pthread_create(&srv_tid, NULL, rpc_server_th, NULL); 431 if (srv_tid_valid != 0) { 432 fprintf(stderr, "pthread_create() failed to create server thread: %d\n", srv_tid_valid); 433 goto out; 434 } 435 436 client_tid_valid = pthread_create(&client_tid, NULL, rpc_client_th, NULL); 437 if (client_tid_valid != 0) { 438 fprintf(stderr, "pthread_create(): failed to create client thread: %d\n", client_tid_valid); 439 goto out; 440 } 441 442 out: 443 if (client_tid_valid == 0) { 444 rc = _sem_timedwait(&g_rpc_client_th_done, JOIN_TIMEOUT_S); 445 if (rc) { 446 fprintf(stderr, "failed to join client thread (rc: %d)\n", rc); 447 err_cnt++; 448 } 449 450 rc = pthread_join(client_tid, (void **)&th_rc); 451 if (rc) { 452 fprintf(stderr, "pthread_join() on client thread failed (rc: %d)\n", rc); 453 err_cnt++; 454 } else if (th_rc) { 455 fprintf(stderr, "client thread failed reported failure(thread rc: %d)\n", (int)th_rc); 456 err_cnt++; 457 } 458 } 459 460 g_rpc_server_th_stop = 1; 461 462 if (srv_tid_valid == 0) { 463 rc = pthread_join(srv_tid, (void **)&th_rc); 464 if (rc) { 465 fprintf(stderr, "pthread_join() on server thread failed (rc: %d)\n", rc); 466 err_cnt++; 467 } else if (th_rc) { 468 fprintf(stderr, "server thread failed reported failure(thread rc: %d)\n", (int)th_rc); 469 err_cnt++; 470 } 471 } 472 473 if (g_conn_close_detected == false) { 474 fprintf(stderr, "Connection close not detected\n"); 475 err_cnt++; 476 } 477 478 sem_destroy(&g_rpc_server_th_listening); 479 sem_destroy(&g_rpc_client_th_done); 480 481 fprintf(stderr, "%s\n", err_cnt == 0 ? "OK" : "FAILED"); 482 return err_cnt ? EXIT_FAILURE : 0; 483 } 484