2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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"
27 #include "openvswitch/json.h"
29 #include "openvswitch/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,
61 static const struct option long_options
[] = {
62 {"verbose", optional_argument
, NULL
, 'v'},
63 {"help", no_argument
, NULL
, 'h'},
65 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
66 STREAM_SSL_LONG_OPTIONS
,
69 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
72 int c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
82 vlog_set_verbosity(optarg
);
85 DAEMON_OPTION_HANDLERS
87 STREAM_SSL_OPTION_HANDLERS
89 case OPT_BOOTSTRAP_CA_CERT
:
90 stream_ssl_set_ca_cert_file(optarg
, true);
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
);
112 stream_usage("JSON-RPC", true, true, true);
115 printf("\nOther options:\n"
116 " -h, --help display this help message\n");
120 /* Command helper functions. */
123 parse_json(const char *s
)
125 struct json
*json
= json_from_string(s
);
126 if (json
->type
== JSON_STRING
) {
127 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
133 print_and_free_json(struct json
*json
)
135 char *string
= json_to_string(json
, JSSF_SORT
);
141 /* Command implementations. */
144 handle_rpc(struct jsonrpc
*rpc
, struct jsonrpc_msg
*msg
, bool *done
)
146 if (msg
->type
== JSONRPC_REQUEST
) {
147 struct jsonrpc_msg
*reply
= NULL
;
148 if (!strcmp(msg
->method
, "echo")) {
149 reply
= jsonrpc_create_reply(json_clone(msg
->params
), msg
->id
);
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
);
156 jsonrpc_send(rpc
, reply
);
158 } else if (msg
->type
== JSONRPC_NOTIFY
) {
159 if (!strcmp(msg
->method
, "shutdown")) {
163 ovs_error(0, "unknown notification %s", msg
->method
);
167 ovs_error(0, "unsolicited JSON-RPC reply or error");
173 do_listen(struct ovs_cmdl_context
*ctx
)
175 struct pstream
*pstream
;
176 struct jsonrpc
**rpcs
;
177 size_t n_rpcs
, allocated_rpcs
;
181 error
= jsonrpc_pstream_open(ctx
->argv
[1], &pstream
, DSCP_DEFAULT
);
183 ovs_fatal(error
, "could not listen on \"%s\"", ctx
->argv
[1]);
189 n_rpcs
= allocated_rpcs
= 0;
192 struct stream
*stream
;
195 /* Accept new connections. */
196 error
= pstream_accept(pstream
, &stream
);
198 if (n_rpcs
>= allocated_rpcs
) {
199 rpcs
= x2nrealloc(rpcs
, &allocated_rpcs
, sizeof *rpcs
);
201 rpcs
[n_rpcs
++] = jsonrpc_open(stream
);
202 } else if (error
!= EAGAIN
) {
203 ovs_fatal(error
, "pstream_accept failed");
206 /* Service existing connections. */
207 for (i
= 0; i
< n_rpcs
; ) {
208 struct jsonrpc
*rpc
= rpcs
[i
];
209 struct jsonrpc_msg
*msg
;
212 if (!jsonrpc_get_backlog(rpc
)) {
213 error
= jsonrpc_recv(rpc
, &msg
);
215 error
= handle_rpc(rpc
, msg
, &done
);
216 jsonrpc_msg_destroy(msg
);
217 } else if (error
== EAGAIN
) {
223 error
= jsonrpc_get_status(rpc
);
227 ovs_error(error
, "connection closed");
228 memmove(&rpcs
[i
], &rpcs
[i
+ 1],
229 (n_rpcs
- i
- 1) * sizeof *rpcs
);
236 /* Wait for something to do. */
237 if (done
&& !n_rpcs
) {
240 pstream_wait(pstream
);
241 for (i
= 0; i
< n_rpcs
; i
++) {
242 struct jsonrpc
*rpc
= rpcs
[i
];
245 if (!jsonrpc_get_backlog(rpc
)) {
246 jsonrpc_recv_wait(rpc
);
252 pstream_close(pstream
);
256 do_request(struct ovs_cmdl_context
*ctx
)
258 struct jsonrpc_msg
*msg
;
261 struct stream
*stream
;
266 method
= ctx
->argv
[2];
267 params
= parse_json(ctx
->argv
[3]);
268 msg
= jsonrpc_create_request(method
, params
, NULL
);
269 string
= jsonrpc_msg_is_valid(msg
);
271 ovs_fatal(0, "not a valid JSON-RPC request: %s", string
);
274 error
= stream_open_block(jsonrpc_stream_open(ctx
->argv
[1], &stream
,
275 DSCP_DEFAULT
), &stream
);
277 ovs_fatal(error
, "could not open \"%s\"", ctx
->argv
[1]);
279 rpc
= jsonrpc_open(stream
);
281 error
= jsonrpc_send(rpc
, msg
);
283 ovs_fatal(error
, "could not send request");
286 error
= jsonrpc_recv_block(rpc
, &msg
);
288 ovs_fatal(error
, "error waiting for reply");
290 print_and_free_json(jsonrpc_msg_to_json(msg
));
296 do_notify(struct ovs_cmdl_context
*ctx
)
298 struct jsonrpc_msg
*msg
;
301 struct stream
*stream
;
306 method
= ctx
->argv
[2];
307 params
= parse_json(ctx
->argv
[3]);
308 msg
= jsonrpc_create_notify(method
, params
);
309 string
= jsonrpc_msg_is_valid(msg
);
311 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string
);
314 error
= stream_open_block(jsonrpc_stream_open(ctx
->argv
[1], &stream
,
315 DSCP_DEFAULT
), &stream
);
317 ovs_fatal(error
, "could not open \"%s\"", ctx
->argv
[1]);
319 rpc
= jsonrpc_open(stream
);
321 error
= jsonrpc_send_block(rpc
, msg
);
323 ovs_fatal(error
, "could not send notification");
329 do_help(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
334 static struct ovs_cmdl_command all_commands
[] = {
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
},
342 static struct ovs_cmdl_command
*
343 get_all_commands(void)
348 OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main
);