openflow: Make stats replies more like other OpenFlow messages.
[sliver-openvswitch.git] / lib / ofp-util.c
index dfbbf1b..c3d9187 100644 (file)
@@ -410,19 +410,17 @@ ofputil_decode_vendor(const struct ofp_header *oh,
 static int
 check_nxstats_msg(const struct ofp_header *oh)
 {
-    const struct ofp_stats_request *osr;
+    const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
     ovs_be32 vendor;
 
-    osr = (const struct ofp_stats_request *) oh;
-
-    memcpy(&vendor, osr->body, sizeof vendor);
+    memcpy(&vendor, osm + 1, sizeof vendor);
     if (vendor != htonl(NX_VENDOR_ID)) {
         VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
                      "unknown vendor %"PRIx32, ntohl(vendor));
         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
     }
 
-    if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
+    if (ntohs(osm->header.length) < sizeof(struct nicira_stats_msg)) {
         VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
     }
@@ -502,35 +500,34 @@ static int
 ofputil_decode_ofpst_request(const struct ofp_header *oh,
                              const struct ofputil_msg_type **typep)
 {
-    enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
     static const struct ofputil_msg_type ofpst_requests[] = {
         { OFPUTIL_OFPST_DESC_REQUEST,
           OFPST_DESC, "OFPST_DESC request",
-          OSR_SIZE, 0 },
+          sizeof(struct ofp_stats_msg), 0 },
 
         { OFPUTIL_OFPST_FLOW_REQUEST,
           OFPST_FLOW, "OFPST_FLOW request",
-          OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
+          sizeof(struct ofp_flow_stats_request), 0 },
 
         { OFPUTIL_OFPST_AGGREGATE_REQUEST,
           OFPST_AGGREGATE, "OFPST_AGGREGATE request",
-          OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
+          sizeof(struct ofp_flow_stats_request), 0 },
 
         { OFPUTIL_OFPST_TABLE_REQUEST,
           OFPST_TABLE, "OFPST_TABLE request",
-          OSR_SIZE, 0 },
+          sizeof(struct ofp_stats_msg), 0 },
 
         { OFPUTIL_OFPST_PORT_REQUEST,
           OFPST_PORT, "OFPST_PORT request",
-          OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
+          sizeof(struct ofp_port_stats_request), 0 },
 
         { OFPUTIL_OFPST_QUEUE_REQUEST,
           OFPST_QUEUE, "OFPST_QUEUE request",
-          OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
+          sizeof(struct ofp_queue_stats_request), 0 },
 
         { 0,
           OFPST_VENDOR, "OFPST_VENDOR request",
-          OSR_SIZE + sizeof(uint32_t), 1 },
+          sizeof(struct ofp_vendor_stats_msg), 1 },
     };
 
     static const struct ofputil_msg_category ofpst_request_category = {
@@ -539,14 +536,13 @@ ofputil_decode_ofpst_request(const struct ofp_header *oh,
         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
     };
 
-    const struct ofp_stats_request *osr;
+    const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
     int error;
 
-    osr = (const struct ofp_stats_request *) oh;
     error = ofputil_lookup_openflow_message(&ofpst_request_category,
-                                            ntohs(osr->type),
+                                            ntohs(request->type),
                                             ntohs(oh->length), typep);
-    if (!error && osr->type == htons(OFPST_VENDOR)) {
+    if (!error && request->type == htons(OFPST_VENDOR)) {
         error = ofputil_decode_nxst_request(oh, typep);
     }
     return error;
@@ -556,35 +552,34 @@ static int
 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
                            const struct ofputil_msg_type **typep)
 {
-    enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
     static const struct ofputil_msg_type ofpst_replies[] = {
         { OFPUTIL_OFPST_DESC_REPLY,
           OFPST_DESC, "OFPST_DESC reply",
-          OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
+          sizeof(struct ofp_desc_stats), 0 },
 
         { OFPUTIL_OFPST_FLOW_REPLY,
           OFPST_FLOW, "OFPST_FLOW reply",
-          OSR_SIZE, 1 },
+          sizeof(struct ofp_stats_msg), 1 },
 
         { OFPUTIL_OFPST_AGGREGATE_REPLY,
           OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
-          OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
+          sizeof(struct ofp_aggregate_stats_reply), 0 },
 
         { OFPUTIL_OFPST_TABLE_REPLY,
           OFPST_TABLE, "OFPST_TABLE reply",
-          OSR_SIZE, sizeof(struct ofp_table_stats) },
+          sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
 
         { OFPUTIL_OFPST_PORT_REPLY,
           OFPST_PORT, "OFPST_PORT reply",
-          OSR_SIZE, sizeof(struct ofp_port_stats) },
+          sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
 
         { OFPUTIL_OFPST_QUEUE_REPLY,
           OFPST_QUEUE, "OFPST_QUEUE reply",
-          OSR_SIZE, sizeof(struct ofp_queue_stats) },
+          sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
 
         { 0,
           OFPST_VENDOR, "OFPST_VENDOR reply",
-          OSR_SIZE + sizeof(uint32_t), 1 },
+          sizeof(struct ofp_vendor_stats_msg), 1 },
     };
 
     static const struct ofputil_msg_category ofpst_reply_category = {
@@ -593,13 +588,13 @@ ofputil_decode_ofpst_reply(const struct ofp_header *oh,
         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
     };
 
-    const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
+    const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
     int error;
 
     error = ofputil_lookup_openflow_message(&ofpst_reply_category,
-                                           ntohs(osr->type),
+                                           ntohs(reply->type),
                                            ntohs(oh->length), typep);
-    if (!error && osr->type == htons(OFPST_VENDOR)) {
+    if (!error && reply->type == htons(OFPST_VENDOR)) {
         error = ofputil_decode_nxst_reply(oh, typep);
     }
     return error;
@@ -682,11 +677,11 @@ ofputil_decode_msg_type(const struct ofp_header *oh,
 
         { 0,
           OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
-          sizeof(struct ofp_stats_request), 1 },
+          sizeof(struct ofp_stats_msg), 1 },
 
         { 0,
           OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
-          sizeof(struct ofp_stats_reply), 1 },
+          sizeof(struct ofp_stats_msg), 1 },
 
         { OFPUTIL_OFPT_BARRIER_REQUEST,
           OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
@@ -1018,7 +1013,8 @@ ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
                                   const struct ofp_header *oh,
                                   bool aggregate)
 {
-    const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
+    const struct ofp_flow_stats_request *ofsr =
+        (const struct ofp_flow_stats_request *) oh;
 
     fsr->aggregate = aggregate;
     ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
@@ -1102,11 +1098,8 @@ ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
         struct ofp_flow_stats_request *ofsr;
         int type;
 
-        BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
-                          == sizeof(struct ofp_aggregate_stats_request));
-
         type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
-        ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
+        ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
         ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
         ofsr->table_id = fsr->table_id;
         ofsr->out_port = htons(fsr->out_port);
@@ -1116,7 +1109,7 @@ ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
         int subtype;
 
         subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
-        ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
+        ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
         match_len = nx_put_match(msg, &fsr->match);
 
         nfsr = msg->data;
@@ -1152,7 +1145,7 @@ ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
     if (!msg->l2) {
         msg->l2 = msg->data;
         if (code == OFPUTIL_OFPST_FLOW_REPLY) {
-            ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
+            ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
         } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
             ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
         } else {
@@ -1526,56 +1519,149 @@ update_openflow_length(struct ofpbuf *buffer)
     oh->length = htons(buffer->size);
 }
 
-/* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
- * space allocated for the 'body' member.  Returns the first byte of the 'body'
- * member. */
+static void
+put_stats__(ovs_be32 xid, uint8_t ofp_type,
+            ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
+            struct ofpbuf *msg)
+{
+    if (ofpst_type == htons(OFPST_VENDOR)) {
+        struct nicira_stats_msg *nsm;
+
+        nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
+        nsm->vsm.osm.type = ofpst_type;
+        nsm->vsm.vendor = htonl(NX_VENDOR_ID);
+        nsm->subtype = nxst_subtype;
+    } else {
+        struct ofp_stats_msg *osm;
+
+        osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
+        osm->type = ofpst_type;
+    }
+}
+
+/* Creates a statistics request message with total length 'openflow_len'
+ * (including all headers) and the given 'ofpst_type', and stores the buffer
+ * containing the new message in '*bufferp'.  If 'ofpst_type' is OFPST_VENDOR
+ * then 'nxst_subtype' is used as the Nicira vendor extension statistics
+ * subtype (otherwise 'nxst_subtype' is ignored).
+ *
+ * Initializes bytes following the headers to all-bits-zero.
+ *
+ * Returns the first byte of the new message. */
 void *
-ofputil_make_stats_request(size_t body_len, uint16_t type,
-                           struct ofpbuf **bufferp)
+ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
+                           uint32_t nxst_subtype, struct ofpbuf **bufferp)
 {
-    struct ofp_stats_request *osr;
-    osr = make_openflow((offsetof(struct ofp_stats_request, body)
-                        + body_len), OFPT_STATS_REQUEST, bufferp);
-    osr->type = htons(type);
-    osr->flags = htons(0);
-    return osr->body;
+    struct ofpbuf *msg;
+
+    msg = *bufferp = ofpbuf_new(openflow_len);
+    put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
+                htons(ofpst_type), htonl(nxst_subtype), msg);
+    ofpbuf_padto(msg, openflow_len);
+
+    return msg->data;
+}
+
+static void
+put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
+{
+    assert(request->header.type == OFPT_STATS_REQUEST ||
+           request->header.type == OFPT_STATS_REPLY);
+    put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
+                (request->type != htons(OFPST_VENDOR)
+                 ? htonl(0)
+                 : ((const struct nicira_stats_msg *) request)->subtype),
+                msg);
 }
 
