2018-08-30 11:05:39 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
#include "spdk/event.h"
|
|
|
|
#include "spdk/jsonrpc.h"
|
2018-10-22 09:27:09 +00:00
|
|
|
#include "spdk/util.h"
|
|
|
|
#include "spdk/rpc.h"
|
|
|
|
|
2018-08-30 11:05:39 +00:00
|
|
|
|
|
|
|
#define RPC_MAX_METHODS 200
|
2018-10-22 09:27:09 +00:00
|
|
|
#define JOIN_TIMEOUT_S 1
|
2018-08-30 11:05:39 +00:00
|
|
|
|
|
|
|
static const char *g_rpcsock_addr = SPDK_DEFAULT_RPC_ADDR;
|
|
|
|
static int g_addr_family = AF_UNIX;
|
|
|
|
|
|
|
|
#define RPC_MAX_METHODS 200
|
|
|
|
|
|
|
|
struct get_jsonrpc_methods_resp {
|
|
|
|
char *method_names[RPC_MAX_METHODS];
|
|
|
|
size_t method_num;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2018-10-12 19:19:14 +00:00
|
|
|
get_jsonrpc_method_json_parser(struct get_jsonrpc_methods_resp *resp,
|
2018-08-30 11:05:39 +00:00
|
|
|
const struct spdk_json_val *result)
|
|
|
|
{
|
|
|
|
return spdk_json_decode_array(result, spdk_json_decode_string, resp->method_names,
|
|
|
|
RPC_MAX_METHODS, &resp->method_num, sizeof(char *));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
spdk_jsonrpc_client_check_rpc_method(struct spdk_jsonrpc_client *client, char *method_name)
|
|
|
|
{
|
|
|
|
int rc, i;
|
2018-10-12 19:19:14 +00:00
|
|
|
struct spdk_jsonrpc_client_response *json_resp = NULL;
|
2018-08-30 11:05:39 +00:00
|
|
|
struct get_jsonrpc_methods_resp resp = {};
|
|
|
|
struct spdk_json_write_ctx *w;
|
|
|
|
struct spdk_jsonrpc_client_request *request;
|
|
|
|
|
|
|
|
request = spdk_jsonrpc_client_create_request();
|
|
|
|
if (request == NULL) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-10-12 13:26:51 +00:00
|
|
|
w = spdk_jsonrpc_begin_request(request, 1, "get_rpc_methods");
|
2018-08-30 11:05:39 +00:00
|
|
|
spdk_jsonrpc_end_request(request, w);
|
|
|
|
spdk_jsonrpc_client_send_request(client, request);
|
|
|
|
|
2018-10-22 09:27:09 +00:00
|
|
|
do {
|
2018-10-18 13:53:31 +00:00
|
|
|
rc = spdk_jsonrpc_client_poll(client, 1);
|
|
|
|
} while (rc == 0 || rc == -ENOTCONN);
|
2018-10-22 09:27:09 +00:00
|
|
|
|
2018-10-18 13:53:31 +00:00
|
|
|
if (rc <= 0) {
|
|
|
|
SPDK_ERRLOG("Failed to get response: %d\n", rc);
|
|
|
|
rc = -1;
|
2018-10-12 19:19:14 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_resp = spdk_jsonrpc_client_get_response(client);
|
|
|
|
if (json_resp == NULL) {
|
2018-10-22 09:27:09 +00:00
|
|
|
SPDK_ERRLOG("spdk_jsonrpc_client_get_response() failed\n");
|
2018-10-18 13:53:31 +00:00
|
|
|
rc = -1;
|
2018-10-12 19:19:14 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for error response */
|
|
|
|
if (json_resp->error != NULL) {
|
2018-10-22 09:27:09 +00:00
|
|
|
SPDK_ERRLOG("Unexpected error response\n");
|
2018-10-12 19:19:14 +00:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(json_resp->result);
|
2018-08-30 11:05:39 +00:00
|
|
|
|
2018-10-12 19:19:14 +00:00
|
|
|
rc = get_jsonrpc_method_json_parser(&resp, json_resp->result);
|
2018-08-30 11:05:39 +00:00
|
|
|
if (rc) {
|
2018-10-22 09:27:09 +00:00
|
|
|
SPDK_ERRLOG("get_jsonrpc_method_json_parser() failed\n");
|
2018-08-30 11:05:39 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (int)resp.method_num; i++) {
|
|
|
|
if (strcmp(method_name, resp.method_names[i]) == 0) {
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = -1;
|
2018-10-22 09:27:09 +00:00
|
|
|
SPDK_ERRLOG("Method '%s' not found in response\n", method_name);
|
2018-08-30 11:05:39 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
for (i = 0; i < (int)resp.method_num; i++) {
|
|
|
|
SPDK_NOTICELOG("%s\n", resp.method_names[i]);
|
|
|
|
free(resp.method_names[i]);
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:19:14 +00:00
|
|
|
spdk_jsonrpc_client_free_response(json_resp);
|
2018-08-30 11:05:39 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:27:09 +00:00
|
|
|
static void
|
|
|
|
rpc_test_method_startup(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
|
|
|
|
{
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
|
|
|
"rpc_test_method_startup(): Method body not implemented");
|
|
|
|
}
|
|
|
|
SPDK_RPC_REGISTER("test_method_startup", rpc_test_method_startup, SPDK_RPC_STARTUP)
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_test_method_runtime(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
|
|
|
|
{
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
|
|
|
"rpc_test_method_runtime(): Method body not implemented");
|
|
|
|
}
|
|
|
|
SPDK_RPC_REGISTER("test_method_runtime", rpc_test_method_runtime, SPDK_RPC_RUNTIME)
|
|
|
|
|
|
|
|
/* Helper function */
|
|
|
|
static int
|
|
|
|
_sem_timedwait(sem_t *sem, __time_t sec)
|
|
|
|
{
|
|
|
|
struct timespec timeout;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &timeout);
|
|
|
|
timeout.tv_sec += sec;
|
|
|
|
|
|
|
|
return sem_timedwait(sem, &timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile int g_rpc_server_th_stop;
|
|
|
|
static sem_t g_rpc_server_th_listening;
|
|
|
|
static sem_t g_rpc_server_th_done;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
rpc_server_th(void *arg)
|
2018-08-30 11:05:39 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2018-10-22 09:27:09 +00:00
|
|
|
|
|
|
|
rc = spdk_rpc_listen(g_rpcsock_addr);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "spdk_rpc_listen() failed: %d\n", rc);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_rpc_server_th_listening);
|
|
|
|
|
|
|
|
while (!g_rpc_server_th_stop) {
|
|
|
|
spdk_rpc_accept();
|
|
|
|
usleep(50);
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_rpc_close();
|
|
|
|
out:
|
|
|
|
sem_post(&g_rpc_server_th_done);
|
|
|
|
|
|
|
|
return (void *)(intptr_t)rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sem_t g_rpc_client_th_done;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
rpc_client_th(void *arg)
|
|
|
|
{
|
|
|
|
struct spdk_jsonrpc_client *client = NULL;
|
2018-08-30 11:05:39 +00:00
|
|
|
char *method_name = "get_rpc_methods";
|
2018-10-22 09:27:09 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
|
|
rc = _sem_timedwait(&g_rpc_server_th_listening, 2);
|
|
|
|
if (rc == -1) {
|
|
|
|
fprintf(stderr, "Timeout waiting for server thread to start listening: %d\n", errno);
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-30 11:05:39 +00:00
|
|
|
|
|
|
|
client = spdk_jsonrpc_client_connect(g_rpcsock_addr, g_addr_family);
|
|
|
|
if (!client) {
|
2018-10-22 09:27:09 +00:00
|
|
|
fprintf(stderr, "spdk_jsonrpc_client_connect() failed: %d\n", errno);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2018-08-30 11:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_jsonrpc_client_check_rpc_method(client, method_name);
|
2018-10-22 09:27:09 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "spdk_jsonrpc_client_check_rpc_method() failed: %d\n", errno);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (client) {
|
|
|
|
spdk_jsonrpc_client_close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_rpc_client_th_done);
|
|
|
|
return (void *)(intptr_t)rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
pthread_t srv_tid, client_tid;
|
|
|
|
int srv_tid_valid;
|
|
|
|
int client_tid_valid = -1;
|
|
|
|
int th_rc = INT_MIN;
|
|
|
|
int rc = 0, err_cnt = 0;
|
|
|
|
|
|
|
|
sem_init(&g_rpc_server_th_listening, 0, 0);
|
|
|
|
sem_init(&g_rpc_server_th_done, 0, 0);
|
|
|
|
sem_init(&g_rpc_client_th_done, 0, 0);
|
|
|
|
|
|
|
|
srv_tid_valid = pthread_create(&srv_tid, NULL, rpc_server_th, NULL);
|
|
|
|
if (srv_tid_valid != 0) {
|
|
|
|
fprintf(stderr, "pthread_create() failed to create server thread: %d\n", srv_tid_valid);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
client_tid_valid = pthread_create(&client_tid, NULL, rpc_client_th, NULL);
|
|
|
|
if (client_tid_valid != 0) {
|
|
|
|
fprintf(stderr, "pthread_create(): failed to create client thread: %d\n", client_tid_valid);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (client_tid_valid == 0) {
|
|
|
|
rc = _sem_timedwait(&g_rpc_client_th_done, JOIN_TIMEOUT_S);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "failed to join client thread (rc: %d)\n", rc);
|
|
|
|
err_cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = pthread_join(client_tid, (void **)&th_rc);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "pthread_join() on cliennt thread failed (rc: %d)\n", rc);
|
|
|
|
err_cnt++;
|
|
|
|
} else if (th_rc) {
|
|
|
|
fprintf(stderr, "cliennt thread failed reported failure(thread rc: %d)\n", th_rc);
|
|
|
|
err_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_rpc_server_th_stop = 1;
|
|
|
|
|
|
|
|
if (srv_tid_valid == 0) {
|
|
|
|
rc = _sem_timedwait(&g_rpc_server_th_done, JOIN_TIMEOUT_S);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "server thread failed to exit in %d sec: (rc: %d)\n", JOIN_TIMEOUT_S, rc);
|
|
|
|
err_cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = pthread_join(srv_tid, (void **)&th_rc);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "pthread_join() on cliennt thread failed (rc: %d)\n", rc);
|
|
|
|
err_cnt++;
|
|
|
|
} else if (th_rc) {
|
|
|
|
fprintf(stderr, "cliennt thread failed reported failure(thread rc: %d)\n", th_rc);
|
|
|
|
err_cnt++;
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 11:05:39 +00:00
|
|
|
|
2018-10-22 09:27:09 +00:00
|
|
|
sem_destroy(&g_rpc_server_th_listening);
|
|
|
|
sem_destroy(&g_rpc_server_th_done);
|
|
|
|
sem_destroy(&g_rpc_client_th_done);
|
2018-08-30 11:05:39 +00:00
|
|
|
|
2018-10-22 09:27:09 +00:00
|
|
|
fprintf(stderr, "%s\n", err_cnt == 0 ? "OK" : "FAILED");
|
|
|
|
return err_cnt ? EXIT_FAILURE : 0;
|
2018-08-30 11:05:39 +00:00
|
|
|
}
|