Better abstract OpenFlow error codes.
[sliver-openvswitch.git] / lib / ofp-print.c
index fe4af4c..aff12b6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
 #include "learn.h"
 #include "multipath.h"
 #include "nx-match.h"
+#include "ofp-errors.h"
 #include "ofp-util.h"
 #include "ofpbuf.h"
 #include "openflow/openflow.h"
@@ -45,7 +46,7 @@
 #include "util.h"
 
 static void ofp_print_queue_name(struct ds *string, uint32_t port);
-static void ofp_print_error(struct ds *, int error);
+static void ofp_print_error(struct ds *, enum ofperr);
 
 
 /* Returns a string that represents the contents of the Ethernet frame in the
@@ -60,42 +61,87 @@ ofp_packet_to_string(const void *data, size_t len)
     ofpbuf_use_const(&buf, data, len);
     flow_extract(&buf, 0, 0, 0, &flow);
     flow_format(&ds, &flow);
+
+    if (buf.l7) {
+        if (flow.nw_proto == IPPROTO_TCP) {
+            struct tcp_header *th = buf.l4;
+            ds_put_format(&ds, " tcp_csum:%"PRIx16,
+                          ntohs(th->tcp_csum));
+        } else if (flow.nw_proto == IPPROTO_UDP) {
+            struct udp_header *uh = buf.l4;
+            ds_put_format(&ds, " udp_csum:%"PRIx16,
+                          ntohs(uh->udp_csum));
+        }
+    }
+
     ds_put_char(&ds, '\n');
 
     return ds_cstr(&ds);
 }
 
 static void
-ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
+ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
                     int verbosity)
 {
-    size_t len = ntohs(op->header.length);
-    size_t data_len;
+    struct ofputil_packet_in pin;
+    int error;
+    int i;
+
+    error = ofputil_decode_packet_in(&pin, oh);
+    if (error) {
+        ofp_print_error(string, error);
+        return;
+    }
+
+    if (pin.table_id) {
+        ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
+    }
+
+    if (pin.cookie) {
+        ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
+    }
+
+    ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
+    ofputil_format_port(pin.fmd.in_port, string);
+
+    if (pin.fmd.tun_id_mask) {
+        ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
+        if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
+            ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
+        }
+    }
 
-    ds_put_format(string, " total_len=%"PRIu16" in_port=",
-                  ntohs(op->total_len));
-    ofputil_format_port(ntohs(op->in_port), string);
+    for (i = 0; i < FLOW_N_REGS; i++) {
+        if (pin.fmd.reg_masks[i]) {
+            ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
+            if (pin.fmd.reg_masks[i] != UINT32_MAX) {
+                ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
+            }
+        }
+    }
 
-    if (op->reason == OFPR_ACTION)
+    if (pin.reason == OFPR_ACTION) {
         ds_put_cstr(string, " (via action)");
-    else if (op->reason != OFPR_NO_MATCH)
-        ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
+    } else if (pin.reason != OFPR_NO_MATCH) {
+        ds_put_format(string, " (***reason %"PRIu8"***)", pin.reason);
+    }
 
-    data_len = len - offsetof(struct ofp_packet_in, data);
-    ds_put_format(string, " data_len=%zu", data_len);
-    if (op->buffer_id == htonl(UINT32_MAX)) {
+    ds_put_format(string, " data_len=%zu", pin.packet_len);
+    if (pin.buffer_id == UINT32_MAX) {
         ds_put_format(string, " (unbuffered)");
-        if (ntohs(op->total_len) != data_len)
+        if (pin.total_len != pin.packet_len) {
             ds_put_format(string, " (***total_len != data_len***)");
+        }
     } else {
-        ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
-        if (ntohs(op->total_len) < data_len)
+        ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
+        if (pin.total_len < pin.packet_len) {
             ds_put_format(string, " (***total_len < data_len***)");
+        }
     }
     ds_put_char(string, '\n');
 
     if (verbosity > 0) {
-        char *packet = ofp_packet_to_string(op->data, data_len);
+        char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
         ds_put_cstr(string, packet);
         free(packet);
     }
@@ -701,7 +747,7 @@ ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
 {
     struct ofputil_flow_mod fm;
     bool need_priority;
-    int error;
+    enum ofperr error;
 
     error = ofputil_decode_flow_mod(&fm, oh, true);
     if (error) {
@@ -800,7 +846,7 @@ static void
 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
 {
     struct ofputil_flow_removed fr;
-    int error;
+    enum ofperr error;
 
     error = ofputil_decode_flow_removed(&fr, oh);
     if (error) {
@@ -851,14 +897,12 @@ ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
 }
 
 static void
-ofp_print_error(struct ds *string, int error)
+ofp_print_error(struct ds *string, enum ofperr error)
 {
     if (string->length) {
         ds_put_char(string, ' ');
     }
-    ds_put_cstr(string, "***decode error: ");
-    ofputil_format_error(string, error);
-    ds_put_cstr(string, "***\n");
+    ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
 }
 
 static void
@@ -867,32 +911,26 @@ ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
     size_t len = ntohs(oem->header.length);
     size_t payload_ofs, payload_len;
     const void *payload;
-    int error;
+    enum ofperr error;
     char *s;
 
-    error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
-    if (!is_ofp_error(error)) {
-        ofp_print_error(string, error);
+    error = ofperr_decode_msg(&oem->header, &payload_ofs);
+    if (!error) {
+        ds_put_cstr(string, "***decode error***");
         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
         return;
     }
 
-    ds_put_char(string, ' ');
-    ofputil_format_error(string, error);
-    ds_put_char(string, '\n');
+    ds_put_format(string, " %s\n", ofperr_get_name(error));
 
     payload = (const uint8_t *) oem + payload_ofs;
     payload_len = len - payload_ofs;
-    switch (get_ofp_err_type(error)) {
-    case OFPET_HELLO_FAILED:
+    if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
         ds_put_printable(string, payload, payload_len);
-        break;
-
-    default:
+    } else {
         s = ofp_to_string(payload, payload_len, 1);
         ds_put_cstr(string, s);
         free(s);
-        break;
     }
 }
 
@@ -931,7 +969,7 @@ ofp_print_flow_stats_request(struct ds *string,
                              const struct ofp_stats_msg *osm)
 {
     struct ofputil_flow_stats_request fsr;
-    int error;
+    enum ofperr error;
 
     error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
     if (error) {
@@ -1208,14 +1246,14 @@ ofp_print_nxt_role_message(struct ds *string,
 
 static void
 ofp_print_nxt_flow_mod_table_id(struct ds *string,
-                                const struct nxt_flow_mod_table_id *nfmti)
+                                const struct nx_flow_mod_table_id *nfmti)
 {
     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
 }
 
 static void
 ofp_print_nxt_set_flow_format(struct ds *string,
-                              const struct nxt_set_flow_format *nsff)
+                              const struct nx_set_flow_format *nsff)
 {
     uint32_t format = ntohl(nsff->format);
 
@@ -1227,6 +1265,20 @@ ofp_print_nxt_set_flow_format(struct ds *string,
     }
 }
 
+static void
+ofp_print_nxt_set_packet_in_format(struct ds *string,
+                                   const struct nx_set_packet_in_format *nspf)
+{
+    uint32_t format = ntohl(nspf->format);
+
+    ds_put_cstr(string, " format=");
+    if (ofputil_packet_in_format_is_valid(format)) {
+        ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
+    } else {
+        ds_put_format(string, "%"PRIu32, format);
+    }
+}
+
 static void
 ofp_to_string__(const struct ofp_header *oh,
                 const struct ofputil_msg_type *type, struct ds *string,
@@ -1274,6 +1326,7 @@ ofp_to_string__(const struct ofp_header *oh,
         break;
 
     case OFPUTIL_OFPT_PACKET_IN:
+    case OFPUTIL_NXT_PACKET_IN:
         ofp_print_packet_in(string, msg, verbosity);
         break;
 
@@ -1377,6 +1430,10 @@ ofp_to_string__(const struct ofp_header *oh,
         ofp_print_nxt_set_flow_format(string, msg);
         break;
 
+    case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
+        ofp_print_nxt_set_packet_in_format(string, msg);
+        break;
+
     case OFPUTIL_NXT_FLOW_MOD:
         ofp_print_flow_mod(string, msg, code, verbosity);
         break;
@@ -1403,9 +1460,6 @@ ofp_to_string(const void *oh_, size_t len, int verbosity)
     } else if (len < sizeof(struct ofp_header)) {
         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
                       len);
-    } else if (oh->version != OFP_VERSION) {
-        ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
-                      oh->version);
     } else if (ntohs(oh->length) > len) {
         ds_put_format(&string,
                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
@@ -1416,7 +1470,7 @@ ofp_to_string(const void *oh_, size_t len, int verbosity)
                       ntohs(oh->length), len);
     } else {
         const struct ofputil_msg_type *type;
-        int error;
+        enum ofperr error;
 
         error = ofputil_decode_msg_type(oh, &type);
         if (!error) {