xref: /spdk/include/spdk/rpc.h (revision da50ac950bff84f027c45af1799a41f0e1440103)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #ifndef SPDK_RPC_CONFIG_H_
8 #define SPDK_RPC_CONFIG_H_
9 
10 #include "spdk/stdinc.h"
11 
12 #include "spdk/jsonrpc.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct spdk_rpc_server;
19 
20 /**
21  * Verify correctness of registered RPC methods and aliases.
22  *
23  * Incorrect registrations include:
24  * - multiple RPC methods registered with the same name
25  * - RPC alias registered with a method that does not exist
26  * - RPC alias registered that points to another alias
27  *
28  * \return true if registrations are all correct, false otherwise
29  */
30 bool spdk_rpc_verify_methods(void);
31 
32 /**
33  * Start listening for RPC connections.
34  *
35  * Deprecated, will be removed in 24.09 release.
36  *
37  * \param listen_addr Listening address.
38  *
39  * \return 0 on success, -1 on failure.
40  */
41 int spdk_rpc_listen(const char *listen_addr);
42 
43 /**
44  * Poll the RPC server.
45  *
46  * Deprecated, will be removed in 24.09 release.
47  */
48 void spdk_rpc_accept(void);
49 
50 /**
51  * Stop listening for RPC connections.
52  *
53  * Deprecated, will be removed in 24.09 release.
54  */
55 void spdk_rpc_close(void);
56 
57 /**
58  * Start listening for RPC connections on given address.
59  *
60  * \param listen_addr Listening address.
61  *
62  * \return new RPC server or NULL on failure.
63  */
64 struct spdk_rpc_server *spdk_rpc_server_listen(const char *listen_addr);
65 
66 /**
67  * Poll the RPC server.
68  *
69  * \param server RPC server, which will be polled for connections.
70  */
71 void spdk_rpc_server_accept(struct spdk_rpc_server *server);
72 
73 /**
74  * Stop a server from listening and free it.
75  *
76  * \param server RPC server, which will be stopped and be freed.
77  */
78 void spdk_rpc_server_close(struct spdk_rpc_server *server);
79 
80 /**
81  * Function signature for RPC request handlers.
82  *
83  * \param request RPC request to handle.
84  * \param params Parameters associated with the RPC request.
85  */
86 typedef void (*spdk_rpc_method_handler)(struct spdk_jsonrpc_request *request,
87 					const struct spdk_json_val *params);
88 
89 /**
90  * Register an RPC method.
91  *
92  * \param method Name for the registered method.
93  * \param func Function registered for this method to handle the RPC request.
94  * \param state_mask State mask of the registered method. If the bit of the state of
95  * the RPC server is set in the state_mask, the method is allowed. Otherwise, it is rejected.
96  */
97 void spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func,
98 			      uint32_t state_mask);
99 
100 /**
101  * Register a deprecated alias for an RPC method.
102  *
103  * \param method Name for the registered method.
104  * \param alias Alias for the registered method.
105  */
106 void spdk_rpc_register_alias_deprecated(const char *method, const char *alias);
107 
108 /**
109  * Check if \c method is allowed for \c state_mask
110  *
111  * \param method Method name
112  * \param state_mask state mask to check against
113  * \return 0 if method is allowed or negative error code:
114  * -EPERM method is not allowed
115  * -ENOENT method not found
116  */
117 int spdk_rpc_is_method_allowed(const char *method, uint32_t state_mask);
118 
119 /**
120  * Return state mask of the method
121  *
122  * \param method Method name
123  * \param[out] state_mask State mask of the method
124  * \retval 0 if method is found and \b state_mask is filled
125  * \retval -ENOENT if method is not found
126  */
127 int spdk_rpc_get_method_state_mask(const char *method, uint32_t *state_mask);
128 
129 #define SPDK_RPC_STARTUP	0x1
130 #define SPDK_RPC_RUNTIME	0x2
131 
132 /* Give SPDK_RPC_REGISTER a higher execution priority than
133  * SPDK_RPC_REGISTER_ALIAS_DEPRECATED to ensure all of the RPCs are registered
134  * before we try registering any aliases.  Some older versions of clang may
135  * otherwise execute the constructors in a different order than
136  * defined in the source file (see issue #892).
137  */
138 #define SPDK_RPC_REGISTER(method, func, state_mask) \
139 static void __attribute__((constructor(1000))) rpc_register_##func(void) \
140 { \
141 	spdk_rpc_register_method(method, func, state_mask); \
142 }
143 
144 #define SPDK_RPC_REGISTER_ALIAS_DEPRECATED(method, alias) \
145 static void __attribute__((constructor(1001))) rpc_register_##alias(void) \
146 { \
147 	spdk_rpc_register_alias_deprecated(#method, #alias); \
148 }
149 
150 /**
151  * Set the state mask of the RPC server. Any RPC method whose state mask is
152  * equal to the state of the RPC server is allowed.
153  *
154  * \param state_mask New state mask of the RPC server.
155  */
156 void spdk_rpc_set_state(uint32_t state_mask);
157 
158 /**
159  * Get the current state of the RPC server.
160  *
161  * \return The current state of the RPC server.
162  */
163 uint32_t spdk_rpc_get_state(void);
164 
165 /*
166  * Mark only the given RPC methods as allowed.
167  *
168  * \param rpc_allowlist string array of method names, terminated with a NULL.
169  */
170 void spdk_rpc_set_allowlist(const char **rpc_allowlist);
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif
177