]>
Commit | Line | Data |
---|---|---|
f2129093 | 1 | /* |
e91b927d | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. |
f2129093 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
3f636c7e | 18 | #undef NDEBUG |
f2129093 | 19 | #include "jsonrpc.h" |
f2129093 BP |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
22 | #include <getopt.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
f2129093 BP |
25 | #include "command-line.h" |
26 | #include "daemon.h" | |
ee89ea7b | 27 | #include "openvswitch/json.h" |
3f636c7e | 28 | #include "ovstest.h" |
f2129093 | 29 | #include "poll-loop.h" |
9467fe62 | 30 | #include "stream-ssl.h" |
f2129093 BP |
31 | #include "stream.h" |
32 | #include "timeval.h" | |
33 | #include "util.h" | |
e6211adc | 34 | #include "openvswitch/vlog.h" |
f2129093 | 35 | |
cab50449 | 36 | OVS_NO_RETURN static void usage(void); |
f2129093 | 37 | static void parse_options(int argc, char *argv[]); |
5f383751 | 38 | static struct ovs_cmdl_command *get_all_commands(void); |
f2129093 | 39 | |
eadd1644 AZ |
40 | static void |
41 | test_jsonrpc_main(int argc, char *argv[]) | |
f2129093 | 42 | { |
1636c761 | 43 | struct ovs_cmdl_context ctx = { .argc = 0, }; |
5f383751 | 44 | ovs_cmdl_proctitle_init(argc, argv); |
f2129093 | 45 | set_program_name(argv[0]); |
dad530c1 | 46 | service_start(&argc, &argv); |
f2129093 | 47 | parse_options(argc, argv); |
1636c761 RB |
48 | ctx.argc = argc - optind; |
49 | ctx.argv = argv + optind; | |
50 | ovs_cmdl_run_command(&ctx, get_all_commands()); | |
f2129093 BP |
51 | } |
52 | ||
53 | static void | |
54 | parse_options(int argc, char *argv[]) | |
55 | { | |
9467fe62 | 56 | enum { |
8274ae95 | 57 | OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1, |
e18a1d08 ER |
58 | DAEMON_OPTION_ENUMS, |
59 | SSL_OPTION_ENUMS, | |
9467fe62 | 60 | }; |
07fc4ed3 | 61 | static const struct option long_options[] = { |
e3c17733 BP |
62 | {"verbose", optional_argument, NULL, 'v'}, |
63 | {"help", no_argument, NULL, 'h'}, | |
f2129093 | 64 | DAEMON_LONG_OPTIONS, |
e3c17733 | 65 | {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT}, |
bf8f2167 | 66 | STREAM_SSL_LONG_OPTIONS, |
e3c17733 | 67 | {NULL, 0, NULL, 0}, |
f2129093 | 68 | }; |
5f383751 | 69 | char *short_options = ovs_cmdl_long_options_to_short_options(long_options); |
f2129093 BP |
70 | |
71 | for (;;) { | |
72 | int c = getopt_long(argc, argv, short_options, long_options, NULL); | |
73 | if (c == -1) { | |
74 | break; | |
75 | } | |
76 | ||
77 | switch (c) { | |
78 | case 'h': | |
79 | usage(); | |
80 | ||
81 | case 'v': | |
82 | vlog_set_verbosity(optarg); | |
83 | break; | |
84 | ||
85 | DAEMON_OPTION_HANDLERS | |
86 | ||
9467fe62 BP |
87 | STREAM_SSL_OPTION_HANDLERS |
88 | ||
89 | case OPT_BOOTSTRAP_CA_CERT: | |
90 | stream_ssl_set_ca_cert_file(optarg, true); | |
91 | break; | |
9467fe62 | 92 | |
f2129093 BP |
93 | case '?': |
94 | exit(EXIT_FAILURE); | |
95 | ||
96 | default: | |
97 | abort(); | |
98 | } | |
99 | } | |
100 | free(short_options); | |
101 | } | |
102 | ||
103 | static void | |
104 | usage(void) | |
105 | { | |
106 | printf("%s: JSON-RPC test utility\n" | |
107 | "usage: %s [OPTIONS] COMMAND [ARG...]\n" | |
108 | " listen LOCAL listen for connections on LOCAL\n" | |
109 | " request REMOTE METHOD PARAMS send request, print reply\n" | |
110 | " notify REMOTE METHOD PARAMS send notification and exit\n", | |
111 | program_name, program_name); | |
9467fe62 | 112 | stream_usage("JSON-RPC", true, true, true); |
f2129093 BP |
113 | daemon_usage(); |
114 | vlog_usage(); | |
115 | printf("\nOther options:\n" | |
116 | " -h, --help display this help message\n"); | |
117 | exit(EXIT_SUCCESS); | |
118 | } | |
119 | \f | |
120 | /* Command helper functions. */ | |
121 | ||
122 | static struct json * | |
123 | parse_json(const char *s) | |
124 | { | |
125 | struct json *json = json_from_string(s); | |
126 | if (json->type == JSON_STRING) { | |
127 | ovs_fatal(0, "\"%s\": %s", s, json->u.string); | |
128 | } | |
129 | return json; | |
130 | } | |
131 | ||
132 | static void | |
133 | print_and_free_json(struct json *json) | |
134 | { | |
135 | char *string = json_to_string(json, JSSF_SORT); | |
136 | json_destroy(json); | |
137 | puts(string); | |
138 | free(string); | |
139 | } | |
140 | \f | |
141 | /* Command implementations. */ | |
142 | ||
c1ce8fbf | 143 | static int |
f2129093 BP |
144 | handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done) |
145 | { | |
f2129093 | 146 | if (msg->type == JSONRPC_REQUEST) { |
c1ce8fbf | 147 | struct jsonrpc_msg *reply = NULL; |
f2129093 BP |
148 | if (!strcmp(msg->method, "echo")) { |
149 | reply = jsonrpc_create_reply(json_clone(msg->params), msg->id); | |
150 | } else { | |
151 | struct json *error = json_object_create(); | |
152 | json_object_put_string(error, "error", "unknown method"); | |
153 | reply = jsonrpc_create_error(error, msg->id); | |
154 | ovs_error(0, "unknown request %s", msg->method); | |
155 | } | |
c1ce8fbf BP |
156 | jsonrpc_send(rpc, reply); |
157 | return 0; | |
f2129093 BP |
158 | } else if (msg->type == JSONRPC_NOTIFY) { |
159 | if (!strcmp(msg->method, "shutdown")) { | |
160 | *done = true; | |
c1ce8fbf | 161 | return 0; |
f2129093 | 162 | } else { |
f2129093 | 163 | ovs_error(0, "unknown notification %s", msg->method); |
c1ce8fbf | 164 | return ENOTTY; |
f2129093 BP |
165 | } |
166 | } else { | |
f2129093 | 167 | ovs_error(0, "unsolicited JSON-RPC reply or error"); |
c1ce8fbf | 168 | return EPROTO; |
f2129093 BP |
169 | } |
170 | } | |
171 | ||
172 | static void | |
1636c761 | 173 | do_listen(struct ovs_cmdl_context *ctx) |
f2129093 BP |
174 | { |
175 | struct pstream *pstream; | |
176 | struct jsonrpc **rpcs; | |
177 | size_t n_rpcs, allocated_rpcs; | |
178 | bool done; | |
179 | int error; | |
180 | ||
1636c761 | 181 | error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT); |
f2129093 | 182 | if (error) { |
1636c761 | 183 | ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]); |
f2129093 BP |
184 | } |
185 | ||
186 | daemonize(); | |
187 | ||
188 | rpcs = NULL; | |
189 | n_rpcs = allocated_rpcs = 0; | |
190 | done = false; | |
191 | for (;;) { | |
192 | struct stream *stream; | |
193 | size_t i; | |
194 | ||
195 | /* Accept new connections. */ | |
196 | error = pstream_accept(pstream, &stream); | |
197 | if (!error) { | |
198 | if (n_rpcs >= allocated_rpcs) { | |
199 | rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs); | |
200 | } | |
201 | rpcs[n_rpcs++] = jsonrpc_open(stream); | |
202 | } else if (error != EAGAIN) { | |
203 | ovs_fatal(error, "pstream_accept failed"); | |
204 | } | |
205 | ||
206 | /* Service existing connections. */ | |
207 | for (i = 0; i < n_rpcs; ) { | |
208 | struct jsonrpc *rpc = rpcs[i]; | |
209 | struct jsonrpc_msg *msg; | |
210 | ||
211 | jsonrpc_run(rpc); | |
212 | if (!jsonrpc_get_backlog(rpc)) { | |
213 | error = jsonrpc_recv(rpc, &msg); | |
214 | if (!error) { | |
c1ce8fbf | 215 | error = handle_rpc(rpc, msg, &done); |
f2129093 | 216 | jsonrpc_msg_destroy(msg); |
c1ce8fbf BP |
217 | } else if (error == EAGAIN) { |
218 | error = 0; | |
f2129093 BP |
219 | } |
220 | } | |
221 | ||
c1ce8fbf BP |
222 | if (!error) { |
223 | error = jsonrpc_get_status(rpc); | |
224 | } | |
f2129093 BP |
225 | if (error) { |
226 | jsonrpc_close(rpc); | |
227 | ovs_error(error, "connection closed"); | |
228 | memmove(&rpcs[i], &rpcs[i + 1], | |
229 | (n_rpcs - i - 1) * sizeof *rpcs); | |
230 | n_rpcs--; | |
231 | } else { | |
232 | i++; | |
233 | } | |
234 | } | |
235 | ||
236 | /* Wait for something to do. */ | |
237 | if (done && !n_rpcs) { | |
238 | break; | |
239 | } | |
240 | pstream_wait(pstream); | |
241 | for (i = 0; i < n_rpcs; i++) { | |
242 | struct jsonrpc *rpc = rpcs[i]; | |
243 | ||
244 | jsonrpc_wait(rpc); | |
245 | if (!jsonrpc_get_backlog(rpc)) { | |
246 | jsonrpc_recv_wait(rpc); | |
247 | } | |
248 | } | |
249 | poll_block(); | |
250 | } | |
93ff0290 BP |
251 | free(rpcs); |
252 | pstream_close(pstream); | |
f2129093 BP |
253 | } |
254 | ||
f2129093 | 255 | static void |
1636c761 | 256 | do_request(struct ovs_cmdl_context *ctx) |
f2129093 BP |
257 | { |
258 | struct jsonrpc_msg *msg; | |
259 | struct jsonrpc *rpc; | |
260 | struct json *params; | |
261 | struct stream *stream; | |
262 | const char *method; | |
263 | char *string; | |
264 | int error; | |
265 | ||
1636c761 RB |
266 | method = ctx->argv[2]; |
267 | params = parse_json(ctx->argv[3]); | |
20bed8be | 268 | msg = jsonrpc_create_request(method, params, NULL); |
f2129093 BP |
269 | string = jsonrpc_msg_is_valid(msg); |
270 | if (string) { | |
271 | ovs_fatal(0, "not a valid JSON-RPC request: %s", string); | |
272 | } | |
273 | ||
1636c761 | 274 | error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, |
f125905c | 275 | DSCP_DEFAULT), &stream); |
f2129093 | 276 | if (error) { |
1636c761 | 277 | ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); |
f2129093 BP |
278 | } |
279 | rpc = jsonrpc_open(stream); | |
280 | ||
281 | error = jsonrpc_send(rpc, msg); | |
282 | if (error) { | |
283 | ovs_fatal(error, "could not send request"); | |
284 | } | |
285 | ||
286 | error = jsonrpc_recv_block(rpc, &msg); | |
287 | if (error) { | |
288 | ovs_fatal(error, "error waiting for reply"); | |
289 | } | |
290 | print_and_free_json(jsonrpc_msg_to_json(msg)); | |
291 | ||
292 | jsonrpc_close(rpc); | |
293 | } | |
294 | ||
295 | static void | |
1636c761 | 296 | do_notify(struct ovs_cmdl_context *ctx) |
f2129093 BP |
297 | { |
298 | struct jsonrpc_msg *msg; | |
299 | struct jsonrpc *rpc; | |
300 | struct json *params; | |
301 | struct stream *stream; | |
302 | const char *method; | |
303 | char *string; | |
304 | int error; | |
305 | ||
1636c761 RB |
306 | method = ctx->argv[2]; |
307 | params = parse_json(ctx->argv[3]); | |
f2129093 BP |
308 | msg = jsonrpc_create_notify(method, params); |
309 | string = jsonrpc_msg_is_valid(msg); | |
310 | if (string) { | |
311 | ovs_fatal(0, "not a JSON RPC-valid notification: %s", string); | |
312 | } | |
313 | ||
1636c761 | 314 | error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, |
f125905c | 315 | DSCP_DEFAULT), &stream); |
f2129093 | 316 | if (error) { |
1636c761 | 317 | ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); |
f2129093 BP |
318 | } |
319 | rpc = jsonrpc_open(stream); | |
320 | ||
321 | error = jsonrpc_send_block(rpc, msg); | |
322 | if (error) { | |
99155935 | 323 | ovs_fatal(error, "could not send notification"); |
f2129093 BP |
324 | } |
325 | jsonrpc_close(rpc); | |
326 | } | |
327 | ||
328 | static void | |
1636c761 | 329 | do_help(struct ovs_cmdl_context *ctx OVS_UNUSED) |
f2129093 BP |
330 | { |
331 | usage(); | |
332 | } | |
333 | ||
5f383751 | 334 | static struct ovs_cmdl_command all_commands[] = { |
1f4a7252 RM |
335 | { "listen", NULL, 1, 1, do_listen, OVS_RO }, |
336 | { "request", NULL, 3, 3, do_request, OVS_RO }, | |
337 | { "notify", NULL, 3, 3, do_notify, OVS_RO }, | |
338 | { "help", NULL, 0, INT_MAX, do_help, OVS_RO }, | |
339 | { NULL, NULL, 0, 0, NULL, OVS_RO }, | |
f2129093 | 340 | }; |
eadd1644 | 341 | |
5f383751 | 342 | static struct ovs_cmdl_command * |
d2586fce GS |
343 | get_all_commands(void) |
344 | { | |
345 | return all_commands; | |
346 | } | |
347 | ||
eadd1644 | 348 | OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main); |