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