From patchwork Mon Apr 27 23:50:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Poirier X-Patchwork-Id: 220429 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA508C83002 for ; Mon, 27 Apr 2020 23:51:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ABFC62072D for ; Mon, 27 Apr 2020 23:51:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="TKenPXte" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726344AbgD0XvD (ORCPT ); Mon, 27 Apr 2020 19:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726244AbgD0XvC (ORCPT ); Mon, 27 Apr 2020 19:51:02 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E0BFC0610D5 for ; Mon, 27 Apr 2020 16:51:02 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id f7so9779423pfa.9 for ; Mon, 27 Apr 2020 16:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=vtY0fL5ZVtBrIXBGjRdLuaI2tiIhNBnCzlrDeA3osCs=; b=TKenPXtelDxBJbE6h7L3/jHw59uWmw9MFIvARi8EkCFlRCKepM4Kpd9m82y/TX7azM lF5Y0WzzvgOcRElHjc5NecfU2xgx99/ORZqzmh13n+W0dWCVjZtL44/F/T7vMQ2C1ZXm OzaX5a6cGXD8mjsEDI3x9VKpYRH9MCUvwuvj8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vtY0fL5ZVtBrIXBGjRdLuaI2tiIhNBnCzlrDeA3osCs=; b=tqWL8EgKMZwg92gx4F9ojM0UlMaXbKzncP8Yqx+j6W18tytp+vjyl1XAA8V93LJHVk DPMwZ59TAWxH34BztJxtTESxmfOIie/YbmEp9P/tA12v+Gd77jdijBSKlyXfXDfa91GR eNW96peAlNYvSMHQIosYVgKaBMtf7mHh9a05jrv8ADpY29HllVijHevLRwjcOvFu2tCe xraBXpb2+2MxUfSSa10NfBIWvItWBZakoR3/gwTEXOFjWAXDylTCsP2zoWqlg3P9TrTV dxIYhlCuZEK9fEpqHDDxuixQoGlgPHYRXygLzFqxtPzfz3O5DLGku9oijiRdBls5psLZ s/jQ== X-Gm-Message-State: AGi0PubcToUw2pucWbIAZl8fRd28Z+00AzLn4c4rlwgQ69dhNwyVRSI2 qQpRXAbBW01k208nvXgoqG2kIgRXOjI= X-Google-Smtp-Source: APiQypIsK6NhWzpYIyIDv2eF3v1gtF7tLOcTzJm9LEo4Aavw1WLYTx+dr4eV+1vP81en2x9fJSDGBQ== X-Received: by 2002:a63:ac57:: with SMTP id z23mr6621355pgn.423.1588031461235; Mon, 27 Apr 2020 16:51:01 -0700 (PDT) Received: from f3.synalogic.ca (ae055068.dynamic.ppp.asahi-net.or.jp. [14.3.55.68]) by smtp.gmail.com with ESMTPSA id 128sm13058106pfy.5.2020.04.27.16.51.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 16:51:00 -0700 (PDT) From: Benjamin Poirier To: netdev@vger.kernel.org Subject: [PATCH iproute2 2/7] bridge: Use consistent column names in vlan output Date: Tue, 28 Apr 2020 08:50:46 +0900 Message-Id: <20200427235051.250058-3-bpoirier@cumulusnetworks.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> References: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Fix singular vs plural. Add a hyphen to clarify that each of those are single fields. Signed-off-by: Benjamin Poirier --- bridge/vlan.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bridge/vlan.c b/bridge/vlan.c index 08b19897..a708e6d2 100644 --- a/bridge/vlan.c +++ b/bridge/vlan.c @@ -538,9 +538,9 @@ static int vlan_show(int argc, char **argv, int subject) } if (!is_json_context()) { - printf("port\tvlan ids"); + printf("port\tvlan-id"); if (subject == VLAN_SHOW_TUNNELINFO) - printf("\ttunnel id"); + printf("\ttunnel-id"); printf("\n"); } @@ -559,7 +559,7 @@ static int vlan_show(int argc, char **argv, int subject) } if (!is_json_context()) - printf("%-16s vlan id\n", "port"); + printf("%-16s vlan-id\n", "port"); if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); From patchwork Mon Apr 27 23:50:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Poirier X-Patchwork-Id: 220428 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6650C83002 for ; Mon, 27 Apr 2020 23:51:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99FAE2072D for ; Mon, 27 Apr 2020 23:51:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="e15KRVd0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726381AbgD0XvH (ORCPT ); Mon, 27 Apr 2020 19:51:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726244AbgD0XvG (ORCPT ); Mon, 27 Apr 2020 19:51:06 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C24C0610D5 for ; Mon, 27 Apr 2020 16:51:05 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id 18so8574027pfv.8 for ; Mon, 27 Apr 2020 16:51:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=EH8Sl7z522Zd8cUL5SMXyaBdw4FbKuGabTcfzp6iVp0=; b=e15KRVd0mwbMy8jlxszzmC2IbB8hynkcGAXxI9UKTlkW9KQSHbsdh2ORkpnTeTP3Ia HTfnkNfI4Fb2ZVMKZxinL9ecJ/Qocwz06E60SPFTkzosyxbKiNKo5bLaxu2Ays7wjHmH dqyF7+ZkX0Qe5mxUk16/FvO4kFdhy8fuyWkPs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EH8Sl7z522Zd8cUL5SMXyaBdw4FbKuGabTcfzp6iVp0=; b=OItIXYGV8bEkQgpeVv8EYcHBLs9HZX903PQsc/LxNVuh1CFU/2HpJD4RBvoWn+ItLM aGm4J4Y/uoEZKjZcs1E52t0JFOMkKnIbkAmiBDYCJp4Wq8vkDy62abgczlqdLatxzryd oQTMvimSsSAOyblxxfmQ7d5ooYYd8VC+6UXhyZLS2m60c2tMY/fi6tyDahtS93doAG9k N6iML1/HZWUbMmUSZGrRzjXrWGHrXIMbUOrzoe5eEEbB+E/TrbEe0KbiGhlq2XFM0C40 BQ9sEHJgn+yJlfpER5+erCpjtMUJZoLU+6N66MP9EokSPOJnfK+wS1osj1AG3zvQimFs djxQ== X-Gm-Message-State: AGi0PuZimGpUDWNSHsd5xSeIjQc4vOniy8kfhwOiGkzNqu2mp0KuYe/r VnDeJm672xeJX/AUbnbNLJzlErB/Cn0= X-Google-Smtp-Source: APiQypIgTPuc+QO/iSHqEziagX39U/r19KNr8JnwtqUlUCGJ2sOj1mcEiVMCZfMKCdZB+7qqOKkCTw== X-Received: by 2002:a63:fc5d:: with SMTP id r29mr19621050pgk.131.1588031464175; Mon, 27 Apr 2020 16:51:04 -0700 (PDT) Received: from f3.synalogic.ca (ae055068.dynamic.ppp.asahi-net.or.jp. [14.3.55.68]) by smtp.gmail.com with ESMTPSA id 128sm13058106pfy.5.2020.04.27.16.51.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 16:51:03 -0700 (PDT) From: Benjamin Poirier To: netdev@vger.kernel.org Subject: [PATCH iproute2 4/7] bridge: Fix output with empty vlan lists Date: Tue, 28 Apr 2020 08:50:48 +0900 Message-Id: <20200427235051.250058-5-bpoirier@cumulusnetworks.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> References: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Consider this configuration: ip link add br0 type bridge ip link add vx0 type vxlan dstport 4789 external ip link set dev vx0 master br0 bridge vlan del vid 1 dev vx0 ip link add vx1 type vxlan dstport 4790 external ip link set dev vx1 master br0 root@vsid:/src/iproute2# ./bridge/bridge vlan port vlan ids br0 1 pvid untagged vx0 None vx1 1 pvid untagged root@vsid:/src/iproute2# Note the useless and inconsistent empty lines. root@vsid:/src/iproute2# ./bridge/bridge vlan tunnelshow port vlan ids tunnel id br0 vx0 None vx1 What's the difference between "None" and ""? root@vsid:/src/iproute2# ./bridge/bridge -j -p vlan tunnelshow [ { "ifname": "br0", "tunnels": [ ] },{ "ifname": "vx1", "tunnels": [ ] } ] Why does vx0 appear in normal output and not json output? Why output an empty list for br0 and vx1? Fix these inconsistencies and avoid outputting entries with no values. This makes the behavior consistent with other iproute2 commands, for example `ip -6 addr`: if an interface doesn't have any ipv6 addresses, it is not part of the listing. Fixes: 8652eeb3ab12 ("bridge: vlan: support for per vlan tunnel info") Signed-off-by: Benjamin Poirier --- bridge/vlan.c | 36 +++++++++++++----------- testsuite/tests/bridge/vlan/show.t | 30 ++++++++++++++++++++ testsuite/tests/bridge/vlan/tunnelshow.t | 2 +- 3 files changed, 50 insertions(+), 18 deletions(-) create mode 100755 testsuite/tests/bridge/vlan/show.t diff --git a/bridge/vlan.c b/bridge/vlan.c index 3ed60951..1ca7322a 100644 --- a/bridge/vlan.c +++ b/bridge/vlan.c @@ -290,8 +290,7 @@ static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex) int rem = RTA_PAYLOAD(list); __u16 last_vid_start = 0; __u32 last_tunid_start = 0; - - open_vlan_port(ifindex, "%s", VLAN_SHOW_TUNNELINFO); + bool opened = false; for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { struct rtattr *ttb[IFLA_BRIDGE_VLAN_TUNNEL_MAX+1]; @@ -331,13 +330,20 @@ static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex) else if (vcheck_ret == 0) continue; + if (!opened) { + open_vlan_port(ifindex, "%s", VLAN_SHOW_TUNNELINFO); + opened = true; + } + open_json_object(NULL); print_range("vlan", last_vid_start, tunnel_vid); print_range("tunid", last_tunid_start, tunnel_id); close_json_object(); print_string(PRINT_FP, NULL, "%s", _SL_); } - close_vlan_port(); + + if (opened) + close_vlan_port(); } static int print_vlan(struct nlmsghdr *n, void *arg) @@ -366,16 +372,8 @@ static int print_vlan(struct nlmsghdr *n, void *arg) return 0; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len); - - /* if AF_SPEC isn't there, vlan table is not preset for this port */ - if (!tb[IFLA_AF_SPEC]) { - if (!filter_vlan && !is_json_context()) { - color_fprintf(stdout, COLOR_IFNAME, "%s", - ll_index_to_name(ifm->ifi_index)); - fprintf(stdout, "\tNone\n"); - } + if (!tb[IFLA_AF_SPEC]) return 0; - } switch (*subject) { case VLAN_SHOW_VLAN: @@ -385,9 +383,7 @@ static int print_vlan(struct nlmsghdr *n, void *arg) print_vlan_tunnel_info(tb[IFLA_AF_SPEC], ifm->ifi_index); break; } - print_string(PRINT_FP, NULL, "%s", _SL_); - fflush(stdout); return 0; } @@ -588,8 +584,7 @@ void print_vlan_info(struct rtattr *tb, int ifindex) struct rtattr *i, *list = tb; int rem = RTA_PAYLOAD(list); __u16 last_vid_start = 0; - - open_vlan_port(ifindex, "%s", VLAN_SHOW_VLAN); + bool opened = false; for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { struct bridge_vlan_info *vinfo; @@ -608,6 +603,11 @@ void print_vlan_info(struct rtattr *tb, int ifindex) else if (vcheck_ret == 0) continue; + if (!opened) { + open_vlan_port(ifindex, "%s", VLAN_SHOW_VLAN); + opened = true; + } + open_json_object(NULL); print_range("vlan", last_vid_start, vinfo->vid); @@ -615,7 +615,9 @@ void print_vlan_info(struct rtattr *tb, int ifindex) close_json_object(); print_string(PRINT_FP, NULL, "%s", _SL_); } - close_vlan_port(); + + if (opened) + close_vlan_port(); } int do_vlan(int argc, char **argv) diff --git a/testsuite/tests/bridge/vlan/show.t b/testsuite/tests/bridge/vlan/show.t new file mode 100755 index 00000000..3def2022 --- /dev/null +++ b/testsuite/tests/bridge/vlan/show.t @@ -0,0 +1,30 @@ +#!/bin/sh + +. lib/generic.sh + +ts_log "[Testing vlan show]" + +BR_DEV="$(rand_dev)" +VX0_DEV="$(rand_dev)" +VX1_DEV="$(rand_dev)" + +ts_ip "$0" "Add $BR_DEV bridge interface" link add $BR_DEV type bridge + +ts_ip "$0" "Add $VX0_DEV vxlan interface" \ + link add $VX0_DEV type vxlan dstport 4789 external +ts_ip "$0" "Enslave $VX0_DEV under $BR_DEV" \ + link set dev $VX0_DEV master $BR_DEV +ts_bridge "$0" "Delete default vlan from $VX0_DEV" \ + vlan del dev $VX0_DEV vid 1 +ts_ip "$0" "Add $VX1_DEV vxlan interface" \ + link add $VX1_DEV type vxlan dstport 4790 external +ts_ip "$0" "Enslave $VX1_DEV under $BR_DEV" \ + link set dev $VX1_DEV master $BR_DEV + +# Test that bridge ports without vlans do not appear in the output +ts_bridge "$0" "Show vlan" vlan +test_on_not "$VX0_DEV" + +# Test that bridge ports without tunnels do not appear in the output +ts_bridge "$0" "Show vlan tunnel info" vlan tunnelshow +test_lines_count 1 # header only diff --git a/testsuite/tests/bridge/vlan/tunnelshow.t b/testsuite/tests/bridge/vlan/tunnelshow.t index fd41bfcb..3e9c12a2 100755 --- a/testsuite/tests/bridge/vlan/tunnelshow.t +++ b/testsuite/tests/bridge/vlan/tunnelshow.t @@ -28,6 +28,6 @@ ts_bridge "$0" "Add tunnel with vni > 16k" \ ts_bridge "$0" "Show tunnel info" vlan tunnelshow dev $VX_DEV test_on "1030\s+65556" -test_lines_count 5 +test_lines_count 4 ts_bridge "$0" "Dump tunnel info" -j vlan tunnelshow dev $VX_DEV From patchwork Mon Apr 27 23:50:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Poirier X-Patchwork-Id: 220427 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CB14C83002 for ; Mon, 27 Apr 2020 23:51:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E37512072D for ; Mon, 27 Apr 2020 23:51:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="eg3i/lf5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726410AbgD0XvN (ORCPT ); Mon, 27 Apr 2020 19:51:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726244AbgD0XvI (ORCPT ); Mon, 27 Apr 2020 19:51:08 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01B35C0610D5 for ; Mon, 27 Apr 2020 16:51:08 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id x15so9804555pfa.1 for ; Mon, 27 Apr 2020 16:51:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=GqGGw2A5SG7y0JMZ0ograhoz8J8rbzDeLT8aXj+Jwb0=; b=eg3i/lf50IICFgVlOTstakcTn+uJg9tWxvJhjlU5LDpLvjoXR+t3QBEEEcogd9tKa0 q/f2ziS0hvzEnOio5naScqnV8o4ih1/x0WPDToxWA9c5LpdTEHSYsxEvflTkflW7P8+3 iSA4uNOrJDXHK5G9e45ifANjdDWU4PE6xxbv4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GqGGw2A5SG7y0JMZ0ograhoz8J8rbzDeLT8aXj+Jwb0=; b=IJCkHNFGlCDM3nnCPo4kV/Vhz9SuqBhgN1mWVYlpDMlKnsvHZ6HDVc3iRCfAH5HrL7 dj9z+Mmm86LIMRg26ls6CNPhNdLt0q0T4BWnb8Fx2sZ6bUopF8CvKl0zzVHj8g7uIhrb OtUshLNrJJWmVErlBrB+XVhxQBnGGaq64lIOdH0w/ZY0lzP54LdUu8H0Yj+XnXKCZpXj 63eFDg6JILyJY6A6rPk7FENz64owl7B0nQz/QOcHxubpCnS87oG55cDPxvRSVemeAtAK Ug1NhwSCW5gF7BA6eZ0CpXLaNu+mwzSF31tmZUGScKxa93FlNSaypFIidSNA1afwr4Tr demA== X-Gm-Message-State: AGi0PuZddUCgcYzU0YltpbdTFACIJ00VzDD/dBsF0qnUUG21BDrvlKpM ptw+AlH7EHoOVEC+MSAVEBmspzBojAw= X-Google-Smtp-Source: APiQypLqueG30lPSk8WsYzkRZjFkYbXTFZsUVhe4tcLk8IZvJYswcXBNQ541wn3hooJbNbFjfhEACA== X-Received: by 2002:a63:1d4c:: with SMTP id d12mr25875844pgm.247.1588031467151; Mon, 27 Apr 2020 16:51:07 -0700 (PDT) Received: from f3.synalogic.ca (ae055068.dynamic.ppp.asahi-net.or.jp. [14.3.55.68]) by smtp.gmail.com with ESMTPSA id 128sm13058106pfy.5.2020.04.27.16.51.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 16:51:06 -0700 (PDT) From: Benjamin Poirier To: netdev@vger.kernel.org Subject: [PATCH iproute2 6/7] bridge: Align output columns Date: Tue, 28 Apr 2020 08:50:50 +0900 Message-Id: <20200427235051.250058-7-bpoirier@cumulusnetworks.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> References: <20200427235051.250058-1-bpoirier@cumulusnetworks.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Use fixed column widths to improve readability. Before: root@vsid:/src/iproute2# ./bridge/bridge vlan tunnelshow port vlan-id tunnel-id vx0 1000 1000 1010-1020 1010-1020 1030 65556 vx-longname 10 10 After: root@vsid:/src/iproute2# ./bridge/bridge vlan tunnelshow port vlan-id tunnel-id vx0 1000 1000 1010-1020 1010-1020 1030 65556 vx-longname 10 10 Signed-off-by: Benjamin Poirier --- bridge/vlan.c | 73 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/bridge/vlan.c b/bridge/vlan.c index 1ca7322a..a50a4fc9 100644 --- a/bridge/vlan.c +++ b/bridge/vlan.c @@ -22,6 +22,11 @@ enum vlan_show_subject { VLAN_SHOW_TUNNELINFO, }; +#define VLAN_ID_LEN 9 + +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x) + static void usage(void) { fprintf(stderr, @@ -256,11 +261,11 @@ static int filter_vlan_check(__u16 vid, __u16 flags) return 1; } -static void open_vlan_port(int ifi_index, const char *fmt, - enum vlan_show_subject subject) +static void open_vlan_port(int ifi_index, enum vlan_show_subject subject) { open_json_object(NULL); - print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, + print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", + "%-" __stringify(IFNAMSIZ) "s ", ll_index_to_name(ifi_index)); open_json_array(PRINT_JSON, subject == VLAN_SHOW_VLAN ? "vlans": "tunnels"); @@ -272,16 +277,18 @@ static void close_vlan_port(void) close_json_object(); } -static void print_range(const char *name, __u32 start, __u32 id) +static unsigned int print_range(const char *name, __u32 start, __u32 id) { char end[64]; + int width; snprintf(end, sizeof(end), "%sEnd", name); - print_uint(PRINT_ANY, name, "\t %u", start); + width = print_uint(PRINT_ANY, name, "%u", start); if (start != id) - print_uint(PRINT_ANY, end, "-%u", id); + width += print_uint(PRINT_ANY, end, "-%u", id); + return width; } static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex) @@ -297,6 +304,7 @@ static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex) __u32 tunnel_id = 0; __u16 tunnel_vid = 0; __u16 tunnel_flags = 0; + unsigned int width; int vcheck_ret; if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO) @@ -331,12 +339,25 @@ static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex) continue; if (!opened) { - open_vlan_port(ifindex, "%s", VLAN_SHOW_TUNNELINFO); + open_vlan_port(ifindex, VLAN_SHOW_TUNNELINFO); opened = true; + } else { + print_string(PRINT_FP, NULL, + "%-" __stringify(IFNAMSIZ) "s ", ""); } open_json_object(NULL); - print_range("vlan", last_vid_start, tunnel_vid); + width = print_range("vlan", last_vid_start, tunnel_vid); + if (width <= VLAN_ID_LEN) { + char buf[VLAN_ID_LEN + 1]; + + snprintf(buf, sizeof(buf), "%-*s", + VLAN_ID_LEN - width, ""); + print_string(PRINT_FP, NULL, "%s ", buf); + } else { + fprintf(stderr, "BUG: vlan range too wide, %u\n", + width); + } print_range("tunid", last_tunid_start, tunnel_id); close_json_object(); print_string(PRINT_FP, NULL, "%s", _SL_); @@ -404,20 +425,23 @@ static void print_vlan_flags(__u16 flags) static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats) { open_json_object(NULL); - print_hu(PRINT_ANY, "vid", " %hu", vstats->vid); + print_hu(PRINT_ANY, "vid", "%hu", vstats->vid); print_vlan_flags(vstats->flags); + print_nl(); - print_lluint(PRINT_ANY, "rx_bytes", - "\n RX: %llu bytes", + print_string(PRINT_FP, NULL, "%-" __stringify(IFNAMSIZ) "s ", ""); + print_lluint(PRINT_ANY, "rx_bytes", "RX: %llu bytes", vstats->rx_bytes); print_lluint(PRINT_ANY, "rx_packets", " %llu packets\n", - vstats->rx_packets); - print_lluint(PRINT_ANY, "tx_bytes", - " TX: %llu bytes", + vstats->rx_packets); + + print_string(PRINT_FP, NULL, "%-" __stringify(IFNAMSIZ) "s ", ""); + print_lluint(PRINT_ANY, "tx_bytes", "TX: %llu bytes", vstats->tx_bytes); print_lluint(PRINT_ANY, "tx_packets", " %llu packets\n", - vstats->tx_packets); + vstats->tx_packets); + close_json_object(); } @@ -452,10 +476,11 @@ static void print_vlan_stats_attr(struct rtattr *attr, int ifindex) /* found vlan stats, first time print the interface name */ if (!found_vlan) { - open_vlan_port(ifindex, "%-16s", VLAN_SHOW_VLAN); + open_vlan_port(ifindex, VLAN_SHOW_VLAN); found_vlan = true; } else { - print_string(PRINT_FP, NULL, "%-16s", ""); + print_string(PRINT_FP, NULL, + "%-" __stringify(IFNAMSIZ) "s ", ""); } print_one_vlan_stats(vstats); } @@ -534,9 +559,11 @@ static int vlan_show(int argc, char **argv, int subject) } if (!is_json_context()) { - printf("port\tvlan-id"); + printf("%-" __stringify(IFNAMSIZ) "s %-" + __stringify(VLAN_ID_LEN) "s", "port", + "vlan-id"); if (subject == VLAN_SHOW_TUNNELINFO) - printf("\ttunnel-id"); + printf(" tunnel-id"); printf("\n"); } @@ -555,7 +582,8 @@ static int vlan_show(int argc, char **argv, int subject) } if (!is_json_context()) - printf("%-16s vlan-id\n", "port"); + printf("%-" __stringify(IFNAMSIZ) "s vlan-id\n", + "port"); if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); @@ -604,8 +632,11 @@ void print_vlan_info(struct rtattr *tb, int ifindex) continue; if (!opened) { - open_vlan_port(ifindex, "%s", VLAN_SHOW_VLAN); + open_vlan_port(ifindex, VLAN_SHOW_VLAN); opened = true; + } else { + print_string(PRINT_FP, NULL, "%-" + __stringify(IFNAMSIZ) "s ", ""); } open_json_object(NULL);