Merge branch "partner", to simplify partner integration.
[sliver-openvswitch.git] / utilities / dpctl.c
index 302d4d1..1d9897d 100644 (file)
@@ -53,6 +53,7 @@
 #include "command-line.h"
 #include "compiler.h"
 #include "dpif.h"
+#include "nicira-ext.h"
 #include "ofp-print.h"
 #include "ofpbuf.h"
 #include "openflow.h"
@@ -189,7 +190,6 @@ usage(void)
            "  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"
@@ -205,6 +205,7 @@ usage(void)
            "  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"
+           "  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"
@@ -323,22 +324,16 @@ static void do_del_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 ofpbuf *b;
-        run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
-        ofp_print(stderr, b->data, b->size, 2);
-        ofpbuf_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 ofpbuf **bufferp)
 {
@@ -364,7 +359,7 @@ dump_transaction(const char *vconn_name, struct ofpbuf *request)
     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);
@@ -385,7 +380,7 @@ dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
     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;
@@ -427,12 +422,32 @@ do_show(int argc UNUSED, char *argv[])
 static void
 do_status(int argc, char *argv[])
 {
-    struct ofpbuf *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) {
-        ofpbuf_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
@@ -786,7 +801,7 @@ static void do_add_flow(int argc, char *argv[])
     size_t size;
     int n_actions = MAX_ADD_ACTS;
 
-    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+    open_vconn(argv[1], &vconn);
 
     /* Parse and send. */
     size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
@@ -819,7 +834,7 @@ static void do_add_flows(int argc, char *argv[])
         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 ofpbuf *buffer;
         struct ofp_flow_mod *ofm;
@@ -866,7 +881,7 @@ 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]);
+    open_vconn(argv[1], &vconn);
     struct ofpbuf *buffer;
     struct ofp_flow_mod *ofm;
     size_t size;
@@ -889,6 +904,29 @@ static void do_del_flows(int argc, char *argv[])
     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[])
 {
@@ -903,7 +941,7 @@ do_probe(int argc, char *argv[])
     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) {
         ofp_fatal(0, "reply does not match request");
@@ -935,7 +973,7 @@ do_mod_port(int argc, char *argv[])
     /* 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;
@@ -1003,7 +1041,7 @@ do_ping(int argc, char *argv[])
         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 ofpbuf *request, *reply;
@@ -1059,7 +1097,7 @@ do_benchmark(int argc, char *argv[])
     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 ofpbuf *request, *reply;