From patchwork Tue Apr 23 07:16:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Haller X-Patchwork-Id: 1089104 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44pF9x02Scz9sMx for ; Tue, 23 Apr 2019 17:16:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726382AbfDWHQf (ORCPT ); Tue, 23 Apr 2019 03:16:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44582 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725888AbfDWHQf (ORCPT ); Tue, 23 Apr 2019 03:16:35 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C58E47E427; Tue, 23 Apr 2019 07:16:34 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-94.ams2.redhat.com [10.36.116.94]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B81911A913; Tue, 23 Apr 2019 07:16:32 +0000 (UTC) From: Thomas Haller To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, Thomas Haller Subject: [PATCH 3/4] iprule: refactor print_rule() to use leading space before printing attribute Date: Tue, 23 Apr 2019 09:16:14 +0200 Message-Id: <20190423071615.19729-3-thaller@redhat.com> In-Reply-To: <20190423071615.19729-1-thaller@redhat.com> References: <20190423071615.19729-1-thaller@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 23 Apr 2019 07:16:34 +0000 (UTC) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When printing the actions, we avoid adding the trailing space after the attribute. Possibly because we expect the action to be the last output on the line and not end with a space. But for FR_ACT_TO_TBL nothing is printed. That means, we add double spaces if a protocol is printed as well: # ip rule add priority 10 protocol 10 type 1 will be printed as 10: from all lookup 1 proto mrt The only visible effect of the patch is to avoid the double-space and avoid a trailing space if the action is FR_ACT_TO_TBL. Signed-off-by: Thomas Haller --- ip/iprule.c | 84 ++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/ip/iprule.c b/ip/iprule.c index ce6b7cd3..b00ca7d1 100644 --- a/ip/iprule.c +++ b/ip/iprule.c @@ -273,34 +273,30 @@ int print_rule(struct nlmsghdr *n, void *arg) print_color_string(PRINT_ANY, ifa_family_color(frh->family), "src", "%s", src); if (frh->src_len != host_len) - print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len); - else - print_string(PRINT_FP, NULL, " ", NULL); + print_uint(PRINT_ANY, "srclen", "/%u", frh->src_len); } else if (frh->src_len) { print_string(PRINT_ANY, "src", "from %s", "0"); - print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len); + print_uint(PRINT_ANY, "srclen", "/%u", frh->src_len); } else { - print_string(PRINT_ANY, "src", "from %s ", "all"); + print_string(PRINT_ANY, "src", "from %s", "all"); } if (tb[FRA_DST]) { const char *dst = rt_addr_n2a_rta(frh->family, tb[FRA_DST]); - print_string(PRINT_FP, NULL, "to ", NULL); + print_string(PRINT_FP, NULL, " to ", NULL); print_color_string(PRINT_ANY, ifa_family_color(frh->family), "dst", "%s", dst); if (frh->dst_len != host_len) - print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len); - else - print_string(PRINT_FP, NULL, " ", NULL); + print_uint(PRINT_ANY, "dstlen", "/%u", frh->dst_len); } else if (frh->dst_len) { - print_string(PRINT_ANY, "dst", "to %s", "0"); - print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len); + print_string(PRINT_ANY, "dst", " to %s", "0"); + print_uint(PRINT_ANY, "dstlen", "/%u", frh->dst_len); } if (frh->tos) { print_string(PRINT_ANY, "tos", - "tos %s ", + " tos %s", rtnl_dsfield_n2a(frh->tos, b1, sizeof(b1))); } @@ -312,34 +308,34 @@ int print_rule(struct nlmsghdr *n, void *arg) if (tb[FRA_FWMASK] && (mask = rta_getattr_u32(tb[FRA_FWMASK])) != 0xFFFFFFFF) { - print_0xhex(PRINT_ANY, "fwmark", "fwmark %#llx", mark); - print_0xhex(PRINT_ANY, "fwmask", "/%#llx ", mask); + print_0xhex(PRINT_ANY, "fwmark", " fwmark %#llx", mark); + print_0xhex(PRINT_ANY, "fwmask", "/%#llx", mask); } else { - print_0xhex(PRINT_ANY, "fwmark", "fwmark %#llx ", mark); + print_0xhex(PRINT_ANY, "fwmark", " fwmark %#llx", mark); } } if (tb[FRA_IFNAME]) { if (!is_json_context()) - fprintf(fp, "iif "); + fprintf(fp, " iif "); print_color_string(PRINT_ANY, COLOR_IFNAME, - "iif", "%s ", + "iif", "%s", rta_getattr_str(tb[FRA_IFNAME])); if (frh->flags & FIB_RULE_IIF_DETACHED) - print_null(PRINT_ANY, "iif_detached", "[detached] ", + print_null(PRINT_ANY, "iif_detached", " [detached]", NULL); } if (tb[FRA_OIFNAME]) { if (!is_json_context()) - fprintf(fp, "oif "); + fprintf(fp, " oif "); - print_color_string(PRINT_ANY, COLOR_IFNAME, "oif", "%s ", + print_color_string(PRINT_ANY, COLOR_IFNAME, "oif", "%s", rta_getattr_str(tb[FRA_OIFNAME])); if (frh->flags & FIB_RULE_OIF_DETACHED) - print_null(PRINT_ANY, "oif_detached", "[detached] ", + print_null(PRINT_ANY, "oif_detached", " [detached]", NULL); } @@ -348,19 +344,19 @@ int print_rule(struct nlmsghdr *n, void *arg) if (mdev) print_null(PRINT_ANY, "l3mdev", - "lookup [l3mdev-table] ", NULL); + " lookup [l3mdev-table]", NULL); } if (tb[FRA_UID_RANGE]) { struct fib_rule_uid_range *r = RTA_DATA(tb[FRA_UID_RANGE]); - print_uint(PRINT_ANY, "uid_start", "uidrange %u", r->start); - print_uint(PRINT_ANY, "uid_end", "-%u ", r->end); + print_uint(PRINT_ANY, "uid_start", " uidrange %u", r->start); + print_uint(PRINT_ANY, "uid_end", "-%u", r->end); } if (tb[FRA_IP_PROTO]) { SPRINT_BUF(pbuf); - print_string(PRINT_ANY, "ipproto", "ipproto %s ", + print_string(PRINT_ANY, "ipproto", " ipproto %s", inet_proto_n2a(rta_getattr_u8(tb[FRA_IP_PROTO]), pbuf, sizeof(pbuf))); } @@ -369,11 +365,11 @@ int print_rule(struct nlmsghdr *n, void *arg) struct fib_rule_port_range *r = RTA_DATA(tb[FRA_SPORT_RANGE]); if (r->start == r->end) { - print_uint(PRINT_ANY, "sport", "sport %u ", r->start); + print_uint(PRINT_ANY, "sport", " sport %u", r->start); } else { - print_uint(PRINT_ANY, "sport_start", "sport %u", + print_uint(PRINT_ANY, "sport_start", " sport %u", r->start); - print_uint(PRINT_ANY, "sport_end", "-%u ", r->end); + print_uint(PRINT_ANY, "sport_end", "-%u", r->end); } } @@ -381,24 +377,24 @@ int print_rule(struct nlmsghdr *n, void *arg) struct fib_rule_port_range *r = RTA_DATA(tb[FRA_DPORT_RANGE]); if (r->start == r->end) { - print_uint(PRINT_ANY, "dport", "dport %u ", r->start); + print_uint(PRINT_ANY, "dport", " dport %u", r->start); } else { - print_uint(PRINT_ANY, "dport_start", "dport %u", + print_uint(PRINT_ANY, "dport_start", " dport %u", r->start); - print_uint(PRINT_ANY, "dport_end", "-%u ", r->end); + print_uint(PRINT_ANY, "dport_end", "-%u", r->end); } } if (tb[FRA_TUN_ID]) { __u64 tun_id = ntohll(rta_getattr_u64(tb[FRA_TUN_ID])); - print_u64(PRINT_ANY, "tun_id", "tun_id %llu ", tun_id); + print_u64(PRINT_ANY, "tun_id", " tun_id %llu", tun_id); } table = frh_get_table(frh, tb); if (table) { print_string(PRINT_ANY, "table", - "lookup %s ", + " lookup %s", rtnl_rttable_n2a(table, b1, sizeof(b1))); if (tb[FRA_SUPPRESS_PREFIXLEN]) { @@ -406,7 +402,7 @@ int print_rule(struct nlmsghdr *n, void *arg) if (pl != -1) print_int(PRINT_ANY, "suppress_prefixlen", - "suppress_prefixlength %d ", pl); + " suppress_prefixlength %d", pl); } if (tb[FRA_SUPPRESS_IFGROUP]) { @@ -417,7 +413,7 @@ int print_rule(struct nlmsghdr *n, void *arg) = rtnl_group_n2a(group, b1, sizeof(b1)); print_string(PRINT_ANY, "suppress_ifgroup", - "suppress_ifgroup %s ", grname); + " suppress_ifgroup %s", grname); } } } @@ -429,10 +425,12 @@ int print_rule(struct nlmsghdr *n, void *arg) to &= 0xFFFF; if (from) print_string(PRINT_ANY, - "flow_from", "realms %s/", + "flow_from", " realms %s/", rtnl_rtrealm_n2a(from, b1, sizeof(b1))); + else + print_string(PRINT_FP, NULL, " ", NULL); - print_string(PRINT_ANY, "flow_to", "%s ", + print_string(PRINT_ANY, "flow_to", "%s", rtnl_rtrealm_n2a(to, b1, sizeof(b1))); } @@ -443,24 +441,24 @@ int print_rule(struct nlmsghdr *n, void *arg) gateway = format_host_rta(frh->family, tb[RTA_GATEWAY]); print_string(PRINT_ANY, "nat_gateway", - "map-to %s", gateway); + " map-to %s", gateway); } else { - print_null(PRINT_ANY, "masquerade", "masquerade", NULL); + print_null(PRINT_ANY, "masquerade", " masquerade", NULL); } } else if (frh->action == FR_ACT_GOTO) { if (tb[FRA_GOTO]) - print_uint(PRINT_ANY, "goto", "goto %u", + print_uint(PRINT_ANY, "goto", " goto %u", rta_getattr_u32(tb[FRA_GOTO])); else - print_string(PRINT_ANY, "goto", "goto %s", "none"); + print_string(PRINT_ANY, "goto", " goto %s", "none"); if (frh->flags & FIB_RULE_UNRESOLVED) print_null(PRINT_ANY, "unresolved", " [unresolved]", NULL); } else if (frh->action == FR_ACT_NOP) { - print_null(PRINT_ANY, "nop", "nop", NULL); + print_null(PRINT_ANY, "nop", " nop", NULL); } else if (frh->action != FR_ACT_TO_TBL) { - print_string(PRINT_ANY, "action", "%s", + print_string(PRINT_ANY, "action", " %s", rtnl_rtntype_n2a(frh->action, b1, sizeof(b1))); }