-/* Creates a stats request message with Nicira as vendor and the given
- * 'subtype', of total length 'openflow_len'.  Returns the message. */
+/* Creates a statistics reply message with total length 'openflow_len'
+ * (including all headers) and the same type (either a standard OpenFlow
+ * statistics type or a Nicira extension type and subtype) as 'request', and
+ * stores the buffer containing the new message in '*bufferp'.
+ *
+ * Initializes bytes following the headers to all-bits-zero.
+ *
+ * Returns the first byte of the new message. */
 void *
-ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
-                             struct ofpbuf **bufferp)
+ofputil_make_stats_reply(size_t openflow_len,
+                         const struct ofp_stats_msg *request,
+                         struct ofpbuf **bufferp)
 {
-    struct nicira_stats_msg *nsm;
+    struct ofpbuf *msg;
+
+    msg = *bufferp = ofpbuf_new(openflow_len);
+    put_stats_reply__(request, msg);
+    ofpbuf_padto(msg, openflow_len);
 
-    nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
-    nsm->type = htons(OFPST_VENDOR);
-    nsm->flags = htons(0);
-    nsm->vendor = htonl(NX_VENDOR_ID);
-    nsm->subtype = htonl(subtype);
-    return nsm;
+    return msg->data;
+}
+
+/* Initializes 'replies' as a list of ofpbufs that will contain a series of
+ * replies to 'request', which should be an OpenFlow or Nicira extension
+ * statistics request.  Initially 'replies' will have a single reply message
+ * that has only a header.  The functions ofputil_reserve_stats_reply() and
+ * ofputil_append_stats_reply() may be used to add to the reply. */
+void
+ofputil_start_stats_reply(const struct ofp_stats_msg *request,
+                          struct list *replies)
+{
+    struct ofpbuf *msg;
+
+    msg = ofpbuf_new(1024);
+    put_stats_reply__(request, msg);
+
+    list_init(replies);
+    list_push_back(replies, &msg->list_node);
+}
+
+/* Prepares to append up to 'len' bytes to the series of statistics replies in
+ * 'replies', which should have been initialized with
+ * ofputil_start_stats_reply().  Returns an ofpbuf with at least 'len' bytes of
+ * tailroom.  (The 'len' bytes have not actually be allocated; the caller must
+ * do so with e.g. ofpbuf_put_uninit().) */
+struct ofpbuf *
+ofputil_reserve_stats_reply(size_t len, struct list *replies)
+{
+    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+    struct ofp_stats_msg *osm = msg->data;
+
+    if (msg->size + len <= UINT16_MAX) {
+        ofpbuf_prealloc_tailroom(msg, len);
+    } else {
+        osm->flags |= htons(OFPSF_REPLY_MORE);
+
+        msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
+        put_stats_reply__(osm, msg);
+        list_push_back(replies, &msg->list_node);
+    }
+    return msg;
+}
+
+/* Appends 'len' bytes to the series of statistics replies in 'replies', and
+ * returns the first byte. */
+void *
+ofputil_append_stats_reply(size_t len, struct list *replies)
+{
+    return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
 }
 
