meta-flow: Correctly set destination MAC in mf_set_flow_value().
[sliver-openvswitch.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, NULL, 'v'},
61         {"help", no_argument, NULL, 'h'},
62         DAEMON_LONG_OPTIONS,
63         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
64         STREAM_SSL_LONG_OPTIONS,
65         {NULL, 0, NULL, 0},
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
85         STREAM_SSL_OPTION_HANDLERS
86
87         case OPT_BOOTSTRAP_CA_CERT:
88             stream_ssl_set_ca_cert_file(optarg, true);
89             break;
90
91         case '?':
92             exit(EXIT_FAILURE);
93
94         default:
95             abort();
96         }
97     }
98     free(short_options);
99 }
100
101 static void
102 usage(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);
110     stream_usage("JSON-RPC", true, true, true);
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
120 static struct json *
121 parse_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
130 static void
131 print_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
141 static void
142 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
143 {
144     struct jsonrpc_msg *reply = NULL;
145     if (msg->type == JSONRPC_REQUEST) {
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         }
154
155     } else if (msg->type == JSONRPC_NOTIFY) {
156         if (!strcmp(msg->method, "shutdown")) {
157             *done = true;
158         } else {
159             jsonrpc_error(rpc, ENOTTY);
160             ovs_error(0, "unknown notification %s", msg->method);
161         }
162     } else {
163         jsonrpc_error(rpc, EPROTO);
164         ovs_error(0, "unsolicited JSON-RPC reply or error");
165     }
166
167     if (reply) {
168         jsonrpc_send(rpc, reply);
169     }
170 }
171
172 static void
173 do_listen(int argc OVS_UNUSED, char *argv[])
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(argv[1], &pstream);
182     if (error) {
183         ovs_fatal(error, "could not listen on \"%s\"", 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                     handle_rpc(rpc, msg, &done);
216                     jsonrpc_msg_destroy(msg);
217                 }
218             }
219
220             error = jsonrpc_get_status(rpc);
221             if (error) {
222                 jsonrpc_close(rpc);
223                 ovs_error(error, "connection closed");
224                 memmove(&rpcs[i], &rpcs[i + 1],
225                         (n_rpcs - i - 1) * sizeof *rpcs);
226                 n_rpcs--;
227             } else {
228                 i++;
229             }
230         }
231
232         /* Wait for something to do. */
233         if (done && !n_rpcs) {
234             break;
235         }
236         pstream_wait(pstream);
237         for (i = 0; i < n_rpcs; i++) {
238             struct jsonrpc *rpc = rpcs[i];
239
240             jsonrpc_wait(rpc);
241             if (!jsonrpc_get_backlog(rpc)) {
242                 jsonrpc_recv_wait(rpc);
243             }
244         }
245         poll_block();
246     }
247     free(rpcs);
248     pstream_close(pstream);
249 }
250
251 static void
252 do_request(int argc OVS_UNUSED, char *argv[])
253 {
254     struct jsonrpc_msg *msg;
255     struct jsonrpc *rpc;
256     struct json *params;
257     struct stream *stream;
258     const char *method;
259     char *string;
260     int error;
261
262     method = argv[2];
263     params = parse_json(argv[3]);
264     msg = jsonrpc_create_request(method, params, NULL);
265     string = jsonrpc_msg_is_valid(msg);
266     if (string) {
267         ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
268     }
269
270     error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
271     if (error) {
272         ovs_fatal(error, "could not open \"%s\"", argv[1]);
273     }
274     rpc = jsonrpc_open(stream);
275
276     error = jsonrpc_send(rpc, msg);
277     if (error) {
278         ovs_fatal(error, "could not send request");
279     }
280
281     error = jsonrpc_recv_block(rpc, &msg);
282     if (error) {
283         ovs_fatal(error, "error waiting for reply");
284     }
285     print_and_free_json(jsonrpc_msg_to_json(msg));
286
287     jsonrpc_close(rpc);
288 }
289
290 static void
291 do_notify(int argc OVS_UNUSED, char *argv[])
292 {
293     struct jsonrpc_msg *msg;
294     struct jsonrpc *rpc;
295     struct json *params;
296     struct stream *stream;
297     const char *method;
298     char *string;
299     int error;
300
301     method = argv[2];
302     params = parse_json(argv[3]);
303     msg = jsonrpc_create_notify(method, params);
304     string = jsonrpc_msg_is_valid(msg);
305     if (string) {
306         ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
307     }
308
309     error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
310     if (error) {
311         ovs_fatal(error, "could not open \"%s\"", argv[1]);
312     }
313     rpc = jsonrpc_open(stream);
314
315     error = jsonrpc_send_block(rpc, msg);
316     if (error) {
317         ovs_fatal(error, "could not send notification");
318     }
319     jsonrpc_close(rpc);
320 }
321
322 static void
323 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
324 {
325     usage();
326 }
327
328 static struct command all_commands[] = {
329     { "listen", 1, 1, do_listen },
330     { "request", 3, 3, do_request },
331     { "notify", 3, 3, do_notify },
332     { "help", 0, INT_MAX, do_help },
333     { NULL, 0, 0, NULL },
334 };