ofp: Add support for bundles extension in OpenFlow 1.3.
[cascardo/ovs.git] / lib / ofp-prop.c
index 41b6203..1512442 100644 (file)
@@ -23,6 +23,7 @@
 #include "ofp-errors.h"
 #include "openvswitch/vlog.h"
 #include "util.h"
+#include "uuid.h"
 
 struct ofp_prop_be16 {
     ovs_be16 type;
@@ -53,8 +54,10 @@ ofpprop_type_to_exp_type(uint64_t type)
 
 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
  * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
- * nonnull, the entire property, including the header, in '*property'.  Returns
- * 0 if successful, otherwise an OpenFlow error code.
+ * nonnull, the entire property, including the header, in '*property'.  Points
+ * 'property->header' to the property header (which could be ofp_prop_header or
+ * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
+ * successful, otherwise an OpenFlow error code.
  *
  * This function treats property types 'min_exp' and larger as introducing
  * experimenter properties.  For most kinds of properties, 0xffff is the
@@ -117,11 +120,17 @@ ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
 
 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
  * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
- * nonnull, the entire property, including the header, in '*property'.  Returns
- * 0 if successful, otherwise an error code.
+ * nonnull, the entire property, including the header, in '*property'.  Points
+ * 'property->header' to the property header (which could be ofp_prop_header or
+ * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
+ * successful, otherwise an error code.
  *
- * This function pulls the property's stated size padded out to a multiple of
- * 8 bytes, which is the common case for OpenFlow properties. */
+ * This function treats property type 0xffff as introducing an experimenter
+ * property.  Use ofpprop_pull__() instead if some other behavior is needed.
+ *
+ * This function pulls the property's stated size padded out to a multiple of 8
+ * bytes, which is the common case for OpenFlow properties.  Use
+ * ofpprop_pull__() instead if some other behavior is needed.*/
 enum ofperr
 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
 {
@@ -159,6 +168,41 @@ ofpprop_parse_be32(const struct ofpbuf *property, ovs_be32 *value)
     return 0;
 }
 
+/* Attempts to parse 'property' as a property containing a 64-bit value.  If
+ * successful, stores the value into '*value' and returns 0; otherwise returns
+ * an OpenFlow error. */
+enum ofperr
+ofpprop_parse_be64(const struct ofpbuf *property, ovs_be64 *value)
+{
+    ovs_be64 *p;
+    size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
+    if (property->size != be64_offset + sizeof *p) {
+        return OFPERR_OFPBPC_BAD_LEN;
+    }
+
+    p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
+    *value = *p;
+    return 0;
+}
+
+/* Attempts to parse 'property' as a property containing a 8-bit value.  If
+ * successful, stores the value into '*value' and returns 0; otherwise returns
+ * an OpenFlow error. */
+enum ofperr
+ofpprop_parse_u8(const struct ofpbuf *property, uint8_t *value)
+{
+    /* OpenFlow 1.5 and earlier don't have any 8-bit properties, but it uses
+     * 8-byte properties for 16-bit values, which doesn't really make sense.
+     * Be forgiving by allowing any size payload as long as it's at least big
+     * enough. */
+    uint8_t *p = property->msg;
+    if (ofpbuf_msgsize(property) < sizeof *p) {
+        return OFPERR_OFPBPC_BAD_LEN;
+    }
+    *value = *p;
+    return 0;
+}
+
 /* Attempts to parse 'property' as a property containing a 16-bit value.  If
  * successful, stores the value into '*value' and returns 0; otherwise returns
  * an OpenFlow error. */
@@ -190,6 +234,58 @@ ofpprop_parse_u32(const struct ofpbuf *property, uint32_t *value)
     return 0;
 }
 
+/* Attempts to parse 'property' as a property containing a 64-bit value.  If
+ * successful, stores the value into '*value' and returns 0; otherwise returns
+ * an OpenFlow error. */
+enum ofperr
+ofpprop_parse_u64(const struct ofpbuf *property, uint64_t *value)
+{
+    ovs_be64 *p;
+    size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
+    if (property->size != be64_offset + sizeof *p) {
+        return OFPERR_OFPBPC_BAD_LEN;
+    }
+
+    p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
+    *value = ntohll(*p);
+    return 0;
+}
+
+/* Attempts to parse 'property' as a property containing a UUID.  If
+ * successful, stores the value into '*uuid' and returns 0; otherwise returns
+ * an OpenFlow error. */
+enum ofperr
+ofpprop_parse_uuid(const struct ofpbuf *property, struct uuid *uuid)
+{
+    struct uuid *p = property->msg;
+    if (ofpbuf_msgsize(property) != sizeof *p) {
+        return OFPERR_OFPBPC_BAD_LEN;
+    }
+    *uuid = *p;
+    return 0;
+}
+
+/* Attempts to parse 'property' as a property that contains nested properties.
+ * If successful, stores the nested data into '*nested' and returns 0;
+ * otherwise returns an OpenFlow error.
+ *
+ * The only thing special about nested properties is that the property header
+ * is followed by 4 bytes of padding, so that the nested properties begin at an
+ * 8-byte aligned offset.  This function can be used in other situations where
+ * this is the case. */
+enum ofperr
+ofpprop_parse_nested(const struct ofpbuf *property, struct ofpbuf *nested)
+{
+    size_t nested_offset = ROUND_UP(ofpbuf_headersize(property), 8);
+    if (property->size < nested_offset) {
+        return OFPERR_OFPBPC_BAD_LEN;
+    }
+
+    ofpbuf_use_const(nested, property->data, property->size);
+    ofpbuf_pull(nested, nested_offset);
+    return 0;
+}
+
 /* Adds a property with the given 'type' and 'len'-byte contents 'value' to
  * 'msg', padding the property out to a multiple of 8 bytes. */
 void
@@ -200,6 +296,28 @@ ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
     ofpprop_end(msg, start_ofs);
 }
 
+/* Adds a property with the given 'type' to 'msg', consisting of a struct
+ * ofp_prop_header or ofp_prop_experimenter followed by enough zero bytes to
+ * total 'len' bytes, followed by padding to bring the property up to a
+ * multiple of 8 bytes.  Returns the property header. */
+void *
+ofpprop_put_zeros(struct ofpbuf *msg, uint64_t type, size_t len)
+{
+    void *header = ofpbuf_put_zeros(msg, ROUND_UP(len, 8));
+    if (!ofpprop_is_experimenter(type)) {
+        struct ofp_prop_header *oph = header;
+        oph->type = htons(type);
+        oph->len = htons(len);
+    } else {
+        struct ofp_prop_experimenter *ope = header;
+        ope->type = htons(0xffff);
+        ope->len = htons(len);
+        ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
+        ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
+    }
+    return header;
+}
+
 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
 void
 ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
@@ -224,6 +342,25 @@ ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
     ofpprop_put(msg, type, &value, sizeof value);
 }
 
