]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/q_htb.c
lib: introduce print_nl
[mirror_iproute2.git] / tc / q_htb.c
index 7d5409085ad2b4c2e69923bc24524af64eeab1bd..c8b2941d945b7e2cde4473920338adefe7ac48df 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>
@@ -49,7 +48,7 @@ static void explain(void)
                " mtu      max packet size we create rate map for {1600}\n"
                " prio     priority of leaf; lower are served first {0}\n"
                " quantum  how much bytes to serve from leaf at once {use r2q}\n"
-               "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
+               "\nTC HTB version %d.%d\n", HTB_TC_VER>>16, HTB_TC_VER&0xffff
                );
 }
 
@@ -59,16 +58,16 @@ static void explain1(char *arg)
     explain();
 }
 
-
-static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
+static int htb_parse_opt(struct qdisc_util *qu, int argc,
+                        char **argv, struct nlmsghdr *n, const char *dev)
 {
        unsigned int direct_qlen = ~0U;
-       struct tc_htb_glob opt;
+       struct tc_htb_glob opt = {
+               .rate2quantum = 10,
+               .version = 3,
+       };
        struct rtattr *tail;
-       unsigned i; char *p;
-       memset(&opt,0,sizeof(opt));
-       opt.rate2quantum = 10;
-       opt.version = 3;
+       unsigned int i; char *p;
 
        while (argc > 0) {
                if (matches(*argv, "r2q") == 0) {
@@ -83,8 +82,8 @@ static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                        }
                } else if (matches(*argv, "debug") == 0) {
                        NEXT_ARG(); p = *argv;
-                       for (i=0; i<16; i++,p++) {
-                               if (*p<'0' || *p>'3') break;
+                       for (i = 0; i < 16; i++, p++) {
+                               if (*p < '0' || *p > '3') break;
                                opt.debug |= (*p-'0')<<(2*i);
                        }
                } else if (matches(*argv, "direct_qlen") == 0) {
@@ -99,39 +98,34 @@ static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                }
                argc--; argv++;
        }
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+       tail = addattr_nest(n, 1024, TCA_OPTIONS);
        addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
        if (direct_qlen != ~0U)
                addattr_l(n, 2024, TCA_HTB_DIRECT_QLEN,
                          &direct_qlen, sizeof(direct_qlen));
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       addattr_nest_end(n, tail);
        return 0;
 }
 
