ofp-util: Add more functions for supporting OpenFlow error codes.
authorBen Pfaff <blp@nicira.com>
Tue, 29 Jun 2010 23:08:07 +0000 (16:08 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 29 Jun 2010 23:14:22 +0000 (16:14 -0700)
lib/ofp-util.h
lib/xfif.c

index e4927f6..5a54748 100644 (file)
@@ -84,7 +84,38 @@ void normalize_match(struct ofp_match *);
 
 void hton_ofp_phy_port(struct ofp_phy_port *);
 void ntoh_ofp_phy_port(struct ofp_phy_port *);
+\f
+/* OpenFlow errors.
+ *
+ * OpenFlow errors have two 16-bit parts: a "type" and a "code".  A "type" has
+ * a unique meaning.  The "code" values are different for each "type".
+ *
+ * We embed OpenFlow errors in the same space as errno values by shifting
+ * 'type' left 16 bits and adding the 'code'.  An "int" value is thus broken
+ * into a few different ranges:
+ *
+ *      - 0: success.
+ *
+ *      - 1...65535: system errno values.
+ *
+ *        The assumption that system errno values are less than 65536 is true
+ *        on at least Linux, FreeBSD, OpenBSD, and Windows.  RFC 1813 defines
+ *        NFSv3-specific errno codes starting at 10000, another hint that this
+ *        is a reasonable assumption.
+ *
+ *        C and POSIX say that errno values are positive.
+ *
+ *      - 65536...INT_MAX: OpenFlow errors.
+ *
+ *        In OpenFlow, a "type" of 0 is valid, but it corresponds to
+ *        OFPET_HELLO_FAILED.  That's not a general-purpose error: only the
+ *        vconn library would ever care to send it.  So we ignore it.
+ *
+ *      - negative values: not used.
+ */
 
+/* Returns the OpenFlow error with the specified 'type' and 'code' as an
+ * integer. */
 static inline int
 ofp_mkerr(uint16_t type, uint16_t code)
 {
@@ -92,4 +123,35 @@ ofp_mkerr(uint16_t type, uint16_t code)
     return (type << 16) | code;
 }
 
+/* Returns true if 'error' is in the range of values used as OpenFlow error
+ * codes as explained above. */
+static inline bool
+is_ofp_error(int error)
+{
+    return error >= 0x10000;
+}
+
+/* Returns true if 'error' appears to be a system errno value. */
+static inline bool
+is_errno(int error)
+{
+    return error < 0x10000;
+}
+
+/* Returns the "type" part of the OpenFlow error code 'error' (which must be in
+ * the format explained above). */
+static inline uint16_t
+get_ofp_err_type(int error)
+{
+    return error >> 16;
+}
+
+/* Returns the "code" part of the OpenFlow error code 'error' (which must be in
+ * the format explained above). */
+static inline uint16_t
+get_ofp_err_code(int error)
+{
+    return error & 0xffff;
+}
+
 #endif /* ofp-util.h */
index b19c706..91e8949 100644 (file)
@@ -30,6 +30,7 @@
 #include "netlink.h"
 #include "xflow-util.h"
 #include "ofp-print.h"
+#include "ofp-util.h"
 #include "ofpbuf.h"
 #include "packets.h"
 #include "poll-loop.h"
@@ -1134,9 +1135,13 @@ log_operation(const struct xfif *xfif, const char *operation, int error)
 {
     if (!error) {
         VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", xfif_name(xfif), operation);
-    } else {
+    } else if (is_errno(error)) {
         VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
                      xfif_name(xfif), operation, strerror(error));
+    } else {
+        VLOG_WARN_RL(&error_rl, "%s: %s failed (%d/%d)",
+                     xfif_name(xfif), operation,
+                     get_ofp_err_type(error), get_ofp_err_code(error));
     }
 }