ofp-msgs: New functions ofpmp_version() and ofpmp_decode_raw().
authorBen Pfaff <blp@nicira.com>
Wed, 7 May 2014 22:11:28 +0000 (15:11 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 14 May 2014 17:31:41 +0000 (10:31 -0700)
Each of these allows code in ofp-util.c to be simplified.

Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/ofp-msgs.c
lib/ofp-msgs.h
lib/ofp-util.c
lib/ofp-util.h
ofproto/ofproto.c

index b67e47a..e54918e 100644 (file)
@@ -935,6 +935,31 @@ 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 list *replies)
+{
+    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+    const struct ofp_header *oh = ofpbuf_data(msg);
+
+    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 list *replies)
+{
+    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+    enum ofperr error;
+    enum ofpraw raw;
+
+    error = ofpraw_decode_partial(&raw, ofpbuf_data(msg), ofpbuf_size(msg));
+    ovs_assert(!error);
+    return raw;
+}
+
 static ovs_be16 *
 ofpmp_flags__(const struct ofp_header *oh)
 {
index ded9042..df7569e 100644 (file)
@@ -639,6 +639,9 @@ struct ofpbuf *ofpmp_reserve(struct list *, size_t len);
 void *ofpmp_append(struct list *, size_t len);
 void ofpmp_postappend(struct list *, size_t start_ofs);
 
+enum ofp_version ofpmp_version(struct list *);
+enum ofpraw ofpmp_decode_raw(struct list *);
+
 /* Decoding multipart replies. */
 uint16_t ofpmp_flags(const struct ofp_header *);
 bool ofpmp_more(const struct ofp_header *);
index f2c1c65..33bc73c 100644 (file)
@@ -2855,10 +2855,9 @@ ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
     size_t start_ofs = ofpbuf_size(reply);
-    enum ofpraw raw;
-    enum ofp_version version = ((struct ofp_header *)ofpbuf_data(reply))->version;
+    enum ofp_version version = ofpmp_version(replies);
+    enum ofpraw raw = ofpmp_decode_raw(replies);
 
-    ofpraw_decode_partial(&raw, ofpbuf_data(reply), ofpbuf_size(reply));
     if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
         struct ofp11_flow_stats *ofs;
 
@@ -3778,11 +3777,10 @@ ofputil_put_phy_port(enum ofp_version ofp_version,
 }
 
 void
-ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
-                                     const struct ofputil_phy_port *pp,
+ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
                                      struct list *replies)
 {
-    switch (ofp_version) {
+    switch (ofpmp_version(replies)) {
     case OFP10_VERSION: {
         struct ofp10_phy_port *opp;
 
@@ -5149,14 +5147,13 @@ void
 ofputil_append_flow_update(const struct ofputil_flow_update *update,
                            struct list *replies)
 {
+    enum ofp_version version = ofpmp_version(replies);
     struct nx_flow_update_header *nfuh;
     struct ofpbuf *msg;
     size_t start_ofs;
-    enum ofp_version version;
 
     msg = ofpbuf_from_list(list_back(replies));
     start_ofs = ofpbuf_size(msg);
-    version = ((struct ofp_header *)msg->frame)->version;
 
     if (update->event == NXFME_ABBREV) {
         struct nx_flow_update_abbrev *nfua;
@@ -5986,10 +5983,7 @@ void
 ofputil_append_port_stat(struct list *replies,
                          const struct ofputil_port_stats *ops)
 {
-    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
-    struct ofp_header *oh = ofpbuf_data(msg);
-
-    switch ((enum ofp_version)oh->version) {
+    switch (ofpmp_version(replies)) {
     case OFP13_VERSION: {
         struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
         ofputil_port_stats_to_ofp13(ops, reply);
@@ -6321,15 +6315,13 @@ void
 ofputil_append_group_stats(struct list *replies,
                            const struct ofputil_group_stats *gs)
 {
-    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
-    struct ofp_header *oh = ofpbuf_data(msg);
     size_t bucket_counter_size;
     struct ofp11_bucket_counter *bucket_counters;
     size_t length;
 
     bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
 
-    switch ((enum ofp_version) oh->version) {
+    switch (ofpmp_version(replies)) {
     case OFP11_VERSION:
     case OFP12_VERSION:{
             struct ofp11_group_stats *gs11;
@@ -6538,10 +6530,10 @@ ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
                                 struct list *replies)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
+    enum ofp_version version = ofpmp_version(replies);
     struct ofp11_group_desc_stats *ogds;
     struct ofputil_bucket *bucket;
     size_t start_ogds;
-    enum ofp_version version = ((struct ofp_header *)ofpbuf_data(reply))->version;
 
     start_ogds = ofpbuf_size(reply);
     ofpbuf_put_zeros(reply, sizeof *ogds);
@@ -7032,10 +7024,7 @@ void
 ofputil_append_queue_stat(struct list *replies,
                           const struct ofputil_queue_stats *oqs)
 {
-    struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
-    struct ofp_header *oh = ofpbuf_data(msg);
-
-    switch ((enum ofp_version)oh->version) {
+    switch (ofpmp_version(replies)) {
     case OFP13_VERSION: {
         struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
         ofputil_queue_stats_to_ofp13(oqs, reply);
index 14c83d5..63f4271 100644 (file)
@@ -872,8 +872,7 @@ uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
 
 /* Encoding OpenFlow stats messages. */
-void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
-                                          const struct ofputil_phy_port *pp,
+void ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
                                           struct list *replies);
 
 /* Encoding simple OpenFlow messages. */
index d92bafd..557f124 100644 (file)
@@ -3206,15 +3206,12 @@ handle_port_desc_stats_request(struct ofconn *ofconn,
                                const struct ofp_header *request)
 {
     struct ofproto *p = ofconn_get_ofproto(ofconn);
-    enum ofp_version version;
     struct ofport *port;
     struct list replies;
 
     ofpmp_init(&replies, request);
-
-    version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
     HMAP_FOR_EACH (port, hmap_node, &p->ports) {
-        ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
+        ofputil_append_port_desc_stats_reply(&port->pp, &replies);
     }
 
     ofconn_send_replies(ofconn, &replies);