2009-07-08 13:19:16 -07:00
|
|
|
/*
|
2011-05-04 13:49:42 -07:00
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
|
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"
|
|
|
|
#include "dynamic-string.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "timeval.h"
|
|
|
|
#include "unixctl.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
static void usage(void);
|
|
|
|
static const char *parse_command_line(int argc, char *argv[]);
|
|
|
|
static struct unixctl_client *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
|
|
|
{
|
2009-11-09 14:46:38 -08:00
|
|
|
struct unixctl_client *client;
|
|
|
|
const char *target;
|
|
|
|
struct ds request;
|
|
|
|
int code, error;
|
2009-07-08 13:19:16 -07:00
|
|
|
char *reply;
|
2009-11-09 14:46:38 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
set_program_name(argv[0]);
|
|
|
|
|
|
|
|
/* Parse command line and connect to target. */
|
|
|
|
target = parse_command_line(argc, argv);
|
|
|
|
client = connect_to_target(target);
|
|
|
|
|
|
|
|
/* Compose request. */
|
|
|
|
ds_init(&request);
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
if (i != optind) {
|
|
|
|
ds_put_char(&request, ' ');
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-11-09 14:46:38 -08:00
|
|
|
ds_put_cstr(&request, argv[i]);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
/* Transact request and process reply. */
|
|
|
|
error = unixctl_client_transact(client, ds_cstr(&request), &code, &reply);
|
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);
|
|
|
|
}
|
|
|
|
if (code / 100 != 2) {
|
2010-12-08 14:27:05 -08:00
|
|
|
fputs(reply, stderr);
|
2009-11-09 14:46:38 -08:00
|
|
|
ovs_error(0, "%s: server returned reply code %03d", target, code);
|
|
|
|
exit(2);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-11-09 14:46:38 -08:00
|
|
|
fputs(reply, stdout);
|
|
|
|
|
2010-02-02 14:18:01 -08:00
|
|
|
unixctl_client_destroy(client);
|
|
|
|
free(reply);
|
|
|
|
ds_destroy(&request);
|
|
|
|
|
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\
|
|
|
|
help List commands supported by the target\n\
|
|
|
|
vlog/list List current logging levels\n\
|
|
|
|
vlog/set MODULE[:FACILITY[:LEVEL]]\n\
|
|
|
|
Set MODULE and FACILITY log level to LEVEL\n\
|
|
|
|
MODULE may be any valid module name or 'ANY'\n\
|
|
|
|
FACILITY may be 'syslog', 'console', 'file', or 'ANY' (default)\n\
|
|
|
|
LEVEL may be 'off', 'emer', 'err', 'warn', 'info', or 'dbg' (default)\n\
|
|
|
|
vlog/reopen Make the program reopen its log file\n\
|
|
|
|
Other options:\n\
|
|
|
|
-h, --help Print this helpful information\n\
|
|
|
|
-V, --version Display 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
|
|
|
{
|
|
|
|
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'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
2011-05-04 13:49:42 -07:00
|
|
|
{NULL, 0, NULL, 0},
|
2009-07-08 13:19:16 -07:00
|
|
|
};
|
2009-11-09 14:46:38 -08:00
|
|
|
const char *target;
|
|
|
|
int e_options;
|
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;
|
|
|
|
|
2009-11-09 14:46:38 -08:00
|
|
|
option = getopt_long(argc, argv, "+t:hVe", 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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct unixctl_client *
|
|
|
|
connect_to_target(const char *target)
|
|
|
|
{
|
|
|
|
struct unixctl_client *client;
|
|
|
|
char *socket_name;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
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);
|
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
|
|
|
|