From patchwork Thu May 12 00:40:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Pettit X-Patchwork-Id: 621331 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (archives.nicira.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 3r4vKx3JxQz9t0t for ; Thu, 12 May 2016 10:40:52 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id E9DF110AC2; Wed, 11 May 2016 17:40:50 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx3v3.cudamail.com (mx3.cudamail.com [64.34.241.5]) by archives.nicira.com (Postfix) with ESMTPS id DAB6510AC0 for ; Wed, 11 May 2016 17:40:49 -0700 (PDT) Received: from bar6.cudamail.com (localhost [127.0.0.1]) by mx3v3.cudamail.com (Postfix) with ESMTPS id 326FF16111A for ; Wed, 11 May 2016 18:40:49 -0600 (MDT) X-ASG-Debug-ID: 1463013648-0b323747d68a760001-byXFYA Received: from mx3-pf3.cudamail.com ([192.168.14.3]) by bar6.cudamail.com with ESMTP id gmwJ2dUFChHeJhCk (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 11 May 2016 18:40:48 -0600 (MDT) X-Barracuda-Envelope-From: jpettit@ovn.org X-Barracuda-RBL-Trusted-Forwarder: 192.168.14.3 Received: from unknown (HELO relay3-d.mail.gandi.net) (217.70.183.195) by mx3-pf3.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 12 May 2016 00:40:47 -0000 Received-SPF: pass (mx3-pf3.cudamail.com: SPF record at ovn.org designates 217.70.183.195 as permitted sender) X-Barracuda-Apparent-Source-IP: 217.70.183.195 X-Barracuda-RBL-IP: 217.70.183.195 Received: from mfilter13-d.gandi.net (mfilter13-d.gandi.net [217.70.178.141]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 541C1A80BF for ; Thu, 12 May 2016 02:40:46 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter13-d.gandi.net Received: from relay3-d.mail.gandi.net ([IPv6:::ffff:217.70.183.195]) by mfilter13-d.gandi.net (mfilter13-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id hy_zP3Qr0XWj for ; Thu, 12 May 2016 02:40:44 +0200 (CEST) X-Originating-IP: 208.91.2.4 Received: from localhost.localdomain (unknown [208.91.2.4]) (Authenticated sender: jpettit@ovn.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 58D10A80CD for ; Thu, 12 May 2016 02:40:44 +0200 (CEST) X-CudaMail-Envelope-Sender: jpettit@ovn.org From: Justin Pettit To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-V3-510058607 X-CudaMail-DTE: 051116 X-CudaMail-Originating-IP: 217.70.183.195 Date: Wed, 11 May 2016 17:40:10 -0700 X-ASG-Orig-Subj: [##CM-V3-510058607##][PATCH] dpctl: Sort port listing in "show" command. Message-Id: <1463013610-11423-1-git-send-email-jpettit@ovn.org> X-Mailer: git-send-email 1.9.1 X-Barracuda-Connect: UNKNOWN[192.168.14.3] X-Barracuda-Start-Time: 1463013648 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Subject: [ovs-dev] [PATCH] dpctl: Sort port listing in "show" command. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" 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 Acked-by: Ben Pfaff --- lib/dpctl.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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 *);