X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fofp-msgs.c;h=b67e47a7c10ea3f230241c88807966642aafec4d;hb=28c5588e8e1a8d091c5d2275232c35f2968a97fa;hp=057422886976d3cfd1cc5fd245ccf0b24820931b;hpb=e032c08628186e3bf351fc9e111cf919d0d06073;p=sliver-openvswitch.git diff --git a/lib/ofp-msgs.c b/lib/ofp-msgs.c index 057422886..b67e47a7c 100644 --- a/lib/ofp-msgs.c +++ b/lib/ofp-msgs.c @@ -23,6 +23,7 @@ #include "ofpbuf.h" #include "openflow/nicira-ext.h" #include "openflow/openflow.h" +#include "ovs-thread.h" #include "vlog.h" VLOG_DEFINE_THIS_MODULE(ofp_msgs); @@ -109,8 +110,11 @@ static enum ofperr ofpraw_from_ofphdrs(enum ofpraw *, const struct ofphdrs *); static ovs_be32 alloc_xid(void) { - static uint32_t next_xid = 1; - return htonl(next_xid++); + static atomic_uint32_t next_xid = ATOMIC_VAR_INIT(1); + uint32_t xid; + + atomic_add(&next_xid, 1, &xid); + return htonl(xid); } static uint32_t @@ -256,22 +260,50 @@ ofphdrs_decode_assert(struct ofphdrs *hdrs, } static bool -ofphdrs_is_stat(const struct ofphdrs *hdrs) +ofp_is_stat_request(enum ofp_version version, uint8_t type) { - switch ((enum ofp_version) hdrs->version) { + switch (version) { case OFP10_VERSION: - return (hdrs->type == OFPT10_STATS_REQUEST || - hdrs->type == OFPT10_STATS_REPLY); + return type == OFPT10_STATS_REQUEST; case OFP11_VERSION: case OFP12_VERSION: case OFP13_VERSION: - return (hdrs->type == OFPT11_STATS_REQUEST || - hdrs->type == OFPT11_STATS_REPLY); + case OFP14_VERSION: + return type == OFPT11_STATS_REQUEST; } return false; } +static bool +ofp_is_stat_reply(enum ofp_version version, uint8_t type) +{ + switch (version) { + case OFP10_VERSION: + return type == OFPT10_STATS_REPLY; + case OFP11_VERSION: + case OFP12_VERSION: + case OFP13_VERSION: + case OFP14_VERSION: + return type == OFPT11_STATS_REPLY; + } + + return false; +} + +static bool +ofp_is_stat(enum ofp_version version, uint8_t type) +{ + return (ofp_is_stat_request(version, type) || + ofp_is_stat_reply(version, type)); +} + +static bool +ofphdrs_is_stat(const struct ofphdrs *hdrs) +{ + return ofp_is_stat(hdrs->version, hdrs->type); +} + size_t ofphdrs_len(const struct ofphdrs *hdrs) { @@ -292,6 +324,7 @@ ofphdrs_len(const struct ofphdrs *hdrs) case OFP11_VERSION: case OFP12_VERSION: case OFP13_VERSION: + case OFP14_VERSION: if (hdrs->type == OFPT11_STATS_REQUEST || hdrs->type == OFPT11_STATS_REPLY) { return (hdrs->stat == OFPST_VENDOR @@ -320,19 +353,36 @@ ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh) return ofpraw_pull(raw, &msg); } +/* Does the same job as ofpraw_decode(), except that it assert-fails if + * ofpraw_decode() would have reported an error. Thus, it's able to use the + * return value for the OFPRAW_* message type instead of an error code. + * + * (It only makes sense to use this function if you previously called + * ofpraw_decode() on the message and thus know that it's OK.) */ +enum ofpraw +ofpraw_decode_assert(const struct ofp_header *oh) +{ + enum ofperr error; + enum ofpraw raw; + + error = ofpraw_decode(&raw, oh); + ovs_assert(!error); + return raw; +} + /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts - * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and - * stores the type into '*rawp'. On failure, returns an OFPERR_* error code - * and zeros '*rawp'. + * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success, + * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_* + * error code and zeros '*rawp'. * * This function checks that the message has a valid length for its particular * type of message, and returns an error if not. * * In addition to setting '*rawp', this function pulls off the OpenFlow header * (including the stats headers, vendor header, and any subtype header) with - * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header - * and 'msg->l3' just beyond the headers (that is, to the final value of - * msg->data). */ + * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow + * header and 'msg->l3' just beyond the headers (that is, to the final value of + * ofpbuf_data(msg)). */ enum ofperr ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg) { @@ -349,11 +399,12 @@ ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg) enum ofpraw raw; /* Set default outputs. */ - msg->l2 = msg->l3 = msg->data; + msg->frame = ofpbuf_data(msg); + ofpbuf_set_l3(msg, msg->frame); *rawp = 0; - len = msg->size; - error = ofphdrs_decode(&hdrs, msg->data, len); + len = ofpbuf_size(msg); + error = ofphdrs_decode(&hdrs, ofpbuf_data(msg), len); if (error) { return error; } @@ -365,8 +416,8 @@ ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg) info = raw_info_get(raw); instance = raw_instance_get(info, hdrs.version); - msg->l2 = ofpbuf_pull(msg, instance->hdrs_len); - msg->l3 = msg->data; + msg->frame = ofpbuf_pull(msg, instance->hdrs_len); + ofpbuf_set_l3(msg, ofpbuf_data(msg)); min_len = instance->hdrs_len + info->min_body; switch (info->extra_multiple) { @@ -460,10 +511,10 @@ static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid, * Each 'raw' value is valid only for certain OpenFlow versions. The caller * must specify a valid (raw, version) pair. * - * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header - * and 'l3' points just after it, to where the message's body will start. The - * caller must actually allocate the body into the space reserved for it, - * e.g. with ofpbuf_put_uninit(). + * In the returned ofpbuf, 'frame' points to the beginning of the + * OpenFlow header and 'l3' points just after it, to where the + * message's body will start. The caller must actually allocate the + * body into the space reserved for it, e.g. with ofpbuf_put_uninit(). * * The caller owns the returned ofpbuf and must free it when it is no longer * needed, e.g. with ofpbuf_delete(). */ @@ -507,10 +558,10 @@ ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request, * value. Every stats request has a corresponding reply, so the (raw, version) * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here. * - * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header - * and 'l3' points just after it, to where the message's body will start. The - * caller must actually allocate the body into the space reserved for it, - * e.g. with ofpbuf_put_uninit(). + * In the returned ofpbuf, 'frame' points to the beginning of the + * OpenFlow header and 'l3' points just after it, to where the + * message's body will start. The caller must actually allocate the + * body into the space reserved for it, e.g. with ofpbuf_put_uninit(). * * The caller owns the returned ofpbuf and must free it when it is no longer * needed, e.g. with ofpbuf_delete(). */ @@ -540,7 +591,7 @@ ofpraw_alloc_stats_reply(const struct ofp_header *request, * Each 'raw' value is valid only for certain OpenFlow versions. The caller * must specify a valid (raw, version) pair. * - * Upon return, 'buf->l2' points to the beginning of the OpenFlow header and + * Upon return, 'buf->frame' points to the beginning of the OpenFlow header and * 'buf->l3' points just after it, to where the message's body will start. The * caller must actually allocating the body into the space reserved for it, * e.g. with ofpbuf_put_uninit(). */ @@ -580,10 +631,10 @@ ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request, * value. Every stats request has a corresponding reply, so the (raw, version) * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here. * - * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header - * and 'l3' points just after it, to where the message's body will start. The - * caller must actually allocate the body into the space reserved for it, - * e.g. with ofpbuf_put_uninit(). + * In the returned ofpbuf, 'frame' points to the beginning of the + * OpenFlow header and 'l3' points just after it, to where the + * message's body will start. The caller must actually allocate the + * body into the space reserved for it, e.g. with ofpbuf_put_uninit(). * * The caller owns the returned ofpbuf and must free it when it is no longer * needed, e.g. with ofpbuf_delete(). */ @@ -613,17 +664,17 @@ ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid, ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body + extra_tailroom)); - buf->l2 = ofpbuf_put_uninit(buf, instance->hdrs_len); - buf->l3 = ofpbuf_tail(buf); + buf->frame = ofpbuf_put_uninit(buf, instance->hdrs_len); + ofpbuf_set_l3(buf, ofpbuf_tail(buf)); - oh = buf->l2; + oh = buf->frame; oh->version = version; oh->type = hdrs->type; - oh->length = htons(buf->size); + oh->length = htons(ofpbuf_size(buf)); oh->xid = xid; if (hdrs->type == OFPT_VENDOR) { - struct nicira_header *nh = buf->l2; + struct nicira_header *nh = buf->frame; ovs_assert(hdrs->vendor == NX_VENDOR_ID); nh->vendor = htonl(hdrs->vendor); @@ -631,43 +682,43 @@ ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid, } else if (version == OFP10_VERSION && (hdrs->type == OFPT10_STATS_REQUEST || hdrs->type == OFPT10_STATS_REPLY)) { - struct ofp10_stats_msg *osm = buf->l2; + struct ofp10_stats_msg *osm = buf->frame; osm->type = htons(hdrs->stat); osm->flags = htons(0); if (hdrs->stat == OFPST_VENDOR) { - struct ofp10_vendor_stats_msg *ovsm = buf->l2; + struct ofp10_vendor_stats_msg *ovsm = buf->frame; ovsm->vendor = htonl(hdrs->vendor); if (hdrs->vendor == NX_VENDOR_ID) { - struct nicira10_stats_msg *nsm = buf->l2; + struct nicira10_stats_msg *nsm = buf->frame; nsm->subtype = htonl(hdrs->subtype); memset(nsm->pad, 0, sizeof nsm->pad); } else { - NOT_REACHED(); + OVS_NOT_REACHED(); } } } else if (version != OFP10_VERSION && (hdrs->type == OFPT11_STATS_REQUEST || hdrs->type == OFPT11_STATS_REPLY)) { - struct ofp11_stats_msg *osm = buf->l2; + struct ofp11_stats_msg *osm = buf->frame; osm->type = htons(hdrs->stat); osm->flags = htons(0); memset(osm->pad, 0, sizeof osm->pad); if (hdrs->stat == OFPST_VENDOR) { - struct ofp11_vendor_stats_msg *ovsm = buf->l2; + struct ofp11_vendor_stats_msg *ovsm = buf->frame; ovsm->vendor = htonl(hdrs->vendor); if (hdrs->vendor == NX_VENDOR_ID) { - struct nicira11_stats_msg *nsm = buf->l2; + struct nicira11_stats_msg *nsm = buf->frame; nsm->subtype = htonl(hdrs->subtype); } else { - NOT_REACHED(); + OVS_NOT_REACHED(); } } } @@ -706,11 +757,12 @@ ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version) case OFP11_VERSION: case OFP12_VERSION: case OFP13_VERSION: + case OFP14_VERSION: ovs_assert(hdrs.type == OFPT11_STATS_REQUEST); hdrs.type = OFPT11_STATS_REPLY; break; default: - NOT_REACHED(); + OVS_NOT_REACHED(); } error = ofpraw_from_ofphdrs(&reply_raw, &hdrs); @@ -738,18 +790,18 @@ ofptype_decode(enum ofptype *typep, const struct ofp_header *oh) } /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts - * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and - * stores the type into '*typep'. On failure, returns an OFPERR_* error code - * and zeros '*typep'. + * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success, + * returns 0 and stores the type into '*typep'. On failure, returns an + * OFPERR_* error code and zeros '*typep'. * * This function checks that the message has a valid length for its particular * type of message, and returns an error if not. * * In addition to setting '*typep', this function pulls off the OpenFlow header * (including the stats headers, vendor header, and any subtype header) with - * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header - * and 'msg->l3' just beyond the headers (that is, to the final value of - * msg->data). */ + * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow + * header and 'msg->l3' just beyond the headers (that is, to the final value of + * ofpbuf_data(msg)). */ enum ofperr ofptype_pull(enum ofptype *typep, struct ofpbuf *buf) { @@ -772,12 +824,12 @@ ofptype_from_ofpraw(enum ofpraw raw) } /* Updates the 'length' field of the OpenFlow message in 'buf' to - * 'buf->size'. */ + * 'ofpbuf_size(buf)'. */ void ofpmsg_update_length(struct ofpbuf *buf) { struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh); - oh->length = htons(buf->size); + oh->length = htons(ofpbuf_size(buf)); } /* Returns just past the Openflow header (including the stats headers, vendor @@ -790,6 +842,13 @@ ofpmsg_body(const struct ofp_header *oh) ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length)); return (const uint8_t *) oh + ofphdrs_len(&hdrs); } + +/* Return if it's a stat/multipart (OFPST) request message. */ +bool +ofpmsg_is_stat_request(const struct ofp_header *oh) +{ + return ofp_is_stat_request(oh->version, oh->type); +} static ovs_be16 *ofpmp_flags__(const struct ofp_header *); @@ -821,7 +880,7 @@ ofpmp_reserve(struct list *replies, size_t len) { struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); - if (msg->size + len <= UINT16_MAX) { + if (ofpbuf_size(msg) + len <= UINT16_MAX) { ofpbuf_prealloc_tailroom(msg, len); return msg; } else { @@ -829,14 +888,16 @@ ofpmp_reserve(struct list *replies, size_t len) struct ofpbuf *next; struct ofphdrs hdrs; - ofphdrs_decode_assert(&hdrs, msg->data, msg->size); + ofphdrs_decode_assert(&hdrs, ofpbuf_data(msg), ofpbuf_size(msg)); hdrs_len = ofphdrs_len(&hdrs); next = ofpbuf_new(MAX(1024, hdrs_len + len)); - ofpbuf_put(next, msg->data, hdrs_len); + ofpbuf_put(next, ofpbuf_data(msg), hdrs_len); + next->frame = ofpbuf_data(next); + ofpbuf_set_l3(next, ofpbuf_tail(next)); list_push_back(replies, &next->list_node); - *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE); + *ofpmp_flags__(ofpbuf_data(msg)) |= htons(OFPSF_REPLY_MORE); return next; } @@ -866,11 +927,11 @@ ofpmp_postappend(struct list *replies, size_t start_ofs) struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); ovs_assert(start_ofs <= UINT16_MAX); - if (msg->size > UINT16_MAX) { - size_t len = msg->size - start_ofs; + if (ofpbuf_size(msg) > UINT16_MAX) { + size_t len = ofpbuf_size(msg) - start_ofs; memcpy(ofpmp_append(replies, len), - (const uint8_t *) msg->data + start_ofs, len); - msg->size = start_ofs; + (const uint8_t *) ofpbuf_data(msg) + start_ofs, len); + ofpbuf_set_size(msg, start_ofs); } } @@ -883,9 +944,10 @@ ofpmp_flags__(const struct ofp_header *oh) case OFP11_VERSION: case OFP12_VERSION: case OFP13_VERSION: + case OFP14_VERSION: return &((struct ofp11_stats_msg *) oh)->flags; default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -969,9 +1031,10 @@ ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs) static void ofpmsgs_init(void) { + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; const struct raw_info *info; - if (raw_instance_map.buckets) { + if (!ovsthread_once_start(&once)) { return; } @@ -989,4 +1052,6 @@ ofpmsgs_init(void) ofphdrs_hash(&inst->hdrs)); } } + + ovsthread_once_done(&once); }