ofp-util: Work on decoding OF1.1 flow_mods.
[openvswitch] / lib / ofp-errors.c
index 028475e114ba14c2f0b7c930ee16b78b4d2c62c7..13b5c4cbca1f29c6332be71a8438c75c8f44233f 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include "byte-order.h"
 #include "dynamic-string.h"
+#include "ofp-msgs.h"
 #include "ofp-util.h"
 #include "ofpbuf.h"
 #include "openflow/openflow.h"
@@ -25,9 +26,17 @@ ofperr_domain_from_version(uint8_t version)
 {
     return (version == ofperr_of10.version ? &ofperr_of10
             : version == ofperr_of11.version ? &ofperr_of11
+            : version == ofperr_of12.version ? &ofperr_of12
             : NULL);
 }
 
+/* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow error domain 'domain'. */
+const char *
+ofperr_domain_get_name(const struct ofperr_domain *domain)
+{
+    return domain->name;
+}
+
 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
 bool
 ofperr_is_valid(enum ofperr error)
@@ -97,6 +106,24 @@ ofperr_get_name(enum ofperr error)
             : "<invalid>");
 }
 
+/* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
+ * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
+ * "OFPHFC_INCOMPATIBLE".
+ *
+ * This is probably useful only for debugging and testing. */
+enum ofperr
+ofperr_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < OFPERR_N_ERRORS; i++) {
+        if (!strcmp(name, error_names[i])) {
+            return i + OFPERR_OFS;
+        }
+    }
+    return 0;
+}
+
 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
  * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
  * a valid OFPERR_* value. */
@@ -108,6 +135,15 @@ ofperr_get_description(enum ofperr error)
             : "<invalid>");
 }
 
+static const struct pair *
+ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
+{
+    size_t ofs = error - OFPERR_OFS;
+
+    assert(ofperr_is_valid(error));
+    return &domain->errors[ofs];
+}
+
 static struct ofpbuf *
 ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
                     ovs_be32 xid, const void *data, size_t data_len)
@@ -115,7 +151,6 @@ ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
     struct ofp_error_msg *oem;
     const struct pair *pair;
     struct ofpbuf *buf;
-    size_t ofs;
 
     if (!domain) {
         return NULL;
@@ -140,28 +175,30 @@ ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
         return NULL;
     }
 
-    ofs = error - OFPERR_OFS;
-    pair = &domain->errors[ofs];
+    pair = ofperr_get_pair__(error, domain);
     if (!ofperr_is_nx_extension(error)) {
-        oem = make_openflow_xid(data_len + sizeof *oem, OFPT_ERROR, xid, &buf);
+        buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
+                               sizeof *oem + data_len);
+
+        oem = ofpbuf_put_uninit(buf, sizeof *oem);
         oem->type = htons(pair->type);
         oem->code = htons(pair->code);
     } else {
         struct nx_vendor_error *nve;
 
-        oem = make_openflow_xid(data_len + sizeof *oem + sizeof *nve,
-                                OFPT_ERROR, xid, &buf);
+        buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
+                               sizeof *oem + sizeof *nve + data_len);
+
+        oem = ofpbuf_put_uninit(buf, sizeof *oem);
         oem->type = htons(NXET_VENDOR);
         oem->code = htons(NXVC_VENDOR_ERROR);
 
-        nve = (struct nx_vendor_error *) oem->data;
+        nve = ofpbuf_put_uninit(buf, sizeof *nve);
         nve->vendor = htonl(NX_VENDOR_ID);
         nve->type = htons(pair->type);
         nve->code = htons(pair->code);
     }
-    oem->header.version = domain->version;
 
-    buf->size -= data_len;
     ofpbuf_put(buf, data, data_len);
 
     return buf;
@@ -210,37 +247,58 @@ ofperr_encode_hello(enum ofperr error, const struct ofperr_domain *domain,
     return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
 }
 
+/* Returns the value that would go into an OFPT_ERROR message's 'type' for
+ * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
+ * 'domain'.
+ *
+ * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
+int
+ofperr_get_type(enum ofperr error, const struct ofperr_domain *domain)
+{
+    return ofperr_get_pair__(error, domain)->type;
+}
+
+/* Returns the value that would go into an OFPT_ERROR message's 'code' for
+ * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
+ * 'domain' or if 'error' represents a category rather than a specific error.
+ *
+ * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
+int
+ofperr_get_code(enum ofperr error, const struct ofperr_domain *domain)
+{
+    return ofperr_get_pair__(error, domain)->code;
+}
+
 /* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
  * Returns an OFPERR_* constant on success, 0 on failure.
  *
- * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
- * to the payload starting from 'oh' and on failure it is set to 0. */
+ * If 'payload' is nonnull, on success '*payload' is initialized to the
+ * error's payload, and on failure it is cleared. */
 enum ofperr
-ofperr_decode_msg(const struct ofp_header *oh, size_t *payload_ofs)
+ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 
     const struct ofperr_domain *domain;
     const struct ofp_error_msg *oem;
+    enum ofpraw raw;
     uint16_t type, code;
     enum ofperr error;
     struct ofpbuf b;
 
-    if (payload_ofs) {
-        *payload_ofs = 0;
+    if (payload) {
+        memset(payload, 0, sizeof *payload);
     }
 
     /* Pull off the error message. */
     ofpbuf_use_const(&b, oh, ntohs(oh->length));
-    oem = ofpbuf_try_pull(&b, sizeof *oem);
-    if (!oem) {
+    error = ofpraw_pull(&raw, &b);
+    if (error) {
         return 0;
     }
+    oem = ofpbuf_pull(&b, sizeof *oem);
 
-    /* Check message type and version. */
-    if (oh->type != OFPT_ERROR) {
-        return 0;
-    }
+    /* Check version. */
     domain = ofperr_domain_from_version(oh->version);
     if (!domain) {
         return 0;
@@ -270,8 +328,8 @@ ofperr_decode_msg(const struct ofp_header *oh, size_t *payload_ofs)
     if (!error) {
         error = ofperr_decode_type(domain, type);
     }
-    if (error && payload_ofs) {
-        *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
+    if (error && payload) {
+        ofpbuf_use_const(payload, b.data, b.size);
     }
     return error;
 }