2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "command-line.h"
29 #include "poll-loop.h"
30 #include "stream-ssl.h"
34 #include "openvswitch/vlog.h"
36 OVS_NO_RETURN static void usage(void);
37 static void parse_options(int argc, char *argv[]);
38 static struct ovs_cmdl_command *get_all_commands(void);
41 test_jsonrpc_main(int argc, char *argv[])
43 struct ovs_cmdl_context ctx = { .argc = 0, };
44 ovs_cmdl_proctitle_init(argc, argv);
45 set_program_name(argv[0]);
46 service_start(&argc, &argv);
47 parse_options(argc, argv);
48 ctx.argc = argc - optind;
49 ctx.argv = argv + optind;
50 ovs_cmdl_run_command(&ctx, get_all_commands());
54 parse_options(int argc, char *argv[])
57 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
60 static const struct option long_options[] = {
61 {"verbose", optional_argument, NULL, 'v'},
62 {"help", no_argument, NULL, 'h'},
64 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
65 STREAM_SSL_LONG_OPTIONS,
68 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
71 int c = getopt_long(argc, argv, short_options, long_options, NULL);
81 vlog_set_verbosity(optarg);
84 DAEMON_OPTION_HANDLERS
86 STREAM_SSL_OPTION_HANDLERS
88 case OPT_BOOTSTRAP_CA_CERT:
89 stream_ssl_set_ca_cert_file(optarg, true);
105 printf("%s: JSON-RPC test utility\n"
106 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
107 " listen LOCAL listen for connections on LOCAL\n"
108 " request REMOTE METHOD PARAMS send request, print reply\n"
109 " notify REMOTE METHOD PARAMS send notification and exit\n",
110 program_name, program_name);
111 stream_usage("JSON-RPC", true, true, true);
114 printf("\nOther options:\n"
115 " -h, --help display this help message\n");
119 /* Command helper functions. */
122 parse_json(const char *s)
124 struct json *json = json_from_string(s);
125 if (json->type == JSON_STRING) {
126 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
132 print_and_free_json(struct json *json)
134 char *string = json_to_string(json, JSSF_SORT);
140 /* Command implementations. */
143 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
145 if (msg->type == JSONRPC_REQUEST) {
146 struct jsonrpc_msg *reply = NULL;
147 if (!strcmp(msg->method, "echo")) {
148 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
150 struct json *error = json_object_create();
151 json_object_put_string(error, "error", "unknown method");
152 reply = jsonrpc_create_error(error, msg->id);
153 ovs_error(0, "unknown request %s", msg->method);
155 jsonrpc_send(rpc, reply);
157 } else if (msg->type == JSONRPC_NOTIFY) {
158 if (!strcmp(msg->method, "shutdown")) {
162 ovs_error(0, "unknown notification %s", msg->method);
166 ovs_error(0, "unsolicited JSON-RPC reply or error");
172 do_listen(struct ovs_cmdl_context *ctx)
174 struct pstream *pstream;
175 struct jsonrpc **rpcs;
176 size_t n_rpcs, allocated_rpcs;
180 error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT);
182 ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]);
188 n_rpcs = allocated_rpcs = 0;
191 struct stream *stream;
194 /* Accept new connections. */
195 error = pstream_accept(pstream, &stream);
197 if (n_rpcs >= allocated_rpcs) {
198 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
200 rpcs[n_rpcs++] = jsonrpc_open(stream);
201 } else if (error != EAGAIN) {
202 ovs_fatal(error, "pstream_accept failed");
205 /* Service existing connections. */
206 for (i = 0; i < n_rpcs; ) {
207 struct jsonrpc *rpc = rpcs[i];
208 struct jsonrpc_msg *msg;
211 if (!jsonrpc_get_backlog(rpc)) {
212 error = jsonrpc_recv(rpc, &msg);
214 error = handle_rpc(rpc, msg, &done);
215 jsonrpc_msg_destroy(msg);
216 } else if (error == EAGAIN) {
222 error = jsonrpc_get_status(rpc);
226 ovs_error(error, "connection closed");
227 memmove(&rpcs[i], &rpcs[i + 1],
228 (n_rpcs - i - 1) * sizeof *rpcs);
235 /* Wait for something to do. */
236 if (done && !n_rpcs) {
239 pstream_wait(pstream);
240 for (i = 0; i < n_rpcs; i++) {
241 struct jsonrpc *rpc = rpcs[i];
244 if (!jsonrpc_get_backlog(rpc)) {
245 jsonrpc_recv_wait(rpc);
251 pstream_close(pstream);
255 do_request(struct ovs_cmdl_context *ctx)
257 struct jsonrpc_msg *msg;
260 struct stream *stream;
265 method = ctx->argv[2];
266 params = parse_json(ctx->argv[3]);
267 msg = jsonrpc_create_request(method, params, NULL);
268 string = jsonrpc_msg_is_valid(msg);
270 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
273 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
274 DSCP_DEFAULT), &stream);
276 ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
278 rpc = jsonrpc_open(stream);
280 error = jsonrpc_send(rpc, msg);
282 ovs_fatal(error, "could not send request");
285 error = jsonrpc_recv_block(rpc, &msg);
287 ovs_fatal(error, "error waiting for reply");
289 print_and_free_json(jsonrpc_msg_to_json(msg));
295 do_notify(struct ovs_cmdl_context *ctx)
297 struct jsonrpc_msg *msg;
300 struct stream *stream;
305 method = ctx->argv[2];
306 params = parse_json(ctx->argv[3]);
307 msg = jsonrpc_create_notify(method, params);
308 string = jsonrpc_msg_is_valid(msg);
310 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
313 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
314 DSCP_DEFAULT), &stream);
316 ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
318 rpc = jsonrpc_open(stream);
320 error = jsonrpc_send_block(rpc, msg);
322 ovs_fatal(error, "could not send notification");
328 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
333 static struct ovs_cmdl_command all_commands[] = {
334 { "listen", NULL, 1, 1, do_listen },
335 { "request", NULL, 3, 3, do_request },
336 { "notify", NULL, 3, 3, do_notify },
337 { "help", NULL, 0, INT_MAX, do_help },
338 { NULL, NULL, 0, 0, NULL },
341 static struct ovs_cmdl_command *
342 get_all_commands(void)
347 OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);