/*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "byte-order.h"
#include "classifier.h"
#include "command-line.h"
+#include "daemon.h"
#include "compiler.h"
#include "dirs.h"
#include "dynamic-string.h"
#include "netlink.h"
#include "nx-match.h"
#include "odp-util.h"
+#include "ofp-errors.h"
#include "ofp-parse.h"
#include "ofp-print.h"
#include "ofp-util.h"
#include "ofproto/ofproto.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
+#include "packets.h"
+#include "poll-loop.h"
#include "random.h"
#include "stream-ssl.h"
#include "timeval.h"
+#include "unixctl.h"
#include "util.h"
#include "vconn.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(ofctl);
-/* --strict: Use strict matching for flow mod commands? */
+/* --strict: Use strict matching for flow mod commands? Additionally governs
+ * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
+ */
static bool strict;
-/* --readd: If ture, on replace-flows, re-add even flows that have not changed
+/* --readd: If true, on replace-flows, re-add even flows that have not changed
* (to reset flow counters). */
static bool readd;
* particular flow format or -1 to let ovs-ofctl choose intelligently. */
static int preferred_flow_format = -1;
+/* -P, --packet-in-format: Packet IN format to use in monitor and snoop
+ * commands. Either one of NXPIF_* to force a particular packet_in format, or
+ * -1 to let ovs-ofctl choose the default. */
+static int preferred_packet_in_format = -1;
+
/* -m, --more: Additional verbosity for ofp-print functions. */
static int verbosity;
enum {
OPT_STRICT = UCHAR_MAX + 1,
OPT_READD,
+ DAEMON_OPTION_ENUMS,
VLOG_OPTION_ENUMS
};
static struct option long_options[] = {
{"strict", no_argument, NULL, OPT_STRICT},
{"readd", no_argument, NULL, OPT_READD},
{"flow-format", required_argument, NULL, 'F'},
+ {"packet-in-format", required_argument, NULL, 'P'},
{"more", no_argument, NULL, 'm'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
+ DAEMON_LONG_OPTIONS,
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{NULL, 0, NULL, 0},
}
break;
+ case 'P':
+ preferred_packet_in_format =
+ ofputil_packet_in_format_from_string(optarg);
+ if (preferred_packet_in_format < 0) {
+ ovs_fatal(0, "unknown packet-in format `%s'", optarg);
+ }
+ break;
+
case 'm':
verbosity++;
break;
readd = true;
break;
+ DAEMON_OPTION_HANDLERS
VLOG_OPTION_HANDLERS
STREAM_SSL_OPTION_HANDLERS
" mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
" del-flows SWITCH [FLOW] delete matching FLOWs\n"
" replace-flows SWITCH FILE replace flows with those in FILE\n"
- " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
+ " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
+ " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
+ " execute ACTIONS on PACKET\n"
+ " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
+ " print packets received from SWITCH\n"
+ " snoop SWITCH snoop on SWITCH and its controller\n"
"\nFor OpenFlow switches and controllers:\n"
" probe TARGET probe whether TARGET is up\n"
" ping TARGET [N] latency of N-byte echos\n"
"where SWITCH or TARGET is an active OpenFlow connection method.\n",
program_name, program_name);
vconn_usage(true, false, false);
+ daemon_usage();
vlog_usage();
printf("\nOther options:\n"
" --strict use strict match for flow commands\n"
" --readd replace flows that haven't changed\n"
" -F, --flow-format=FORMAT force particular flow format\n"
+ " -P, --packet-in-format=FRMT force particular packet in format\n"
" -m, --more be more verbose printing OpenFlow\n"
" -t, --timeout=SECS give up after SECS seconds\n"
" -h, --help display this help message\n"
exit(EXIT_SUCCESS);
}
+static void
+ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[] OVS_UNUSED, void *exiting_)
+{
+ bool *exiting = exiting_;
+ *exiting = true;
+ unixctl_command_reply(conn, 200, "");
+}
+
static void run(int retval, const char *message, ...)
PRINTF_FORMAT(2, 3);
config = reply->data;
*config_ = *config;
+
+ ofpbuf_delete(reply);
}
static void
do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
}
+static void
+set_packet_in_format(struct vconn *vconn,
+ enum nx_packet_in_format packet_in_format)
+{
+ struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
+ transact_noreply(vconn, spif);
+ VLOG_DBG("%s: using user-specified packet in format %s",
+ vconn_get_name(vconn),
+ ofputil_packet_in_format_to_string(packet_in_format));
+}
+
+static int
+monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
+{
+ struct ofp_switch_config config;
+ enum ofp_config_flags flags;
+
+ fetch_switch_config(vconn, &config);
+ flags = ntohs(config.flags);
+ if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
+ /* Set the invalid ttl config. */
+ flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
+
+ config.flags = htons(flags);
+ set_switch_config(vconn, &config);
+
+ /* Then retrieve the configuration to see if it really took. OpenFlow
+ * doesn't define error reporting for bad modes, so this is all we can
+ * do. */
+ fetch_switch_config(vconn, &config);
+ flags = ntohs(config.flags);
+ if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
+ ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
+ "switch probably doesn't support mode)");
+ return -EOPNOTSUPP;
+ }
+ }
+ return 0;
+}
+
static void
monitor_vconn(struct vconn *vconn)
{
+ struct unixctl_server *server;
+ bool exiting = false;
+ int error;
+
+ daemon_save_fd(STDERR_FILENO);
+ daemonize_start();
+ error = unixctl_server_create(NULL, &server);
+ if (error) {
+ ovs_fatal(error, "failed to create unixctl server");
+ }
+ unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
+ daemonize_complete();
+
for (;;) {
struct ofpbuf *b;
- run(vconn_recv_block(vconn, &b), "vconn_recv");
- ofp_print(stderr, b->data, b->size, verbosity + 2);
- ofpbuf_delete(b);
+ int retval;
+
+ unixctl_server_run(server);
+
+ for (;;) {
+ retval = vconn_recv(vconn, &b);
+ if (retval == EAGAIN) {
+ break;
+ }
+
+ run(retval, "vconn_recv");
+ ofp_print(stderr, b->data, b->size, verbosity + 2);
+ ofpbuf_delete(b);
+ }
+
+ if (exiting) {
+ break;
+ }
+
+ vconn_run(vconn);
+ vconn_run_wait(vconn);
+ vconn_recv_wait(vconn);
+ unixctl_server_wait(server);
+ poll_block();
}
+ vconn_close(vconn);
+ unixctl_server_destroy(server);
}
static void
config.miss_send_len = htons(atoi(argv[2]));
set_switch_config(vconn, &config);
}
+ if (argc > 3) {
+ if (!strcmp(argv[3], "invalid_ttl")) {
+ monitor_set_invalid_ttl_to_controller(vconn);
+ }
+ }
+ if (preferred_packet_in_format >= 0) {
+ set_packet_in_format(vconn, preferred_packet_in_format);
+ } else {
+ struct ofpbuf *spif, *reply;
+
+ spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
+ run(vconn_transact_noreply(vconn, spif, &reply),
+ "talking to %s", vconn_get_name(vconn));
+ if (reply) {
+ char *s = ofp_to_string(reply->data, reply->size, 2);
+ VLOG_DBG("%s: failed to set packet in format to nxm, controller"
+ " replied: %s. Falling back to the switch default.",
+ vconn_get_name(vconn), s);
+ free(s);
+ ofpbuf_delete(reply);
+ }
+ }
+
monitor_vconn(vconn);
}
vconn_close(vconn);
}
+static void
+do_packet_out(int argc, char *argv[])
+{
+ struct ofputil_packet_out po;
+ struct ofpbuf actions;
+ struct vconn *vconn;
+ int i;
+
+ ofpbuf_init(&actions, sizeof(union ofp_action));
+ parse_ofp_actions(argv[3], &actions);
+
+ po.buffer_id = UINT32_MAX;
+ po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
+ : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
+ : str_to_port_no(argv[1], argv[2]));
+ po.actions = actions.data;
+ po.n_actions = actions.size / sizeof(union ofp_action);
+
+ open_vconn(argv[1], &vconn);
+ for (i = 4; i < argc; i++) {
+ struct ofpbuf *packet, *opo;
+ const char *error_msg;
+
+ error_msg = eth_from_hex(argv[i], &packet);
+ if (error_msg) {
+ ovs_fatal(0, "%s", error_msg);
+ }
+
+ po.packet = packet->data;
+ po.packet_len = packet->size;
+ opo = ofputil_encode_packet_out(&po);
+ transact_noreply(vconn, opo);
+ ofpbuf_delete(packet);
+ }
+ vconn_close(vconn);
+}
+
static void
do_mod_port(int argc OVS_UNUSED, char *argv[])
{
struct ofputil_flow_stats fs;
int retval;
- retval = ofputil_decode_flow_stats_reply(&fs, reply);
+ retval = ofputil_decode_flow_stats_reply(&fs, reply, false);
if (retval) {
if (retval != EOF) {
ovs_fatal(0, "parse error in reply");
struct ofpbuf nx_match;
struct cls_rule rule;
ovs_be64 cookie, cookie_mask;
+ enum ofperr error;
int match_len;
- int error;
char *s;
/* Delete comments, skip blank lines. */
match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
/* Convert nx_match to cls_rule. */
- error = nx_pull_match(&nx_match, match_len, 0, &rule,
- &cookie, &cookie_mask);
+ if (strict) {
+ error = nx_pull_match(&nx_match, match_len, 0, &rule,
+ &cookie, &cookie_mask);
+ } else {
+ error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
+ &cookie, &cookie_mask);
+ }
+
if (!error) {
char *out;
puts(out);
free(out);
} else {
- printf("nx_pull_match() returned error %x (%s)\n", error,
- ofputil_error_to_string(error));
+ printf("nx_pull_match() returned error %s\n",
+ ofperr_get_name(error));
}
ofpbuf_uninit(&nx_match);
static const struct command all_commands[] = {
{ "show", 1, 1, do_show },
- { "monitor", 1, 2, do_monitor },
+ { "monitor", 1, 3, do_monitor },
{ "snoop", 1, 1, do_snoop },
{ "dump-desc", 1, 1, do_dump_desc },
{ "dump-tables", 1, 1, do_dump_tables },
{ "del-flows", 1, 2, do_del_flows },
{ "replace-flows", 2, 2, do_replace_flows },
{ "diff-flows", 2, 2, do_diff_flows },
+ { "packet-out", 4, INT_MAX, do_packet_out },
{ "dump-ports", 1, 2, do_dump_ports },
{ "mod-port", 3, 3, do_mod_port },
{ "get-frags", 1, 1, do_get_frags },