[ovs-dev] [PATCH] ofp-util: compile group stats with visual studio

Andy Zhou azhou at nicira.com
Thu Apr 24 21:48:11 UTC 2014


Visual studio does not support 0 size array within a struct,
but supports flexible array. For example, char p[0] is not supported,
but char p[] is O.K.  GCC supports both.

Flexible array can not directly replace a zero sized array.
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html lists the
differences.

Commits 644cfd84772eb7d8 and 6fdaa45a6f6c9 make use of 0 size array.
Convert them to flexible array so that visual studio can compile them
as well.

Reported-by: Gurucharan Shetty <gshetty at nicira.com>
Signed-off-by: Andy Zhou <azhou at nicira.com>
---
 include/openflow/openflow-1.1.h |  8 ++++++--
 include/openflow/openflow-1.3.h |  4 ++--
 lib/ofp-util.c                  | 37 +++++++++++++++++++++++--------------
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/include/openflow/openflow-1.1.h b/include/openflow/openflow-1.1.h
index e3e2d39..4d0a81e 100644
--- a/include/openflow/openflow-1.1.h
+++ b/include/openflow/openflow-1.1.h
@@ -696,7 +696,7 @@ struct ofp11_bucket_counter {
 OFP_ASSERT(sizeof(struct ofp11_bucket_counter) == 16);
 
 /* Body of reply to OFPST11_GROUP request */
-struct ofp11_group_stats {
+struct ofp11_group_stats_base {
     ovs_be16 length;           /* Length of this entry. */
     uint8_t pad[2];            /* Align to 64 bits. */
     ovs_be32 group_id;         /* Group identifier. */
@@ -705,7 +705,11 @@ struct ofp11_group_stats {
     uint8_t pad2[4];           /* Align to 64 bits. */
     ovs_be64 packet_count;     /* Number of packets processed by group. */
     ovs_be64 byte_count;       /* Number of bytes processed by group. */
-    struct ofp11_bucket_counter bucket_stats[0];
+};
+
+struct ofp11_group_stats {
+    struct ofp11_group_stats_base gs;
+    struct ofp11_bucket_counter bucket_stats[];
 };
 OFP_ASSERT(sizeof(struct ofp11_group_stats) == 32);
 
diff --git a/include/openflow/openflow-1.3.h b/include/openflow/openflow-1.3.h
index 8e9f6f7..e715106 100644
--- a/include/openflow/openflow-1.3.h
+++ b/include/openflow/openflow-1.3.h
@@ -384,11 +384,11 @@ OFP_ASSERT(sizeof(struct ofp13_queue_stats) == 40);
 
 /* Body of reply to OFPMP13_GROUP request */
 struct ofp13_group_stats {
-    struct ofp11_group_stats gs;
+    struct ofp11_group_stats_base gs;
     ovs_be32 duration_sec;    /* Time group has been alive in seconds. */
     ovs_be32 duration_nsec;   /* Time group has been alive in nanoseconds
                                  beyond duration_sec. */
-    struct ofp11_bucket_counter bucket_stats[0];
+    struct ofp11_bucket_counter bucket_stats[];
 };
 OFP_ASSERT(sizeof(struct ofp13_group_stats) == 40);
 
diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index 3484394..37dc63f 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -6274,18 +6274,24 @@ ofputil_encode_group_desc_request(enum ofp_version ofp_version)
 }
 
 static void
-ofputil_group_stats_to_ofp11__(const struct ofputil_group_stats *gs,
-                               struct ofp11_group_stats *gs11, size_t length,
-                               struct ofp11_bucket_counter bucket_cnts[])
+ofputil_group_stats_base_to_ofp11__(const struct ofputil_group_stats *gs,
+                                    struct ofp11_group_stats_base *gsb11,
+                                    size_t length)
 {
-    int i;
+    memset(gsb11, 0, length);
+    gsb11->length = htons(length);
+    gsb11->group_id = htonl(gs->group_id);
+    gsb11->ref_count = htonl(gs->ref_count);
+    gsb11->packet_count = htonll(gs->packet_count);
+    gsb11->byte_count = htonll(gs->byte_count);
+}
 
-    memset(gs11, 0, length);
-    gs11->length = htons(length);
-    gs11->group_id = htonl(gs->group_id);
-    gs11->ref_count = htonl(gs->ref_count);
-    gs11->packet_count = htonll(gs->packet_count);
-    gs11->byte_count = htonll(gs->byte_count);
+static void
+ofputil_group_stats_bucket_counters_to_ofp11__(
+    const struct ofputil_group_stats *gs,
+    struct ofp11_bucket_counter bucket_cnts[])
+{
+    int i;
 
     for (i = 0; i < gs->n_buckets; i++) {
        bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
@@ -6295,16 +6301,19 @@ ofputil_group_stats_to_ofp11__(const struct ofputil_group_stats *gs,
 
 static void
 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
-                             struct ofp11_group_stats *gs11, size_t length)
+                             struct ofp11_group_stats *gs11,
+                             size_t length)
 {
-    ofputil_group_stats_to_ofp11__(gs, gs11, length, gs11->bucket_stats);
+    ofputil_group_stats_base_to_ofp11__(gs, &gs11->gs, length);
+    ofputil_group_stats_bucket_counters_to_ofp11__(gs, gs11->bucket_stats);
 }
 
 static void
 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
                              struct ofp13_group_stats *gs13, size_t length)
 {
-    ofputil_group_stats_to_ofp11__(gs, &gs13->gs, length, gs13->bucket_stats);
+    ofputil_group_stats_base_to_ofp11__(gs, &gs13->gs, length);
+    ofputil_group_stats_bucket_counters_to_ofp11__(gs, gs13->bucket_stats);
     gs13->duration_sec = htonl(gs->duration_sec);
     gs13->duration_nsec = htonl(gs->duration_nsec);
 }
@@ -6447,7 +6456,7 @@ ofputil_decode_group_stats_reply(struct ofpbuf *msg,
                                  struct ofputil_group_stats *gs)
 {
     struct ofp11_bucket_counter *obc;
-    struct ofp11_group_stats *ogs11;
+    struct ofp11_group_stats_base *ogs11;
     enum ofpraw raw;
     enum ofperr error;
     size_t base_len;
-- 
1.9.1




More information about the dev mailing list