]> git.proxmox.com Git - ovs.git/blob - tests/test-jsonrpc.c
Fix ovs-dpctl-top by removing 3 wrong hunks in py3-compat.patch.
[ovs.git] / tests / test-jsonrpc.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 #undef NDEBUG
19 #include "jsonrpc.h"
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "command-line.h"
26 #include "daemon.h"
27 #include "openvswitch/json.h"
28 #include "ovstest.h"
29 #include "openvswitch/poll-loop.h"
30 #include "stream-ssl.h"
31 #include "stream.h"
32 #include "timeval.h"
33 #include "util.h"
34 #include "openvswitch/vlog.h"
35
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);
39
40 static void
41 test_jsonrpc_main(int argc, char *argv[])
42 {
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());
51 }
52
53 static void
54 parse_options(int argc, char *argv[])
55 {
56 enum {
57 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
58 DAEMON_OPTION_ENUMS,
59 SSL_OPTION_ENUMS,
60 };
61 static const struct option long_options[] = {
62 {"verbose", optional_argument, NULL, 'v'},
63 {"help", no_argument, NULL, 'h'},
64 DAEMON_LONG_OPTIONS,
65 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
66 STREAM_SSL_LONG_OPTIONS,
67 {NULL, 0, NULL, 0},
68 };
69 char *short_options = ovs_cmdl_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 STREAM_SSL_OPTION_HANDLERS
88
89 case OPT_BOOTSTRAP_CA_CERT:
90 stream_ssl_set_ca_cert_file(optarg, true);
91 break;
92
93 case '?':
94 exit(EXIT_FAILURE);
95
96 default:
97 abort();
98 }
99 }
100 free(short_options);
101 }
102
103 static void
104 usage(void)
105 {
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);
113 daemon_usage();
114 vlog_usage();
115 printf("\nOther options:\n"
116 " -h, --help display this help message\n");
117 exit(EXIT_SUCCESS);
118 }
119 \f
120 /* Command helper functions. */
121
122 static struct json *
123 parse_json(const char *s)
124 {
125 struct json *json = json_from_string(s);
126 if (json->type == JSON_STRING) {
127 ovs_fatal(0, "\"%s\": %s", s, json->string);
128 }
129 return json;
130 }
131
132 static void
133 print_and_free_json(struct json *json)
134 {
135 char *string = json_to_string(json, JSSF_SORT);
136 json_destroy(json);
137 puts(string);
138 free(string);
139 }
140 \f
141 /* Command implementations. */
142
143 static int
144 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
145 {
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);
150 } else {
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);
155 }
156 jsonrpc_send(rpc, reply);
157 return 0;
158 } else if (msg->type == JSONRPC_NOTIFY) {
159 if (!strcmp(msg->method, "shutdown")) {
160 *done = true;
161 return 0;
162 } else {
163 ovs_error(0, "unknown notification %s", msg->method);
164 return ENOTTY;
165 }
166 } else {
167 ovs_error(0, "unsolicited JSON-RPC reply or error");
168 return EPROTO;
169 }
170 }
171
172 static void
173 do_listen(struct ovs_cmdl_context *ctx)
174 {
175 struct pstream *pstream;
176 struct jsonrpc **rpcs;
177 size_t n_rpcs, allocated_rpcs;
178 bool done;
179 int error;
180
181 error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT);
182 if (error) {
183 ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]);
184 }
185
186 daemonize();
187
188 rpcs = NULL;
189 n_rpcs = allocated_rpcs = 0;
190 done = false;
191 for (;;) {
192 struct stream *stream;
193 size_t i;
194
195 /* Accept new connections. */
196 error = pstream_accept(pstream, &stream);
197 if (!error) {
198 if (n_rpcs >= allocated_rpcs) {
199 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
200 }
201 rpcs[n_rpcs++] = jsonrpc_open(stream);
202 } else if (error != EAGAIN) {
203 ovs_fatal(error, "pstream_accept failed");
204 }
205
206 /* Service existing connections. */
207 for (i = 0; i < n_rpcs; ) {
208 struct jsonrpc *rpc = rpcs[i];
209 struct jsonrpc_msg *msg;
210
211 jsonrpc_run(rpc);
212 if (!jsonrpc_get_backlog(rpc)) {
213 error = jsonrpc_recv(rpc, &msg);
214 if (!error) {
215 error = handle_rpc(rpc, msg, &done);
216 jsonrpc_msg_destroy(msg);
217 } else if (error == EAGAIN) {
218 error = 0;
219 }
220 }
221
222 if (!error) {
223 error = jsonrpc_get_status(rpc);
224 }
225 if (error) {
226 jsonrpc_close(rpc);
227 ovs_error(error, "connection closed");
228 memmove(&rpcs[i], &rpcs[i + 1],
229 (n_rpcs - i - 1) * sizeof *rpcs);
230 n_rpcs--;
231 } else {
232 i++;
233 }
234 }
235
236 /* Wait for something to do. */
237 if (done && !n_rpcs) {
238 break;
239 }
240 pstream_wait(pstream);
241 for (i = 0; i < n_rpcs; i++) {
242 struct jsonrpc *rpc = rpcs[i];
243
244 jsonrpc_wait(rpc);
245 if (!jsonrpc_get_backlog(rpc)) {
246 jsonrpc_recv_wait(rpc);
247 }
248 }
249 poll_block();
250 }
251 free(rpcs);
252 pstream_close(pstream);
253 }
254
255 static void
256 do_request(struct ovs_cmdl_context *ctx)
257 {
258 struct jsonrpc_msg *msg;
259 struct jsonrpc *rpc;
260 struct json *params;
261 struct stream *stream;
262 const char *method;
263 char *string;
264 int error;
265
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);
270 if (string) {
271 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
272 }
273
274 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
275 DSCP_DEFAULT), -1, &stream);
276 if (error) {
277 ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
278 }
279 rpc = jsonrpc_open(stream);
280
281 error = jsonrpc_send(rpc, msg);
282 if (error) {
283 ovs_fatal(error, "could not send request");
284 }
285
286 error = jsonrpc_recv_block(rpc, &msg);
287 if (error) {
288 ovs_fatal(error, "error waiting for reply");
289 }
290 print_and_free_json(jsonrpc_msg_to_json(msg));
291
292 jsonrpc_close(rpc);
293 }
294
295 static void
296 do_notify(struct ovs_cmdl_context *ctx)
297 {
298 struct jsonrpc_msg *msg;
299 struct jsonrpc *rpc;
300 struct json *params;
301 struct stream *stream;
302 const char *method;
303 char *string;
304 int error;
305
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);
310 if (string) {
311 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
312 }
313
314 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
315 DSCP_DEFAULT), -1, &stream);
316 if (error) {
317 ovs_fatal(error, "could not open \"%s\"", ctx->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(struct ovs_cmdl_context *ctx OVS_UNUSED)
330 {
331 usage();
332 }
333
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 },
340 };
341
342 static struct ovs_cmdl_command *
343 get_all_commands(void)
344 {
345 return all_commands;
346 }
347
348 OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);