[ovs-dev] [of1.5 v2 04/20] ofp-util: Remove ofputil_count_phy_ports().

Ben Pfaff blp at nicira.com
Sat May 10 02:40:20 UTC 2014


It's harder to calculate the number of ports in a given amount of space in
OpenFlow 1.4 and later, because the ofp_port structure becomes variable
length in those versions.  This commit removes the one caller, replacing
it by a version that doesn't need to know the number of ports in advance.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/ofp-print.c | 27 ++++++++++++++++-----------
 lib/ofp-util.c  |  7 -------
 lib/ofp-util.h  |  1 -
 3 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/lib/ofp-print.c b/lib/ofp-print.c
index a2e515d..909a846 100644
--- a/lib/ofp-print.c
+++ b/lib/ofp-print.c
@@ -418,28 +418,33 @@ static void
 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
                     struct ofpbuf *b)
 {
-    size_t n_ports;
     struct ofputil_phy_port *ports;
-    enum ofperr error;
+    size_t allocated_ports, n_ports;
+    int retval;
     size_t i;
 
-    n_ports = ofputil_count_phy_ports(ofp_version, b);
+    ports = NULL;
+    allocated_ports = 0;
+    for (n_ports = 0; ; n_ports++) {
+        if (n_ports >= allocated_ports) {
+            ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
+        }
 
-    ports = xmalloc(n_ports * sizeof *ports);
-    for (i = 0; i < n_ports; i++) {
-        error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
-        if (error) {
-            ofp_print_error(string, error);
-            goto exit;
+        retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
+        if (retval) {
+            break;
         }
     }
+
     qsort(ports, n_ports, sizeof *ports, compare_ports);
     for (i = 0; i < n_ports; i++) {
         ofp_print_phy_port(string, &ports[i]);
     }
-
-exit:
     free(ports);
+
+    if (retval != EOF) {
+        ofp_print_error(string, retval);
+    }
 }
 
 static const char *
diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index 33bc73c..1ba743f 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -5570,13 +5570,6 @@ ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
     }
 }
 
-/* Given a buffer 'b' that contains an array of OpenFlow ports of type
- * 'ofp_version', returns the number of elements. */
-size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
-{
-    return ofpbuf_size(b) / ofputil_get_phy_port_size(ofp_version);
-}
-
 /* ofp-util.def lists the mapping from names to action. */
 static const char *const names[OFPUTIL_N_ACTIONS] = {
     NULL,
diff --git a/lib/ofp-util.h b/lib/ofp-util.h
index 63f4271..bb3d647 100644
--- a/lib/ofp-util.h
+++ b/lib/ofp-util.h
@@ -572,7 +572,6 @@ bool ofputil_switch_features_has_ports(struct ofpbuf *b);
 /* phy_port helper functions. */
 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
                           struct ofputil_phy_port *);
-size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
 
 /* Abstract ofp_port_status. */
 struct ofputil_port_status {
-- 
1.9.1




More information about the dev mailing list