From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: [RFC iproute2-next 05/16] iproute: refactor metrics print Date: Thu, 1 Feb 2018 17:19:35 -0800 Message-ID: <20180202011946.21929-6-sthemmin@microsoft.com> References: <20180202011946.21929-1-sthemmin@microsoft.com> Cc: netdev@vger.kernel.org, Stephen Hemminger To: dsahern@gmail.com Return-path: Received: from mail-pf0-f196.google.com ([209.85.192.196]:35472 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751812AbeBBBUL (ORCPT ); Thu, 1 Feb 2018 20:20:11 -0500 Received: by mail-pf0-f196.google.com with SMTP id t12so16424443pfg.2 for ; Thu, 01 Feb 2018 17:20:11 -0800 (PST) In-Reply-To: <20180202011946.21929-1-sthemmin@microsoft.com> Sender: netdev-owner@vger.kernel.org List-ID: Make a separate function to improve readability and enable easier JSON conversion. Signed-off-by: Stephen Hemminger --- ip/iproute.c | 117 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/ip/iproute.c b/ip/iproute.c index 76a564922b5c..42d1678b9690 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -443,6 +443,65 @@ static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci) ci->rta_ts, ci->rta_tsage); } +static void print_rta_metrics(FILE *fp, const struct rtattr *rta) +{ + struct rtattr *mxrta[RTAX_MAX+1]; + unsigned int mxlock = 0; + int i; + + parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)); + + if (mxrta[RTAX_LOCK]) + mxlock = rta_getattr_u32(mxrta[RTAX_LOCK]); + + for (i = 2; i <= RTAX_MAX; i++) { + __u32 val = 0U; + + if (mxrta[i] == NULL && !(mxlock & (1 << i))) + continue; + + if (mxrta[i] != NULL && i != RTAX_CC_ALGO) + val = rta_getattr_u32(mxrta[i]); + + if (i == RTAX_HOPLIMIT && (int)val == -1) + continue; + + if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i]) + fprintf(fp, "%s ", mx_names[i]); + else + fprintf(fp, "metric %d ", i); + + if (mxlock & (1<= 1000) + fprintf(fp, "%gs ", val/1e3); + else + fprintf(fp, "%ums ", val); + break; + case RTAX_CC_ALGO: + fprintf(fp, "%s ", rta_getattr_str(mxrta[i])); + break; + } + } +} + int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = (FILE *)arg; @@ -619,63 +678,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO])); } - if (tb[RTA_METRICS]) { - int i; - unsigned int mxlock = 0; - struct rtattr *mxrta[RTAX_MAX+1]; - - parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), - RTA_PAYLOAD(tb[RTA_METRICS])); - if (mxrta[RTAX_LOCK]) - mxlock = rta_getattr_u32(mxrta[RTAX_LOCK]); - - for (i = 2; i <= RTAX_MAX; i++) { - __u32 val = 0U; - - if (mxrta[i] == NULL && !(mxlock & (1 << i))) - continue; - - if (mxrta[i] != NULL && i != RTAX_CC_ALGO) - val = rta_getattr_u32(mxrta[i]); - - if (i == RTAX_HOPLIMIT && (int)val == -1) - continue; - - if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i]) - fprintf(fp, "%s ", mx_names[i]); - else - fprintf(fp, "metric %d ", i); - - if (mxlock & (1<= 1000) - fprintf(fp, "%gs ", val/1e3); - else - fprintf(fp, "%ums ", val); - break; - case RTAX_CC_ALGO: - fprintf(fp, "%s ", rta_getattr_str(mxrta[i])); - break; - } - } - } if (tb[RTA_IIF] && filter.iifmask != -1) { fprintf(fp, "iif %s ", ll_index_to_name(rta_getattr_u32(tb[RTA_IIF]))); -- 2.15.1