diff mbox

[ovs-dev] dpctl: Sort port listing in "show" command.

Message ID 1463013610-11423-1-git-send-email-jpettit@ovn.org
State Accepted
Headers show

Commit Message

Justin Pettit May 12, 2016, 12:40 a.m. UTC
The port listing did not consistently print in the same order.  While it
is a better user experience to see the ports printed in order, more
importantly, this fixes a unit test ("dpctl - add-if set-if del-if")
that would occasionally fail due to expecting that the ports are printed
in order.

Signed-off-by: Justin Pettit <jpettit@ovn.org>
---
 lib/dpctl.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Ben Pfaff May 17, 2016, 5:27 a.m. UTC | #1
On Wed, May 11, 2016 at 05:40:10PM -0700, Justin Pettit wrote:
> The port listing did not consistently print in the same order.  While it
> is a better user experience to see the ports printed in order, more
> importantly, this fixes a unit test ("dpctl - add-if set-if del-if")
> that would occasionally fail due to expecting that the ports are printed
> in order.
> 
> Signed-off-by: Justin Pettit <jpettit@ovn.org>

Acked-by: Ben Pfaff <blp@ovn.org>
Justin Pettit May 17, 2016, 6:40 a.m. UTC | #2
> On May 16, 2016, at 10:27 PM, Ben Pfaff <blp@ovn.org> wrote:
> 
> On Wed, May 11, 2016 at 05:40:10PM -0700, Justin Pettit wrote:
>> The port listing did not consistently print in the same order.  While it
>> is a better user experience to see the ports printed in order, more
>> importantly, this fixes a unit test ("dpctl - add-if set-if del-if")
>> that would occasionally fail due to expecting that the ports are printed
>> in order.
>> 
>> Signed-off-by: Justin Pettit <jpettit@ovn.org>
> 
> Acked-by: Ben Pfaff <blp@ovn.org>

Thanks!  I pushed it to master.

--Justin
diff mbox

Patch

diff --git a/lib/dpctl.c b/lib/dpctl.c
index 11b6ed0..ffe702e 100644
--- a/lib/dpctl.c
+++ b/lib/dpctl.c
@@ -508,6 +508,18 @@  print_human_size(struct dpctl_params *dpctl_p, uint64_t value)
     }
 }
 
+/* qsort comparison function. */
+static int
+compare_port_nos(const void *a_, const void *b_)
+{
+    const odp_port_t *ap = a_;
+    const odp_port_t *bp = b_;
+    uint32_t a = odp_to_u32(*ap);
+    uint32_t b = odp_to_u32(*bp);
+
+    return a < b ? -1 : a > b;
+}
+
 static void
 show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
 {
@@ -531,7 +543,25 @@  show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
         }
     }
 
+    odp_port_t *port_nos = NULL;
+    size_t allocated_port_nos = 0, n_port_nos = 0;
     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
+        if (n_port_nos >= allocated_port_nos) {
+            port_nos = x2nrealloc(port_nos, &allocated_port_nos,
+                                  sizeof *port_nos);
+        }
+
+        port_nos[n_port_nos] = dpif_port.port_no;
+        n_port_nos++;
+    }
+
+    qsort(port_nos, n_port_nos, sizeof *port_nos, compare_port_nos);
+
+    for (int i = 0; i < n_port_nos; i++) {
+        if (dpif_port_query_by_number(dpif, port_nos[i], &dpif_port)) {
+            continue;
+        }
+
         dpctl_print(dpctl_p, "\tport %u: %s",
                     dpif_port.port_no, dpif_port.name);
 
@@ -580,6 +610,7 @@  show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
             if (error) {
                 dpctl_print(dpctl_p, ", open failed (%s)",
                             ovs_strerror(error));
+                dpif_port_destroy(&dpif_port);
                 continue;
             }
             error = netdev_get_stats(netdev, &s);
@@ -612,7 +643,10 @@  show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
                             ovs_strerror(error));
             }
         }
+        dpif_port_destroy(&dpif_port);
     }
+
+    free(port_nos);
 }
 
 typedef void (*dps_for_each_cb)(struct dpif *, struct dpctl_params *);