]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/q_tbf.c
Move the use_iec declaration to the tools
[mirror_iproute2.git] / tc / q_tbf.c
index f3022b65f76a9e8f1741781a7a372cd334e57789..5135b1d670d66eefab242af9b36656900b01706a 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 
 static void explain(void)
 {
-       fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
-       fprintf(stderr, "               [ peakrate KBPS ] [ latency TIME ] ");
-       fprintf(stderr, "[ overhead BYTES ] [ linklayer TYPE ]\n");
+       fprintf(stderr,
+               "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n"
+               "       [ peakrate KBPS ] [ latency TIME ] "
+               "[ overhead BYTES ] [ linklayer TYPE ]\n");
 }
 
 static void explain1(const char *arg, const char *val)
@@ -36,21 +36,20 @@ static void explain1(const char *arg, const char *val)
 }
 
 
-static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
+static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv,
+                        struct nlmsghdr *n, const char *dev)
 {
-       int ok=0;
-       struct tc_tbf_qopt opt;
+       int ok = 0;
+       struct tc_tbf_qopt opt = {};
        __u32 rtab[256];
        __u32 ptab[256];
-       unsigned buffer=0, mtu=0, mpu=0, latency=0;
-       int Rcell_log=-1, Pcell_log = -1;
-       unsigned short overhead=0;
+       unsigned buffer = 0, mtu = 0, mpu = 0, latency = 0;
+       int Rcell_log =  -1, Pcell_log = -1;
+       unsigned short overhead = 0;
        unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
        struct rtattr *tail;
        __u64 rate64 = 0, prate64 = 0;
 
-       memset(&opt, 0, sizeof(opt));
-
        while (argc > 0) {
                if (matches(*argv, "limit") == 0) {
                        NEXT_ARG();
@@ -86,6 +85,7 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                        strcmp(*argv, "buffer") == 0 ||
                        strcmp(*argv, "maxburst") == 0) {
                        const char *parm_name = *argv;
+
                        NEXT_ARG();
                        if (buffer) {
                                fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
@@ -99,6 +99,7 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                } else if (strcmp(*argv, "mtu") == 0 ||
                           strcmp(*argv, "minburst") == 0) {
                        const char *parm_name = *argv;
+
                        NEXT_ARG();
                        if (mtu) {
                                fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n");
@@ -126,7 +127,12 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                                fprintf(stderr, "tbf: duplicate \"rate\" specification\n");
                                return -1;
                        }
-                       if (get_rate64(&rate64, *argv)) {
+                       if (strchr(*argv, '%')) {
+                               if (get_percent_rate64(&rate64, *argv, dev)) {
+                                       explain1("rate", *argv);
+                                       return -1;
+                               }
+                       } else if (get_rate64(&rate64, *argv)) {
                                explain1("rate", *argv);
                                return -1;
                        }
@@ -137,7 +143,12 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                                fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n");
                                return -1;
                        }
-                       if (get_rate64(&prate64, *argv)) {
+                       if (strchr(*argv, '%')) {
+                               if (get_percent_rate64(&prate64, *argv, dev)) {
+                                       explain1("peakrate", *argv);
+                                       return -1;
+                               }
+                       } else if (get_rate64(&prate64, *argv)) {
                                explain1("peakrate", *argv);
                                return -1;
                        }
@@ -167,12 +178,12 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                argc--; argv++;
        }
 
-        int verdict = 0;
+       int verdict = 0;
 
-        /* Be nice to the user: try to emit all error messages in
-         * one go rather than reveal one more problem when a
-         * previous one has been fixed.
-         */
+       /* Be nice to the user: try to emit all error messages in
+        * one go rather than reveal one more problem when a
+        * previous one has been fixed.
+        */
        if (rate64 == 0) {
                fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n");
                verdict = -1;
@@ -193,18 +204,20 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                verdict = -1;
        }
 
-        if (verdict != 0) {
-                explain();
-                return verdict;
-        }
+       if (verdict != 0) {
+               explain();
+               return verdict;
+       }
 
        opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
        opt.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
 
        if (opt.limit == 0) {
                double lim = rate64*(double)latency/TIME_UNITS_PER_SEC + buffer;
+
                if (prate64) {
                        double lim2 = prate64*(double)latency/TIME_UNITS_PER_SEC + mtu;
+
                        if (lim2 < lim)
                                lim = lim2;
                }
@@ -229,8 +242,7 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
        }
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+       tail = addattr_nest(n, 1024, TCA_OPTIONS);
        addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
        addattr_l(n, 2124, TCA_TBF_BURST, &buffer, sizeof(buffer));
        if (rate64 >= (1ULL << 32))
@@ -242,7 +254,7 @@ static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                addattr_l(n, 3224, TCA_TBF_PBURST, &mtu, sizeof(mtu));
                addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
        }
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       addattr_nest_end(n, tail);
        return 0;
 }
 
@@ -252,8 +264,9 @@ static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        struct tc_tbf_qopt *qopt;
        unsigned int linklayer;
        double buffer, mtu;
-       double latency;
+       double latency, lat2;
        __u64 rate64 = 0, prate64 = 0;
+
        SPRINT_BUF(b1);
        SPRINT_BUF(b2);
        SPRINT_BUF(b3);
@@ -273,52 +286,79 @@ 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]);
-       fprintf(f, "rate %s ", sprint_rate(rate64, b1));
+       print_u64(PRINT_JSON, "rate", NULL, rate64);
+       print_string(PRINT_FP, NULL, "rate %s ", sprint_rate(rate64, b1));
        buffer = tc_calc_xmitsize(rate64, qopt->buffer);
        if (show_details) {
-               fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
-                       1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
+               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));
        } else {
-               fprintf(f, "burst %s ", sprint_size(buffer, b1));
+               print_u64(PRINT_JSON, "burst", NULL, buffer);
+               print_string(PRINT_FP, NULL, "burst %s ",
+                            sprint_size(buffer, b1));
        }
        if (show_raw)
