2009-10-26 15:04:05 -07:00
|
|
|
|
/*
|
2014-04-01 00:47:01 -07:00
|
|
|
|
* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
|
2009-10-26 15:04:05 -07:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2014-10-29 11:34:40 -07:00
|
|
|
|
#undef NDEBUG
|
2009-10-26 15:04:05 -07:00
|
|
|
|
#include "jsonrpc.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "command-line.h"
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
#include "json.h"
|
2014-10-29 11:34:40 -07:00
|
|
|
|
#include "ovstest.h"
|
2009-10-26 15:04:05 -07:00
|
|
|
|
#include "poll-loop.h"
|
2009-12-21 13:13:48 -08:00
|
|
|
|
#include "stream-ssl.h"
|
2009-10-26 15:04:05 -07:00
|
|
|
|
#include "stream.h"
|
|
|
|
|
#include "timeval.h"
|
|
|
|
|
#include "util.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2009-10-26 15:04:05 -07:00
|
|
|
|
|
2014-12-15 14:10:38 +01:00
|
|
|
|
OVS_NO_RETURN static void usage(void);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
static void parse_options(int argc, char *argv[]);
|
2015-03-16 12:01:55 -04:00
|
|
|
|
static struct ovs_cmdl_command *get_all_commands(void);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
|
2014-04-01 00:47:01 -07:00
|
|
|
|
static void
|
|
|
|
|
test_jsonrpc_main(int argc, char *argv[])
|
2009-10-26 15:04:05 -07:00
|
|
|
|
{
|
2015-03-16 12:01:55 -04:00
|
|
|
|
ovs_cmdl_proctitle_init(argc, argv);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
set_program_name(argv[0]);
|
2014-06-12 11:56:57 -07:00
|
|
|
|
service_start(&argc, &argv);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
parse_options(argc, argv);
|
2015-03-16 12:01:55 -04:00
|
|
|
|
ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands());
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
parse_options(int argc, char *argv[])
|
|
|
|
|
{
|
2009-12-21 13:13:48 -08:00
|
|
|
|
enum {
|
2011-01-28 12:39:15 -08:00
|
|
|
|
OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
|
|
|
|
|
DAEMON_OPTION_ENUMS
|
2009-12-21 13:13:48 -08:00
|
|
|
|
};
|
2013-04-23 16:40:56 -07:00
|
|
|
|
static const struct option long_options[] = {
|
2011-05-04 13:49:42 -07:00
|
|
|
|
{"verbose", optional_argument, NULL, 'v'},
|
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
2009-10-26 15:04:05 -07:00
|
|
|
|
DAEMON_LONG_OPTIONS,
|
2011-05-04 13:49:42 -07:00
|
|
|
|
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
|
2011-05-10 09:17:37 -07:00
|
|
|
|
STREAM_SSL_LONG_OPTIONS,
|
2011-05-04 13:49:42 -07:00
|
|
|
|
{NULL, 0, NULL, 0},
|
2009-10-26 15:04:05 -07:00
|
|
|
|
};
|
2015-03-16 12:01:55 -04:00
|
|
|
|
char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
int c = getopt_long(argc, argv, short_options, long_options, NULL);
|
|
|
|
|
if (c == -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 'h':
|
|
|
|
|
usage();
|
|
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
|
vlog_set_verbosity(optarg);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
DAEMON_OPTION_HANDLERS
|
|
|
|
|
|
2009-12-21 13:13:48 -08:00
|
|
|
|
STREAM_SSL_OPTION_HANDLERS
|
|
|
|
|
|
|
|
|
|
case OPT_BOOTSTRAP_CA_CERT:
|
|
|
|
|
stream_ssl_set_ca_cert_file(optarg, true);
|
|
|
|
|
break;
|
|
|
|
|
|
2009-10-26 15:04:05 -07:00
|
|
|
|
case '?':
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(short_options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage(void)
|
|
|
|
|
{
|
|
|
|
|
printf("%s: JSON-RPC test utility\n"
|
|
|
|
|
"usage: %s [OPTIONS] COMMAND [ARG...]\n"
|
|
|
|
|
" listen LOCAL listen for connections on LOCAL\n"
|
|
|
|
|
" request REMOTE METHOD PARAMS send request, print reply\n"
|
|
|
|
|
" notify REMOTE METHOD PARAMS send notification and exit\n",
|
|
|
|
|
program_name, program_name);
|
2009-12-21 13:13:48 -08:00
|
|
|
|
stream_usage("JSON-RPC", true, true, true);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
daemon_usage();
|
|
|
|
|
vlog_usage();
|
|
|
|
|
printf("\nOther options:\n"
|
|
|
|
|
" -h, --help display this help message\n");
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Command helper functions. */
|
|
|
|
|
|
|
|
|
|
static struct json *
|
|
|
|
|
parse_json(const char *s)
|
|
|
|
|
{
|
|
|
|
|
struct json *json = json_from_string(s);
|
|
|
|
|
if (json->type == JSON_STRING) {
|
|
|
|
|
ovs_fatal(0, "\"%s\": %s", s, json->u.string);
|
|
|
|
|
}
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
print_and_free_json(struct json *json)
|
|
|
|
|
{
|
|
|
|
|
char *string = json_to_string(json, JSSF_SORT);
|
|
|
|
|
json_destroy(json);
|
|
|
|
|
puts(string);
|
|
|
|
|
free(string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Command implementations. */
|
|
|
|
|
|
2012-02-17 16:47:36 -08:00
|
|
|
|
static int
|
2009-10-26 15:04:05 -07:00
|
|
|
|
handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
|
|
|
|
|
{
|
|
|
|
|
if (msg->type == JSONRPC_REQUEST) {
|
2012-02-17 16:47:36 -08:00
|
|
|
|
struct jsonrpc_msg *reply = NULL;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
if (!strcmp(msg->method, "echo")) {
|
|
|
|
|
reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
|
|
|
|
|
} else {
|
|
|
|
|
struct json *error = json_object_create();
|
|
|
|
|
json_object_put_string(error, "error", "unknown method");
|
|
|
|
|
reply = jsonrpc_create_error(error, msg->id);
|
|
|
|
|
ovs_error(0, "unknown request %s", msg->method);
|
|
|
|
|
}
|
2012-02-17 16:47:36 -08:00
|
|
|
|
jsonrpc_send(rpc, reply);
|
|
|
|
|
return 0;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
} else if (msg->type == JSONRPC_NOTIFY) {
|
|
|
|
|
if (!strcmp(msg->method, "shutdown")) {
|
|
|
|
|
*done = true;
|
2012-02-17 16:47:36 -08:00
|
|
|
|
return 0;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
} else {
|
|
|
|
|
ovs_error(0, "unknown notification %s", msg->method);
|
2012-02-17 16:47:36 -08:00
|
|
|
|
return ENOTTY;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ovs_error(0, "unsolicited JSON-RPC reply or error");
|
2012-02-17 16:47:36 -08:00
|
|
|
|
return EPROTO;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-02-11 11:11:23 -08:00
|
|
|
|
do_listen(int argc OVS_UNUSED, char *argv[])
|
2009-10-26 15:04:05 -07:00
|
|
|
|
{
|
|
|
|
|
struct pstream *pstream;
|
|
|
|
|
struct jsonrpc **rpcs;
|
|
|
|
|
size_t n_rpcs, allocated_rpcs;
|
|
|
|
|
bool done;
|
|
|
|
|
int error;
|
|
|
|
|
|
2012-03-10 15:58:10 -08:00
|
|
|
|
error = jsonrpc_pstream_open(argv[1], &pstream, DSCP_DEFAULT);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
daemonize();
|
|
|
|
|
|
|
|
|
|
rpcs = NULL;
|
|
|
|
|
n_rpcs = allocated_rpcs = 0;
|
|
|
|
|
done = false;
|
|
|
|
|
for (;;) {
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
/* Accept new connections. */
|
|
|
|
|
error = pstream_accept(pstream, &stream);
|
|
|
|
|
if (!error) {
|
|
|
|
|
if (n_rpcs >= allocated_rpcs) {
|
|
|
|
|
rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
|
|
|
|
|
}
|
|
|
|
|
rpcs[n_rpcs++] = jsonrpc_open(stream);
|
|
|
|
|
} else if (error != EAGAIN) {
|
|
|
|
|
ovs_fatal(error, "pstream_accept failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Service existing connections. */
|
|
|
|
|
for (i = 0; i < n_rpcs; ) {
|
|
|
|
|
struct jsonrpc *rpc = rpcs[i];
|
|
|
|
|
struct jsonrpc_msg *msg;
|
|
|
|
|
|
|
|
|
|
jsonrpc_run(rpc);
|
|
|
|
|
if (!jsonrpc_get_backlog(rpc)) {
|
|
|
|
|
error = jsonrpc_recv(rpc, &msg);
|
|
|
|
|
if (!error) {
|
2012-02-17 16:47:36 -08:00
|
|
|
|
error = handle_rpc(rpc, msg, &done);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
jsonrpc_msg_destroy(msg);
|
2012-02-17 16:47:36 -08:00
|
|
|
|
} else if (error == EAGAIN) {
|
|
|
|
|
error = 0;
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-17 16:47:36 -08:00
|
|
|
|
if (!error) {
|
|
|
|
|
error = jsonrpc_get_status(rpc);
|
|
|
|
|
}
|
2009-10-26 15:04:05 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
jsonrpc_close(rpc);
|
|
|
|
|
ovs_error(error, "connection closed");
|
|
|
|
|
memmove(&rpcs[i], &rpcs[i + 1],
|
|
|
|
|
(n_rpcs - i - 1) * sizeof *rpcs);
|
|
|
|
|
n_rpcs--;
|
|
|
|
|
} else {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wait for something to do. */
|
|
|
|
|
if (done && !n_rpcs) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pstream_wait(pstream);
|
|
|
|
|
for (i = 0; i < n_rpcs; i++) {
|
|
|
|
|
struct jsonrpc *rpc = rpcs[i];
|
|
|
|
|
|
|
|
|
|
jsonrpc_wait(rpc);
|
|
|
|
|
if (!jsonrpc_get_backlog(rpc)) {
|
|
|
|
|
jsonrpc_recv_wait(rpc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
poll_block();
|
|
|
|
|
}
|
2010-02-01 14:57:48 -08:00
|
|
|
|
free(rpcs);
|
|
|
|
|
pstream_close(pstream);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-02-11 11:11:23 -08:00
|
|
|
|
do_request(int argc OVS_UNUSED, char *argv[])
|
2009-10-26 15:04:05 -07:00
|
|
|
|
{
|
|
|
|
|
struct jsonrpc_msg *msg;
|
|
|
|
|
struct jsonrpc *rpc;
|
|
|
|
|
struct json *params;
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
const char *method;
|
|
|
|
|
char *string;
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
method = argv[2];
|
|
|
|
|
params = parse_json(argv[3]);
|
2009-12-01 16:32:03 -08:00
|
|
|
|
msg = jsonrpc_create_request(method, params, NULL);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
string = jsonrpc_msg_is_valid(msg);
|
|
|
|
|
if (string) {
|
|
|
|
|
ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-10 15:58:10 -08:00
|
|
|
|
error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
|
|
|
|
|
DSCP_DEFAULT), &stream);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
ovs_fatal(error, "could not open \"%s\"", argv[1]);
|
|
|
|
|
}
|
|
|
|
|
rpc = jsonrpc_open(stream);
|
|
|
|
|
|
|
|
|
|
error = jsonrpc_send(rpc, msg);
|
|
|
|
|
if (error) {
|
|
|
|
|
ovs_fatal(error, "could not send request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error = jsonrpc_recv_block(rpc, &msg);
|
|
|
|
|
if (error) {
|
|
|
|
|
ovs_fatal(error, "error waiting for reply");
|
|
|
|
|
}
|
|
|
|
|
print_and_free_json(jsonrpc_msg_to_json(msg));
|
|
|
|
|
|
|
|
|
|
jsonrpc_close(rpc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-02-11 11:11:23 -08:00
|
|
|
|
do_notify(int argc OVS_UNUSED, char *argv[])
|
2009-10-26 15:04:05 -07:00
|
|
|
|
{
|
|
|
|
|
struct jsonrpc_msg *msg;
|
|
|
|
|
struct jsonrpc *rpc;
|
|
|
|
|
struct json *params;
|
|
|
|
|
struct stream *stream;
|
|
|
|
|
const char *method;
|
|
|
|
|
char *string;
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
method = argv[2];
|
|
|
|
|
params = parse_json(argv[3]);
|
|
|
|
|
msg = jsonrpc_create_notify(method, params);
|
|
|
|
|
string = jsonrpc_msg_is_valid(msg);
|
|
|
|
|
if (string) {
|
|
|
|
|
ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-10 15:58:10 -08:00
|
|
|
|
error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
|
|
|
|
|
DSCP_DEFAULT), &stream);
|
2009-10-26 15:04:05 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
ovs_fatal(error, "could not open \"%s\"", argv[1]);
|
|
|
|
|
}
|
|
|
|
|
rpc = jsonrpc_open(stream);
|
|
|
|
|
|
|
|
|
|
error = jsonrpc_send_block(rpc, msg);
|
|
|
|
|
if (error) {
|
2010-08-25 10:26:40 -07:00
|
|
|
|
ovs_fatal(error, "could not send notification");
|
2009-10-26 15:04:05 -07:00
|
|
|
|
}
|
|
|
|
|
jsonrpc_close(rpc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-02-11 11:11:23 -08:00
|
|
|
|
do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
|
2009-10-26 15:04:05 -07:00
|
|
|
|
{
|
|
|
|
|
usage();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 12:01:55 -04:00
|
|
|
|
static struct ovs_cmdl_command all_commands[] = {
|
2014-10-16 13:27:32 -07:00
|
|
|
|
{ "listen", NULL, 1, 1, do_listen },
|
|
|
|
|
{ "request", NULL, 3, 3, do_request },
|
|
|
|
|
{ "notify", NULL, 3, 3, do_notify },
|
|
|
|
|
{ "help", NULL, 0, INT_MAX, do_help },
|
|
|
|
|
{ NULL, NULL, 0, 0, NULL },
|
2009-10-26 15:04:05 -07:00
|
|
|
|
};
|
2014-04-01 00:47:01 -07:00
|
|
|
|
|
2015-03-16 12:01:55 -04:00
|
|
|
|
static struct ovs_cmdl_command *
|
2014-04-03 16:07:43 -07:00
|
|
|
|
get_all_commands(void)
|
|
|
|
|
{
|
|
|
|
|
return all_commands;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 00:47:01 -07:00
|
|
|
|
OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);
|