Implement new statistics format in kernel and userspace switches.
[sliver-openvswitch.git] / utilities / dpctl.c
index 80808ac..a66975a 100644 (file)
@@ -172,6 +172,7 @@ usage(void)
            "  dump-flows SWITCH           print all flow entries\n"
            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
            "  add-flows SWITCH FILE       add flows from FILE\n"
+           "  del-flows SWITCH FLOW       delete matching FLOWs\n"
            "where each SWITCH is an active OpenFlow connection method.\n",
            program_name, program_name);
     vconn_usage(true, false);
@@ -197,7 +198,7 @@ static void run(int retval, const char *message, ...)
         if (retval == EOF) {
             fputs(": unexpected end of file\n", stderr);
         } else {
-            vfprintf(stderr, ": %s\n", strerror(retval));
+            fprintf(stderr, ": %s\n", strerror(retval));
         }
 
         exit(EXIT_FAILURE);
@@ -261,7 +262,7 @@ static void do_del_port(int argc UNUSED, char *argv[])
 static void do_monitor(int argc UNUSED, char *argv[])
 {
     struct dpif dp;
-    open_nl_vconn(argv[1], false, &dp);
+    open_nl_vconn(argv[1], true, &dp);
     for (;;) {
         struct buffer *b;
         run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
@@ -343,6 +344,17 @@ alloc_openflow_buffer(size_t openflow_len, uint8_t type,
        return oh;
 }
 
+static void *
+alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
+{
+    struct ofp_stats_request *rq;
+    rq = alloc_openflow_buffer((offsetof(struct ofp_stats_request, body)
+                                + body_len), OFPT_STATS_REQUEST, bufferp);
+    rq->type = htons(type);
+    rq->flags = htons(0);
+    return rq->body;
+}
+
 static void
 send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
 {
@@ -377,29 +389,76 @@ transact_openflow(struct vconn *vconn, struct buffer *request)
 }
 
 static void
-dump_transaction(const char *vconn_name, uint8_t request_type)
+dump_transaction(const char *vconn_name, struct buffer *request)
 {
     struct vconn *vconn;
-    struct buffer *request, *reply;
+    struct buffer *reply;
 
     run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
-    alloc_openflow_buffer(sizeof(struct ofp_header), request_type, &request);
     reply = transact_openflow(vconn, request);
     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;
+    alloc_openflow_buffer(sizeof(struct ofp_header), request_type, &request);
+    dump_transaction(vconn_name, request);
+}
+
+static void
+dump_stats_transaction(const char *vconn_name, struct buffer *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);
+    send_openflow_buffer(vconn, request);
+    while (!done) {
+        uint32_t recv_xid;
+        struct buffer *reply;
+
+        run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
+        recv_xid = ((struct ofp_header *) reply->data)->xid;
+        if (send_xid == recv_xid) {
+            struct ofp_stats_reply *osr;
+
+            ofp_print(stdout, reply->data, reply->size, 1);
+
+            osr = buffer_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);
+    }
+    vconn_close(vconn);
+}
+
+static void
+dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
+{
+    struct buffer *request;
+    alloc_stats_request(0, stats_type, &request);
+    dump_stats_transaction(vconn_name, request);
+}
+
 static void
 do_show(int argc UNUSED, char *argv[])
 {
-    dump_transaction(argv[1], OFPT_FEATURES_REQUEST);
+    dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
+    dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
 }
 
 
 static void
 do_dump_tables(int argc, char *argv[])
 {
-    dump_transaction(argv[1], OFPT_TABLE_STAT_REQUEST);
+    dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
 }
 
 
@@ -455,7 +514,7 @@ str_to_action(const char *str, struct ofp_action *action)
 
 static void
 str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
-            uint8_t *table_idx)
+            uint8_t *table_idx, uint16_t *priority)
 {
     struct field {
         const char *name;
@@ -485,6 +544,9 @@ str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
     if (table_idx) {
         *table_idx = 0xff;
     }
+    if (priority) {
+        *priority = OFP_DEFAULT_PRIORITY;
+    }
     memset(match, 0, sizeof *match);
     wildcards = OFPFW_ALL;
     for (name = strtok(string, "="), value = strtok(NULL, " \t\n");
@@ -505,6 +567,11 @@ str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
             continue;
         }
 
+        if (priority && !strcmp(name, "priority")) {
+            *priority = atoi(value);
+            continue;
+        }
+
         for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
             if (!strcmp(f->name, name)) {
                 goto found;
@@ -551,24 +618,21 @@ str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
 
 static void do_dump_flows(int argc, char *argv[])
 {
-    struct vconn *vconn;
-    struct buffer *request, *reply;
-    struct ofp_flow_stat_request *fsr;
+    struct ofp_flow_stats_request *req;
+    struct buffer *request;
 
-    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
-    fsr = alloc_openflow_buffer(sizeof *fsr, OFPT_FLOW_STAT_REQUEST, &request);
-    str_to_flow(argc > 2 ? argv[2] : "", &fsr->match, NULL, &fsr->table_id);
-    fsr->type = OFPFS_INDIV;
-    fsr->pad = 0;
-    reply = transact_openflow(vconn, request);
-    ofp_print(stdout, reply->data, reply->size, 1);
-    vconn_close(vconn);
+    req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
+    str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, &req->table_id, 
+            NULL);
+    req->type = OFPFS_INDIV;
+    req->pad = 0;
+
+    dump_stats_transaction(argv[1], request);
 }
 
 static void do_add_flows(int argc, char *argv[])
 {
     struct vconn *vconn;
-    char vconn_name[16];
 
     FILE *file;
     char line[1024];
@@ -578,10 +642,11 @@ static void do_add_flows(int argc, char *argv[])
         fatal(errno, "%s: open", argv[2]);
     }
 
-    run(vconn_open_block(vconn_name, &vconn), "connecting to %s", argv[1]);
+    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
     while (fgets(line, sizeof line, file)) {
         struct buffer *buffer;
         struct ofp_flow_mod *ofm;
+        uint16_t priority;
         size_t size;
 
         char *comment;
@@ -600,21 +665,48 @@ static void do_add_flows(int argc, char *argv[])
         /* Parse and send. */
         size = sizeof *ofm + sizeof ofm->actions[0];
         ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+        str_to_flow(line, &ofm->match, &ofm->actions[0], NULL, &priority);
         ofm->command = htons(OFPFC_ADD);
         ofm->max_idle = htons(50);
         ofm->buffer_id = htonl(UINT32_MAX);
-        ofm->group_id = htonl(0);
-        str_to_flow(line, &ofm->match, &ofm->actions[0], NULL);
-        run(vconn_send_block(vconn, buffer), "send OpenFlow packet");
+        ofm->priority = htons(priority);
+        ofm->reserved = htonl(0);
+
+        send_openflow_buffer(vconn, buffer);
     }
     vconn_close(vconn);
     fclose(file);
 }
 
+static void do_del_flows(int argc, char *argv[])
+{
+    struct vconn *vconn;
+
+    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+    struct buffer *buffer;
+    struct ofp_flow_mod *ofm;
+    size_t size;
+
+
+    /* Parse and send. */
+    size = sizeof *ofm;
+    ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+    ofm->command = htons(OFPFC_DELETE);
+    ofm->max_idle = htons(0);
+    ofm->buffer_id = htonl(UINT32_MAX);
+    ofm->priority = htons(0);
+    ofm->reserved = htonl(0);
+    str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, NULL);
+
+    send_openflow_buffer(vconn, buffer);
+
+    vconn_close(vconn);
+}
+
 static void
 do_dump_ports(int argc, char *argv[])
 {
-    dump_transaction(argv[1], OFPT_PORT_STAT_REQUEST);
+    dump_trivial_stats_transaction(argv[1], OFPST_PORT);
 }
 
 static void do_help(int argc UNUSED, char *argv[] UNUSED)
@@ -638,5 +730,7 @@ static struct command all_commands[] = {
     { "dump-tables", 1, 1, do_dump_tables },
     { "dump-flows", 1, 2, do_dump_flows },
     { "add-flows", 2, 2, do_add_flows },
+    { "del-flows", 1, 2, do_del_flows },
     { "dump-ports", 1, 1, do_dump_ports },
+    { NULL, 0, 0, NULL },
 };