Fixed problem where the first line of a stats reply message was not showing up on...
[sliver-openvswitch.git] / lib / ofp-print.c
index 8b2da7e..142943f 100644 (file)
@@ -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, "%d", 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));
@@ -378,7 +378,7 @@ static void ofp_print_match(struct ds *f, const struct ofp_match *om)
     print_wild(f, "] proto", w & OFPFW_NW_PROTO, "%u", om->nw_proto);
     print_wild(f, " tport[", w & OFPFW_TP_SRC, "%d", ntohs(om->tp_src));
     print_wild(f, "->", w & OFPFW_TP_DST, "%d", ntohs(om->tp_dst));
-    ds_put_cstr(f, "]\n");
+    ds_put_cstr(f, "]");
 }
 
 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
@@ -406,8 +406,10 @@ ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
 
     ofp_print_match(string, &ofe->match);
     ds_put_format(string, 
-         " secs%d pkts%lld bytes%lld\n", ntohl(ofe->duration),
-         ntohll(ofe->packet_count), ntohll(ofe->byte_count));
+         " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"n", 
+         ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
+         ntohl(ofe->duration), ntohll(ofe->packet_count), 
+         ntohll(ofe->byte_count));
 }
 
 /* Pretty-print the OFPT_ERROR_MSG packet of 'len' bytes at 'oh' to 'string'
@@ -455,31 +457,48 @@ ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
         ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
     }
 
-    if (fsr->type == OFPFS_INDIV) {
-        ds_put_cstr(string, " type=indiv, ");
-    } else if (fsr->type == OFPFS_AGGREGATE) {
-        ds_put_cstr(string, " type=aggregate, ");
-    } else {
-        ds_put_format(string, " ***type=%"PRIu8"***, ", fsr->type);
-    }
     ofp_print_match(string, &fsr->match);
 }
 
 static void
-ofp_flow_stats_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_stats_reply *fsr = oh;
-    const struct ofp_flow_stats *fs;
-    size_t n;
+    const char *body = body_;
+    const char *pos = body;
+    for (;;) {
+        const struct ofp_flow_stats *fs;
+        ptrdiff_t bytes_left = body + len - pos;
+        size_t length;
+
+        if (bytes_left < sizeof *fs) {
+            if (bytes_left != 0) {
+                ds_put_format(string, " ***%td leftover bytes at end***",
+                              bytes_left);
+            }
+            break;
+        }
 
-    n = (len - offsetof(struct ofp_flow_stats_reply, flows)) / sizeof *fs;
-    ds_put_format(string, " %zu flows\n", n);
-    if (verbosity < 1) {
-        return;
-    }
+        fs = (const void *) pos;
+        length = ntohs(fs->length);
+        if (length < sizeof *fs) {
+            ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
+                          length, sizeof *fs);
+            break;
+        } else if (length > bytes_left) {
+            ds_put_format(string,
+                          " ***length=%zu but only %td bytes left***",
+                          length, bytes_left);
+            break;
+        } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
+            ds_put_format(string,
+                          " ***length=%zu has %zu bytes leftover in "
+                          "final action***",
+                          length,
+                          (length - sizeof *fs) % sizeof fs->actions[0]);
+            break;
+        }
 
-    for (fs = &fsr->flows[0]; fs < &fsr->flows[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", ", 
@@ -487,25 +506,53 @@ ofp_flow_stats_reply(struct ds *string, const void *oh, size_t len,
         ds_put_format(string, "n_packets=%"PRIu64", ",
                     ntohll(fs->packet_count));
         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
+        ds_put_format(string, "max_idle=%"PRIu16", ", ntohs(fs->max_idle));
         ofp_print_match(string, &fs->match);
+        ofp_print_actions(string, fs->actions, length - sizeof *fs);
+        ds_put_char(string, '\n');
+
+        pos += length;
      }
 }
 
 static void
-ofp_port_stats_reply(struct ds *string, const void *oh, size_t len,
-                    int verbosity)
+ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
+                            int verbosity) 
 {
-    const struct ofp_port_stats_reply *psr = oh;
-    const struct ofp_port_stats *ps;
-    size_t n;
+    const struct ofp_aggregate_stats_request *asr = oh;
+
+    if (asr->table_id == 0xff) {
+        ds_put_format(string, " table_id=any, ");
+    } else {
+        ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
+    }
 
-    n = (len - offsetof(struct ofp_port_stats_reply, ports)) / sizeof *ps;
+    ofp_print_match(string, &asr->match);
+}
+
+static void
+ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
+                          int verbosity)
+{
+    const struct ofp_aggregate_stats_reply *asr = body_;
+
+    ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
+    ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
+    ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
+}
+
+static void
+ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
+                     int verbosity)
+{
+    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));
@@ -514,20 +561,17 @@ ofp_port_stats_reply(struct ds *string, const void *oh, size_t len,
 }
 
 static void
-ofp_table_stats_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_stats_reply *tsr = oh;
-    const struct ofp_table_stats *ts;
-    size_t n;
-
-    n = (len - offsetof(struct ofp_table_stats_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';
@@ -541,6 +585,115 @@ ofp_table_stats_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_AGGREGATE] = {
+            "aggregate",
+            { sizeof(struct ofp_aggregate_stats_request),
+              sizeof(struct ofp_aggregate_stats_request),
+              ofp_aggregate_stats_request },
+            { sizeof(struct ofp_aggregate_stats_reply),
+              sizeof(struct ofp_aggregate_stats_reply),
+              ofp_aggregate_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)\n", 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;
@@ -608,35 +761,15 @@ static const struct openflow_packet packets[] = {
         sizeof (struct ofp_error_msg),
         ofp_print_error_msg,
     },
-    [OFPT_FLOW_STATS_REQUEST] = {
-        "flow_stats_request",
-        sizeof (struct ofp_flow_stats_request),
-        ofp_flow_stats_request,
-    },
-    [OFPT_FLOW_STATS_REPLY] = {
-        "flow_stats_reply",
-        sizeof (struct ofp_flow_stats_reply),
-        ofp_flow_stats_reply,
-    },
-    [OFPT_PORT_STATS_REQUEST] = {
-        "port_stats_request",
-        sizeof (struct ofp_port_stats_request),
-        NULL,
-    },
-    [OFPT_PORT_STATS_REPLY] = {
-        "port_stats_reply",
-        sizeof (struct ofp_port_stats_reply),
-        ofp_port_stats_reply,
-    },
-    [OFPT_TABLE_STATS_REQUEST] = {
-        "table_stats_request",
-        sizeof (struct ofp_table_stats_request),
-        NULL,
+    [OFPT_STATS_REQUEST] = {
+        "stats_request",
+        sizeof (struct ofp_stats_request),
+        ofp_stats_request,
     },
-    [OFPT_TABLE_STATS_REPLY] = {
-        "table_stats_reply",
-        sizeof (struct ofp_table_stats_reply),
-        ofp_table_stats_reply,
+    [OFPT_STATS_REPLY] = {
+        "stats_reply",
+        sizeof (struct ofp_stats_reply),
+        ofp_stats_reply,
     },
 };
 
@@ -683,7 +816,7 @@ ofp_to_string(const void *oh_, size_t len, int verbosity)
                 len, pkt->min_size);
     } else if (!pkt->printer) {
         if (len > sizeof *oh) {
-            ds_put_format(&string, " length=%zu (decoder not implemented)\n",
+            ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
                           ntohs(oh->length)); 
         }
     } else {
@@ -692,6 +825,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