Small nits below, Acked-by: Jarno Rajahalme <ja...@ovn.org>
> On Jan 18, 2016, at 11:27 PM, Ben Pfaff <b...@ovn.org> wrote: > > These will have users in upcoming commits. Unlike the previously > added helpers, there isn't any existing code that can immediately > use them. > > Signed-off-by: Ben Pfaff <b...@ovn.org> > --- > lib/ofp-prop.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- > lib/ofp-prop.h | 10 +++++ > lib/ofpbuf.h | 8 ++++ > 3 files changed, 131 insertions(+), 3 deletions(-) > > diff --git a/lib/ofp-prop.c b/lib/ofp-prop.c > index 4215e40..76031b6 100644 > --- a/lib/ofp-prop.c > +++ b/lib/ofp-prop.c > @@ -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; > @@ -111,8 +112,10 @@ 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->msg' to just past the property header (which could be > + * ofp_prop_header or ofp_prop_experimenter) in 'property'. Returns 0 if > + * successful, otherwise an error code. Maybe should mention the setting of ‘property->header’ as well, esp. since it is used by the new ofpbuf_headersize() function? > * > * This function pulls the property's stated size padded out to a multiple of > * 8 bytes, which is the common case for OpenFlow properties. */ > @@ -153,6 +156,40 @@ 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) > +{ > + size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8); > + if (property->size != be64_offset + 8) { Maybe use 'sizeof *p' here as well for consistency? > + return OFPERR_OFPBPC_BAD_LEN; > + } > + > + ovs_be64 *p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + 8); > + *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. */ > @@ -184,6 +221,36 @@ 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) > +{ > + size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8); > + if (property->size != be64_offset + 8) { And here? > + return OFPERR_OFPBPC_BAD_LEN; > + } > + > + ovs_be64 *p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + 8); > + *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; > +} > + > /* 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 > @@ -218,6 +285,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) > @@ -232,7 +318,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 > @@ -245,6 +337,24 @@ 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 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 > diff --git a/lib/ofp-prop.h b/lib/ofp-prop.h > index 2b4908a..7cd107c 100644 > --- a/lib/ofp-prop.h > +++ b/lib/ofp-prop.h > @@ -49,6 +49,7 @@ > #include "openvswitch/types.h" > > struct ofpbuf; > +struct uuid; > struct vlog_module; > > /* Given an OpenFlow experimenter ID (e.g. NX_VENDOR_ID) and exp_type, yields > @@ -74,17 +75,26 @@ enum ofperr ofpprop_pull(struct ofpbuf *msg, struct > ofpbuf *property, > > enum ofperr ofpprop_parse_be16(const struct ofpbuf *, ovs_be16 *value); > enum ofperr ofpprop_parse_be32(const struct ofpbuf *, ovs_be32 *value); > +enum ofperr ofpprop_parse_be64(const struct ofpbuf *, ovs_be64 *value); > +enum ofperr ofpprop_parse_u8(const struct ofpbuf *, uint8_t *value); > enum ofperr ofpprop_parse_u16(const struct ofpbuf *, uint16_t *value); > enum ofperr ofpprop_parse_u32(const struct ofpbuf *, uint32_t *value); > +enum ofperr ofpprop_parse_u64(const struct ofpbuf *, uint64_t *value); > +enum ofperr ofpprop_parse_uuid(const struct ofpbuf *, struct uuid *); > > /* Serializing properties. */ > void ofpprop_put(struct ofpbuf *, uint64_t type, > const void *value, size_t len); > void ofpprop_put_be16(struct ofpbuf *, uint64_t type, ovs_be16 value); > void ofpprop_put_be32(struct ofpbuf *, uint64_t type, ovs_be32 value); > +void ofpprop_put_be64(struct ofpbuf *, uint64_t type, ovs_be64 value); > +void ofpprop_put_u8(struct ofpbuf *, uint64_t type, uint8_t value); > void ofpprop_put_u16(struct ofpbuf *, uint64_t type, uint16_t value); > void ofpprop_put_u32(struct ofpbuf *, uint64_t type, uint32_t value); > +void ofpprop_put_u64(struct ofpbuf *, uint64_t type, uint64_t value); > void ofpprop_put_bitmap(struct ofpbuf *, uint64_t type, uint64_t bitmap); > +void ofpprop_put_flag(struct ofpbuf *, uint64_t type); > +void ofpprop_put_uuid(struct ofpbuf *, uint64_t type, const struct uuid *); > > size_t ofpprop_start(struct ofpbuf *, uint64_t type); > void ofpprop_end(struct ofpbuf *, size_t start_ofs); > diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h > index 678b1f1..dd72cde 100644 > --- a/lib/ofpbuf.h > +++ b/lib/ofpbuf.h > @@ -194,6 +194,14 @@ static inline size_t ofpbuf_tailroom(const struct ofpbuf > *b) > return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b); > } > > +/* Returns the number of bytes from 'b->header' to 'b->msg', that is, the > + * length of 'b''s header. */ > +static inline size_t > +ofpbuf_headersize(const struct ofpbuf *b) > +{ > + return (char *)b->msg - (char *)b->header; > +} > + > /* Returns the number of bytes from 'b->msg' to 'b->data + b->size', that is, > * the length of the used space in 'b' starting from 'msg'. */ > static inline size_t > -- > 2.1.3 > > _______________________________________________ > dev mailing list > dev@openvswitch.org > http://openvswitch.org/mailman/listinfo/dev _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev