2009-07-08 13:19:16 -07:00
|
|
|
/*
|
2014-08-04 12:36:04 -07:00
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2014 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -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:
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* 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.
|
2009-07-08 13:19:16 -07:00
|
|
|
*/
|
2009-11-09 14:46:38 -08:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "command-line.h"
|
2009-11-09 14:46:38 -08:00
|
|
|
#include "daemon.h"
|
|
|
|
#include "dirs.h"
|
2016-03-03 10:20:46 -08:00
|
|
|
#include "openvswitch/dynamic-string.h"
|
2012-02-14 20:53:59 -08:00
|
|
|
#include "jsonrpc.h"
|
2011-12-02 15:29:19 -08:00
|
|
|
#include "process.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "timeval.h"
|
|
|
|
#include "unixctl.h"
|
|
|
|
#include "util.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
#include "openvswitch/vlog.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
static void usage(void);
|
|
|
|
static const char *parse_command_line(int argc, char *argv[]);
|
2012-02-14 20:53:59 -08:00
|
|
|
static struct jsonrpc *connect_to_target(const char *target);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
char *cmd_result, *cmd_error;
|
|
|
|
struct jsonrpc *client;
|
|
|
|
char *cmd, **cmd_argv;
|
2009-11-09 14:46:38 -08:00
|
|
|
const char *target;
|
2012-02-14 20:53:59 -08:00
|
|
|
int cmd_argc;
|
|
|
|
int error;
|
2009-11-09 14:46:38 -08:00
|
|
|
|
|
|
|
set_program_name(argv[0]);
|
|
|
|
|
|
|
|
/* Parse command line and connect to target. */
|
|
|
|
target = parse_command_line(argc, argv);
|
|
|
|
client = connect_to_target(target);
|
|
|
|
|
|
|
|
/* Transact request and process reply. */
|
2012-02-14 20:53:59 -08:00
|
|
|
cmd = argv[optind++];
|
|
|
|
cmd_argc = argc - optind;
|
|
|
|
cmd_argv = cmd_argc ? argv + optind : NULL;
|
|
|
|
error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv,
|
|
|
|
&cmd_result, &cmd_error);
|
2009-07-08 13:19:16 -07:00
|
|
|
if (error) {
|
2009-11-09 14:46:38 -08:00
|
|
|
ovs_fatal(error, "%s: transaction error", target);
|
|
|
|
}
|
2012-02-14 20:53:59 -08:00
|
|
|
|
|
|
|
if (cmd_error) {
|
2014-03-27 15:40:25 -07:00
|
|
|
jsonrpc_close(client);
|
2012-02-14 20:53:59 -08:00
|
|
|
fputs(cmd_error, stderr);
|
|
|
|
ovs_error(0, "%s: server returned an error", target);
|
2009-11-09 14:46:38 -08:00
|
|
|
exit(2);
|
2012-02-14 20:53:59 -08:00
|
|
|
} else if (cmd_result) {
|
|
|
|
fputs(cmd_result, stdout);
|
|
|
|
} else {
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2010-02-02 14:18:01 -08:00
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
jsonrpc_close(client);
|
|
|
|
free(cmd_result);
|
|
|
|
free(cmd_error);
|
2009-11-09 14:46:38 -08:00
|
|
|
return 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-09 14:46:38 -08:00
|
|
|
usage(void)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2011-07-28 10:19:42 -07:00
|
|
|
printf("\
|
|
|
|
%s, for querying and controlling Open vSwitch daemon\n\
|
|
|
|
usage: %s [TARGET] COMMAND [ARG...]\n\
|
|
|
|
Targets:\n\
|
|
|
|
-t, --target=TARGET pidfile or socket to contact\n\
|
|
|
|
Common commands:\n\
|
2014-10-17 11:11:36 -07:00
|
|
|
list-commands List commands supported by the target\n\
|
2011-08-01 21:18:00 -07:00
|
|
|
version Print version of the target\n\
|
2011-07-28 10:19:42 -07:00
|
|
|
vlog/list List current logging levels\n\
|
2015-06-13 13:28:02 -07:00
|
|
|
vlog/list-pattern List logging patterns for each destination.\n\
|
2012-02-08 15:29:15 -08:00
|
|
|
vlog/set [SPEC]\n\
|
|
|
|
Set log levels as detailed in SPEC, which may include:\n\
|
|
|
|
A valid module name (all modules, by default)\n\
|
2015-01-06 13:05:00 -08:00
|
|
|
'syslog', 'console', 'file' (all destinations, by default))\n\
|
2012-02-08 15:29:15 -08:00
|
|
|
'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\
|
2011-07-28 10:19:42 -07:00
|
|
|
vlog/reopen Make the program reopen its log file\n\
|
|
|
|
Other options:\n\
|
2012-03-06 16:10:46 -08:00
|
|
|
--timeout=SECS wait at most SECS seconds for a response\n\
|
2011-07-28 10:19:42 -07:00
|
|
|
-h, --help Print this helpful information\n\
|
2011-08-01 21:18:00 -07:00
|
|
|
-V, --version Display ovs-appctl version information\n",
|
2009-11-09 14:46:38 -08:00
|
|
|
program_name, program_name);
|
|
|
|
exit(EXIT_SUCCESS);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
static const char *
|
|
|
|
parse_command_line(int argc, char *argv[])
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2014-08-04 12:36:04 -07:00
|
|
|
enum {
|
2014-09-11 17:24:35 -07:00
|
|
|
OPT_START = UCHAR_MAX + 1,
|
2014-08-04 12:36:04 -07:00
|
|
|
VLOG_OPTION_ENUMS
|
|
|
|
};
|
2009-07-08 13:19:16 -07:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"target", required_argument, NULL, 't'},
|
2009-11-09 14:46:38 -08:00
|
|
|
{"execute", no_argument, NULL, 'e'},
|
2009-07-08 13:19:16 -07:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2014-09-11 17:24:35 -07:00
|
|
|
{"option", no_argument, NULL, 'o'},
|
2009-07-08 13:19:16 -07:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2012-03-06 16:10:46 -08:00
|
|
|
{"timeout", required_argument, NULL, 'T'},
|
2014-08-04 12:36:04 -07:00
|
|
|
VLOG_LONG_OPTIONS,
|
2011-05-04 13:49:42 -07:00
|
|
|
{NULL, 0, NULL, 0},
|
2009-07-08 13:19:16 -07:00
|
|
|
};
|
2015-03-16 12:01:55 -04:00
|
|
|
char *short_options_ = ovs_cmdl_long_options_to_short_options(long_options);
|
2014-08-04 12:36:04 -07:00
|
|
|
char *short_options = xasprintf("+%s", short_options_);
|
2009-11-09 14:46:38 -08:00
|
|
|
const char *target;
|
|
|
|
int e_options;
|
2018-08-14 10:53:16 +03:00
|
|
|
unsigned int timeout = 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
target = NULL;
|
|
|
|
e_options = 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
for (;;) {
|
|
|
|
int option;
|
|
|
|
|
2014-08-04 12:36:04 -07:00
|
|
|
option = getopt_long(argc, argv, short_options, long_options, NULL);
|
2009-07-08 13:19:16 -07:00
|
|
|
if (option == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (option) {
|
|
|
|
case 't':
|
2009-11-09 14:46:38 -08:00
|
|
|
if (target) {
|
|
|
|
ovs_fatal(0, "-t or --target may be specified only once");
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-11-09 14:46:38 -08:00
|
|
|
target = optarg;
|
2009-07-08 13:19:16 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2009-11-09 14:46:38 -08:00
|
|
|
/* We ignore -e for compatibility. Older versions specified the
|
|
|
|
* command as the argument to -e. Since the current version takes
|
|
|
|
* the command as non-option arguments and we say that -e has no
|
|
|
|
* arguments, this just works in the common case. */
|
|
|
|
if (e_options++) {
|
|
|
|
ovs_fatal(0, "-e or --execute may be speciifed only once");
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
2009-11-09 14:46:38 -08:00
|
|
|
usage();
|
2009-07-08 13:19:16 -07:00
|
|
|
break;
|
|
|
|
|
2014-09-11 17:24:35 -07:00
|
|
|
case 'o':
|
2015-03-16 12:01:55 -04:00
|
|
|
ovs_cmdl_print_options(long_options);
|
2014-09-11 17:24:35 -07:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
2012-03-06 16:10:46 -08:00
|
|
|
case 'T':
|
2018-08-14 10:53:15 +03:00
|
|
|
if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
|
|
|
|
ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg);
|
|
|
|
}
|
2012-03-06 16:10:46 -08:00
|
|
|
break;
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
case 'V':
|
2011-08-02 12:16:44 -07:00
|
|
|
ovs_print_version(0, 0);
|
2009-07-08 13:19:16 -07:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
2014-08-04 12:36:04 -07:00
|
|
|
VLOG_OPTION_HANDLERS
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
default:
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 12:36:04 -07:00
|
|
|
free(short_options_);
|
|
|
|
free(short_options);
|
2009-11-09 14:46:38 -08:00
|
|
|
|
2018-08-14 10:53:16 +03:00
|
|
|
ctl_timeout_setup(timeout);
|
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
if (optind >= argc) {
|
|
|
|
ovs_fatal(0, "at least one non-option argument is required "
|
|
|
|
"(use --help for help)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return target ? target : "ovs-vswitchd";
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
static struct jsonrpc *
|
2009-11-09 14:46:38 -08:00
|
|
|
connect_to_target(const char *target)
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
struct jsonrpc *client;
|
2009-11-09 14:46:38 -08:00
|
|
|
char *socket_name;
|
|
|
|
int error;
|
|
|
|
|
2014-03-28 08:37:36 -07:00
|
|
|
#ifndef _WIN32
|
2009-11-09 14:46:38 -08:00
|
|
|
if (target[0] != '/') {
|
|
|
|
char *pidfile_name;
|
|
|
|
pid_t pid;
|
|
|
|
|
2010-11-29 12:28:26 -08:00
|
|
|
pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
|
2009-11-09 14:46:38 -08:00
|
|
|
pid = read_pidfile(pidfile_name);
|
|
|
|
if (pid < 0) {
|
|
|
|
ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
|
|
|
|
}
|
|
|
|
free(pidfile_name);
|
|
|
|
socket_name = xasprintf("%s/%s.%ld.ctl",
|
2010-11-29 12:28:26 -08:00
|
|
|
ovs_rundir(), target, (long int) pid);
|
2014-03-28 08:37:36 -07:00
|
|
|
#else
|
|
|
|
/* On windows, if the 'target' contains ':', we make an assumption that
|
|
|
|
* it is an absolute path. */
|
|
|
|
if (!strchr(target, ':')) {
|
|
|
|
socket_name = xasprintf("%s/%s.ctl", ovs_rundir(), target);
|
|
|
|
#endif
|
2009-11-09 14:46:38 -08:00
|
|
|
} else {
|
|
|
|
socket_name = xstrdup(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = unixctl_client_create(socket_name, &client);
|
|
|
|
if (error) {
|
|
|
|
ovs_fatal(error, "cannot connect to \"%s\"", socket_name);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-11-09 14:46:38 -08:00
|
|
|
free(socket_name);
|
|
|
|
|
|
|
|
return client;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-11-09 14:46:38 -08:00
|
|
|
|