]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-jsonrpc.c
xenserver: Add support for disabling in-band management via XAPI.
[mirror_ovs.git] / tests / test-jsonrpc.c
1 /*
2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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>
18
19 #include "jsonrpc.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "command-line.h"
28 #include "daemon.h"
29 #include "json.h"
30 #include "poll-loop.h"
31 #include "stream-ssl.h"
32 #include "stream.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 static struct command all_commands[];
38
39 static void usage(void) NO_RETURN;
40 static void parse_options(int argc, char *argv[]);
41
42 int
43 main(int argc, char *argv[])
44 {
45 proctitle_init(argc, argv);
46 set_program_name(argv[0]);
47 parse_options(argc, argv);
48 run_command(argc - optind, argv + optind, all_commands);
49 return 0;
50 }
51
52 static void
53 parse_options(int argc, char *argv[])
54 {
55 enum {
56 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
57 DAEMON_OPTION_ENUMS
58 };
59 static struct option long_options[] = {
60 {"verbose", optional_argument, 0, 'v'},
61 {"help", no_argument, 0, 'h'},
62 DAEMON_LONG_OPTIONS,
63 #ifdef HAVE_OPENSSL
64 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
65 STREAM_SSL_LONG_OPTIONS
66 #endif
67 {0, 0, 0, 0},
68 };
69 char *short_options = long_options_to_short_options(long_options);
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
87 #ifdef HAVE_OPENSSL
88 STREAM_SSL_OPTION_HANDLERS
89
90 case OPT_BOOTSTRAP_CA_CERT:
91 stream_ssl_set_ca_cert_file(optarg, true);
92 break;
93 #endif
94
95 case '?':
96 exit(EXIT_FAILURE);
97
98 default:
99 abort();
100 }
101 }
102 free(short_options);
103 }
104
105 static void
106 usage(void)
107 {
108 printf("%s: JSON-RPC test utility\n"
109 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110 " listen LOCAL listen for connections on LOCAL\n"
111 " request REMOTE METHOD PARAMS send request, print reply\n"
112 " notify REMOTE METHOD PARAMS send notification and exit\n",
113 program_name, program_name);
114 stream_usage("JSON-RPC", true, true, true);
115 daemon_usage();
116 vlog_usage();
117 printf("\nOther options:\n"
118 " -h, --help display this help message\n");
119 exit(EXIT_SUCCESS);
120 }
121 \f
122 /* Command helper functions. */
123
124 static struct json *
125 parse_json(const char *s)
126 {
127 struct json *json = json_from_string(s);
128 if (json->type == JSON_STRING) {
129 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
130 }
131 return json;
132 }
133
134 static void
135 print_and_free_json(struct json *json)
136 {
137 char *string = json_to_string(json, JSSF_SORT);
138 json_destroy(json);
139 puts(string);
140 free(string);
141 }
142 \f
143 /* Command implementations. */
144
145 static void
146 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
147 {
148 struct jsonrpc_msg *reply = NULL;
149 if (msg->type == JSONRPC_REQUEST) {
150 if (!strcmp(msg->method, "echo")) {
151 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
152 } else {
153 struct json *error = json_object_create();
154 json_object_put_string(error, "error", "unknown method");
155 reply = jsonrpc_create_error(error, msg->id);
156 ovs_error(0, "unknown request %s", msg->method);
157 }
158
159 } else if (msg->type == JSONRPC_NOTIFY) {
160 if (!strcmp(msg->method, "shutdown")) {
161 *done = true;
162 } else {
163 jsonrpc_error(rpc, ENOTTY);
164 ovs_error(0, "unknown notification %s", msg->method);
165 }
166 } else {
167 jsonrpc_error(rpc, EPROTO);
168 ovs_error(0, "unsolicited JSON-RPC reply or error");
169 }
170
171 if (reply) {
172 jsonrpc_send(rpc, reply);
173 }
174 }
175
176 static void
177 do_listen(int argc OVS_UNUSED, char *argv[])
178 {
179 struct pstream *pstream;
180 struct jsonrpc **rpcs;
181 size_t n_rpcs, allocated_rpcs;
182 bool done;
183 int error;
184
185 die_if_already_running();
186
187 error = jsonrpc_pstream_open(argv[1], &pstream);
188 if (error) {
189 ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
190 }
191
192 daemonize();
193
194 rpcs = NULL;
195 n_rpcs = allocated_rpcs = 0;
196 done = false;
197 for (;;) {
198 struct stream *stream;
199 size_t i;
200
201 /* Accept new connections. */
202 error = pstream_accept(pstream, &stream);
203 if (!error) {
204 if (n_rpcs >= allocated_rpcs) {
205 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
206 }
207 rpcs[n_rpcs++] = jsonrpc_open(stream);
208 } else if (error != EAGAIN) {
209 ovs_fatal(error, "pstream_accept failed");
210 }
211
212 /* Service existing connections. */
213 for (i = 0; i < n_rpcs; ) {
214 struct jsonrpc *rpc = rpcs[i];
215 struct jsonrpc_msg *msg;
216
217 jsonrpc_run(rpc);
218 if (!jsonrpc_get_backlog(rpc)) {
219 error = jsonrpc_recv(rpc, &msg);
220 if (!error) {
221 handle_rpc(rpc, msg, &done);
222 jsonrpc_msg_destroy(msg);
223 }
224 }
225
226 error = jsonrpc_get_status(rpc);
227 if (error) {
228 jsonrpc_close(rpc);
229 ovs_error(error, "connection closed");
230 memmove(&rpcs[i], &rpcs[i + 1],
231 (n_rpcs - i - 1) * sizeof *rpcs);
232 n_rpcs--;
233 } else {
234 i++;
235 }
236 }
237
238 /* Wait for something to do. */
239 if (done && !n_rpcs) {
240 break;
241 }
242 pstream_wait(pstream);
243 for (i = 0; i < n_rpcs; i++) {
244 struct jsonrpc *rpc = rpcs[i];
245
246 jsonrpc_wait(rpc);
247 if (!jsonrpc_get_backlog(rpc)) {
248 jsonrpc_recv_wait(rpc);
249 }
250 }
251 poll_block();
252 }
253 free(rpcs);
254 pstream_close(pstream);
255 }
256
257 static void
258 do_request(int argc OVS_UNUSED, char *argv[])
259 {
260 struct jsonrpc_msg *msg;
261 struct jsonrpc *rpc;
262 struct json *params;
263 struct stream *stream;
264 const char *method;
265 char *string;
266 int error;
267
268 method = argv[2];
269 params = parse_json(argv[3]);
270 msg = jsonrpc_create_request(method, params, NULL);
271 string = jsonrpc_msg_is_valid(msg);
272 if (string) {
273 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
274 }
275
276 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
277 if (error) {
278 ovs_fatal(error, "could not open \"%s\"", argv[1]);
279 }
280 rpc = jsonrpc_open(stream);
281
282 error = jsonrpc_send(rpc, msg);
283 if (error) {
284 ovs_fatal(error, "could not send request");
285 }
286
287 error = jsonrpc_recv_block(rpc, &msg);
288 if (error) {
289 ovs_fatal(error, "error waiting for reply");
290 }
291 print_and_free_json(jsonrpc_msg_to_json(msg));
292
293 jsonrpc_close(rpc);
294 }
295
296 static void
297 do_notify(int argc OVS_UNUSED, char *argv[])
298 {
299 struct jsonrpc_msg *msg;
300 struct jsonrpc *rpc;
301 struct json *params;
302 struct stream *stream;
303 const char *method;
304 char *string;
305 int error;
306
307 method = argv[2];
308 params = parse_json(argv[3]);
309 msg = jsonrpc_create_notify(method, params);
310 string = jsonrpc_msg_is_valid(msg);
311 if (string) {
312 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
313 }
314
315 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
316 if (error) {
317 ovs_fatal(error, "could not open \"%s\"", argv[1]);
318 }
319 rpc = jsonrpc_open(stream);
320
321 error = jsonrpc_send_block(rpc, msg);
322 if (error) {
323 ovs_fatal(error, "could not send notification");
324 }
325 jsonrpc_close(rpc);
326 }
327
328 static void
329 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
330 {
331 usage();
332 }
333
334 static struct command all_commands[] = {
335 { "listen", 1, 1, do_listen },
336 { "request", 3, 3, do_request },
337 { "notify", 3, 3, do_notify },
338 { "help", 0, INT_MAX, do_help },
339 { NULL, 0, 0, NULL },
340 };