#include <unistd.h>
#include <sys/time.h>
-#include "command-line.h"
-#include "compiler.h"
-#include "buffer.h"
-#include "dpif.h"
#ifdef HAVE_NETLINK
+#include "netdev.h"
#include "netlink.h"
#include "openflow-netlink.h"
#endif
-#include "util.h"
-#include "socket-util.h"
-#include "openflow.h"
+
+#include "command-line.h"
+#include "compiler.h"
+#include "dpif.h"
+#include "nicira-ext.h"
#include "ofp-print.h"
+#include "ofpbuf.h"
+#include "openflow.h"
#include "packets.h"
#include "random.h"
+#include "socket-util.h"
#include "timeval.h"
-#include "vconn.h"
+#include "util.h"
#include "vconn-ssl.h"
+#include "vconn.h"
#include "vlog.h"
#define THIS_MODULE VLM_dpctl
#define DEFAULT_IDLE_TIMEOUT 60
#define MAX_ADD_ACTS 5
-static const char* ifconfigbin = "/sbin/ifconfig";
-
#define MOD_PORT_CMD_UP "up"
#define MOD_PORT_CMD_DOWN "down"
#define MOD_PORT_CMD_FLOOD "flood"
argc -= optind;
argv += optind;
if (argc < 1)
- fatal(0, "missing command name; use --help for help");
+ ofp_fatal(0, "missing command name; use --help for help");
for (p = all_commands; p->name != NULL; p++) {
if (!strcmp(p->name, argv[0])) {
int n_arg = argc - 1;
if (n_arg < p->min_args)
- fatal(0, "'%s' command requires at least %d arguments",
- p->name, p->min_args);
+ ofp_fatal(0, "'%s' command requires at least %d arguments",
+ p->name, p->min_args);
else if (n_arg > p->max_args)
- fatal(0, "'%s' command takes at most %d arguments",
- p->name, p->max_args);
+ ofp_fatal(0, "'%s' command takes at most %d arguments",
+ p->name, p->max_args);
else {
p->handler(argc, argv);
exit(0);
}
}
}
- fatal(0, "unknown command '%s'; use --help for help", argv[0]);
+ ofp_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
return 0;
}
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout <= 0) {
- fatal(0, "value %s on -t or --timeout is not at least 1",
- optarg);
+ ofp_fatal(0, "value %s on -t or --timeout is not at least 1",
+ optarg);
} else {
time_alarm(timeout);
}
" deldp nl:DP_ID delete local datapath DP_ID\n"
" addif nl:DP_ID IFACE... add each IFACE as a port on DP_ID\n"
" delif nl:DP_ID IFACE... delete each IFACE from DP_ID\n"
- " monitor nl:DP_ID print packets received\n"
#endif
"\nFor local datapaths and remote switches:\n"
" show SWITCH show basic information\n"
" status SWITCH [KEY] report statistics (about KEY)\n"
- " dump-version SWITCH print version information\n"
+ " dump-desc SWITCH print switch description\n"
" dump-tables SWITCH print table stats\n"
" mod-port SWITCH IFACE ACT modify port behavior\n"
" dump-ports SWITCH print port statistics\n"
" dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
" add-flow SWITCH FLOW add flow described by FLOW\n"
" add-flows SWITCH FILE add flows from FILE\n"
- " del-flows SWITCH FLOW delete matching FLOWs\n"
+ " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
+ " del-flows SWITCH [FLOW] delete matching FLOWs\n"
+ " monitor SWITCH print packets received from SWITCH\n"
"\nFor local datapaths, remote switches, and controllers:\n"
" probe VCONN probe whether VCONN is up\n"
" ping VCONN [N] latency of N-byte echos\n"
#ifdef HAVE_NETLINK
/* Netlink-only commands. */
-static int if_up(const char* intf)
+static int if_up(const char *netdev_name)
{
- char command[256];
- snprintf(command, sizeof command, "%s %s up &> /dev/null",
- ifconfigbin, intf);
- return system(command);
+ struct netdev *netdev;
+ int retval;
+
+ retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
+ if (!retval) {
+ retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
+ netdev_close(netdev);
+ }
+ return retval;
}
static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
if (strncmp(name, "nl:", 3)
|| strlen(name) < 4
|| name[strspn(name + 3, "0123456789") + 3]) {
- fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
+ ofp_fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
}
run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
}
for (i = 2; i < argc; i++) {
int retval = function(&dp, argv[i]);
if (retval) {
- error(retval, "failed to %s %s %s %s",
- operation, argv[i], preposition, argv[1]);
+ ofp_error(retval, "failed to %s %s %s %s",
+ operation, argv[i], preposition, argv[1]);
failure = true;
}
}
static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
{
- if_up(netdev);
- return dpif_add_port(dpif, netdev);
+ int retval = if_up(netdev);
+ return retval ? retval : dpif_add_port(dpif, netdev);
}
static void do_add_port(int argc UNUSED, char *argv[])
{
add_del_ports(argc, argv, dpif_del_port, "remove", "from");
}
-
-static void do_monitor(int argc UNUSED, char *argv[])
-{
- struct dpif dp;
- open_nl_vconn(argv[1], true, &dp);
- for (;;) {
- struct buffer *b;
- run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
- ofp_print(stderr, b->data, b->size, 2);
- buffer_delete(b);
- }
-}
#endif /* HAVE_NETLINK */
\f
/* Generic commands. */
+static void
+open_vconn(const char *name, struct vconn **vconnp)
+{
+ run(vconn_open_block(name, OFP_VERSION, vconnp), "connecting to %s", name);
+}
+
static void *
-alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
+alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
{
struct ofp_stats_request *rq;
rq = make_openflow((offsetof(struct ofp_stats_request, body)
}
static void
-send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
+send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
{
update_openflow_length(buffer);
run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
}
static void
-dump_transaction(const char *vconn_name, struct buffer *request)
+dump_transaction(const char *vconn_name, struct ofpbuf *request)
{
struct vconn *vconn;
- struct buffer *reply;
+ struct ofpbuf *reply;
update_openflow_length(request);
- run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
+ open_vconn(vconn_name, &vconn);
run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
ofp_print(stdout, reply->data, reply->size, 1);
vconn_close(vconn);
static void
dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
{
- struct buffer *request;
+ struct ofpbuf *request;
make_openflow(sizeof(struct ofp_header), request_type, &request);
dump_transaction(vconn_name, request);
}
static void
-dump_stats_transaction(const char *vconn_name, struct buffer *request)
+dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
{
uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
struct vconn *vconn;
bool done = false;
- run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
+ open_vconn(vconn_name, &vconn);
send_openflow_buffer(vconn, request);
while (!done) {
uint32_t recv_xid;
- struct buffer *reply;
+ struct ofpbuf *reply;
run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
recv_xid = ((struct ofp_header *) reply->data)->xid;
ofp_print(stdout, reply->data, reply->size, 1);
- osr = buffer_at(reply, 0, sizeof *osr);
+ osr = ofpbuf_at(reply, 0, sizeof *osr);
done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
} else {
VLOG_DBG("received reply with xid %08"PRIx32" "
"!= expected %08"PRIx32, recv_xid, send_xid);
}
- buffer_delete(reply);
+ ofpbuf_delete(reply);
}
vconn_close(vconn);
}
static void
dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
{
- struct buffer *request;
+ struct ofpbuf *request;
alloc_stats_request(0, stats_type, &request);
dump_stats_transaction(vconn_name, request);
}
static void
do_status(int argc, char *argv[])
{
- struct buffer *request;
- alloc_stats_request(0, OFPST_SWITCH, &request);
+ struct nicira_header *request, *reply;
+ struct vconn *vconn;
+ struct ofpbuf *b;
+
+ request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
+ request->vendor_id = htonl(NX_VENDOR_ID);
+ request->subtype = htonl(NXT_STATUS_REQUEST);
if (argc > 2) {
- buffer_put(request, argv[2], strlen(argv[2]));
+ ofpbuf_put(b, argv[2], strlen(argv[2]));
}
- dump_stats_transaction(argv[1], request);
+ open_vconn(argv[1], &vconn);
+ run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
+ vconn_close(vconn);
+
+ if (b->size < sizeof *reply) {
+ ofp_fatal(0, "short reply (%zu bytes)", b->size);
+ }
+ reply = b->data;
+ if (reply->header.type != OFPT_VENDOR
+ || reply->vendor_id != ntohl(NX_VENDOR_ID)
+ || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
+ ofp_print(stderr, b->data, b->size, 2);
+ ofp_fatal(0, "bad reply");
+ }
+
+ fwrite(reply + 1, b->size, 1, stdout);
}
static void
-do_dump_version(int argc, char *argv[])
+do_dump_desc(int argc, char *argv[])
{
- dump_trivial_stats_transaction(argv[1], OFPST_VERSION);
+ dump_trivial_stats_transaction(argv[1], OFPST_DESC);
}
static void
errno = 0;
value = strtoul(str, &tail, 0);
if (errno == EINVAL || errno == ERANGE || *tail) {
- fatal(0, "invalid numeric format %s", str);
+ ofp_fatal(0, "invalid numeric format %s", str);
}
return value;
}
{
if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
- fatal(0, "invalid mac address %s", str);
+ ofp_fatal(0, "invalid mac address %s", str);
}
}
name = strtok_r(str, "//", &save_ptr);
retval = name ? lookup_ip(name, &in_addr) : EINVAL;
if (retval) {
- fatal(0, "%s: could not convert to IP address", str);
+ ofp_fatal(0, "%s: could not convert to IP address", str);
}
*ip = in_addr.s_addr;
/* Verify that the rest of the bits are 1-bits. */
for (; i < 32; i++) {
if (!(nm & (1u << i))) {
- fatal(0, "%s: %s is not a valid netmask", str, netmask);
+ ofp_fatal(0, "%s: %s is not a valid netmask",
+ str, netmask);
}
}
} else {
int prefix = atoi(netmask);
if (prefix <= 0 || prefix > 32) {
- fatal(0, "%s: network prefix bits not between 1 and 32", str);
+ ofp_fatal(0, "%s: network prefix bits not between 1 and 32",
+ str);
}
n_wild = 32 - prefix;
}
} else if (strspn(act, "0123456789") == strlen(act)) {
port = str_to_int(act);
} else {
- fatal(0, "Unknown action: %s", act);
+ ofp_fatal(0, "Unknown action: %s", act);
}
if (port != OFPP_MAX) {
if (action) {
char *act_str = strstr(string, "action");
if (!act_str) {
- fatal(0, "must specify an action");
+ ofp_fatal(0, "must specify an action");
}
*(act_str-1) = '\0';
act_str = strchr(act_str, '=');
if (!act_str) {
- fatal(0, "must specify an action");
+ ofp_fatal(0, "must specify an action");
}
act_str++;
value = strtok(NULL, ", \t\r\n");
if (!value) {
- fatal(0, "field %s missing value", name);
+ ofp_fatal(0, "field %s missing value", name);
}
if (table_idx && !strcmp(name, "table")) {
}
}
} else {
- fatal(0, "unknown keyword %s", name);
+ ofp_fatal(0, "unknown keyword %s", name);
}
}
}
static void do_dump_flows(int argc, char *argv[])
{
struct ofp_flow_stats_request *req;
- struct buffer *request;
+ struct ofpbuf *request;
req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
static void do_dump_aggregate(int argc, char *argv[])
{
struct ofp_aggregate_stats_request *req;
- struct buffer *request;
+ struct ofpbuf *request;
req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
static void do_add_flow(int argc, char *argv[])
{
struct vconn *vconn;
- struct buffer *buffer;
+ struct ofpbuf *buffer;
struct ofp_flow_mod *ofm;
uint16_t priority, idle_timeout, hard_timeout;
size_t size;
int n_actions = MAX_ADD_ACTS;
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
-
/* Parse and send. */
size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
ofm->priority = htons(priority);
ofm->reserved = htonl(0);
- /* xxx Should we use the buffer library? */
+ /* xxx Should we use the ofpbuf library? */
buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
+ open_vconn(argv[1], &vconn);
send_openflow_buffer(vconn, buffer);
vconn_close(vconn);
}
static void do_add_flows(int argc, char *argv[])
{
struct vconn *vconn;
-
FILE *file;
char line[1024];
file = fopen(argv[2], "r");
if (file == NULL) {
- fatal(errno, "%s: open", argv[2]);
+ ofp_fatal(errno, "%s: open", argv[2]);
}
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+ open_vconn(argv[1], &vconn);
while (fgets(line, sizeof line, file)) {
- struct buffer *buffer;
+ struct ofpbuf *buffer;
struct ofp_flow_mod *ofm;
uint16_t priority, idle_timeout, hard_timeout;
size_t size;
ofm->priority = htons(priority);
ofm->reserved = htonl(0);
- /* xxx Should we use the buffer library? */
+ /* xxx Should we use the ofpbuf library? */
buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
send_openflow_buffer(vconn, buffer);
fclose(file);
}
+static void do_mod_flows(int argc, char *argv[])
+{
+ uint16_t idle_timeout, hard_timeout;
+ struct vconn *vconn;
+ struct ofpbuf *buffer;
+ struct ofp_flow_mod *ofm;
+ size_t size;
+ int n_actions = MAX_ADD_ACTS;
+
+ /* Parse and send. */
+ size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
+ ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
+ str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
+ NULL, NULL, &idle_timeout, &hard_timeout);
+ ofm->command = htons(OFPFC_MODIFY);
+ ofm->idle_timeout = htons(idle_timeout);
+ ofm->hard_timeout = htons(hard_timeout);
+ ofm->buffer_id = htonl(UINT32_MAX);
+ ofm->priority = htons(0);
+ ofm->reserved = htonl(0);
+
+ /* xxx Should we use the buffer library? */
+ buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
+
+ open_vconn(argv[1], &vconn);
+ send_openflow_buffer(vconn, buffer);
+ vconn_close(vconn);
+}
+
static void do_del_flows(int argc, char *argv[])
{
struct vconn *vconn;
uint16_t priority;
-
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
- struct buffer *buffer;
+ struct ofpbuf *buffer;
struct ofp_flow_mod *ofm;
size_t size;
-
/* Parse and send. */
size = sizeof *ofm;
ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
ofm->priority = htons(priority);
ofm->reserved = htonl(0);
+ open_vconn(argv[1], &vconn);
send_openflow_buffer(vconn, buffer);
-
vconn_close(vconn);
}
+static void
+do_monitor(int argc UNUSED, char *argv[])
+{
+ struct vconn *vconn;
+ const char *name;
+
+ /* If the user specified, e.g., "nl:0", append ":1" to it to ensure that
+ * the connection will subscribe to listen for asynchronous messages, such
+ * as packet-in messages. */
+ if (!strncmp(argv[1], "nl:", 3) && strrchr(argv[1], ':') == &argv[1][2]) {
+ name = xasprintf("%s:1", argv[1]);
+ } else {
+ name = argv[1];
+ }
+ open_vconn(argv[1], &vconn);
+ for (;;) {
+ struct ofpbuf *b;
+ run(vconn_recv_block(vconn, &b), "vconn_recv");
+ ofp_print(stderr, b->data, b->size, 2);
+ ofpbuf_delete(b);
+ }
+}
+
static void
do_dump_ports(int argc, char *argv[])
{
static void
do_probe(int argc, char *argv[])
{
- struct buffer *request;
+ struct ofpbuf *request;
struct vconn *vconn;
- struct buffer *reply;
+ struct ofpbuf *reply;
make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+ open_vconn(argv[1], &vconn);
run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
if (reply->size != request->size) {
- fatal(0, "reply does not match request");
+ ofp_fatal(0, "reply does not match request");
}
- buffer_delete(reply);
+ ofpbuf_delete(reply);
vconn_close(vconn);
}
static void
do_mod_port(int argc, char *argv[])
{
- struct buffer *request, *reply;
+ struct ofpbuf *request, *reply;
struct ofp_switch_features *osf;
struct ofp_port_mod *opm;
struct vconn *vconn;
/* Send a "Features Request" to get the information we need in order
* to modify the port. */
make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+ open_vconn(argv[1], &vconn);
run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
osf = reply->data;
}
}
if (port_idx == n_ports) {
- fatal(0, "couldn't find monitored port: %s", argv[2]);
+ ofp_fatal(0, "couldn't find monitored port: %s", argv[2]);
}
opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
opm->mask |= htonl(OFPPFL_NO_FLOOD);
opm->desc.flags |= htonl(OFPPFL_NO_FLOOD);
} else {
- fatal(0, "unknown mod-port command '%s'", argv[3]);
+ ofp_fatal(0, "unknown mod-port command '%s'", argv[3]);
}
send_openflow_buffer(vconn, request);
- buffer_delete(reply);
+ ofpbuf_delete(reply);
vconn_close(vconn);
}
payload = argc > 2 ? atoi(argv[2]) : 64;
if (payload > max_payload) {
- fatal(0, "payload must be between 0 and %zu bytes", max_payload);
+ ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
}
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+ open_vconn(argv[1], &vconn);
for (i = 0; i < 10; i++) {
struct timeval start, end;
- struct buffer *request, *reply;
+ struct ofpbuf *request, *reply;
struct ofp_header *rq_hdr, *rpy_hdr;
rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
random_bytes(rq_hdr + 1, payload);
gettimeofday(&start, NULL);
- run(vconn_transact(vconn, buffer_clone(request), &reply), "transact");
+ run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
gettimeofday(&end, NULL);
rpy_hdr = reply->data;
reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
(1000*(double)(end.tv_sec - start.tv_sec))
+ (.001*(end.tv_usec - start.tv_usec)));
- buffer_delete(request);
- buffer_delete(reply);
+ ofpbuf_delete(request);
+ ofpbuf_delete(reply);
}
vconn_close(vconn);
}
payload_size = atoi(argv[2]);
if (payload_size > max_payload) {
- fatal(0, "payload must be between 0 and %zu bytes", max_payload);
+ ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
}
message_size = sizeof(struct ofp_header) + payload_size;
printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
count, message_size, count * message_size);
- run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+ open_vconn(argv[1], &vconn);
gettimeofday(&start, NULL);
for (i = 0; i < count; i++) {
- struct buffer *request, *reply;
+ struct ofpbuf *request, *reply;
struct ofp_header *rq_hdr;
rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
memset(rq_hdr + 1, 0, payload_size);
run(vconn_transact(vconn, request, &reply), "transact");
- buffer_delete(reply);
+ ofpbuf_delete(reply);
}
gettimeofday(&end, NULL);
vconn_close(vconn);
{ "help", 0, INT_MAX, do_help },
{ "monitor", 1, 1, do_monitor },
- { "dump-version", 1, 1, do_dump_version },
+ { "dump-desc", 1, 1, do_dump_desc },
{ "dump-tables", 1, 1, do_dump_tables },
{ "dump-flows", 1, 2, do_dump_flows },
{ "dump-aggregate", 1, 2, do_dump_aggregate },
{ "add-flow", 2, 2, do_add_flow },
{ "add-flows", 2, 2, do_add_flows },
+ { "mod-flows", 2, 2, do_mod_flows },
{ "del-flows", 1, 2, do_del_flows },
{ "dump-ports", 1, 1, do_dump_ports },
{ "mod-port", 3, 3, do_mod_port },