Implement new statistics format in kernel and userspace switches.
[sliver-openvswitch.git] / lib / ofp-print.c
index c728b5d..4c19bb9 100644 (file)
@@ -302,8 +302,8 @@ ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
     int i;
 
     ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
-    ds_put_format(string, "tables: exact:%d, mac:%d, compressed:%d, general:%d\n",
-           ntohl(osf->n_exact), ntohl(osf->n_mac_only),
+    ds_put_format(string, "tables: exact:%d, compressed:%d, general:%d\n",
+           ntohl(osf->n_exact), 
            ntohl(osf->n_compression), ntohl(osf->n_general));
     ds_put_format(string, "buffers: size:%d, number:%d\n",
            ntohl(osf->buffer_mb), ntohl(osf->n_buffers));
@@ -366,7 +366,7 @@ static void ofp_print_match(struct ds *f, const struct ofp_match *om)
 {
     uint16_t w = ntohs(om->wildcards);
 
-    print_wild(f, "inport", w & OFPFW_IN_PORT, "%04x", ntohs(om->in_port));
+    print_wild(f, "inport", w & OFPFW_IN_PORT, "%d", ntohs(om->in_port));
     print_wild(f, ":vlan", w & OFPFW_DL_VLAN, "%04x", ntohs(om->dl_vlan));
     print_wild(f, " mac[", w & OFPFW_DL_SRC,
                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
@@ -390,14 +390,17 @@ ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
     const struct ofp_flow_mod *ofm = oh;
 
     ofp_print_match(string, &ofm->match);
-    ds_put_format(string, " cmd:%d idle:%d buf:%#x grp:%d\n", ntohs(ofm->command),
-         ntohs(ofm->max_idle), ntohl(ofm->buffer_id), ntohl(ofm->group_id));
+    ds_put_format(string, " cmd:%d idle:%d pri:%d buf:%#x\n", 
+            ntohs(ofm->command), ntohs(ofm->max_idle), 
+            ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
+            ntohl(ofm->buffer_id));
 }
 
 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
  * at the given 'verbosity' level. */
-void ofp_print_flow_expired(struct ds *string, const void *oh, size_t len, 
-        int verbosity)
+static void
+ofp_print_flow_expired(struct ds *string, const void *oh, size_t len, 
+                       int verbosity)
 {
     const struct ofp_flow_expired *ofe = oh;
 
@@ -407,10 +410,23 @@ void ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
          ntohll(ofe->packet_count), ntohll(ofe->byte_count));
 }
 
+/* Pretty-print the OFPT_ERROR_MSG packet of 'len' bytes at 'oh' to 'string'
+ * at the given 'verbosity' level. */
+static void
+ofp_print_error_msg(struct ds *string, const void *oh, size_t len, 
+                       int verbosity)
+{
+    const struct ofp_error_msg *oem = oh;
+
+    ds_put_format(string, 
+         " type%d code%d\n", ntohs(oem->type), ntohs(oem->code));
+}
+
 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
  * at the given 'verbosity' level. */
-void ofp_print_port_status(struct ds *string, const void *oh, size_t len, 
-        int verbosity)
+static void
+ofp_print_port_status(struct ds *string, const void *oh, size_t len, 
+                      int verbosity)
 {
     const struct ofp_port_status *ops = oh;
 
@@ -428,15 +444,15 @@ void ofp_print_port_status(struct ds *string, const void *oh, size_t len,
 }
 
 static void
-ofp_flow_stat_request(struct ds *string, const void *oh, size_t len,
+ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
                       int verbosity) 
 {
-    const struct ofp_flow_stat_request *fsr = oh;
+    const struct ofp_flow_stats_request *fsr = oh;
 
-    if (fsr->table_id == htons(0xffff)) {
+    if (fsr->table_id == 0xff) {
         ds_put_format(string, " table_id=any, ");
     } else {
-        ds_put_format(string, " table_id=%"PRIu16", ", ntohs(fsr->table_id));
+        ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
     }
 
     if (fsr->type == OFPFS_INDIV) {
@@ -450,44 +466,40 @@ ofp_flow_stat_request(struct ds *string, const void *oh, size_t len,
 }
 
 static void
-ofp_flow_stat_reply(struct ds *string, const void *oh, size_t len,
+ofp_flow_stats_reply(struct ds *string, const void *body, size_t len,
                      int verbosity)
 {
-    const struct ofp_flow_stat_reply *fsr = oh;
-    const struct ofp_flow_stats *fs;
-    size_t n;
-
-    n = (len - offsetof(struct ofp_flow_stat_reply, flows)) / sizeof *fs;
+    const struct ofp_flow_stats *fs = body;
+    size_t n = len / sizeof *fs;
     ds_put_format(string, " %zu flows\n", n);
     if (verbosity < 1) {
         return;
     }
 
-    for (fs = &fsr->flows[0]; fs < &fsr->flows[n]; fs++) {
-        ds_put_format(string, "  duration=%"PRIu16" s, ", ntohs(fs->duration));
-        ds_put_format(string, "table_id=%"PRIu16", ", ntohs(fs->table_id));
+    for (; n--; fs++) {
+        ds_put_format(string, "  duration=%"PRIu32" s, ", ntohl(fs->duration));
+        ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
+        ds_put_format(string, "priority=%"PRIu16", ", 
+                    fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
         ds_put_format(string, "n_packets=%"PRIu64", ",
-                      ntohll(fs->packet_count));
+                    ntohll(fs->packet_count));
         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
         ofp_print_match(string, &fs->match);
      }
 }
 
 static void
-ofp_port_stat_reply(struct ds *string, const void *oh, size_t len,
-                    int verbosity)
+ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
+                     int verbosity)
 {
-    const struct ofp_port_stat_reply *psr = oh;
-    const struct ofp_port_stats *ps;
-    size_t n;
-
-    n = (len - offsetof(struct ofp_port_stat_reply, ports)) / sizeof *ps;
+    const struct ofp_port_stats *ps = body;
+    size_t n = len / sizeof *ps;
     ds_put_format(string, " %zu ports\n", n);
     if (verbosity < 1) {
         return;
     }
 
-    for (ps = &psr->ports[0]; ps < &psr->ports[n]; ps++) {
+    for (; n--; ps++) {
         ds_put_format(string, "  port %"PRIu16": ", ntohs(ps->port_no));
         ds_put_format(string, "rx %"PRIu64", ", ntohll(ps->rx_count));
         ds_put_format(string, "tx %"PRIu64", ", ntohll(ps->tx_count));
@@ -496,25 +508,22 @@ ofp_port_stat_reply(struct ds *string, const void *oh, size_t len,
 }
 
 static void
-ofp_table_stat_reply(struct ds *string, const void *oh, size_t len,
+ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
                      int verbosity)
 {
-    const struct ofp_table_stat_reply *tsr = oh;
-    const struct ofp_table_stats *ts;
-    size_t n;
-
-    n = (len - offsetof(struct ofp_table_stat_reply, tables)) / sizeof *ts;
+    const struct ofp_table_stats *ts = body;
+    size_t n = len / sizeof *ts;
     ds_put_format(string, " %zu tables\n", n);
     if (verbosity < 1) {
         return;
     }
 
-    for (ts = &tsr->tables[0]; ts < &tsr->tables[n]; ts++) {
+    for (; n--; ts++) {
         char name[OFP_MAX_TABLE_NAME_LEN + 1];
         strncpy(name, ts->name, sizeof name);
         name[OFP_MAX_TABLE_NAME_LEN] = '\0';
 
-        ds_put_format(string, "  table %"PRIu16": ", ntohs(ts->table_id));
+        ds_put_format(string, "  table %"PRIu8": ", ts->table_id);
         ds_put_format(string, "name %-8s, ", name);
         ds_put_format(string, "max %6"PRIu32", ", ntohl(ts->max_entries));
         ds_put_format(string, "active %6"PRIu32", ", ntohl(ts->active_count));
@@ -523,6 +532,106 @@ ofp_table_stat_reply(struct ds *string, const void *oh, size_t len,
      }
 }
 
+enum stats_direction {
+    REQUEST,
+    REPLY
+};
+
+static void
+print_stats(struct ds *string, int type, const void *body, size_t body_len,
+            int verbosity, enum stats_direction direction)
+{
+    struct stats_msg {
+        size_t min_body, max_body;
+        void (*printer)(struct ds *, const void *, size_t len, int verbosity);
+    };
+
+    struct stats_type {
+        const char *name;
+        struct stats_msg request;
+        struct stats_msg reply;
+    };
+
+    static const struct stats_type stats_types[] = {
+        [OFPST_FLOW] = {
+            "flow",
+            { sizeof(struct ofp_flow_stats_request),
+              sizeof(struct ofp_flow_stats_request),
+              ofp_flow_stats_request },
+            { 0, SIZE_MAX, ofp_flow_stats_reply },
+        },
+        [OFPST_TABLE] = {
+            "table",
+            { 0, 0, NULL },
+            { 0, SIZE_MAX, ofp_table_stats_reply },
+        },
+        [OFPST_PORT] = {
+            "port",
+            { 0, 0, NULL, },
+            { 0, SIZE_MAX, ofp_port_stats_reply },
+        },
+    };
+
+    const struct stats_type *s;
+    const struct stats_msg *m;
+
+    if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
+        ds_put_format(string, " ***unknown type %d***", type);
+        return;
+    }
+    s = &stats_types[type];
+    ds_put_format(string, " type=%d(%s)", type, s->name);
+
+    m = direction == REQUEST ? &s->request : &s->reply;
+    if (body_len < m->min_body || body_len > m->max_body) {
+        ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
+                      body_len, m->min_body, m->max_body);
+        return;
+    }
+    if (m->printer) {
+        m->printer(string, body, body_len, verbosity);
+    }
+}
+
+static void
+ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
+{
+    const struct ofp_stats_request *srq = oh;
+
+    if (srq->flags) {
+        ds_put_format(string, " ***unknown flags %04"PRIx16"***",
+                      ntohs(srq->flags));
+    }
+
+    print_stats(string, ntohs(srq->type), srq->body,
+                len - offsetof(struct ofp_stats_request, body),
+                verbosity, REQUEST);
+}
+
+static void
+ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
+{
+    const struct ofp_stats_reply *srp = oh;
+
+    ds_put_cstr(string, " flags=");
+    if (!srp->flags) {
+        ds_put_cstr(string, "none");
+    } else {
+        uint16_t flags = ntohs(srp->flags);
+        if (flags & OFPSF_REPLY_MORE) {
+            ds_put_cstr(string, "[more]");
+            flags &= ~OFPSF_REPLY_MORE;
+        }
+        if (flags) {
+            ds_put_format(string, "[***unknown%04"PRIx16"***]", flags);
+        }
+    }
+
+    print_stats(string, ntohs(srp->type), srp->body,
+                len - offsetof(struct ofp_stats_reply, body),
+                verbosity, REPLY);
+}
+
 struct openflow_packet {
     const char *name;
     size_t min_size;
@@ -585,35 +694,20 @@ static const struct openflow_packet packets[] = {
         sizeof (struct ofp_port_status),
         ofp_print_port_status
     },
-    [OFPT_FLOW_STAT_REQUEST] = {
-        "flow_stat_request",
-        sizeof (struct ofp_flow_stat_request),
-        ofp_flow_stat_request,
+    [OFPT_ERROR_MSG] = {
+        "error_msg",
+        sizeof (struct ofp_error_msg),
+        ofp_print_error_msg,
     },
-    [OFPT_FLOW_STAT_REPLY] = {
-        "flow_stat_reply",
-        sizeof (struct ofp_flow_stat_reply),
-        ofp_flow_stat_reply,
+    [OFPT_STATS_REQUEST] = {
+        "stats_request",
+        sizeof (struct ofp_stats_request),
+        ofp_stats_request,
     },
-    [OFPT_PORT_STAT_REQUEST] = {
-        "port_stat_request",
-        sizeof (struct ofp_port_stat_request),
-        NULL,
-    },
-    [OFPT_PORT_STAT_REPLY] = {
-        "port_stat_reply",
-        sizeof (struct ofp_port_stat_reply),
-        ofp_port_stat_reply,
-    },
-    [OFPT_TABLE_STAT_REQUEST] = {
-        "table_stat_request",
-        sizeof (struct ofp_table_stat_request),
-        NULL,
-    },
-    [OFPT_TABLE_STAT_REPLY] = {
-        "table_stat_reply",
-        sizeof (struct ofp_table_stat_reply),
-        ofp_table_stat_reply,
+    [OFPT_STATS_REPLY] = {
+        "stats_reply",
+        sizeof (struct ofp_stats_reply),
+        ofp_stats_reply,
     },
 };
 
@@ -632,7 +726,7 @@ ofp_to_string(const void *oh_, size_t len, int verbosity)
         ds_put_cstr(&string, "OpenFlow packet too short:\n");
         ds_put_hex_dump(&string, oh, len, 0, true);
         return ds_cstr(&string);
-    } else if (oh->version != 1) {
+    } else if (oh->version != OFP_VERSION) {
         ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
         ds_put_hex_dump(&string, oh, len, 0, true);
         return ds_cstr(&string);
@@ -669,6 +763,9 @@ ofp_to_string(const void *oh_, size_t len, int verbosity)
     if (verbosity >= 3) {
         ds_put_hex_dump(&string, oh, len, 0, true);
     }
+    if (string.string[string.length - 1] != '\n') {
+        ds_put_char(&string, '\n');
+    }
     return ds_cstr(&string);
 }
 \f