2 * Copyright (c) 2009 Nicira Networks.
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.
27 #include "command-line.h"
30 #include "poll-loop.h"
36 static struct command all_commands[];
38 static void usage(void) NO_RETURN;
39 static void parse_options(int argc, char *argv[]);
42 main(int argc, char *argv[])
44 set_program_name(argv[0]);
47 parse_options(argc, argv);
48 run_command(argc - optind, argv + optind, all_commands);
53 parse_options(int argc, char *argv[])
55 static struct option long_options[] = {
56 {"verbose", optional_argument, 0, 'v'},
57 {"help", no_argument, 0, 'h'},
61 char *short_options = long_options_to_short_options(long_options);
64 int c = getopt_long(argc, argv, short_options, long_options, NULL);
74 vlog_set_verbosity(optarg);
77 DAEMON_OPTION_HANDLERS
92 printf("%s: JSON-RPC test utility\n"
93 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
94 " listen LOCAL listen for connections on LOCAL\n"
95 " request REMOTE METHOD PARAMS send request, print reply\n"
96 " notify REMOTE METHOD PARAMS send notification and exit\n",
97 program_name, program_name);
98 stream_usage("JSON-RPC", true, true);
101 printf("\nOther options:\n"
102 " -h, --help display this help message\n");
106 /* Command helper functions. */
109 parse_json(const char *s)
111 struct json *json = json_from_string(s);
112 if (json->type == JSON_STRING) {
113 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
119 print_and_free_json(struct json *json)
121 char *string = json_to_string(json, JSSF_SORT);
127 /* Command implementations. */
130 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
132 struct jsonrpc_msg *reply = NULL;
133 if (msg->type == JSONRPC_REQUEST) {
134 if (!strcmp(msg->method, "echo")) {
135 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
137 struct json *error = json_object_create();
138 json_object_put_string(error, "error", "unknown method");
139 reply = jsonrpc_create_error(error, msg->id);
140 ovs_error(0, "unknown request %s", msg->method);
143 } else if (msg->type == JSONRPC_NOTIFY) {
144 if (!strcmp(msg->method, "shutdown")) {
147 jsonrpc_error(rpc, ENOTTY);
148 ovs_error(0, "unknown notification %s", msg->method);
151 jsonrpc_error(rpc, EPROTO);
152 ovs_error(0, "unsolicited JSON-RPC reply or error");
156 jsonrpc_send(rpc, reply);
161 do_listen(int argc UNUSED, char *argv[])
163 struct pstream *pstream;
164 struct jsonrpc **rpcs;
165 size_t n_rpcs, allocated_rpcs;
169 die_if_already_running();
171 error = pstream_open(argv[1], &pstream);
173 ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
179 n_rpcs = allocated_rpcs = 0;
182 struct stream *stream;
185 /* Accept new connections. */
186 error = pstream_accept(pstream, &stream);
188 if (n_rpcs >= allocated_rpcs) {
189 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
191 rpcs[n_rpcs++] = jsonrpc_open(stream);
192 } else if (error != EAGAIN) {
193 ovs_fatal(error, "pstream_accept failed");
196 /* Service existing connections. */
197 for (i = 0; i < n_rpcs; ) {
198 struct jsonrpc *rpc = rpcs[i];
199 struct jsonrpc_msg *msg;
202 if (!jsonrpc_get_backlog(rpc)) {
203 error = jsonrpc_recv(rpc, &msg);
205 handle_rpc(rpc, msg, &done);
206 jsonrpc_msg_destroy(msg);
210 error = jsonrpc_get_status(rpc);
213 ovs_error(error, "connection closed");
214 memmove(&rpcs[i], &rpcs[i + 1],
215 (n_rpcs - i - 1) * sizeof *rpcs);
222 /* Wait for something to do. */
223 if (done && !n_rpcs) {
226 pstream_wait(pstream);
227 for (i = 0; i < n_rpcs; i++) {
228 struct jsonrpc *rpc = rpcs[i];
231 if (!jsonrpc_get_backlog(rpc)) {
232 jsonrpc_recv_wait(rpc);
241 do_request(int argc UNUSED, char *argv[])
243 struct jsonrpc_msg *msg;
246 struct stream *stream;
252 params = parse_json(argv[3]);
253 msg = jsonrpc_create_request(method, params, NULL);
254 string = jsonrpc_msg_is_valid(msg);
256 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
259 error = stream_open_block(argv[1], &stream);
261 ovs_fatal(error, "could not open \"%s\"", argv[1]);
263 rpc = jsonrpc_open(stream);
265 error = jsonrpc_send(rpc, msg);
267 ovs_fatal(error, "could not send request");
270 error = jsonrpc_recv_block(rpc, &msg);
272 ovs_fatal(error, "error waiting for reply");
274 print_and_free_json(jsonrpc_msg_to_json(msg));
280 do_notify(int argc UNUSED, char *argv[])
282 struct jsonrpc_msg *msg;
285 struct stream *stream;
291 params = parse_json(argv[3]);
292 msg = jsonrpc_create_notify(method, params);
293 string = jsonrpc_msg_is_valid(msg);
295 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
298 error = stream_open_block(argv[1], &stream);
300 ovs_fatal(error, "could not open \"%s\"", argv[1]);
302 rpc = jsonrpc_open(stream);
304 error = jsonrpc_send_block(rpc, msg);
306 ovs_fatal(error, "could not send request");
312 do_help(int argc UNUSED, char *argv[] UNUSED)
317 static struct command all_commands[] = {
318 { "listen", 1, 1, do_listen },
319 { "request", 3, 3, do_request },
320 { "notify", 3, 3, do_notify },
321 { "help", 0, INT_MAX, do_help },
322 { NULL, 0, 0, NULL },