]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib: Move print_rate() from tc here; modernize
authorPetr Machata <me@pmachata.org>
Sat, 5 Dec 2020 21:13:30 +0000 (22:13 +0100)
committerDavid Ahern <dsahern@gmail.com>
Wed, 9 Dec 2020 02:30:15 +0000 (02:30 +0000)
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 <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
13 files changed:
include/json_print.h
lib/json_print.c
tc/m_police.c
tc/q_cake.c
tc/q_cbq.c
tc/q_fq.c
tc/q_hfsc.c
tc/q_htb.c
tc/q_mqprio.c
tc/q_netem.c
tc/q_tbf.c
tc/tc_util.c
tc/tc_util.h

index 096a999a4de4b7a64e80e40edb4de893aa45e3e5..b6c4c0c80833bb5c1a3ab3138edb985dacd2d6d1 100644 (file)
@@ -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_ */
index 62eeb1f1fb3159ee1984a9d3e0835255e12f6c6c..9ab0f86b83df88ccb9ca2d1761418082c3c83d64 100644 (file)
@@ -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;
+}
index 83b25db49f217f3b35f3baedbae219461d341ae1..64068fcc4a421522fd3b7808481252c1faa287d6 100644 (file)
@@ -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, "");
 
index bf116e80316ca4f78553fcd6aaf77bf015ef7fee..ab9233a04f3904a3d202323609db03e4141c20f2 100644 (file)
@@ -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);
index 6518ef46813ba4272185bf93902acdf7cba66146..4619a37b816027e83a69aff3c7c7c5725826759a 100644 (file)
@@ -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);
                }
index b10d01e901d880d85ac20f75d9f1af69136facec..71a513fb3c72867c69e6bc08c7d543e97c7ae870 100644 (file)
--- 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)) {
index f34b1b2fe2a98f10c5f65ac4f20b26686fcbae4f..81c10210c8842e0aae9faafc9af9900e53a42577 100644 (file)
@@ -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
index 520522266e0098837f456b6e2310e803f26aa5eb..10030a8741fa17efb316e8f871fdffcd5076a6dd 100644 (file)
@@ -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)
index 5499f621525161b18bc06688b2a018e051649670..706452d080926dc809d9d0b20cb2da172768d8ad 100644 (file)
@@ -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, "");
                }
        }
index d01450fc59dcdea09e5a6558a0ed8cc7792ebb30..d93e1c7315d286caa70df3b46f90a7ef7df08fdb 100644 (file)
@@ -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" : "",
index 5135b1d670d66eefab242af9b36656900b01706a..9d483338552135011f7ab6147a004659fe3982af 100644 (file)
@@ -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) {
index b7ff911b63edac989528ea4042ea118c9279f8a7..40efaa9a4b8aff4baaaa7d1c6a9a9ea042b679b2 100644 (file)
@@ -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);
                        }
index c8af4e952109c27b9a69f3ac9fd7ded82e40da4f..e5d533a44e1067d780fc23be682173b38066d2ed 100644 (file)
@@ -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);