-static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
+static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
 {
-       int ok=0;
-       struct tc_htb_opt opt;
-       __u32 rtab[256],ctab[256];
-       unsigned buffer=0,cbuffer=0;
-       int cell_log=-1,ccell_log = -1;
-       unsigned mtu;
+       struct tc_htb_opt opt = {};
+       __u32 rtab[256], ctab[256];
+       unsigned buffer = 0, cbuffer = 0;
+       int cell_log =  -1, ccell_log = -1;
+       unsigned int mtu = 1600; /* eth packet len */
        unsigned short mpu = 0;
        unsigned short overhead = 0;
        unsigned int linklayer  = LINKLAYER_ETHERNET; /* Assume ethernet */
        struct rtattr *tail;
        __u64 ceil64 = 0, rate64 = 0;
 
-       memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
-
        while (argc > 0) {
                if (matches(*argv, "prio") == 0) {
                        NEXT_ARG();
                        if (get_u32(&opt.prio, *argv, 10)) {
                                explain1("prio"); return -1;
                        }
-                       ok++;
                } else if (matches(*argv, "mtu") == 0) {
                        NEXT_ARG();
                        if (get_u32(&mtu, *argv, 10)) {
@@ -165,7 +159,6 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                                explain1("buffer");
                                return -1;
                        }
-                       ok++;
                } else if (matches(*argv, "cburst") == 0 ||
                           strcmp(*argv, "cbuffer") == 0 ||
                           strcmp(*argv, "cmaxburst") == 0) {
@@ -174,29 +167,36 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                                explain1("cbuffer");
                                return -1;
                        }
-                       ok++;
                } else if (strcmp(*argv, "ceil") == 0) {
                        NEXT_ARG();
                        if (ceil64) {
                                fprintf(stderr, "Double \"ceil\" spec\n");
                                return -1;
                        }
-                       if (get_rate64(&ceil64, *argv)) {
+                       if (strchr(*argv, '%')) {
+                               if (get_percent_rate64(&ceil64, *argv, dev)) {
+                                       explain1("ceil");
+                                       return -1;
+                               }
+                       } else if (get_rate64(&ceil64, *argv)) {
                                explain1("ceil");
                                return -1;
                        }
-                       ok++;
                } else if (strcmp(*argv, "rate") == 0) {
                        NEXT_ARG();
                        if (rate64) {
                                fprintf(stderr, "Double \"rate\" spec\n");
                                return -1;
                        }
-                       if (get_rate64(&rate64, *argv)) {
+                       if (strchr(*argv, '%')) {
+                               if (get_percent_rate64(&rate64, *argv, dev)) {
+                                       explain1("rate");
+                                       return -1;
+                               }
+                       } else if (get_rate64(&rate64, *argv)) {
                                explain1("rate");
                                return -1;
                        }
-                       ok++;
                } else if (strcmp(*argv, "help") == 0) {
                        explain();
                        return -1;
@@ -208,9 +208,6 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                argc--; argv++;
        }
 
-       /*      if (!ok)
-               return 0;*/
-
        if (!rate64) {
                fprintf(stderr, "\"rate\" is required.\n");
                return -1;
@@ -247,8 +244,7 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
        }
        opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+       tail = addattr_nest(n, 1024, TCA_OPTIONS);
 
        if (rate64 >= (1ULL << 32))
                addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
@@ -259,7 +255,7 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
        addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
        addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
        addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       addattr_nest_end(n, tail);
        return 0;
 }
 
@@ -268,9 +264,10 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        struct rtattr *tb[TCA_HTB_MAX + 1];
        struct tc_htb_opt *hopt;
        struct tc_htb_glob *gopt;
-       double buffer,cbuffer;
+       double buffer, cbuffer;
        unsigned int linklayer;
        __u64 rate64, ceil64;
+
        SPRINT_BUF(b1);
        SPRINT_BUF(b2);
        SPRINT_BUF(b3);
@@ -285,9 +282,10 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
                if (RTA_PAYLOAD(tb[TCA_HTB_PARMS])  < sizeof(*hopt)) return -1;
 
                if (!hopt->level) {
-                       fprintf(f, "prio %d ", (int)hopt->prio);
+                       print_int(PRINT_ANY, "prio", "prio %d ", (int)hopt->prio);
                        if (show_details)
-                               fprintf(f, "quantum %d ", (int)hopt->quantum);
+                               print_int(PRINT_ANY, "quantum", "quantum %d ",
+                                         (int)hopt->quantum);
                }
 
                rate64 = hopt->rate.rate;
@@ -327,22 +325,27 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
                }
                if (show_raw)
                        fprintf(f, "buffer [%08x] cbuffer [%08x] ",
-                               hopt->buffer,hopt->cbuffer);
+                               hopt->buffer, hopt->cbuffer);
        }
        if (tb[TCA_HTB_INIT]) {
                gopt = RTA_DATA(tb[TCA_HTB_INIT]);
                if (RTA_PAYLOAD(tb[TCA_HTB_INIT])  < sizeof(*gopt)) return -1;
 
-               fprintf(f, "r2q %d default %x direct_packets_stat %u",
-                       gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
-               if (show_details)
-                       fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
+               print_int(PRINT_ANY, "r2q", "r2q %d", gopt->rate2quantum);
+               print_uint(PRINT_ANY, "default", " default %u", gopt->defcls);
+               print_uint(PRINT_ANY, "direct_packets_stat",
+                          " direct_packets_stat %u", gopt->direct_pkts);
+               if (show_details) {
+                       sprintf(b1, "%d.%d", gopt->version >> 16, gopt->version & 0xffff);
+                       print_string(PRINT_ANY, "ver", " ver %s", b1);
+               }
        }
        if (tb[TCA_HTB_DIRECT_QLEN] &&
            RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
                __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
 
-               fprintf(f, " direct_qlen %u", direct_qlen);
+               print_uint(PRINT_ANY, "direct_qlen", " direct_qlen %u",
+                          direct_qlen);
        }
        return 0;
 }
@@ -350,6 +353,7 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
 {
        struct tc_htb_xstats *st;
+
        if (xstats == NULL)
                return 0;
 
@@ -358,16 +362,16 @@ static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstat
 
        st = RTA_DATA(xstats);
        fprintf(f, " lended: %u borrowed: %u giants: %u\n",
-               st->lends,st->borrows,st->giants);
-       fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
+               st->lends, st->borrows, st->giants);
+       fprintf(f, " tokens: %d ctokens: %d\n", st->tokens, st->ctokens);
        return 0;
 }
 
 struct qdisc_util htb_qdisc_util = {
-       .id             = "htb",
+       .id             = "htb",
        .parse_qopt     = htb_parse_opt,
        .print_qopt     = htb_print_opt,
-       .print_xstats   = htb_print_xstats,
+       .print_xstats   = htb_print_xstats,
        .parse_copt     = htb_parse_class_opt,
        .print_copt     = htb_print_opt,
 };