-/* Returns the first byte of the 'body' member of the ofp_stats_request or
- * ofp_stats_reply in 'oh'. */
+/* Returns the first byte past the ofp_stats_msg header in 'oh'. */
 const void *
 ofputil_stats_body(const struct ofp_header *oh)
 {
     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
-    return ((const struct ofp_stats_request *) oh)->body;
+    return (const struct ofp_stats_msg *) oh + 1;
 }
 
-/* Returns the length of the 'body' member of the ofp_stats_request or
- * ofp_stats_reply in 'oh'. */
+/* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
 size_t
 ofputil_stats_body_len(const struct ofp_header *oh)
 {
     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
-    return ntohs(oh->length) - sizeof(struct ofp_stats_request);
+    return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
 }
 
-/* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
+/* Returns the first byte past the nicira_stats_msg header in 'oh'. */
 const void *
 ofputil_nxstats_body(const struct ofp_header *oh)
 {
@@ -1583,7 +1669,7 @@ ofputil_nxstats_body(const struct ofp_header *oh)
     return ((const struct nicira_stats_msg *) oh) + 1;
 }
 
-/* Returns the length of the body of the nicira_stats_msg in 'oh'. */
+/* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
 size_t
 ofputil_nxstats_body_len(const struct ofp_header *oh)
 {