Fixed problem where the first line of a stats reply message was not showing up on...
[sliver-openvswitch.git] / lib / ofp-print.c
index 8b056fd..142943f 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));
@@ -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'
@@ -390,27 +390,45 @@ 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;
 
     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'
+ * 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,10 +446,10 @@ 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 == 0xff) {
         ds_put_format(string, " table_id=any, ");
@@ -439,55 +457,102 @@ ofp_flow_stat_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_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;
+    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_stat_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, ", ntohs(fs->duration));
+        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));
+        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_stat_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_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);
+    }
+
+    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_port_stat_reply *psr = oh;
-    const struct ofp_port_stats *ps;
-    size_t n;
+    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));
+}
 
-    n = (len - offsetof(struct ofp_port_stat_reply, ports)) / sizeof *ps;
+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));
@@ -496,20 +561,17 @@ 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';
@@ -523,6 +585,115 @@ 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_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;
@@ -585,35 +756,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_PORT_STAT_REQUEST] = {
-        "port_stat_request",
-        sizeof (struct ofp_port_stat_request),
-        NULL,
+    [OFPT_STATS_REQUEST] = {
+        "stats_request",
+        sizeof (struct ofp_stats_request),
+        ofp_stats_request,
     },
-    [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 +788,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);
@@ -660,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 {
@@ -669,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