-               fprintf(f, "[%08x] ", qopt->buffer);
+               print_hex(PRINT_ANY, "burst_raw", "[%08x] ", qopt->buffer);
        prate64 = qopt->peakrate.rate;
        if (tb[TCA_TBF_PRATE64] &&
            RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64))
                prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]);
        if (prate64) {
-               fprintf(f, "peakrate %s ", sprint_rate(prate64, b1));
+               print_u64(PRINT_JSON, "peakrate", NULL, prate64);
+               print_string(PRINT_FP, NULL, "peakrate %s ",
+                            sprint_rate(prate64, b1));
                if (qopt->mtu || qopt->peakrate.mpu) {
                        mtu = tc_calc_xmitsize(prate64, qopt->mtu);
                        if (show_details) {
-                               fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
-                                       1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
+                               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,
+                                          qopt->peakrate.mpu);
+                               print_string(PRINT_FP, NULL, "mpu %s ",
+                                            sprint_size(qopt->peakrate.mpu,
+                                                        b1));
                        } else {
-                               fprintf(f, "minburst %s ", sprint_size(mtu, b1));
+                               print_u64(PRINT_JSON, "minburst", NULL, mtu);
+                               print_string(PRINT_FP, NULL, "minburst %s ",
+                                            sprint_size(mtu, b1));
                        }
                        if (show_raw)
-                               fprintf(f, "[%08x] ", qopt->mtu);
+                               print_hex(PRINT_ANY, "mtu_raw", "[%08x] ",
+                                           qopt->mtu);
                }
        }
 
-       if (show_raw)
-               fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
-
-       latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)rate64) - tc_core_tick2time(qopt->buffer);
+       latency = TIME_UNITS_PER_SEC * (qopt->limit / (double)rate64) -
+                 tc_core_tick2time(qopt->buffer);
        if (prate64) {
-               double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)prate64) - tc_core_tick2time(qopt->mtu);
+               lat2 = TIME_UNITS_PER_SEC * (qopt->limit / (double)prate64) -
+                      tc_core_tick2time(qopt->mtu);
+
                if (lat2 > latency)
                        latency = lat2;
        }
-       fprintf(f, "lat %s ", sprint_time(latency, b1));
-
-       if (qopt->rate.overhead) {
-               fprintf(f, "overhead %d", qopt->rate.overhead);
+       if (latency >= 0.0) {
+               print_u64(PRINT_JSON, "lat", NULL, latency);
+               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 (qopt->rate.overhead)
+               print_int(PRINT_ANY, "overhead", "overhead %d ",
+                         qopt->rate.overhead);
        linklayer = (qopt->rate.linklayer & TC_LINKLAYER_MASK);
        if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
-               fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
+               print_string(PRINT_ANY, "linklayer", "linklayer %s ",
+                            sprint_linklayer(linklayer, b3));
 
        return 0;
 }
@@ -328,4 +368,3 @@ struct qdisc_util tbf_qdisc_util = {
        .parse_qopt     = tbf_parse_opt,
        .print_qopt     = tbf_print_opt,
 };
-