+/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
+void
+ofpprop_put_be64(struct ofpbuf *msg, uint64_t type, ovs_be64 value)
+{
+    size_t start = ofpprop_start(msg, type);
+    ofpbuf_put_zeros(msg, 4);
+    ofpbuf_put(msg, &value, sizeof value);
+    ofpprop_end(msg, start);
+}
+
+/* Adds a property with the given 'type' and 8-bit 'value' to 'msg'. */
+void
+ofpprop_put_u8(struct ofpbuf *msg, uint64_t type, uint8_t value)
+{
+    /* There's no precedent for 8-bit properties in OpenFlow 1.5 and earlier
+     * but let's assume they're done sanely. */
+    ofpprop_put(msg, type, &value, 1);
+}
+
 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
 void
 ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
@@ -238,7 +375,13 @@ ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
     ofpprop_put_be32(msg, type, htonl(value));
 }
 
-/* Adds a property header to 'msg' for each 1-bit in 'bitmap'. */
+/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
+void
+ofpprop_put_u64(struct ofpbuf *msg, uint64_t type, uint64_t value)
+{
+    ofpprop_put_be64(msg, type, htonll(value));
+}
+
 /* Appends a property to 'msg' whose type is 'type' and whose contents is a
  * series of property headers, one for each 1-bit in 'bitmap'. */
 void
@@ -252,6 +395,36 @@ ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
     ofpprop_end(msg, start_ofs);
 }
 
+/* Appends a content-free property with the given 'type' to 'msg'.
+ *
+ * (The idea is that the presence of the property acts as a flag.) */
+void
+ofpprop_put_flag(struct ofpbuf *msg, uint64_t type)
+{
+    size_t start = ofpprop_start(msg, type);
+    ofpprop_end(msg, start);
+}
+
+/* Appends a property to 'msg' with the given 'type' and 'uuid' as its
+ * value. */
+void
+ofpprop_put_uuid(struct ofpbuf *msg, uint64_t type, const struct uuid *uuid)
+{
+    ofpprop_put(msg, type, uuid, sizeof *uuid);
+}
+
+/* Appends a property of type 'type' to 'msg' whose contents are padding to
+ * 8-byte alignment followed by 'nested'.  This is a suitable way to add nested
+ * properties to 'msg'. */
+void
+ofpprop_put_nested(struct ofpbuf *msg, uint64_t type,
+                   const struct ofpbuf *nested)
+{
+    size_t start = ofpprop_start_nested(msg, type);
+    ofpbuf_put(msg, nested->data, nested->size);
+    ofpprop_end(msg, start);
+}
+
 /* Appends a header for a property of type 'type' to 'msg'.  The caller should
  * add the contents of the property to 'msg', then finish it by calling
  * ofpprop_end().  Returns the offset of the beginning of the property (to pass
@@ -289,6 +462,22 @@ ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
     ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
 }
 
+/* Appends a header for a property of type 'type' to 'msg', followed by padding
+ * suitable for putting nested properties into the property; that is, padding
+ * to an 8-byte alignment.
+ *
+ * This otherwise works like ofpprop_start().
+ *
+ * There's no need for ofpprop_end_nested(), because ofpprop_end() works fine
+ * for this case. */
+size_t
+ofpprop_start_nested(struct ofpbuf *msg, uint64_t type)
+{
+    size_t start_ofs = ofpprop_start(msg, type);
+    ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
+    return start_ofs;
+}
+
 enum ofperr
 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
                 uint64_t type)