netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / ofp-msgs.c
index 48ecafc..de20655 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 Nicira, Inc.
+ * Copyright (c) 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
 
 #include <config.h>
 #include "ofp-msgs.h"
-#include <assert.h>
 #include "byte-order.h"
 #include "dynamic-string.h"
 #include "hash.h"
@@ -24,7 +23,8 @@
 #include "ofpbuf.h"
 #include "openflow/nicira-ext.h"
 #include "openflow/openflow.h"
-#include "vlog.h"
+#include "ovs-thread.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(ofp_msgs);
 
@@ -35,6 +35,78 @@ VLOG_DEFINE_THIS_MODULE(ofp_msgs);
 #define OFPT11_STATS_REPLY 19
 #define OFPST_VENDOR 0xffff
 
+/* Vendor extension message. */
+struct ofp_vendor_header {
+    struct ofp_header header;   /* OFPT_VENDOR. */
+    ovs_be32 vendor;            /* Vendor ID:
+                                 * - MSB 0: low-order bytes are IEEE OUI.
+                                 * - MSB != 0: defined by OpenFlow
+                                 *   consortium. */
+
+    /* In theory everything after 'vendor' is vendor specific.  In practice,
+     * the vendors we support put a 32-bit subtype here.  We'll change this
+     * structure if we start adding support for other vendor formats. */
+    ovs_be32 subtype;           /* Vendor-specific subtype. */
+
+    /* Followed by vendor-defined additional data. */
+};
+OFP_ASSERT(sizeof(struct ofp_vendor_header) == 16);
+
+/* Statistics request or reply message. */
+struct ofp10_stats_msg {
+    struct ofp_header header;
+    ovs_be16 type;              /* One of the OFPST_* constants. */
+    ovs_be16 flags;             /* Requests: always 0.
+                                 * Replies: 0 or OFPSF_REPLY_MORE. */
+};
+OFP_ASSERT(sizeof(struct ofp10_stats_msg) == 12);
+
+/* Vendor extension stats message. */
+struct ofp10_vendor_stats_msg {
+    struct ofp10_stats_msg osm; /* Type OFPST_VENDOR. */
+    ovs_be32 vendor;            /* Vendor ID:
+                                 * - MSB 0: low-order bytes are IEEE OUI.
+                                 * - MSB != 0: defined by OpenFlow
+                                 *   consortium. */
+    /* Followed by vendor-defined arbitrary additional data. */
+};
+OFP_ASSERT(sizeof(struct ofp10_vendor_stats_msg) == 16);
+
+struct ofp11_stats_msg {
+    struct ofp_header header;
+    ovs_be16 type;              /* One of the OFPST_* constants. */
+    ovs_be16 flags;             /* OFPSF_REQ_* flags (none yet defined). */
+    uint8_t pad[4];
+    /* Followed by the body of the request. */
+};
+OFP_ASSERT(sizeof(struct ofp11_stats_msg) == 16);
+
+/* Vendor extension stats message. */
+struct ofp11_vendor_stats_msg {
+    struct ofp11_stats_msg osm; /* Type OFPST_VENDOR. */
+    ovs_be32 vendor;            /* Vendor ID:
+                                 * - MSB 0: low-order bytes are IEEE OUI.
+                                 * - MSB != 0: defined by OpenFlow
+                                 *   consortium. */
+
+    /* In theory everything after 'vendor' is vendor specific.  In practice,
+     * the vendors we support put a 32-bit subtype here.  We'll change this
+     * structure if we start adding support for other vendor formats. */
+    ovs_be32 subtype;           /* Vendor-specific subtype. */
+
+    /* Followed by vendor-defined additional data. */
+};
+OFP_ASSERT(sizeof(struct ofp11_vendor_stats_msg) == 24);
+
+/* Header for Nicira vendor stats request and reply messages in OpenFlow
+ * 1.0. */
+struct nicira10_stats_msg {
+    struct ofp10_vendor_stats_msg vsm; /* Vendor NX_VENDOR_ID. */
+    ovs_be32 subtype;           /* One of NXST_* below. */
+    uint8_t pad[4];             /* Align to 64-bits. */
+};
+OFP_ASSERT(sizeof(struct nicira10_stats_msg) == 24);
+
 /* A thin abstraction of OpenFlow headers:
  *
  *   - 'version' and 'type' come straight from struct ofp_header, so these are
@@ -110,15 +182,16 @@ 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_count next_xid = ATOMIC_COUNT_INIT(1);
+
+    return htonl(atomic_count_inc(&next_xid));
 }
 \f
 static uint32_t
 ofphdrs_hash(const struct ofphdrs *hdrs)
 {
-    BUILD_ASSERT_DECL(sizeof *hdrs == 12);
-    return hash_words((const uint32_t *) hdrs, 3, 0);
+    BUILD_ASSERT_DECL(sizeof *hdrs % 4 == 0);
+    return hash_bytes32((const uint32_t *) hdrs, sizeof *hdrs, 0);
 }
 
 static bool
@@ -158,15 +231,8 @@ ofphdrs_decode(struct ofphdrs *hdrs,
 
         ovh = (const struct ofp_vendor_header *) oh;
         hdrs->vendor = ntohl(ovh->vendor);
-        if (hdrs->vendor == NX_VENDOR_ID) {
-            /* Get Nicira message subtype (NXT_*). */
-            const struct nicira_header *nh;
-
-            if (length < sizeof *nh) {
-                return OFPERR_OFPBRC_BAD_LEN;
-            }
-            nh = (const struct nicira_header *) oh;
-            hdrs->subtype = ntohl(nh->subtype);
+        if (hdrs->vendor == NX_VENDOR_ID || hdrs->vendor == ONF_VENDOR_ID) {
+            hdrs->subtype = ntohl(ovh->subtype);
         } else {
             log_bad_vendor(hdrs->vendor);
             return OFPERR_OFPBRC_BAD_VENDOR;
@@ -229,15 +295,9 @@ ofphdrs_decode(struct ofphdrs *hdrs,
 
             ovsm = (const struct ofp11_vendor_stats_msg *) oh;
             hdrs->vendor = ntohl(ovsm->vendor);
-            if (hdrs->vendor == NX_VENDOR_ID) {
-                /* Get Nicira statistic type (NXST_*). */
-                const struct nicira11_stats_msg *nsm;
-
-                if (length < sizeof *nsm) {
-                    return OFPERR_OFPBRC_BAD_LEN;
-                }
-                nsm = (const struct nicira11_stats_msg *) oh;
-                hdrs->subtype = ntohl(nsm->subtype);
+            if (hdrs->vendor == NX_VENDOR_ID ||
+                hdrs->vendor == ONF_VENDOR_ID) {
+                hdrs->subtype = ntohl(ovsm->subtype);
             } else {
                 log_bad_vendor(hdrs->vendor);
                 return OFPERR_OFPBRC_BAD_VENDOR;
@@ -253,40 +313,85 @@ ofphdrs_decode_assert(struct ofphdrs *hdrs,
                       const struct ofp_header *oh, size_t length)
 {
     enum ofperr error = ofphdrs_decode(hdrs, oh, length);
-    assert(!error);
+    ovs_assert(!error);
+}
+
+static bool
+ofp_is_stat_request(enum ofp_version version, uint8_t type)
+{
+    switch (version) {
+    case OFP10_VERSION:
+        return type == OFPT10_STATS_REQUEST;
+    case OFP11_VERSION:
+    case OFP12_VERSION:
+    case OFP13_VERSION:
+    case OFP14_VERSION:
+    case OFP15_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:
+    case OFP15_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 (hdrs->version == OFP10_VERSION
-            ? (hdrs->type == OFPT10_STATS_REQUEST ||
-               hdrs->type == OFPT10_STATS_REPLY)
-            : (hdrs->type == OFPT11_STATS_REQUEST ||
-               hdrs->type == OFPT11_STATS_REPLY));
+    return ofp_is_stat(hdrs->version, hdrs->type);
 }
 
 size_t
 ofphdrs_len(const struct ofphdrs *hdrs)
 {
     if (hdrs->type == OFPT_VENDOR) {
-        return sizeof(struct nicira_header);
+        return sizeof(struct ofp_vendor_header);
     }
 
-    if (hdrs->version == OFP10_VERSION) {
+    switch ((enum ofp_version) hdrs->version) {
+    case OFP10_VERSION:
         if (hdrs->type == OFPT10_STATS_REQUEST ||
             hdrs->type == OFPT10_STATS_REPLY) {
             return (hdrs->stat == OFPST_VENDOR
                     ? sizeof(struct nicira10_stats_msg)
                     : sizeof(struct ofp10_stats_msg));
         }
-    } else {
+        break;
+
+    case OFP11_VERSION:
+    case OFP12_VERSION:
+    case OFP13_VERSION:
+    case OFP14_VERSION:
+    case OFP15_VERSION:
         if (hdrs->type == OFPT11_STATS_REQUEST ||
             hdrs->type == OFPT11_STATS_REPLY) {
             return (hdrs->stat == OFPST_VENDOR
-                    ? sizeof(struct nicira11_stats_msg)
+                    ? sizeof(struct ofp11_vendor_stats_msg)
                     : sizeof(struct ofp11_stats_msg));
         }
+        break;
     }
 
     return sizeof(struct ofp_header);
@@ -302,25 +407,40 @@ ofphdrs_len(const struct ofphdrs *hdrs)
 enum ofperr
 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
 {
-    struct ofpbuf msg;
-
-    ofpbuf_use_const(&msg, oh, ntohs(oh->length));
+    struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
     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 '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'.
  *
  * 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->header' to the start of the OpenFlow
+ * header and 'msg->msg' just beyond the headers (that is, to the final value
+ * of msg->data). */
 enum ofperr
 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
 {
@@ -337,7 +457,8 @@ ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
     enum ofpraw raw;
 
     /* Set default outputs. */
-    msg->l2 = msg->l3 = msg->data;
+    msg->header = msg->data;
+    msg->msg = msg->header;
     *rawp = 0;
 
     len = msg->size;
@@ -353,8 +474,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->header = ofpbuf_pull(msg, instance->hdrs_len);
+    msg->msg = msg->data;
 
     min_len = instance->hdrs_len + info->min_body;
     switch (info->extra_multiple) {
@@ -391,11 +512,11 @@ ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
 }
 
 /* Does the same job as ofpraw_pull(), except that it assert-fails if
- * ofpbuf_pull() would have reported an error.  Thus, it's able to use the
+ * ofpraw_pull() 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
- * ofpbuf_decode() on the message and thus know that it's OK.) */
+ * ofpraw_decode() on the message and thus know that it's OK.) */
 enum ofpraw
 ofpraw_pull_assert(struct ofpbuf *msg)
 {
@@ -403,7 +524,7 @@ ofpraw_pull_assert(struct ofpbuf *msg)
     enum ofpraw raw;
 
     error = ofpraw_pull(&raw, msg);
-    assert(!error);
+    ovs_assert(!error);
     return raw;
 }
 
@@ -448,10 +569,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, 'header' points to the beginning of the
+ * OpenFlow header and 'msg' 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(). */
@@ -495,10 +616,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, 'header' points to the beginning of the
+ * OpenFlow header and 'msg' 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(). */
@@ -512,10 +633,10 @@ ofpraw_alloc_stats_reply(const struct ofp_header *request,
 
     error = ofpraw_decode_partial(&request_raw, request,
                                   ntohs(request->length));
-    assert(!error);
+    ovs_assert(!error);
 
     reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
-    assert(reply_raw);
+    ovs_assert(reply_raw);
 
     return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
 }
@@ -528,9 +649,9 @@ 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
- * '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,
+ * Upon return, 'buf->header' points to the beginning of the OpenFlow header
+ * and 'buf->msg' 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(). */
 void
 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
@@ -568,10 +689,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, 'header' points to the beginning of the
+ * OpenFlow header and 'msg' 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(). */
@@ -582,10 +703,10 @@ ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
     enum ofpraw raw;
 
     error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
-    assert(!error);
+    ovs_assert(!error);
 
     raw = ofpraw_stats_request_to_reply(raw, request->version);
-    assert(raw);
+    ovs_assert(raw);
 
     ofpraw_put__(raw, request->version, request->xid, 0, buf);
 }
@@ -601,62 +722,55 @@ 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->header = ofpbuf_put_uninit(buf, instance->hdrs_len);
+    buf->msg = ofpbuf_tail(buf);
 
-    oh = buf->l2;
+    oh = buf->header;
     oh->version = version;
     oh->type = hdrs->type;
     oh->length = htons(buf->size);
     oh->xid = xid;
 
     if (hdrs->type == OFPT_VENDOR) {
-        struct nicira_header *nh = buf->l2;
+        struct ofp_vendor_header *ovh = buf->header;
 
-        assert(hdrs->vendor == NX_VENDOR_ID);
-        nh->vendor = htonl(hdrs->vendor);
-        nh->subtype = htonl(hdrs->subtype);
+        ovh->vendor = htonl(hdrs->vendor);
+        ovh->subtype = htonl(hdrs->subtype);
     } 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->header;
 
         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->header;
 
             ovsm->vendor = htonl(hdrs->vendor);
             if (hdrs->vendor == NX_VENDOR_ID) {
-                struct nicira10_stats_msg *nsm = buf->l2;
+                struct nicira10_stats_msg *nsm = buf->header;
 
                 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->header;
 
         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->header;
 
             ovsm->vendor = htonl(hdrs->vendor);
-            if (hdrs->vendor == NX_VENDOR_ID) {
-                struct nicira11_stats_msg *nsm = buf->l2;
-
-                nsm->subtype = htonl(hdrs->subtype);
-            } else {
-                NOT_REACHED();
-            }
+            ovsm->subtype = htonl(hdrs->subtype);
         }
     }
 }
@@ -686,16 +800,25 @@ ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
     enum ofperr error;
 
     hdrs = instance->hdrs;
-    if (hdrs.version == OFP10_VERSION) {
-        assert(hdrs.type == OFPT10_STATS_REQUEST);
+    switch ((enum ofp_version)hdrs.version) {
+    case OFP10_VERSION:
+        ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
         hdrs.type = OFPT10_STATS_REPLY;
-    } else {
-        assert(hdrs.type == OFPT11_STATS_REQUEST);
+        break;
+    case OFP11_VERSION:
+    case OFP12_VERSION:
+    case OFP13_VERSION:
+    case OFP14_VERSION:
+    case OFP15_VERSION:
+        ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
         hdrs.type = OFPT11_STATS_REPLY;
+        break;
+    default:
+        OVS_NOT_REACHED();
     }
 
     error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
-    assert(!error);
+    ovs_assert(!error);
 
     return reply_raw;
 }
@@ -719,18 +842,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 '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'.
  *
  * 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->header' to the start of the OpenFlow
+ * header and 'msg->msg' just beyond the headers (that is, to the final value
+ * of msg->data). */
 enum ofperr
 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
 {
@@ -751,6 +874,13 @@ ofptype_from_ofpraw(enum ofpraw raw)
 {
     return raw_info_get(raw)->type;
 }
+
+const char *
+ofptype_get_name(enum ofptype type)
+{
+    ovs_assert(type < ARRAY_SIZE(type_names));
+    return type_names[type];
+}
 \f
 /* Updates the 'length' field of the OpenFlow message in 'buf' to
  * 'buf->size'. */
@@ -761,7 +891,7 @@ ofpmsg_update_length(struct ofpbuf *buf)
     oh->length = htons(buf->size);
 }
 
-/* Returns just past the Openflow header (including the stats headers, vendor
+/* Returns just past the OpenFlow header (including the stats headers, vendor
  * header, and any subtype header) in 'oh'. */
 const void *
 ofpmsg_body(const struct ofp_header *oh)
@@ -771,14 +901,23 @@ 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);
+}
 \f
+static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
+
 /* Initializes 'replies' as a new list of stats messages that reply to
  * 'request', which must be a stats request message.  Initially the list will
  * consist of only a single reply part without any body.  The caller should
  * use calls to the other ofpmp_*() functions to add to the body and split the
  * message into multiple parts, if necessary. */
 void
-ofpmp_init(struct list *replies, const struct ofp_header *request)
+ofpmp_init(struct ovs_list *replies, const struct ofp_header *request)
 {
     struct ofpbuf *msg;
 
@@ -796,7 +935,7 @@ ofpmp_init(struct list *replies, const struct ofp_header *request)
  * have not actually been allocated, so the caller must do so with
  * e.g. ofpbuf_put_uninit(). */
 struct ofpbuf *
-ofpmp_reserve(struct list *replies, size_t len)
+ofpmp_reserve(struct ovs_list *replies, size_t len)
 {
     struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
 
@@ -813,8 +952,12 @@ ofpmp_reserve(struct list *replies, size_t len)
 
         next = ofpbuf_new(MAX(1024, hdrs_len + len));
         ofpbuf_put(next, msg->data, hdrs_len);
+        next->header = next->data;
+        next->msg = ofpbuf_tail(next);
         list_push_back(replies, &next->list_node);
 
+        *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE);
+
         return next;
     }
 }
@@ -822,7 +965,7 @@ ofpmp_reserve(struct list *replies, size_t len)
 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
  * returns the first byte. */
 void *
-ofpmp_append(struct list *replies, size_t len)
+ofpmp_append(struct ovs_list *replies, size_t len)
 {
     return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
 }
@@ -838,11 +981,11 @@ ofpmp_append(struct list *replies, size_t len)
  * the first 'start_ofs' bytes, the second one containing the bytes from that
  * offset onward. */
 void
-ofpmp_postappend(struct list *replies, size_t start_ofs)
+ofpmp_postappend(struct ovs_list *replies, size_t start_ofs)
 {
     struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
 
-    assert(start_ofs <= UINT16_MAX);
+    ovs_assert(start_ofs <= UINT16_MAX);
     if (msg->size > UINT16_MAX) {
         size_t len = msg->size - start_ofs;
         memcpy(ofpmp_append(replies, len),
@@ -851,12 +994,46 @@ ofpmp_postappend(struct list *replies, size_t start_ofs)
     }
 }
 
+/* Returns the OpenFlow version of the replies being constructed in 'replies',
+ * which should have been initialized by ofpmp_init(). */
+enum ofp_version
+ofpmp_version(struct ovs_list *replies)
+{
+    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+    const struct ofp_header *oh = msg->data;
+
+    return oh->version;
+}
+
+/* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
+ * should have been initialized by ofpmp_init(). */
+enum ofpraw
+ofpmp_decode_raw(struct ovs_list *replies)
+{
+    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+    enum ofperr error;
+    enum ofpraw raw;
+
+    error = ofpraw_decode_partial(&raw, msg->data, msg->size);
+    ovs_assert(!error);
+    return raw;
+}
+
 static ovs_be16 *
 ofpmp_flags__(const struct ofp_header *oh)
 {
-    return (oh->version == OFP10_VERSION
-            ? &((struct ofp10_stats_msg *) oh)->flags
-            : &((struct ofp11_stats_msg *) oh)->flags);
+    switch ((enum ofp_version)oh->version) {
+    case OFP10_VERSION:
+        return &((struct ofp10_stats_msg *) oh)->flags;
+    case OFP11_VERSION:
+    case OFP12_VERSION:
+    case OFP13_VERSION:
+    case OFP14_VERSION:
+    case OFP15_VERSION:
+        return &((struct ofp11_stats_msg *) oh)->flags;
+    default:
+        OVS_NOT_REACHED();
+    }
 }
 
 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
@@ -885,14 +1062,14 @@ raw_info_get(enum ofpraw raw)
 {
     ofpmsgs_init();
 
-    assert(raw < ARRAY_SIZE(raw_infos));
+    ovs_assert(raw < ARRAY_SIZE(raw_infos));
     return &raw_infos[raw];
 }
 
 static struct raw_instance *
 raw_instance_get(const struct raw_info *info, uint8_t version)
 {
-    assert(version >= info->min_version && version <= info->max_version);
+    ovs_assert(version >= info->min_version && version <= info->max_version);
     return &info->instances[version - info->min_version];
 }
 
@@ -939,9 +1116,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;
     }
 
@@ -959,4 +1137,6 @@ ofpmsgs_init(void)
                         ofphdrs_hash(&inst->hdrs));
         }
     }
+
+    ovsthread_once_done(&once);
 }