Merge branch "partner", to simplify partner integration.
[sliver-openvswitch.git] / utilities / dpctl.c
index a008e3c..1d9897d 100644 (file)
 #include "openflow-netlink.h"
 #endif
 
-#include "buffer.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"
@@ -100,24 +101,24 @@ int main(int argc, char *argv[])
     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;
 }
@@ -148,8 +149,8 @@ parse_options(int argc, char *argv[])
         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);
             }
@@ -264,7 +265,7 @@ 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");
 }
@@ -297,8 +298,8 @@ static void add_del_ports(int argc UNUSED, char *argv[],
     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;
         }
     }
@@ -327,8 +328,14 @@ static void do_del_port(int argc UNUSED, char *argv[])
 \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)
@@ -339,20 +346,20 @@ alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
 }
 
 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);
@@ -361,23 +368,23 @@ dump_transaction(const char *vconn_name, struct buffer *request)
 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;
@@ -386,13 +393,13 @@ dump_stats_transaction(const char *vconn_name, struct buffer *request)
 
             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);
 }
@@ -400,7 +407,7 @@ dump_stats_transaction(const char *vconn_name, struct buffer *request)
 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);
 }
@@ -415,12 +422,32 @@ do_show(int argc UNUSED, char *argv[])
 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
@@ -445,7 +472,7 @@ str_to_int(const char *str)
     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;
 }
@@ -455,7 +482,7 @@ str_to_mac(const char *str, uint8_t mac[6])
 {
     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);
     }
 }
 
@@ -471,7 +498,7 @@ str_to_ip(const char *str_, uint32_t *ip)
     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;
 
@@ -494,13 +521,15 @@ str_to_ip(const char *str_, uint32_t *ip)
             /* 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;
         }
@@ -567,7 +596,7 @@ str_to_action(char *str, struct ofp_action *action, int *n_actions)
         } 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) {
@@ -668,13 +697,13 @@ str_to_flow(char *string, struct ofp_match *match,
     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++;
@@ -700,7 +729,7 @@ str_to_flow(char *string, struct ofp_match *match,
 
             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")) {
@@ -730,7 +759,7 @@ str_to_flow(char *string, struct ofp_match *match,
                     }
                 }
             } else {
-                fatal(0, "unknown keyword %s", name);
+                ofp_fatal(0, "unknown keyword %s", name);
             }
         }
     }
@@ -740,7 +769,7 @@ str_to_flow(char *string, struct ofp_match *match,
 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, 
@@ -753,7 +782,7 @@ static void do_dump_flows(int argc, char *argv[])
 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,
@@ -766,13 +795,13 @@ static void do_dump_aggregate(int argc, char *argv[])
 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]);
+    open_vconn(argv[1], &vconn);
 
     /* Parse and send. */
     size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
@@ -786,7 +815,7 @@ static void do_add_flow(int argc, char *argv[])
     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);
@@ -802,12 +831,12 @@ static void do_add_flows(int argc, char *argv[])
 
     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;
@@ -838,7 +867,7 @@ static void do_add_flows(int argc, char *argv[])
         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);
@@ -852,8 +881,8 @@ 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;
+    open_vconn(argv[1], &vconn);
+    struct ofpbuf *buffer;
     struct ofp_flow_mod *ofm;
     size_t size;
 
@@ -889,12 +918,12 @@ do_monitor(int argc UNUSED, char *argv[])
     } else {
         name = argv[1];
     }
-    run(vconn_open_block(argv[1], &vconn), "connecting to %s", name);
+    open_vconn(argv[1], &vconn);
     for (;;) {
-        struct buffer *b;
+        struct ofpbuf *b;
         run(vconn_recv_block(vconn, &b), "vconn_recv");
         ofp_print(stderr, b->data, b->size, 2);
-        buffer_delete(b);
+        ofpbuf_delete(b);
     }
 }
 
@@ -907,24 +936,24 @@ 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;
@@ -944,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;
@@ -966,7 +995,7 @@ do_mod_port(int argc, char *argv[])
         }
     }
     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);
@@ -990,12 +1019,12 @@ do_mod_port(int argc, char *argv[])
         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);
 }
 
@@ -1009,13 +1038,13 @@ do_ping(int argc, char *argv[])
 
     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,
@@ -1023,7 +1052,7 @@ do_ping(int argc, char *argv[])
         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;
@@ -1040,8 +1069,8 @@ do_ping(int argc, char *argv[])
                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);
 }
@@ -1059,7 +1088,7 @@ do_benchmark(int argc, char *argv[])
 
     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;
 
@@ -1068,16 +1097,16 @@ 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 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);