From: Stephen Hemminger <sthem...@microsoft.com>

Also, fix a spelling error in usage message.

Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
---
 tc/q_netem.c | 94 +++++++++++++++++++++++++---------------------------
 1 file changed, 46 insertions(+), 48 deletions(-)

diff --git a/tc/q_netem.c b/tc/q_netem.c
index 623ec9038ad3..3844ec9adaae 100644
--- a/tc/q_netem.c
+++ b/tc/q_netem.c
@@ -39,7 +39,7 @@ static void explain(void)
 "                 [ loss state P13 [P31 [P32 [P23 P14]]]\n" \
 "                 [ loss gemodel PERCENT [R [1-H [1-K]]]\n" \
 "                 [ ecn ]\n" \
-"                 [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \
+"                 [ reorder PERCENT [CORRELATION] [ gap DISTANCE ]]\n" \
 "                 [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n");
 }
 
@@ -70,15 +70,18 @@ static int get_percent(__u32 *percent, const char *str)
        return 0;
 }
 
-static void print_percent(char *buf, int len, __u32 per)
+/*
+ * output percent in human readable format or
+ * in json as floating point (ie. .12 == 12%)
+ */
+static void print_percent(const char *key, const char *fmt, __u32 per)
 {
-       snprintf(buf, len, "%g%%", (100. * per) / UINT32_MAX);
-}
+       double percent = (double) per / UINT32_MAX;
+       SPRINT_BUF(b1);
 
-static char *sprint_percent(__u32 per, char *buf)
-{
-       print_percent(buf, SPRINT_BSIZE-1, per);
-       return buf;
+       print_float(PRINT_JSON, key, NULL, per);
+       snprintf(b1, sizeof(b1), "%g%%", 100. * percent);
+       print_string(PRINT_FP, NULL, fmt, b1);
 }
 
 /*
@@ -526,8 +529,6 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, 
struct rtattr *opt)
        int len;
        __u64 rate64 = 0;
 
-       SPRINT_BUF(b1);
-
        if (opt == NULL)
                return 0;
 
@@ -585,83 +586,80 @@ static int netem_print_opt(struct qdisc_util *qu, FILE 
*f, struct rtattr *opt)
                }
        }
 
-       fprintf(f, "limit %d", qopt.limit);
+       print_uint(PRINT_ANY, "limit", "limit %u", qopt.limit);
 
        if (qopt.latency) {
-               fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
+               print_ticks("delay", " delay %s", qopt.latency);
 
                if (qopt.jitter) {
-                       fprintf(f, "  %s", sprint_ticks(qopt.jitter, b1));
+                       print_ticks("jitter", "  %s", qopt.jitter);
                        if (cor && cor->delay_corr)
-                               fprintf(f, " %s", 
sprint_percent(cor->delay_corr, b1));
+                               print_percent("delay_corr", " %s",
+                                             cor->delay_corr);
                }
        }
 
        if (qopt.loss) {
-               fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
+               print_percent("loss", " loss %s", qopt.loss);
                if (cor && cor->loss_corr)
-                       fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
+                       print_percent("loss_corr", " %s", cor->loss_corr);
        }
 
        if (gimodel) {
-               fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, 
b1));
-               fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1));
-               fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1));
-               fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1));
-               fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1));
+               print_string(PRINT_ANY, "loss", "loss %s", "state");
+               print_percent("p13", "p13 %s", gimodel->p13);
+               print_percent("p31", " p31 %s", gimodel->p31);
+               print_percent("p32", " p32 %s", gimodel->p32);
+               print_percent("p23", " p23 %s", gimodel->p23);
+               print_percent("p14", " p14 %s", gimodel->p14);
        }
 
        if (gemodel) {
-               fprintf(f, " loss gemodel p %s",
-                       sprint_percent(gemodel->p, b1));
-               fprintf(f, " r %s", sprint_percent(gemodel->r, b1));
-               fprintf(f, " 1-h %s", sprint_percent(UINT32_MAX -
-                                                    gemodel->h, b1));
-               fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1));
+               print_string(PRINT_ANY, "loss", "loss %s", "gemodel");
+               print_percent("p", "p %s", gemodel->p);
+               print_percent("r", " r %s", gemodel->r);
+               print_percent("1_h", " 1-h %s", UINT32_MAX - gemodel->h);
+               print_percent("1_k", " 1-k %s", gemodel->k1);
        }
 
        if (qopt.duplicate) {
-               fprintf(f, " duplicate %s",
-                       sprint_percent(qopt.duplicate, b1));
+               print_percent("duplicate", " duplicate %s", qopt.duplicate);
                if (cor && cor->dup_corr)
-                       fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
+                       print_percent("dup_corr", " %s", cor->dup_corr);
        }
 
        if (reorder && reorder->probability) {
-               fprintf(f, " reorder %s",
-                       sprint_percent(reorder->probability, b1));
+               print_percent("reorder", " reorder %s", reorder->probability);
                if (reorder->correlation)
-                       fprintf(f, " %s",
-                               sprint_percent(reorder->correlation, b1));
+                       print_percent("reorder_corr", " %s",
+                                     reorder->correlation);
        }
 
        if (corrupt && corrupt->probability) {
-               fprintf(f, " corrupt %s",
-                       sprint_percent(corrupt->probability, b1));
+               print_percent("corrupt", " corrupt %s", corrupt->probability);
                if (corrupt->correlation)
-                       fprintf(f, " %s",
-                               sprint_percent(corrupt->correlation, b1));
+                       print_percent("corrupt_corr", " %s", 
corrupt->correlation);
        }
 
        if (rate && rate->rate) {
-               if (rate64)
-                       fprintf(f, " rate %s", sprint_rate(rate64, b1));
-               else
-                       fprintf(f, " rate %s", sprint_rate(rate->rate, b1));
+               print_rate("rate", "rate %s", rate64 ? : rate->rate);
+
                if (rate->packet_overhead)
-                       fprintf(f, " packetoverhead %d", rate->packet_overhead);
+                       print_uint(PRINT_ANY, "packet_overhead",
+                                  " packetoverhead %d", rate->packet_overhead);
                if (rate->cell_size)
-                       fprintf(f, " cellsize %u", rate->cell_size);
+                       print_uint(PRINT_ANY, "cell_size",
+                                  " cellsize %u", rate->cell_size);
                if (rate->cell_overhead)
-                       fprintf(f, " celloverhead %d", rate->cell_overhead);
+                       print_uint(PRINT_ANY, "cell_overhead",
+                                  " celloverhead %d", rate->cell_overhead);
        }
 
        if (ecn)
-               fprintf(f, " ecn ");
+               print_null(PRINT_ANY, "ecn", " ecn", NULL);
 
        if (qopt.gap)
-               fprintf(f, " gap %lu", (unsigned long)qopt.gap);
-
+               print_uint(PRINT_ANY, "gap", " gap %u", qopt.gap);
 
        return 0;
 }
-- 
2.18.0

Reply via email to