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