From patchwork Sat Dec 5 21:13:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953503 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 E8E5EC433FE for ; Sat, 5 Dec 2020 21:15:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BC9F023108 for ; Sat, 5 Dec 2020 21:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726061AbgLEVPC (ORCPT ); Sat, 5 Dec 2020 16:15:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725270AbgLEVPC (ORCPT ); Sat, 5 Dec 2020 16:15:02 -0500 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050::465:102]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BF25C0613CF for ; Sat, 5 Dec 2020 13:14:21 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4CpMlG70LhzQlVP; Sat, 5 Dec 2020 22:14:18 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AIhyi6Ww79w9PO28BVLYAAcyTrj3CvnLU7tjEBFmb10=; b=I8jTkNFn7bp5MQLl2fopwzWxAba6aj3qyGBQ/cRytR2WPBfSR4C0i7VsHEI3+KffTWQrkM kMcIfzmidgRes+8OYvv1vOKTVympt0BHqgb7AZODHAwNTqh8J8bT1pqiqfyvPK+4tvRA5d qDi2NKi5EVSwgXUiM4ZnAk1nEIda4b3FK62c8784BXKDYprizzObvJq55kEZVe8ZcCmCJl nLdKgbp/eN2ITdtCiwiDQo/hGTY8Rqowc7Ch5ePrIdOBRjreLx3p7wecn0ZusDAGPZ1b9e EOktgPyTz6L6YqdaNOlojsBBVVaDAOjrS78o2gLJcQcBn3Tta7TiVnEQV4Qu4Q== Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id E33RKfmlbwJV; Sat, 5 Dec 2020 22:14:14 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 1/7] Move the use_iec declaration to the tools Date: Sat, 5 Dec 2020 22:13:29 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 2.06 / 15.00 / 15.00 X-Rspamd-Queue-Id: DCBDD1723 X-Rspamd-UID: 5546d4 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com The tools "ip" and "tc" use a flag "use_iec", which indicates whether, when formatting rate values, the prefixes "K", "M", etc. should refer to powers of 1024, or powers of 1000. The flag is currently kept as a global variable in "ip" and "tc", but is nonetheless declared in util.h. Instead, move the declaration to tool-specific headers ip/ip_common.h and tc/tc_common.h. Signed-off-by: Petr Machata --- include/utils.h | 1 - ip/ip_common.h | 2 ++ tc/tc_common.h | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/utils.h b/include/utils.h index 588fceb72442..01454f71cb1a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -20,7 +20,6 @@ extern int preferred_family; extern int human_readable; -extern int use_iec; extern int show_stats; extern int show_details; extern int show_raw; diff --git a/ip/ip_common.h b/ip/ip_common.h index 227eddd3baf2..9a31e837563f 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -6,6 +6,8 @@ #include "json_print.h" +extern int use_iec; + struct link_filter { int ifindex; int family; diff --git a/tc/tc_common.h b/tc/tc_common.h index 802fb7f01fe4..58dc9d6a6c4f 100644 --- a/tc/tc_common.h +++ b/tc/tc_common.h @@ -27,3 +27,4 @@ int check_size_table_opts(struct tc_sizespec *s); extern int show_graph; extern bool use_names; +extern int use_iec; From patchwork Sat Dec 5 21:13:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953513 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 F373CC4361A for ; Sat, 5 Dec 2020 21:15:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA68B2310B for ; Sat, 5 Dec 2020 21:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727387AbgLEVPn (ORCPT ); Sat, 5 Dec 2020 16:15:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbgLEVPm (ORCPT ); Sat, 5 Dec 2020 16:15:42 -0500 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [IPv6:2001:67c:2050::465:101]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 008F7C061A53 for ; Sat, 5 Dec 2020 13:14:47 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4CpMlJ5jXgzQl34; Sat, 5 Dec 2020 22:14:20 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202858; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0vcUPHYcHtJPXjLbkX0D7Hk1Q7QTaXXfpNpd21yLODw=; b=lxwM0sNLiAjalKmykwdPZ3zduocKiGaj4m11QuB9XQTDQqIG5DhtWlZJGMYa//+jnBtSlE ieHsMwGLPd0oKYciYfS6PZu6eg01XEjwe/aQyeOomlolmsh0eZD1y2VLTnoJNZpZQsjQww I9JlMhOaLC4Fd1nrKbgTgHTECSAI0RKCPoqu9wPvzAU18m8VZ1v66OQmfdkznaS53qw+N6 MOCDhhdXvFQM18KqWnUqNWjtACbuCWsugrwopYC4ufrDC3CFKXJUGZVu+ynlicqeEGxoRS o6km19r47ewjvuUzHfpaQ3uyheBSN7DI/piGfV7qDgK4JyoqsClVFE7PBx1C4A== Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter05.heinlein-hosting.de (spamfilter05.heinlein-hosting.de [80.241.56.123]) (amavisd-new, port 10030) with ESMTP id zZLmEofYmkna; Sat, 5 Dec 2020 22:14:17 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 2/7] lib: Move print_rate() from tc here; modernize Date: Sat, 5 Dec 2020 22:13:30 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 1.69 / 15.00 / 15.00 X-Rspamd-Queue-Id: B0BA6171F X-Rspamd-UID: 5d46f4 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com The functions print_rate() and sprint_rate() are useful for formatting rate-like values. The DCB tool would find these useful in the maxrate subtool. However, the current interface to these functions uses a global variable use_iec as a flag indicating whether 1024- or 1000-based powers should be used when formatting the rate value. For general use, a global variable is not a great way of passing arguments to a function. Besides, it is unlike most other printing functions in that it deals in buffers and ignores JSON. Therefore make the interface to print_rate() explicit by converting use_iec to an ordinary parameter. Since the interface changes anyway, convert it to follow the pattern of other json_print functions (except for the now-explicit use_iec parameter). Move to json_print.c. Add a wrapper to tc, so that all the call sites do not need to repeat the use_iec global variable argument, and convert all call sites. In q_cake.c, the conversion is not straightforward due to usage of a macro that is shared across numerous data types. Simply hand-roll the corresponding code, which seems better than making an extra helper for one call site. Drop sprint_rate() now that everybody just uses print_rate(). Signed-off-by: Petr Machata --- Notes: v2: - Adapt q_mqprio.c patch, the file changed since v1. include/json_print.h | 10 ++++++++++ lib/json_print.c | 32 ++++++++++++++++++++++++++++++++ tc/m_police.c | 9 ++++----- tc/q_cake.c | 28 ++++++++++++++++------------ tc/q_cbq.c | 14 ++++---------- tc/q_fq.c | 25 +++++++++---------------- tc/q_hfsc.c | 4 ++-- tc/q_htb.c | 4 ++-- tc/q_mqprio.c | 6 ++---- tc/q_netem.c | 4 +--- tc/q_tbf.c | 7 ++----- tc/tc_util.c | 37 +++++++------------------------------ tc/tc_util.h | 4 ++-- 13 files changed, 93 insertions(+), 91 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index 096a999a4de4..b6c4c0c80833 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -86,4 +86,14 @@ _PRINT_NAME_VALUE_FUNC(uint, unsigned int, u); _PRINT_NAME_VALUE_FUNC(string, const char*, s); #undef _PRINT_NAME_VALUE_FUNC +int print_color_rate(bool use_iec, enum output_type t, enum color_attr color, + const char *key, const char *fmt, unsigned long long rate); + +static inline int print_rate(bool use_iec, enum output_type t, + const char *key, const char *fmt, + unsigned long long rate) +{ + return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate); +} + #endif /* _JSON_PRINT_H_ */ diff --git a/lib/json_print.c b/lib/json_print.c index 62eeb1f1fb31..9ab0f86b83df 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -308,3 +308,35 @@ void print_nl(void) if (!_jw) printf("%s", _SL_); } + +int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, + const char *key, const char *fmt, unsigned long long rate) +{ + unsigned long kilo = use_iec ? 1024 : 1000; + const char *str = use_iec ? "i" : ""; + static char *units[5] = {"", "K", "M", "G", "T"}; + char *buf; + int rc; + int i; + + if (_IS_JSON_CONTEXT(type)) + return print_color_lluint(type, color, key, "%llu", rate); + + rate <<= 3; /* bytes/sec -> bits/sec */ + + for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { + if (rate < kilo) + break; + if (((rate % kilo) != 0) && rate < 1000*kilo) + break; + rate /= kilo; + } + + rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], str); + if (rc < 0) + return -1; + + rc = print_color_string(type, color, key, fmt, buf); + free(buf); + return rc; +} diff --git a/tc/m_police.c b/tc/m_police.c index 83b25db49f21..64068fcc4a42 100644 --- a/tc/m_police.c +++ b/tc/m_police.c @@ -269,7 +269,7 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg) rate64 = rta_getattr_u64(tb[TCA_POLICE_RATE64]); fprintf(f, " police 0x%x ", p->index); - fprintf(f, "rate %s ", sprint_rate(rate64, b1)); + tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64); buffer = tc_calc_xmitsize(rate64, p->burst); fprintf(f, "burst %s ", sprint_size(buffer, b1)); fprintf(f, "mtu %s ", sprint_size(p->mtu, b1)); @@ -282,12 +282,11 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg) prate64 = rta_getattr_u64(tb[TCA_POLICE_PEAKRATE64]); if (prate64) - fprintf(f, "peakrate %s ", sprint_rate(prate64, b1)); + tc_print_rate(PRINT_FP, NULL, "peakrate %s ", prate64); if (tb[TCA_POLICE_AVRATE]) - fprintf(f, "avrate %s ", - sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]), - b1)); + tc_print_rate(PRINT_FP, NULL, "avrate %s ", + rta_getattr_u32(tb[TCA_POLICE_AVRATE])); print_action_control(f, "action ", p->action, ""); diff --git a/tc/q_cake.c b/tc/q_cake.c index bf116e80316c..ab9233a04f39 100644 --- a/tc/q_cake.c +++ b/tc/q_cake.c @@ -445,11 +445,10 @@ static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_CAKE_BASE_RATE64] && RTA_PAYLOAD(tb[TCA_CAKE_BASE_RATE64]) >= sizeof(bandwidth)) { bandwidth = rta_getattr_u64(tb[TCA_CAKE_BASE_RATE64]); - if (bandwidth) { - print_uint(PRINT_JSON, "bandwidth", NULL, bandwidth); - print_string(PRINT_FP, NULL, "bandwidth %s ", - sprint_rate(bandwidth, b1)); - } else + if (bandwidth) + tc_print_rate(PRINT_ANY, "bandwidth", "bandwidth %s ", + bandwidth); + else print_string(PRINT_ANY, "bandwidth", "bandwidth %s ", "unlimited"); } @@ -650,12 +649,10 @@ static int cake_print_xstats(struct qdisc_util *qu, FILE *f, GET_STAT_U32(MEMORY_LIMIT)); } - if (st[TCA_CAKE_STATS_CAPACITY_ESTIMATE64]) { - print_string(PRINT_FP, NULL, " capacity estimate: %s\n", - sprint_rate(GET_STAT_U64(CAPACITY_ESTIMATE64), b1)); - print_uint(PRINT_JSON, "capacity_estimate", NULL, - GET_STAT_U64(CAPACITY_ESTIMATE64)); - } + if (st[TCA_CAKE_STATS_CAPACITY_ESTIMATE64]) + tc_print_rate(PRINT_ANY, "capacity_estimate", + " capacity estimate: %s\n", + GET_STAT_U64(CAPACITY_ESTIMATE64)); if (st[TCA_CAKE_STATS_MIN_NETLEN] && st[TCA_CAKE_STATS_MAX_NETLEN]) { @@ -790,7 +787,14 @@ static int cake_print_xstats(struct qdisc_util *qu, FILE *f, #define PRINT_TSTAT_U64(name, attr) PRINT_TSTAT( \ name, attr, "llu", rta_getattr_u64(GET_TSTAT(i, attr))) - SPRINT_TSTAT(rate, u64, " thresh ", THRESHOLD_RATE64); + if (GET_TSTAT(0, THRESHOLD_RATE64)) { + fprintf(f, " thresh "); + for (i = 0; i < num_tins; i++) + tc_print_rate(PRINT_FP, NULL, " %12s", + rta_getattr_u64(GET_TSTAT(i, THRESHOLD_RATE64))); + fprintf(f, "%s", _SL_); + } + SPRINT_TSTAT(time, u32, " target ", TARGET_US); SPRINT_TSTAT(time, u32, " interval", INTERVAL_US); SPRINT_TSTAT(time, u32, " pk_delay", PEAK_DELAY_US); diff --git a/tc/q_cbq.c b/tc/q_cbq.c index 6518ef46813b..4619a37b8160 100644 --- a/tc/q_cbq.c +++ b/tc/q_cbq.c @@ -497,10 +497,7 @@ static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) } if (r) { - char buf[64]; - - print_rate(buf, sizeof(buf), r->rate); - fprintf(f, "rate %s ", buf); + tc_print_rate(PRINT_FP, NULL, "rate %s ", r->rate); linklayer = (r->linklayer & TC_LINKLAYER_MASK); if (linklayer > TC_LINKLAYER_ETHERNET || show_details) fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2)); @@ -533,13 +530,10 @@ static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) else fprintf(f, "prio no-transmit"); if (show_details) { - char buf[64]; - fprintf(f, "/%u ", wrr->cpriority); - if (wrr->weight != 1) { - print_rate(buf, sizeof(buf), wrr->weight); - fprintf(f, "weight %s ", buf); - } + if (wrr->weight != 1) + tc_print_rate(PRINT_FP, NULL, "weight %s ", + wrr->weight); if (wrr->allot) fprintf(f, "allot %ub ", wrr->allot); } diff --git a/tc/q_fq.c b/tc/q_fq.c index b10d01e901d8..71a513fb3c72 100644 --- a/tc/q_fq.c +++ b/tc/q_fq.c @@ -330,32 +330,25 @@ static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) { rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]); - if (rate != ~0U) { - print_uint(PRINT_JSON, "maxrate", NULL, rate); - print_string(PRINT_FP, NULL, "maxrate %s ", - sprint_rate(rate, b1)); - } + if (rate != ~0U) + tc_print_rate(PRINT_ANY, + "maxrate", "maxrate %s ", rate); } if (tb[TCA_FQ_FLOW_DEFAULT_RATE] && RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) { rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]); - if (rate != 0) { - print_uint(PRINT_JSON, "defrate", NULL, rate); - print_string(PRINT_FP, NULL, "defrate %s ", - sprint_rate(rate, b1)); - } + if (rate != 0) + tc_print_rate(PRINT_ANY, + "defrate", "defrate %s ", rate); } if (tb[TCA_FQ_LOW_RATE_THRESHOLD] && RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) { rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]); - if (rate != 0) { - print_uint(PRINT_JSON, "low_rate_threshold", NULL, - rate); - print_string(PRINT_FP, NULL, "low_rate_threshold %s ", - sprint_rate(rate, b1)); - } + if (rate != 0) + tc_print_rate(PRINT_ANY, "low_rate_threshold", + "low_rate_threshold %s ", rate); } if (tb[TCA_FQ_FLOW_REFILL_DELAY] && RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) { diff --git a/tc/q_hfsc.c b/tc/q_hfsc.c index f34b1b2fe2a9..81c10210c884 100644 --- a/tc/q_hfsc.c +++ b/tc/q_hfsc.c @@ -219,9 +219,9 @@ hfsc_print_sc(FILE *f, char *name, struct tc_service_curve *sc) SPRINT_BUF(b1); fprintf(f, "%s ", name); - fprintf(f, "m1 %s ", sprint_rate(sc->m1, b1)); + tc_print_rate(PRINT_FP, NULL, "m1 %s ", sc->m1); fprintf(f, "d %s ", sprint_time(tc_core_ktime2time(sc->d), b1)); - fprintf(f, "m2 %s ", sprint_rate(sc->m2, b1)); + tc_print_rate(PRINT_FP, NULL, "m2 %s ", sc->m2); } static int diff --git a/tc/q_htb.c b/tc/q_htb.c index 520522266e00..10030a8741fa 100644 --- a/tc/q_htb.c +++ b/tc/q_htb.c @@ -299,12 +299,12 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64)) ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]); - fprintf(f, "rate %s ", sprint_rate(rate64, b1)); + tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64); if (hopt->rate.overhead) fprintf(f, "overhead %u ", hopt->rate.overhead); buffer = tc_calc_xmitsize(rate64, hopt->buffer); - fprintf(f, "ceil %s ", sprint_rate(ceil64, b1)); + tc_print_rate(PRINT_FP, NULL, "ceil %s ", ceil64); cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer); linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK); if (linklayer > TC_LINKLAYER_ETHERNET || show_details) diff --git a/tc/q_mqprio.c b/tc/q_mqprio.c index 5499f6215251..706452d08092 100644 --- a/tc/q_mqprio.c +++ b/tc/q_mqprio.c @@ -230,8 +230,6 @@ static int mqprio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) __u64 max_rate64[TC_QOPT_MAX_QUEUE] = {0}; int len; - SPRINT_BUF(b1); - if (opt == NULL) return 0; @@ -295,7 +293,7 @@ static int mqprio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) } open_json_array(PRINT_ANY, is_json_context() ? "min_rate" : " min_rate:"); for (i = 0; i < qopt->num_tc; i++) - print_string(PRINT_ANY, NULL, "%s ", sprint_rate(min_rate64[i], b1)); + tc_print_rate(PRINT_ANY, NULL, "%s ", min_rate64[i]); close_json_array(PRINT_ANY, ""); } @@ -312,7 +310,7 @@ static int mqprio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) } open_json_array(PRINT_ANY, is_json_context() ? "max_rate" : " max_rate:"); for (i = 0; i < qopt->num_tc; i++) - print_string(PRINT_ANY, NULL, "%s ", sprint_rate(max_rate64[i], b1)); + tc_print_rate(PRINT_ANY, NULL, "%s ", max_rate64[i]); close_json_array(PRINT_ANY, ""); } } diff --git a/tc/q_netem.c b/tc/q_netem.c index d01450fc59dc..d93e1c7315d2 100644 --- a/tc/q_netem.c +++ b/tc/q_netem.c @@ -800,9 +800,7 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (rate && rate->rate) { open_json_object("rate"); rate64 = rate64 ? : rate->rate; - print_string(PRINT_FP, NULL, " rate %s", - sprint_rate(rate64, b1)); - print_lluint(PRINT_JSON, "rate", NULL, rate64); + tc_print_rate(PRINT_ANY, "rate", " rate %s", rate64); PRINT_INT_OPT("packetoverhead", rate->packet_overhead); print_uint(PRINT_ANY, "cellsize", rate->cell_size ? " cellsize %u" : "", diff --git a/tc/q_tbf.c b/tc/q_tbf.c index 5135b1d670d6..9d4833385521 100644 --- a/tc/q_tbf.c +++ b/tc/q_tbf.c @@ -286,8 +286,7 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_TBF_RATE64] && RTA_PAYLOAD(tb[TCA_TBF_RATE64]) >= sizeof(rate64)) rate64 = rta_getattr_u64(tb[TCA_TBF_RATE64]); - print_u64(PRINT_JSON, "rate", NULL, rate64); - print_string(PRINT_FP, NULL, "rate %s ", sprint_rate(rate64, b1)); + tc_print_rate(PRINT_ANY, "rate", "rate %s ", rate64); buffer = tc_calc_xmitsize(rate64, qopt->buffer); if (show_details) { sprintf(b1, "%s/%u", sprint_size(buffer, b2), @@ -308,9 +307,7 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64)) prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]); if (prate64) { - print_u64(PRINT_JSON, "peakrate", NULL, prate64); - print_string(PRINT_FP, NULL, "peakrate %s ", - sprint_rate(prate64, b1)); + tc_print_rate(PRINT_FP, "peakrate", "peakrate %s ", prate64); if (qopt->mtu || qopt->peakrate.mpu) { mtu = tc_calc_xmitsize(prate64, qopt->mtu); if (show_details) { diff --git a/tc/tc_util.c b/tc/tc_util.c index b7ff911b63ed..40efaa9a4b8a 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -326,31 +326,10 @@ int get_rate64(__u64 *rate, const char *str) return 0; } -void print_rate(char *buf, int len, __u64 rate) +void tc_print_rate(enum output_type t, const char *key, const char *fmt, + unsigned long long rate) { - extern int use_iec; - unsigned long kilo = use_iec ? 1024 : 1000; - const char *str = use_iec ? "i" : ""; - static char *units[5] = {"", "K", "M", "G", "T"}; - int i; - - rate <<= 3; /* bytes/sec -> bits/sec */ - - for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { - if (rate < kilo) - break; - if (((rate % kilo) != 0) && rate < 1000*kilo) - break; - rate /= kilo; - } - - snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str); -} - -char *sprint_rate(__u64 rate, char *buf) -{ - print_rate(buf, SPRINT_BSIZE-1, rate); - return buf; + print_rate(use_iec, t, key, fmt, rate); } char *sprint_ticks(__u32 ticks, char *buf) @@ -853,8 +832,7 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat sizeof(re))); print_string(PRINT_FP, NULL, "\n%s", prefix); print_lluint(PRINT_JSON, "rate", NULL, re.bps); - print_string(PRINT_FP, NULL, "rate %s", - sprint_rate(re.bps, b1)); + tc_print_rate(PRINT_FP, NULL, "rate %s", re.bps); print_lluint(PRINT_ANY, "pps", " %llupps", re.pps); } else if (tbs[TCA_STATS_RATE_EST]) { struct gnet_stats_rate_est re = {0}; @@ -863,8 +841,7 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); print_string(PRINT_FP, NULL, "\n%s", prefix); print_uint(PRINT_JSON, "rate", NULL, re.bps); - print_string(PRINT_FP, NULL, "rate %s", - sprint_rate(re.bps, b1)); + tc_print_rate(PRINT_FP, NULL, "rate %s", re.bps); print_uint(PRINT_ANY, "pps", " %upps", re.pps); } @@ -916,8 +893,8 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, if (st.bps || st.pps) { fprintf(fp, "rate "); if (st.bps) - fprintf(fp, "%s ", - sprint_rate(st.bps, b1)); + tc_print_rate(PRINT_FP, NULL, "%s ", + st.bps); if (st.pps) fprintf(fp, "%upps ", st.pps); } diff --git a/tc/tc_util.h b/tc/tc_util.h index c8af4e952109..e5d533a44e10 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -84,10 +84,10 @@ int get_size(unsigned int *size, const char *str); int get_size_and_cell(unsigned int *size, int *cell_log, char *str); int get_linklayer(unsigned int *val, const char *arg); -void print_rate(char *buf, int len, __u64 rate); +void tc_print_rate(enum output_type t, const char *key, const char *fmt, + unsigned long long rate); void print_devname(enum output_type type, int ifindex); -char *sprint_rate(__u64 rate, char *buf); char *sprint_size(__u32 size, char *buf); char *sprint_tc_classid(__u32 h, char *buf); char *sprint_ticks(__u32 ticks, char *buf); From patchwork Sat Dec 5 21:13:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953511 X-Patchwork-Delegate: dsahern@gmail.com 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=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED 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 45426C18E57 for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 09D5E23108 for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727258AbgLEVPH (ORCPT ); Sat, 5 Dec 2020 16:15:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725270AbgLEVPE (ORCPT ); Sat, 5 Dec 2020 16:15:04 -0500 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [IPv6:2001:67c:2050::465:103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECB1EC0613D1 for ; Sat, 5 Dec 2020 13:14:23 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4CpMlL256HzQlF0; Sat, 5 Dec 2020 22:14:22 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=chWU38kLcerTJjMXqQeMLEKaJM446jsKMfUYfmQRBq4=; b=Ov8+VXMWR0HiaEK6ZHfyUoN4gnkbaoCZblpEGoEofCeO2y8SitSdZx+btNsNod+RizvH5W btxITVLXi2llYAx3MAbmHb5KcqF4YuXGMOpElSsTe+wZi50lRYqOzqeQTIRqBMOqnZo5Ut IptemkZJADf4axJ5kAULZF0NEpdYidHsDb+mZRwkFL02yo3/OtFYaQUyQYUhkRZBrDaQtQ LnhpxNeGXKfoy91Fh2dHqKfIaVZAY2hoYSNttci2T3JFi01sXlk3VF6Fl7OOyFqNcx3Ko7 UQC8Vf9frWXMj9qxXcdwXkOiN0cS5QD+p/sDK4iIxA6pbvksmiPYa2Yd9IHEAA== Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id np2VZQHx6q0w; Sat, 5 Dec 2020 22:14:18 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 3/7] lib: Move sprint_size() from tc here, add print_size() Date: Sat, 5 Dec 2020 22:13:31 +0100 Message-Id: <27dc2ad1616666a94e84122d19b2fd2659d63291.1607201857.git.me@pmachata.org> In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 1.69 / 15.00 / 15.00 X-Rspamd-Queue-Id: 40E0617B4 X-Rspamd-UID: 858e1f Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com When displaying sizes of various sorts, tc commonly uses the function sprint_size() to format the size into a buffer as a human-readable string. This string is then displayed either using print_string(), or in some code even fprintf(). As a result, a typical sequence of code when formatting a size is something like the following: SPRINT_BUF(b); print_uint(PRINT_JSON, "foo", NULL, foo); print_string(PRINT_FP, NULL, "foo %s ", sprint_size(foo, b)); For a concept as broadly useful as size, it would be better to have a dedicated function in json_print. To that end, move sprint_size() from tc_util to json_print. Add helpers print_size() and print_color_size() that wrap arount sprint_size() and provide the JSON dispatch as appropriate. Since print_size() should be the preferred interface, convert vast majority of uses of sprint_size() to print_size(). Two notable exceptions are: - q_tbf, which does not show the size as such, but uses the string "$human_readable_size/$cell_size" even in JSON. There is simply no way to have print_size() emit the same text, because print_size() in JSON mode should of course just use the raw number, without human-readable frills. - q_cake, which relies on the existence of sprint_size() in its macro-based formatting helpers. There might be ways to convert this particular case, but given q_tbf simply cannot be converted, leave it as is. Signed-off-by: Petr Machata --- include/json_print.h | 4 ++++ lib/json_print.c | 28 ++++++++++++++++++++++++++++ tc/m_gate.c | 6 ++---- tc/m_police.c | 5 ++--- tc/q_cake.c | 16 ++++++---------- tc/q_drr.c | 10 +++------- tc/q_fifo.c | 10 +++------- tc/q_fq.c | 9 +++------ tc/q_fq_codel.c | 5 ++--- tc/q_fq_pie.c | 9 +++------ tc/q_gred.c | 39 ++++++++------------------------------- tc/q_hhf.c | 9 +++------ tc/q_htb.c | 19 ++++++++----------- tc/q_red.c | 13 +++---------- tc/q_sfq.c | 15 +++------------ tc/q_tbf.c | 25 +++++++------------------ tc/tc_util.c | 29 +++-------------------------- tc/tc_util.h | 1 - 18 files changed, 91 insertions(+), 161 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index b6c4c0c80833..1a1ad5ffa552 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -69,6 +69,7 @@ _PRINT_FUNC(on_off, bool) _PRINT_FUNC(null, const char*) _PRINT_FUNC(string, const char*) _PRINT_FUNC(uint, unsigned int) +_PRINT_FUNC(size, __u32) _PRINT_FUNC(u64, uint64_t) _PRINT_FUNC(hhu, unsigned char) _PRINT_FUNC(hu, unsigned short) @@ -96,4 +97,7 @@ static inline int print_rate(bool use_iec, enum output_type t, return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate); } +/* A backdoor to the size formatter. Please use print_size() instead. */ +char *sprint_size(__u32 sz, char *buf); + #endif /* _JSON_PRINT_H_ */ diff --git a/lib/json_print.c b/lib/json_print.c index 9ab0f86b83df..c1df637642fd 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -11,6 +11,7 @@ #include #include +#include #include "utils.h" #include "json_print.h" @@ -340,3 +341,30 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, free(buf); return rc; } + +char *sprint_size(__u32 sz, char *buf) +{ + size_t len = SPRINT_BSIZE - 1; + double tmp = sz; + + if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) + snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); + else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) + snprintf(buf, len, "%gKb", rint(tmp/1024)); + else + snprintf(buf, len, "%ub", sz); + + return buf; +} + +int print_color_size(enum output_type type, enum color_attr color, + const char *key, const char *fmt, __u32 sz) +{ + SPRINT_BUF(buf); + + if (_IS_JSON_CONTEXT(type)) + return print_color_uint(type, color, key, "%u", sz); + + sprint_size(sz, buf); + return print_color_string(type, color, key, fmt, buf); +} diff --git a/tc/m_gate.c b/tc/m_gate.c index 4bae86edb58c..892775a35e28 100644 --- a/tc/m_gate.c +++ b/tc/m_gate.c @@ -465,10 +465,8 @@ static int print_gate_list(struct rtattr *list) } if (maxoctets != -1) { - memset(buf, 0, sizeof(buf)); - print_uint(PRINT_JSON, "max_octets", NULL, maxoctets); - print_string(PRINT_FP, NULL, "\t max-octets %s", - sprint_size(maxoctets, buf)); + print_size(PRINT_ANY, "max_octets", "\t max-octets %s", + maxoctets); } else { print_string(PRINT_FP, NULL, "\t max-octets %s", "wildcard"); diff --git a/tc/m_police.c b/tc/m_police.c index 64068fcc4a42..bb51df686288 100644 --- a/tc/m_police.c +++ b/tc/m_police.c @@ -238,7 +238,6 @@ int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) static int print_police(struct action_util *a, FILE *f, struct rtattr *arg) { - SPRINT_BUF(b1); SPRINT_BUF(b2); struct tc_police *p; struct rtattr *tb[TCA_POLICE_MAX+1]; @@ -271,8 +270,8 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg) fprintf(f, " police 0x%x ", p->index); tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64); buffer = tc_calc_xmitsize(rate64, p->burst); - fprintf(f, "burst %s ", sprint_size(buffer, b1)); - fprintf(f, "mtu %s ", sprint_size(p->mtu, b1)); + print_size(PRINT_FP, NULL, "burst %s ", buffer); + print_size(PRINT_FP, NULL, "mtu %s ", p->mtu); if (show_raw) fprintf(f, "[%08x] ", p->burst); diff --git a/tc/q_cake.c b/tc/q_cake.c index ab9233a04f39..b7da731b5510 100644 --- a/tc/q_cake.c +++ b/tc/q_cake.c @@ -434,7 +434,6 @@ static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) int atm = 0; int nat = 0; - SPRINT_BUF(b1); SPRINT_BUF(b2); if (opt == NULL) @@ -573,11 +572,8 @@ static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (mpu) print_uint(PRINT_ANY, "mpu", "mpu %u ", mpu); - if (memlimit) { - print_uint(PRINT_JSON, "memlimit", NULL, memlimit); - print_string(PRINT_FP, NULL, "memlimit %s ", - sprint_size(memlimit, b1)); - } + if (memlimit) + print_size(PRINT_ANY, "memlimit", "memlimit %s ", memlimit); if (fwmark) print_uint(PRINT_FP, NULL, "fwmark 0x%x ", fwmark); @@ -637,11 +633,11 @@ static int cake_print_xstats(struct qdisc_util *qu, FILE *f, if (st[TCA_CAKE_STATS_MEMORY_USED] && st[TCA_CAKE_STATS_MEMORY_LIMIT]) { - print_string(PRINT_FP, NULL, " memory used: %s", - sprint_size(GET_STAT_U32(MEMORY_USED), b1)); + print_size(PRINT_FP, NULL, " memory used: %s", + GET_STAT_U32(MEMORY_USED)); - print_string(PRINT_FP, NULL, " of %s\n", - sprint_size(GET_STAT_U32(MEMORY_LIMIT), b1)); + print_size(PRINT_FP, NULL, " of %s\n", + GET_STAT_U32(MEMORY_LIMIT)); print_uint(PRINT_JSON, "memory_used", NULL, GET_STAT_U32(MEMORY_USED)); diff --git a/tc/q_drr.c b/tc/q_drr.c index f9c90f3035f3..4e829ce3331d 100644 --- a/tc/q_drr.c +++ b/tc/q_drr.c @@ -84,16 +84,14 @@ static int drr_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) { struct rtattr *tb[TCA_DRR_MAX + 1]; - SPRINT_BUF(b1); - if (opt == NULL) return 0; parse_rtattr_nested(tb, TCA_DRR_MAX, opt); if (tb[TCA_DRR_QUANTUM]) - fprintf(f, "quantum %s ", - sprint_size(rta_getattr_u32(tb[TCA_DRR_QUANTUM]), b1)); + print_size(PRINT_FP, NULL, "quantum %s ", + rta_getattr_u32(tb[TCA_DRR_QUANTUM])); return 0; } @@ -101,15 +99,13 @@ static int drr_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstat { struct tc_drr_stats *x; - SPRINT_BUF(b1); - if (xstats == NULL) return 0; if (RTA_PAYLOAD(xstats) < sizeof(*x)) return -1; x = RTA_DATA(xstats); - fprintf(f, " deficit %s ", sprint_size(x->deficit, b1)); + print_size(PRINT_FP, NULL, " deficit %s ", x->deficit); return 0; } diff --git a/tc/q_fifo.c b/tc/q_fifo.c index 61493fbbc5bc..ce82e74dcc5e 100644 --- a/tc/q_fifo.c +++ b/tc/q_fifo.c @@ -67,14 +67,10 @@ static int fifo_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (RTA_PAYLOAD(opt) < sizeof(*qopt)) return -1; qopt = RTA_DATA(opt); - if (strcmp(qu->id, "bfifo") == 0) { - SPRINT_BUF(b1); - print_uint(PRINT_JSON, "limit", NULL, qopt->limit); - print_string(PRINT_FP, NULL, "limit %s", - sprint_size(qopt->limit, b1)); - } else { + if (strcmp(qu->id, "bfifo") == 0) + print_size(PRINT_ANY, "limit", "limit %s", qopt->limit); + else print_uint(PRINT_ANY, "limit", "limit %up", qopt->limit); - } return 0; } diff --git a/tc/q_fq.c b/tc/q_fq.c index 71a513fb3c72..cff21975b897 100644 --- a/tc/q_fq.c +++ b/tc/q_fq.c @@ -315,16 +315,13 @@ static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_FQ_QUANTUM] && RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) { quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]); - print_uint(PRINT_JSON, "quantum", NULL, quantum); - print_string(PRINT_FP, NULL, "quantum %s ", - sprint_size(quantum, b1)); + print_size(PRINT_ANY, "quantum", "quantum %s ", quantum); } if (tb[TCA_FQ_INITIAL_QUANTUM] && RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) { quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]); - print_uint(PRINT_JSON, "initial_quantum", NULL, quantum); - print_string(PRINT_FP, NULL, "initial_quantum %s ", - sprint_size(quantum, b1)); + print_size(PRINT_ANY, "initial_quantum", "initial_quantum %s ", + quantum); } if (tb[TCA_FQ_FLOW_MAX_RATE] && RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) { diff --git a/tc/q_fq_codel.c b/tc/q_fq_codel.c index 1a51302e0e2b..300980652243 100644 --- a/tc/q_fq_codel.c +++ b/tc/q_fq_codel.c @@ -221,9 +221,8 @@ static int fq_codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt if (tb[TCA_FQ_CODEL_MEMORY_LIMIT] && RTA_PAYLOAD(tb[TCA_FQ_CODEL_MEMORY_LIMIT]) >= sizeof(__u32)) { memory_limit = rta_getattr_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]); - print_uint(PRINT_JSON, "memory_limit", NULL, memory_limit); - print_string(PRINT_FP, NULL, "memory_limit %s ", - sprint_size(memory_limit, b1)); + print_size(PRINT_ANY, "memory_limit", "memory_limit %s ", + memory_limit); } if (tb[TCA_FQ_CODEL_ECN] && RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) { diff --git a/tc/q_fq_pie.c b/tc/q_fq_pie.c index c136cd1af124..9cbef47eef88 100644 --- a/tc/q_fq_pie.c +++ b/tc/q_fq_pie.c @@ -232,16 +232,13 @@ static int fq_pie_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_FQ_PIE_QUANTUM] && RTA_PAYLOAD(tb[TCA_FQ_PIE_QUANTUM]) >= sizeof(__u32)) { quantum = rta_getattr_u32(tb[TCA_FQ_PIE_QUANTUM]); - print_uint(PRINT_JSON, "quantum", NULL, quantum); - print_string(PRINT_FP, NULL, "quantum %s ", - sprint_size(quantum, b1)); + print_size(PRINT_ANY, "quantum", "quantum %s ", quantum); } if (tb[TCA_FQ_PIE_MEMORY_LIMIT] && RTA_PAYLOAD(tb[TCA_FQ_PIE_MEMORY_LIMIT]) >= sizeof(__u32)) { memory_limit = rta_getattr_u32(tb[TCA_FQ_PIE_MEMORY_LIMIT]); - print_uint(PRINT_JSON, "memory_limit", NULL, memory_limit); - print_string(PRINT_FP, NULL, "memory_limit %s ", - sprint_size(memory_limit, b1)); + print_size(PRINT_ANY, "memory_limit", "memory_limit %s ", + memory_limit); } if (tb[TCA_FQ_PIE_ECN_PROB] && RTA_PAYLOAD(tb[TCA_FQ_PIE_ECN_PROB]) >= sizeof(__u32)) { diff --git a/tc/q_gred.c b/tc/q_gred.c index 8a1cecffc126..89aeb086038f 100644 --- a/tc/q_gred.c +++ b/tc/q_gred.c @@ -373,18 +373,11 @@ gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt) { __u64 bytes = info ? info->bytes : qopt->bytesin; - SPRINT_BUF(b1); - if (!is_json_context()) printf("\n Queue size: "); - print_uint(PRINT_JSON, "qave", NULL, qopt->qave); - print_string(PRINT_FP, NULL, "average %s ", - sprint_size(qopt->qave, b1)); - - print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog); - print_string(PRINT_FP, NULL, "current %s ", - sprint_size(qopt->backlog, b1)); + print_size(PRINT_ANY, "qave", "average %s ", qopt->qave); + print_size(PRINT_ANY, "backlog", "current %s ", qopt->backlog); if (!is_json_context()) printf("\n Dropped packets: "); @@ -415,9 +408,7 @@ gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt) printf("\n Total packets: "); print_uint(PRINT_ANY, "packets", "%u ", qopt->packets); - - print_uint(PRINT_JSON, "bytes", NULL, bytes); - print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1)); + print_size(PRINT_ANY, "bytes", "(%s) ", bytes); } static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) @@ -431,8 +422,6 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) __u32 *limit = NULL; unsigned int i; - SPRINT_BUF(b1); - if (opt == NULL) return 0; @@ -470,11 +459,8 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) else print_bool(PRINT_ANY, "grio", NULL, false); - if (limit) { - print_uint(PRINT_JSON, "limit", NULL, *limit); - print_string(PRINT_FP, NULL, "limit %s ", - sprint_size(*limit, b1)); - } + if (limit) + print_size(PRINT_ANY, "limit", "limit %s ", *limit); tc_red_print_flags(sopt->flags); @@ -487,18 +473,9 @@ static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP); print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio); - - print_uint(PRINT_JSON, "limit", NULL, qopt->limit); - print_string(PRINT_FP, NULL, "limit %s ", - sprint_size(qopt->limit, b1)); - - print_uint(PRINT_JSON, "min", NULL, qopt->qth_min); - print_string(PRINT_FP, NULL, "min %s ", - sprint_size(qopt->qth_min, b1)); - - print_uint(PRINT_JSON, "max", NULL, qopt->qth_max); - print_string(PRINT_FP, NULL, "max %s ", - sprint_size(qopt->qth_max, b1)); + print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit); + print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min); + print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max); if (infos[i].flags_present) tc_red_print_flags(infos[i].flags); diff --git a/tc/q_hhf.c b/tc/q_hhf.c index f88880117bee..95e49f3dd720 100644 --- a/tc/q_hhf.c +++ b/tc/q_hhf.c @@ -143,9 +143,7 @@ static int hhf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_HHF_QUANTUM] && RTA_PAYLOAD(tb[TCA_HHF_QUANTUM]) >= sizeof(__u32)) { quantum = rta_getattr_u32(tb[TCA_HHF_QUANTUM]); - print_uint(PRINT_JSON, "quantum", NULL, quantum); - print_string(PRINT_FP, NULL, "quantum %s ", - sprint_size(quantum, b1)); + print_size(PRINT_ANY, "quantum", "quantum %s ", quantum); } if (tb[TCA_HHF_HH_FLOWS_LIMIT] && RTA_PAYLOAD(tb[TCA_HHF_HH_FLOWS_LIMIT]) >= sizeof(__u32)) { @@ -162,9 +160,8 @@ static int hhf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (tb[TCA_HHF_ADMIT_BYTES] && RTA_PAYLOAD(tb[TCA_HHF_ADMIT_BYTES]) >= sizeof(__u32)) { admit_bytes = rta_getattr_u32(tb[TCA_HHF_ADMIT_BYTES]); - print_uint(PRINT_JSON, "admit_bytes", NULL, admit_bytes); - print_string(PRINT_FP, NULL, "admit_bytes %s ", - sprint_size(admit_bytes, b1)); + print_size(PRINT_ANY, "admit_bytes", "admit_bytes %s ", + admit_bytes); } if (tb[TCA_HHF_EVICT_TIMEOUT] && RTA_PAYLOAD(tb[TCA_HHF_EVICT_TIMEOUT]) >= sizeof(__u32)) { diff --git a/tc/q_htb.c b/tc/q_htb.c index 10030a8741fa..c609e9749c2c 100644 --- a/tc/q_htb.c +++ b/tc/q_htb.c @@ -269,7 +269,6 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) __u64 rate64, ceil64; SPRINT_BUF(b1); - SPRINT_BUF(b2); SPRINT_BUF(b3); if (opt == NULL) @@ -310,18 +309,16 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (linklayer > TC_LINKLAYER_ETHERNET || show_details) fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3)); if (show_details) { - fprintf(f, "burst %s/%u mpu %s ", - sprint_size(buffer, b1), - 1<rate.cell_log, - sprint_size(hopt->rate.mpu, b2)); - fprintf(f, "cburst %s/%u mpu %s ", - sprint_size(cbuffer, b1), - 1<ceil.cell_log, - sprint_size(hopt->ceil.mpu, b2)); + print_size(PRINT_FP, NULL, "burst %s/", buffer); + fprintf(f, "%u ", 1<rate.cell_log); + print_size(PRINT_FP, NULL, "mpu %s ", hopt->rate.mpu); + print_size(PRINT_FP, NULL, "cburst %s/", cbuffer); + fprintf(f, "%u ", 1<ceil.cell_log); + print_size(PRINT_FP, NULL, "mpu %s ", hopt->ceil.mpu); fprintf(f, "level %d ", (int)hopt->level); } else { - fprintf(f, "burst %s ", sprint_size(buffer, b1)); - fprintf(f, "cburst %s ", sprint_size(cbuffer, b1)); + print_size(PRINT_FP, NULL, "burst %s ", buffer); + print_size(PRINT_FP, NULL, "cburst %s ", cbuffer); } if (show_raw) fprintf(f, "buffer [%08x] cbuffer [%08x] ", diff --git a/tc/q_red.c b/tc/q_red.c index df788f8fc5da..fd50d37d31cb 100644 --- a/tc/q_red.c +++ b/tc/q_red.c @@ -192,10 +192,6 @@ static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) struct tc_red_qopt *qopt; __u32 max_P = 0; - SPRINT_BUF(b1); - SPRINT_BUF(b2); - SPRINT_BUF(b3); - if (opt == NULL) return 0; @@ -217,12 +213,9 @@ static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) qopt->flags = flags_bf->value; } - print_uint(PRINT_JSON, "limit", NULL, qopt->limit); - print_string(PRINT_FP, NULL, "limit %s ", sprint_size(qopt->limit, b1)); - print_uint(PRINT_JSON, "min", NULL, qopt->qth_min); - print_string(PRINT_FP, NULL, "min %s ", sprint_size(qopt->qth_min, b2)); - print_uint(PRINT_JSON, "max", NULL, qopt->qth_max); - print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3)); + print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit); + print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min); + print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max); tc_red_print_flags(qopt->flags); diff --git a/tc/q_sfq.c b/tc/q_sfq.c index 2b9bbcd230b9..d04a440cece7 100644 --- a/tc/q_sfq.c +++ b/tc/q_sfq.c @@ -206,9 +206,6 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) struct tc_sfq_qopt *qopt; struct tc_sfq_qopt_v1 *qopt_ext = NULL; - SPRINT_BUF(b1); - SPRINT_BUF(b2); - SPRINT_BUF(b3); if (opt == NULL) return 0; @@ -219,9 +216,7 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) qopt = RTA_DATA(opt); print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit); - print_uint(PRINT_JSON, "quantum", NULL, qopt->quantum); - print_string(PRINT_FP, NULL, "quantum %s ", - sprint_size(qopt->quantum, b1)); + print_size(PRINT_ANY, "quantum", "quantum %s ", qopt->quantum); if (qopt_ext && qopt_ext->depth) print_uint(PRINT_ANY, "depth", "depth %u ", qopt_ext->depth); @@ -237,12 +232,8 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) qopt->perturb_period); if (qopt_ext && qopt_ext->qth_min) { print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt_ext->Wlog); - print_uint(PRINT_JSON, "min", NULL, qopt_ext->qth_min); - print_string(PRINT_FP, NULL, "min %s ", - sprint_size(qopt_ext->qth_min, b2)); - print_uint(PRINT_JSON, "max", NULL, qopt_ext->qth_max); - print_string(PRINT_FP, NULL, "max %s ", - sprint_size(qopt_ext->qth_max, b3)); + print_size(PRINT_ANY, "min", "min %s ", qopt_ext->qth_min); + print_size(PRINT_ANY, "max", "max %s ", qopt_ext->qth_max); print_float(PRINT_ANY, "probability", "probability %lg ", qopt_ext->max_P / pow(2, 32)); tc_red_print_flags(qopt_ext->flags); diff --git a/tc/q_tbf.c b/tc/q_tbf.c index 9d4833385521..4e5bf382fd03 100644 --- a/tc/q_tbf.c +++ b/tc/q_tbf.c @@ -292,13 +292,9 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) sprintf(b1, "%s/%u", sprint_size(buffer, b2), 1 << qopt->rate.cell_log); print_string(PRINT_ANY, "burst", "burst %s ", b1); - print_uint(PRINT_JSON, "mpu", NULL, qopt->rate.mpu); - print_string(PRINT_FP, NULL, "mpu %s ", - sprint_size(qopt->rate.mpu, b1)); + print_size(PRINT_ANY, "mpu", "mpu %s ", qopt->rate.mpu); } else { - print_u64(PRINT_JSON, "burst", NULL, buffer); - print_string(PRINT_FP, NULL, "burst %s ", - sprint_size(buffer, b1)); + print_size(PRINT_ANY, "burst", "burst %s ", buffer); } if (show_raw) print_hex(PRINT_ANY, "burst_raw", "[%08x] ", qopt->buffer); @@ -314,15 +310,11 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) sprintf(b1, "%s/%u", sprint_size(mtu, b2), 1 << qopt->peakrate.cell_log); print_string(PRINT_ANY, "mtu", "mtu %s ", b1); - print_uint(PRINT_JSON, "mpu", NULL, + print_size(PRINT_ANY, "mpu", "mpu %s ", qopt->peakrate.mpu); - print_string(PRINT_FP, NULL, "mpu %s ", - sprint_size(qopt->peakrate.mpu, - b1)); } else { - print_u64(PRINT_JSON, "minburst", NULL, mtu); - print_string(PRINT_FP, NULL, "minburst %s ", - sprint_size(mtu, b1)); + print_size(PRINT_ANY, "minburst", + "minburst %s ", mtu); } if (show_raw) print_hex(PRINT_ANY, "mtu_raw", "[%08x] ", @@ -344,11 +336,8 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) print_string(PRINT_FP, NULL, "lat %s ", sprint_time(latency, b1)); } - if (show_raw || latency < 0.0) { - print_uint(PRINT_JSON, "limit", NULL, qopt->limit); - print_string(PRINT_FP, NULL, "limit %s ", - sprint_size(qopt->limit, b1)); - } + if (show_raw || latency < 0.0) + print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit); if (qopt->rate.overhead) print_int(PRINT_ANY, "overhead", "overhead %d ", qopt->rate.overhead); diff --git a/tc/tc_util.c b/tc/tc_util.c index 40efaa9a4b8a..ff979c617b9b 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -412,24 +412,6 @@ void print_devname(enum output_type type, int ifindex) "dev", "%s ", ifname); } -static void print_size(char *buf, int len, __u32 sz) -{ - double tmp = sz; - - if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) - snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); - else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) - snprintf(buf, len, "%gKb", rint(tmp/1024)); - else - snprintf(buf, len, "%ub", sz); -} - -char *sprint_size(__u32 size, char *buf) -{ - print_size(buf, SPRINT_BSIZE-1, size); - return buf; -} - static const char *action_n2a(int action) { static char buf[64]; @@ -786,7 +768,6 @@ static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix) void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) { - SPRINT_BUF(b1); struct rtattr *tbs[TCA_STATS_MAX + 1]; parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); @@ -852,10 +833,8 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); if (!tbs[TCA_STATS_RATE_EST]) print_nl(); - print_uint(PRINT_JSON, "backlog", NULL, q.backlog); print_string(PRINT_FP, NULL, "%s", prefix); - print_string(PRINT_FP, NULL, "backlog %s", - sprint_size(q.backlog, b1)); + print_size(PRINT_ANY, "backlog", "backlog %s", q.backlog); print_uint(PRINT_ANY, "qlen", " %up", q.qlen); print_uint(PRINT_FP, NULL, " requeues %u", q.requeues); } @@ -867,8 +846,6 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) { - SPRINT_BUF(b1); - if (tb[TCA_STATS2]) { print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); if (xstats && !*xstats) @@ -901,8 +878,8 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, if (st.qlen || st.backlog) { fprintf(fp, "backlog "); if (st.backlog) - fprintf(fp, "%s ", - sprint_size(st.backlog, b1)); + print_size(PRINT_FP, NULL, "%s ", + st.backlog); if (st.qlen) fprintf(fp, "%up ", st.qlen); } diff --git a/tc/tc_util.h b/tc/tc_util.h index e5d533a44e10..d3b38c69155d 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -88,7 +88,6 @@ void tc_print_rate(enum output_type t, const char *key, const char *fmt, unsigned long long rate); void print_devname(enum output_type type, int ifindex); -char *sprint_size(__u32 size, char *buf); char *sprint_tc_classid(__u32 h, char *buf); char *sprint_ticks(__u32 ticks, char *buf); char *sprint_linklayer(unsigned int linklayer, char *buf); From patchwork Sat Dec 5 21:13:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953505 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 103F6C4361A for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D492F2310B for ; Sat, 5 Dec 2020 21:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727275AbgLEVPH (ORCPT ); Sat, 5 Dec 2020 16:15:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgLEVPF (ORCPT ); Sat, 5 Dec 2020 16:15:05 -0500 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [IPv6:2001:67c:2050::465:201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABB91C0613D4 for ; Sat, 5 Dec 2020 13:14:24 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4CpMlM0V4NzQjhF; Sat, 5 Dec 2020 22:14:23 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yQaGFvfsRhOWH7TTb/nTzNfIAJvbZkrgSJo810vo8sI=; b=IOhtZ7QB8dQFgL5g87ijsPKA3ziSAt1AYfp/TeZpBkAP+1OCemTlh5OfgpKAoe/i+FqtUa FKSin4HJBowvgqui12yrQYcI5A7eYHEyC2tGrPmOLtLf33wuQx4OgMN7y1niVc50A1Qv0w jueLARVI/18laH/bzkU2e7a2NNgFOgv7STZQeSSxw1NISE1M4suncCwfGUGVy2N51TRWgz t8ImuXGLUTuJc1XrJgUggT4PL4gWDdpxcDhWtDRnxRv3E6LNyX4d9MQ0yc+tFt6FywdBR9 2NkE6Q7mHVd1U2KfawhvS+Q4gqqi36N9meK0hyOSmlVd3x8T+jMqawKIZJeRQw== Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id zmweDkFS7ub9; Sat, 5 Dec 2020 22:14:19 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 4/7] lib: sprint_size(): Uncrustify the code a bit Date: Sat, 5 Dec 2020 22:13:32 +0100 Message-Id: <5de07d7849479adb8b660060d06004ab8582ed48.1607201857.git.me@pmachata.org> In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 1.69 / 15.00 / 15.00 X-Rspamd-Queue-Id: 04C4C17B0 X-Rspamd-UID: d88a34 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com Ideally this and the rate printing would both be converted to a common helper, but unfortunately the two format differently and this would break tests and scripts out there. So just make the code look less like a wad of hay. Signed-off-by: Petr Machata --- Notes: v2: - This patch is new. It addresses a request from Stephen Hemminger to clean up the sprint_size() function. lib/json_print.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/json_print.c b/lib/json_print.c index c1df637642fd..d28e957c9603 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -344,13 +344,15 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, char *sprint_size(__u32 sz, char *buf) { + long kilo = 1024; + long mega = kilo * kilo; size_t len = SPRINT_BSIZE - 1; double tmp = sz; - if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) - snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); - else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) - snprintf(buf, len, "%gKb", rint(tmp/1024)); + if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024) + snprintf(buf, len, "%gMb", rint(tmp / mega)); + else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16) + snprintf(buf, len, "%gKb", rint(tmp / kilo)); else snprintf(buf, len, "%ub", sz); From patchwork Sat Dec 5 21:13:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953507 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 4DB8EC4167B for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 278F82310B for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727343AbgLEVPM (ORCPT ); Sat, 5 Dec 2020 16:15:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726120AbgLEVPF (ORCPT ); Sat, 5 Dec 2020 16:15:05 -0500 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [IPv6:2001:67c:2050::465:201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50355C061A4F for ; Sat, 5 Dec 2020 13:14:25 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4CpMlM6RQ2zQlKJ; Sat, 5 Dec 2020 22:14:23 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MOy+Jg0HfU7nTdZjUwcJ7pAVUCmm2te5eFX8paJ4Shg=; b=B581OOcs0gAGCqGn1fa499QzxAJH8OkK7Ks1Wk2eBS6aU+UGKxLNKN3TOg0bmiULZvFQqo T0QHTfuhSC3jKeJPyZIPmAAzEKF+OlT1x7QmhOyrz0EasW5vEbTLkdjRoJxY2bHPZf07eU /74nUY192XKw/dFXwz/bTegYk3wmvsZe2oMu5BPPKnqNOe2HhlLjwMIs8LxudvhrwuNM9H zOCUBkLGsMCpgNCRwmA5rcsRd+3ZV4PGjoy4SuG1Z8+6J02h7v5tkUoc5s6ZMdSsXp1Usp PDq9Sy0SFBPQj+7d1qs4CEXRnxs+q2qebqHIrDJVq3UqNYcbxSub5gyF9QjKAw== Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter05.heinlein-hosting.de (spamfilter05.heinlein-hosting.de [80.241.56.123]) (amavisd-new, port 10030) with ESMTP id jsVS3T50vNwH; Sat, 5 Dec 2020 22:14:20 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 5/7] lib: print_color_rate(): Fix formatting small rates in IEC mode Date: Sat, 5 Dec 2020 22:13:33 +0100 Message-Id: <01ae308a04bd16c8671cfef2d14688f00bef4846.1607201857.git.me@pmachata.org> In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 2.11 / 15.00 / 15.00 X-Rspamd-Queue-Id: D66BA171D X-Rspamd-UID: 430b9f Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com ISO/IEC units are distinguished from the decadic ones by using a prefixes like "Ki", "Mi" instead of "K" and "M". The current code inserts the letter "i" after the decadic unit when in IEC mode. However it does so even when the prefix is an empty string, formatting 1Kbit in IEC mode as "1000ibit". Fix by omitting the letter if there is no prefix. Signed-off-by: Petr Machata --- lib/json_print.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/json_print.c b/lib/json_print.c index d28e957c9603..b086123ad1f4 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -333,7 +333,8 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, rate /= kilo; } - rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], str); + rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], + i > 0 ? str : ""); if (rc < 0) return -1; From patchwork Sat Dec 5 21:13:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953509 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 2C000C4361B for ; Sat, 5 Dec 2020 21:15:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E903823110 for ; Sat, 5 Dec 2020 21:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727304AbgLEVPI (ORCPT ); Sat, 5 Dec 2020 16:15:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgLEVPH (ORCPT ); Sat, 5 Dec 2020 16:15:07 -0500 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [IPv6:2001:67c:2050::465:103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E495C061A51 for ; Sat, 5 Dec 2020 13:14:27 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4CpMlQ0LMYzQlF0; Sat, 5 Dec 2020 22:14:26 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202863; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JkQrbItmoOWRkjiKm/3CKnxyik/biurDVy6i24zBA+A=; b=coTuQfIuiu23iibsd8BXrdqtOKgGlacQWvQJQ/vcPDKXtK3uQdNcVvUOgq4ocl0MZV5r1i EEOcqqRnVTSpUNVrA1Ba7puvloifJHqjm6EkvlDpYjPuyJgVQiS8lgAexmHty9fGE6IHe7 TQRZb7Aaf8oLJInVddYBQIKjyqGrtOlf2rhq3ky1c4SmyBfY3Y6dlbBW8VxsNPVmt7LhNH 47ffeDlYSdOcpQXCliYW1fBv3wmuM8niNXZNlF+rg/D6PqjYNJFchLZCpU7A3uVjSl5a5O A31dSl6RewUTGoJe7YrydSrOVTF2+p4q5BavNtUjh41L0yOUVICPNOZL76Dxxg== Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id kEnpNOahWFNb; Sat, 5 Dec 2020 22:14:22 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 6/7] lib: Move get_rate(), get_rate64() from tc here Date: Sat, 5 Dec 2020 22:13:34 +0100 Message-Id: <08471fe1edf1100b31a50235b751a9c4fa61f147.1607201857.git.me@pmachata.org> In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: * X-Rspamd-Score: 0.22 / 15.00 / 15.00 X-Rspamd-Queue-Id: 9CC25171F X-Rspamd-UID: 050111 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com The functions get_rate() and get_rate64() are useful for parsing rate-like values. The DCB tool will find these useful in the maxrate subtool. Move them over to lib so that they can be easily reused. Signed-off-by: Petr Machata --- include/utils.h | 2 ++ lib/utils.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ tc/tc_util.c | 79 ------------------------------------------------- tc/tc_util.h | 2 -- 4 files changed, 81 insertions(+), 81 deletions(-) diff --git a/include/utils.h b/include/utils.h index 01454f71cb1a..e2073844f2ef 100644 --- a/include/utils.h +++ b/include/utils.h @@ -162,6 +162,8 @@ int get_be64(__be64 *val, const char *arg, int base); int get_be32(__be32 *val, const char *arg, int base); int get_be16(__be16 *val, const char *arg, int base); int get_addr64(__u64 *ap, const char *cp); +int get_rate(unsigned int *rate, const char *str); +int get_rate64(__u64 *rate, const char *str); int hex2mem(const char *buf, uint8_t *mem, int count); char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen); diff --git a/lib/utils.c b/lib/utils.c index a0ba5181160e..1237ae40246c 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -513,6 +513,85 @@ int get_addr64(__u64 *ap, const char *cp) return 1; } +/* See http://physics.nist.gov/cuu/Units/binary.html */ +static const struct rate_suffix { + const char *name; + double scale; +} suffixes[] = { + { "bit", 1. }, + { "Kibit", 1024. }, + { "kbit", 1000. }, + { "mibit", 1024.*1024. }, + { "mbit", 1000000. }, + { "gibit", 1024.*1024.*1024. }, + { "gbit", 1000000000. }, + { "tibit", 1024.*1024.*1024.*1024. }, + { "tbit", 1000000000000. }, + { "Bps", 8. }, + { "KiBps", 8.*1024. }, + { "KBps", 8000. }, + { "MiBps", 8.*1024*1024. }, + { "MBps", 8000000. }, + { "GiBps", 8.*1024.*1024.*1024. }, + { "GBps", 8000000000. }, + { "TiBps", 8.*1024.*1024.*1024.*1024. }, + { "TBps", 8000000000000. }, + { NULL } +}; + +int get_rate(unsigned int *rate, const char *str) +{ + char *p; + double bps = strtod(str, &p); + const struct rate_suffix *s; + + if (p == str) + return -1; + + for (s = suffixes; s->name; ++s) { + if (strcasecmp(s->name, p) == 0) { + bps *= s->scale; + p += strlen(p); + break; + } + } + + if (*p) + return -1; /* unknown suffix */ + + bps /= 8; /* -> bytes per second */ + *rate = bps; + /* detect if an overflow happened */ + if (*rate != floor(bps)) + return -1; + return 0; +} + +int get_rate64(__u64 *rate, const char *str) +{ + char *p; + double bps = strtod(str, &p); + const struct rate_suffix *s; + + if (p == str) + return -1; + + for (s = suffixes; s->name; ++s) { + if (strcasecmp(s->name, p) == 0) { + bps *= s->scale; + p += strlen(p); + break; + } + } + + if (*p) + return -1; /* unknown suffix */ + + bps /= 8; /* -> bytes per second */ + *rate = bps; + return 0; +} + static void set_address_type(inet_prefix *addr) { switch (addr->family) { diff --git a/tc/tc_util.c b/tc/tc_util.c index ff979c617b9b..3a133ad84ff9 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -164,32 +164,6 @@ char *sprint_tc_classid(__u32 h, char *buf) return buf; } -/* See http://physics.nist.gov/cuu/Units/binary.html */ -static const struct rate_suffix { - const char *name; - double scale; -} suffixes[] = { - { "bit", 1. }, - { "Kibit", 1024. }, - { "kbit", 1000. }, - { "mibit", 1024.*1024. }, - { "mbit", 1000000. }, - { "gibit", 1024.*1024.*1024. }, - { "gbit", 1000000000. }, - { "tibit", 1024.*1024.*1024.*1024. }, - { "tbit", 1000000000000. }, - { "Bps", 8. }, - { "KiBps", 8.*1024. }, - { "KBps", 8000. }, - { "MiBps", 8.*1024*1024. }, - { "MBps", 8000000. }, - { "GiBps", 8.*1024.*1024.*1024. }, - { "GBps", 8000000000. }, - { "TiBps", 8.*1024.*1024.*1024.*1024. }, - { "TBps", 8000000000000. }, - { NULL } -}; - /* Parse a percent e.g: '30%' * return: 0 = ok, -1 = error, 1 = out of range */ @@ -273,59 +247,6 @@ int get_percent_rate64(__u64 *rate, const char *str, const char *dev) return get_rate64(rate, r_str); } -int get_rate(unsigned int *rate, const char *str) -{ - char *p; - double bps = strtod(str, &p); - const struct rate_suffix *s; - - if (p == str) - return -1; - - for (s = suffixes; s->name; ++s) { - if (strcasecmp(s->name, p) == 0) { - bps *= s->scale; - p += strlen(p); - break; - } - } - - if (*p) - return -1; /* unknown suffix */ - - bps /= 8; /* -> bytes per second */ - *rate = bps; - /* detect if an overflow happened */ - if (*rate != floor(bps)) - return -1; - return 0; -} - -int get_rate64(__u64 *rate, const char *str) -{ - char *p; - double bps = strtod(str, &p); - const struct rate_suffix *s; - - if (p == str) - return -1; - - for (s = suffixes; s->name; ++s) { - if (strcasecmp(s->name, p) == 0) { - bps *= s->scale; - p += strlen(p); - break; - } - } - - if (*p) - return -1; /* unknown suffix */ - - bps /= 8; /* -> bytes per second */ - *rate = bps; - return 0; -} - void tc_print_rate(enum output_type t, const char *key, const char *fmt, unsigned long long rate) { diff --git a/tc/tc_util.h b/tc/tc_util.h index d3b38c69155d..675fb34269f6 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -76,9 +76,7 @@ struct qdisc_util *get_qdisc_kind(const char *str); struct filter_util *get_filter_kind(const char *str); int get_qdisc_handle(__u32 *h, const char *str); -int get_rate(unsigned int *rate, const char *str); int get_percent_rate(unsigned int *rate, const char *str, const char *dev); -int get_rate64(__u64 *rate, const char *str); int get_percent_rate64(__u64 *rate, const char *str, const char *dev); int get_size(unsigned int *size, const char *str); int get_size_and_cell(unsigned int *size, int *cell_log, char *str); From patchwork Sat Dec 5 21:13:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Machata X-Patchwork-Id: 11953517 X-Patchwork-Delegate: dsahern@gmail.com 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 47D4FC4361A for ; Sat, 5 Dec 2020 21:15:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1A55223108 for ; Sat, 5 Dec 2020 21:15:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727434AbgLEVPr (ORCPT ); Sat, 5 Dec 2020 16:15:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbgLEVPr (ORCPT ); Sat, 5 Dec 2020 16:15:47 -0500 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [IPv6:2001:67c:2050::465:103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D383C061A54 for ; Sat, 5 Dec 2020 13:14:53 -0800 (PST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4CpMlQ5MtRzQlF7; Sat, 5 Dec 2020 22:14:26 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pmachata.org; s=MBO0001; t=1607202864; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yMCNDlNXt76MMhsD27mpaT7LDmRyi7eSltJuKYOFOU4=; b=ioeDe0yrKuUR2wZn9ZJ/M9cd6mktmt8x8vRH4G+sudvI6HaO+s+Wu7+aRIrPJI59wyBpTu hraetD+aC68FgGExnDiXwvnaOGwHBgrN2psxT/ZbmbA2rr2V/vV9fysY5cZoU9JvawVnbr PdXR6VFddeYkbRHrgMNRuv17iCuleg/nYECLONzBb6rOK/r4NOS0xD8puO4kcTn2pWFgMm q1TkoCW2TU1OJmd2f/mq8qTkOUC/sUN0ZJJ60SlMOkVRxrgPjT/C9cDWzQsgLxwOXhmsEX rfoLppU+gDKuMJkrmSA1jdh3GEKQbcRXmnWIJj0Cq6LUgsXEI59kb9QhS+c4yw== Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id xuOyHn-UllG7; Sat, 5 Dec 2020 22:14:23 +0100 (CET) From: Petr Machata To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com, edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org, Petr Machata Subject: [PATCH iproute2-next v2 7/7] lib: Move get_size() from tc here Date: Sat, 5 Dec 2020 22:13:35 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 2.10 / 15.00 / 15.00 X-Rspamd-Queue-Id: 9F7EA17A9 X-Rspamd-UID: 4518e7 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com The function get_size() serves for parsing of sizes using a handly notation that supports units and their prefixes, such as 10Kbit. This will be useful for the DCB buffer size parsing. Move the function from TC to the general library, so that it can be reused. Signed-off-by: Petr Machata --- include/utils.h | 1 + lib/utils.c | 35 +++++++++++++++++++++++++++++++++++ tc/tc_util.c | 35 ----------------------------------- tc/tc_util.h | 1 - 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/include/utils.h b/include/utils.h index e2073844f2ef..1704392525a2 100644 --- a/include/utils.h +++ b/include/utils.h @@ -164,6 +164,7 @@ int get_be16(__be16 *val, const char *arg, int base); int get_addr64(__u64 *ap, const char *cp); int get_rate(unsigned int *rate, const char *str); int get_rate64(__u64 *rate, const char *str); +int get_size(unsigned int *size, const char *str); int hex2mem(const char *buf, uint8_t *mem, int count); char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen); diff --git a/lib/utils.c b/lib/utils.c index 1237ae40246c..de875639c608 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -592,6 +592,41 @@ int get_rate64(__u64 *rate, const char *str) return 0; } +int get_size(unsigned int *size, const char *str) +{ + double sz; + char *p; + + sz = strtod(str, &p); + if (p == str) + return -1; + + if (*p) { + if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0) + sz *= 1024; + else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0) + sz *= 1024*1024*1024; + else if (strcasecmp(p, "gbit") == 0) + sz *= 1024*1024*1024/8; + else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0) + sz *= 1024*1024; + else if (strcasecmp(p, "mbit") == 0) + sz *= 1024*1024/8; + else if (strcasecmp(p, "kbit") == 0) + sz *= 1024/8; + else if (strcasecmp(p, "b") != 0) + return -1; + } + + *size = sz; + + /* detect if an overflow happened */ + if (*size != floor(sz)) + return -1; + + return 0; +} + static void set_address_type(inet_prefix *addr) { switch (addr->family) { diff --git a/tc/tc_util.c b/tc/tc_util.c index 3a133ad84ff9..48065897cee7 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -258,41 +258,6 @@ char *sprint_ticks(__u32 ticks, char *buf) return sprint_time(tc_core_tick2time(ticks), buf); } -int get_size(unsigned int *size, const char *str) -{ - double sz; - char *p; - - sz = strtod(str, &p); - if (p == str) - return -1; - - if (*p) { - if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0) - sz *= 1024; - else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0) - sz *= 1024*1024*1024; - else if (strcasecmp(p, "gbit") == 0) - sz *= 1024*1024*1024/8; - else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0) - sz *= 1024*1024; - else if (strcasecmp(p, "mbit") == 0) - sz *= 1024*1024/8; - else if (strcasecmp(p, "kbit") == 0) - sz *= 1024/8; - else if (strcasecmp(p, "b") != 0) - return -1; - } - - *size = sz; - - /* detect if an overflow happened */ - if (*size != floor(sz)) - return -1; - - return 0; -} - int get_size_and_cell(unsigned int *size, int *cell_log, char *str) { char *slash = strchr(str, '/'); diff --git a/tc/tc_util.h b/tc/tc_util.h index 675fb34269f6..b197bcdd7b80 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -78,7 +78,6 @@ struct filter_util *get_filter_kind(const char *str); int get_qdisc_handle(__u32 *h, const char *str); int get_percent_rate(unsigned int *rate, const char *str, const char *dev); int get_percent_rate64(__u64 *rate, const char *str, const char *dev); -int get_size(unsigned int *size, const char *str); int get_size_and_cell(unsigned int *size, int *cell_log, char *str); int get_linklayer(unsigned int *val, const char *arg);