Each of these allows code in ofp-util.c to be simplified.

Signed-off-by: Ben Pfaff <b...@nicira.com>
---
 lib/ofp-msgs.c    | 25 +++++++++++++++++++++++++
 lib/ofp-msgs.h    |  3 +++
 lib/ofp-util.c    | 29 +++++++++--------------------
 lib/ofp-util.h    |  3 +--
 ofproto/ofproto.c |  5 +----
 5 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/lib/ofp-msgs.c b/lib/ofp-msgs.c
index b67e47a..e54918e 100644
--- a/lib/ofp-msgs.c
+++ b/lib/ofp-msgs.c
@@ -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)
 {
diff --git a/lib/ofp-msgs.h b/lib/ofp-msgs.h
index ded9042..df7569e 100644
--- a/lib/ofp-msgs.h
+++ b/lib/ofp-msgs.h
@@ -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 *);
diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index d178113..beeecab 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -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;
 
@@ -5145,14 +5143,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;
@@ -5982,10 +5979,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);
@@ -6316,11 +6310,9 @@ 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 length;
 
-    switch ((enum ofp_version) oh->version) {
+    switch (ofpmp_version(replies)) {
     case OFP11_VERSION:
     case OFP12_VERSION:{
             struct ofp11_group_stats *reply;
@@ -6529,10 +6521,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);
@@ -7023,10 +7015,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);
diff --git a/lib/ofp-util.h b/lib/ofp-util.h
index f4b5f79..7a24688 100644
--- a/lib/ofp-util.h
+++ b/lib/ofp-util.h
@@ -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. */
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index d92bafd..557f124 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -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);
-- 
1.9.1

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to