ovs-ofctl: Add "packet-out" command.
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
index 6219f94..c565e86 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -45,6 +45,7 @@
 #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"
@@ -61,7 +62,7 @@ VLOG_DEFINE_THIS_MODULE(ofctl);
  */
 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;
 
@@ -208,7 +209,12 @@ usage(void)
            "  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"
@@ -444,6 +450,8 @@ fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
 
     config = reply->data;
     *config_ = *config;
+
+    ofpbuf_delete(reply);
 }
 
 static void
@@ -794,35 +802,43 @@ set_packet_in_format(struct vconn *vconn,
              ofputil_packet_in_format_to_string(packet_in_format));
 }
 
-static void
-monitor_vconn(struct vconn *vconn)
+static int
+monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
 {
-    struct unixctl_server *server;
-    bool exiting = false;
-    int error, fd;
+    struct ofp_switch_config config;
+    enum ofp_config_flags flags;
 
-    if (preferred_packet_in_format >= 0) {
-        set_packet_in_format(vconn, preferred_packet_in_format);
-    } else {
-        struct ofpbuf *spif, *reply;
+    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;
 
-        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);
+        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;
+}
 
-    /* Daemonization will close stderr but we really want to keep it, so make a
-     * copy. */
-    fd = dup(STDERR_FILENO);
+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) {
@@ -831,9 +847,6 @@ monitor_vconn(struct vconn *vconn)
     unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
     daemonize_complete();
 
-    /* Now get stderr back. */
-    dup2(fd, STDERR_FILENO);
-
     for (;;) {
         struct ofpbuf *b;
         int retval;
@@ -861,6 +874,8 @@ monitor_vconn(struct vconn *vconn)
         unixctl_server_wait(server);
         poll_block();
     }
+    vconn_close(vconn);
+    unixctl_server_destroy(server);
 }
 
 static void
@@ -876,6 +891,29 @@ do_monitor(int argc, char *argv[])
         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);
 }
 
@@ -918,6 +956,43 @@ do_probe(int argc OVS_UNUSED, char *argv[])
     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[])
 {
@@ -1319,7 +1394,7 @@ read_flows_from_switch(struct vconn *vconn, enum nx_flow_format flow_format,
                 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");
@@ -1634,7 +1709,7 @@ do_ofp_print(int argc, char *argv[])
 
 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 },
@@ -1647,6 +1722,7 @@ static const struct command all_commands[] = {
     { "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 },