use ovs_assert instead of assert
[sliver-openvswitch.git] / lib / ofp-errors.c
index 2c71312..f2a9e8c 100644 (file)
@@ -31,6 +31,8 @@ ofperr_domain_from_version(enum ofp_version version)
         return &ofperr_of11;
     case OFP12_VERSION:
         return &ofperr_of12;
+    case OFP13_VERSION:
+        return &ofperr_of13;
     default:
         return NULL;
     }
@@ -51,40 +53,6 @@ ofperr_is_valid(enum ofperr error)
     return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
 }
 
-/* Returns true if 'error' is a valid OFPERR_* value that designates a whole
- * category of errors instead of a particular error, e.g. if it is an
- * OFPERR_OFPET_* value, and false otherwise.  */
-bool
-ofperr_is_category(enum ofperr error)
-{
-    return (ofperr_is_valid(error)
-            && ofperr_of10.errors[error - OFPERR_OFS].code == -1
-            && ofperr_of11.errors[error - OFPERR_OFS].code == -1);
-}
-
-/* Returns true if 'error' is a valid OFPERR_* value that is a Nicira
- * extension, e.g. if it is an OFPERR_NX* value, and false otherwise. */
-bool
-ofperr_is_nx_extension(enum ofperr error)
-{
-    return (ofperr_is_valid(error)
-            && (ofperr_of10.errors[error - OFPERR_OFS].code >= 0x100 ||
-                ofperr_of11.errors[error - OFPERR_OFS].code >= 0x100));
-}
-
-/* Returns true if 'error' can be encoded as an OpenFlow error message in
- * 'domain', false otherwise.
- *
- * A given error may not be encodable in some domains because each OpenFlow
- * version tends to introduce new errors and retire some old ones. */
-bool
-ofperr_is_encodable(enum ofperr error, enum ofp_version version)
-{
-    const struct ofperr_domain *domain = ofperr_domain_from_version(version);
-    return (ofperr_is_valid(error)
-            && domain && domain->errors[error - OFPERR_OFS].code >= 0);
-}
-
 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
  * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
  * unknown. */
@@ -95,16 +63,6 @@ ofperr_decode(enum ofp_version version, uint16_t type, uint16_t code)
     return domain ? domain->decode(type, code) : 0;
 }
 
-/* Returns the OFPERR_* value that corresponds to the category 'type' within
- * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
- * unknown. */
-enum ofperr
-ofperr_decode_type(enum ofp_version version, uint16_t type)
-{
-    const struct ofperr_domain *domain = ofperr_domain_from_version(version);
-    return domain ? domain->decode_type(type) : 0;
-}
-
 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
  * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
  *
@@ -152,7 +110,7 @@ ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
 {
     size_t ofs = error - OFPERR_OFS;
 
-    assert(ofperr_is_valid(error));
+    ovs_assert(ofperr_is_valid(error));
     return &domain->errors[ofs];
 }
 
@@ -160,37 +118,34 @@ static struct ofpbuf *
 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
                     ovs_be32 xid, const void *data, size_t data_len)
 {
+    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+    const struct ofperr_domain *domain;
     struct ofp_error_msg *oem;
     const struct pair *pair;
     struct ofpbuf *buf;
-    const struct ofperr_domain *domain;
 
+    /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
     domain = ofperr_domain_from_version(ofp_version);
     if (!domain) {
-        return NULL;
+        VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
+                    "version 0x%02x", ofp_version);
+        domain = &ofperr_of10;
     }
 
-    if (!ofperr_is_encodable(error, ofp_version)) {
-        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
-        if (!ofperr_is_valid(error)) {
-            /* 'error' seems likely to be a system errno value. */
-            VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
-                         error, strerror(error));
-        } else {
-            const char *s = ofperr_get_name(error);
-            if (ofperr_is_category(error)) {
-                VLOG_WARN_RL(&rl, "cannot encode error category (%s)", s);
-            } else {
-                VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
-            }
-        }
-
-        return NULL;
+    /* Make sure 'error' is valid in 'domain', or use a fallback error. */
+    if (!ofperr_is_valid(error)) {
+        /* 'error' seems likely to be a system errno value. */
+        VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
+                    error, strerror(error));
+        error = OFPERR_NXBRC_UNENCODABLE_ERROR;
+    } else if (domain->errors[error - OFPERR_OFS].code < 0) {
+        VLOG_ERR_RL(&rl, "cannot encode %s for %s",
+                    ofperr_get_name(error), domain->name);
+        error = OFPERR_NXBRC_UNENCODABLE_ERROR;
     }
 
     pair = ofperr_get_pair__(error, domain);
-    if (!ofperr_is_nx_extension(error)) {
+    if (pair->code < 0x100) {
         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
                                sizeof *oem + data_len);
 
@@ -214,6 +169,7 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
     }
 
     ofpbuf_put(buf, data, data_len);
+    ofpmsg_update_length(buf);
 
     return buf;
 }
@@ -226,9 +182,6 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
  * The error reply will contain an initial subsequence of 'oh', up to
  * 'oh->length' or 64 bytes, whichever is shorter.
  *
- * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
- * be encoded as OpenFlow version 'oh->version'.
- *
  * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
  * messages.  Use ofperr_encode_hello() instead. */
 struct ofpbuf *
@@ -245,24 +198,11 @@ ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
  *
  * If 'version' is an unknown version then OFP10_VERSION is used.
  * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
- * so in theory this should work.
- *
- * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
- * be encoded in 'domain'. */
+ * so in theory this should work. */
 struct ofpbuf *
 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
                     const char *s)
 {
-    switch (ofp_version) {
-    case OFP10_VERSION:
-    case OFP11_VERSION:
-    case OFP12_VERSION:
-        break;
-
-    default:
-        ofp_version = OFP10_VERSION;
-    }
-
     return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
 }
 
@@ -292,7 +232,7 @@ ofperr_get_code(enum ofperr error, enum ofp_version version)
     return domain ? ofperr_get_pair__(error, domain)->code : -1;
 }
 
-/* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
+/* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
  * Returns an OFPERR_* constant on success, 0 on failure.
  *
  * If 'payload' is nonnull, on success '*payload' is initialized to the
@@ -338,12 +278,8 @@ ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
         code = ntohs(nve->code);
     }
 
-    /* Translate the error type and code into an ofperr.
-     * If we don't know the error type and code, at least try for the type. */
+    /* Translate the error type and code into an ofperr. */
     error = ofperr_decode(oh->version, type, code);
-    if (!error) {
-        error = ofperr_decode_type(oh->version, type);
-    }
     if (error && payload) {
         ofpbuf_use_const(payload, b.data, b.size);
     }