xref: /spdk/lib/init/rpc.c (revision 4d505568327cd1a2c2bf2bc36f497bc87129e401)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation. All rights reserved.
3  *   Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/env.h"
9 #include "spdk/init.h"
10 #include "spdk/thread.h"
11 #include "spdk/log.h"
12 #include "spdk/rpc.h"
13 
14 #define RPC_SELECT_INTERVAL	4000 /* 4ms */
15 
16 static struct spdk_poller *g_rpc_poller = NULL;
17 
18 static int
19 rpc_subsystem_poll(void *arg)
20 {
21 	spdk_rpc_accept();
22 	return SPDK_POLLER_BUSY;
23 }
24 
25 static void
26 rpc_opts_copy(struct spdk_rpc_opts *opts, const struct spdk_rpc_opts *opts_src,
27 	      size_t size)
28 {
29 	assert(opts);
30 	assert(opts_src);
31 
32 	opts->size = size;
33 
34 #define SET_FIELD(field) \
35 	if (offsetof(struct spdk_rpc_opts, field) + sizeof(opts->field) <= size) { \
36 		opts->field = opts_src->field; \
37 	} \
38 
39 	SET_FIELD(log_file);
40 	SET_FIELD(log_level);
41 
42 	/* Do not remove this statement, you should always update this statement when you adding a new field,
43 	 * and do not forget to add the SET_FIELD statement for your added field. */
44 	SPDK_STATIC_ASSERT(sizeof(struct spdk_rpc_opts) == 24, "Incorrect size");
45 
46 #undef SET_FIELD
47 }
48 
49 static void
50 rpc_opts_get_default(struct spdk_rpc_opts *opts, size_t size)
51 {
52 	assert(opts);
53 
54 	opts->size = size;
55 
56 #define SET_FIELD(field, value) \
57 	if (offsetof(struct spdk_rpc_opts, field) + sizeof(opts->field) <= size) { \
58 		opts->field = value; \
59 	} \
60 
61 	SET_FIELD(log_file, NULL);
62 	SET_FIELD(log_level, SPDK_LOG_DISABLED);
63 
64 #undef SET_FIELD
65 }
66 
67 static int
68 rpc_verify_opts_and_methods(const struct spdk_rpc_opts *opts)
69 {
70 	if (!spdk_rpc_verify_methods()) {
71 		return -EINVAL;
72 	}
73 
74 	if (opts != NULL && opts->size == 0) {
75 		SPDK_ERRLOG("size in the options structure should not be zero\n");
76 		return -EINVAL;
77 	}
78 
79 	return 0;
80 }
81 
82 static void
83 rpc_set_spdk_log_opts(const struct spdk_rpc_opts *_opts)
84 {
85 	struct spdk_rpc_opts opts;
86 
87 	rpc_opts_get_default(&opts, sizeof(opts));
88 	if (_opts != NULL) {
89 		rpc_opts_copy(&opts, _opts, _opts->size);
90 	}
91 
92 	spdk_jsonrpc_set_log_file(opts.log_file);
93 	spdk_jsonrpc_set_log_level(opts.log_level);
94 }
95 
96 int
97 spdk_rpc_initialize(const char *listen_addr, const struct spdk_rpc_opts *opts)
98 {
99 	int rc;
100 
101 	if (listen_addr == NULL) {
102 		/* Not treated as an error */
103 		return 0;
104 	}
105 
106 	rc = rpc_verify_opts_and_methods(opts);
107 	if (rc) {
108 		return rc;
109 	}
110 
111 	/* Listen on the requested address */
112 	rc = spdk_rpc_listen(listen_addr);
113 	if (rc != 0) {
114 		SPDK_ERRLOG("Unable to start RPC service at %s\n", listen_addr);
115 		/* TODO: Eventually, treat this as an error. But it historically has not
116 		 * been and many tests rely on this gracefully failing. */
117 		return 0;
118 	}
119 
120 	rpc_set_spdk_log_opts(opts);
121 
122 	/* Register a poller to periodically check for RPCs */
123 	g_rpc_poller = SPDK_POLLER_REGISTER(rpc_subsystem_poll, NULL, RPC_SELECT_INTERVAL);
124 
125 	return 0;
126 }
127 
128 void
129 spdk_rpc_finish(void)
130 {
131 	spdk_rpc_close();
132 	spdk_poller_unregister(&g_rpc_poller);
133 }
134