From 71ce92352cbcb96f179f8ec19dd1e843d91d8828 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 19 Oct 2010 09:55:40 -0700 Subject: [PATCH] ofp-util: Add more functions for supporting OpenFlow error codes. Cross-ported from "wdp" branch. --- lib/dpif.c | 7 +++++- lib/ofp-util.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/dpif.c b/lib/dpif.c index 2cf6a037..e117bdf9 100644 --- a/lib/dpif.c +++ b/lib/dpif.c @@ -30,6 +30,7 @@ #include "netlink.h" #include "odp-util.h" #include "ofp-print.h" +#include "ofp-util.h" #include "ofpbuf.h" #include "packets.h" #include "poll-loop.h" @@ -1085,9 +1086,13 @@ log_operation(const struct dpif *dpif, const char *operation, int error) { if (!error) { VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation); - } else { + } else if (is_errno(error)) { VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)", dpif_name(dpif), operation, strerror(error)); + } else { + VLOG_WARN_RL(&error_rl, "%s: %s failed (%d/%d)", + dpif_name(dpif), operation, + get_ofp_err_type(error), get_ofp_err_code(error)); } } diff --git a/lib/ofp-util.h b/lib/ofp-util.h index dc431d0c..079f13f8 100644 --- a/lib/ofp-util.h +++ b/lib/ofp-util.h @@ -82,7 +82,38 @@ bool action_outputs_to_port(const union ofp_action *, uint16_t port); void normalize_match(struct ofp_match *); char *ofp_match_to_literal_string(const struct ofp_match *match); + +/* 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) { @@ -90,4 +121,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 */ -- 2.30.2