]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/q_htb.c
Use C99 style initializers everywhere
[mirror_iproute2.git] / tc / q_htb.c
index 94b1be7b3c6aee890c2ca6ac5789d7e37e10c7bb..a811c2845e6740459eaddd9de262844f587717bb 100644 (file)
 static void explain(void)
 {
        fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
+               "                      [direct_qlen P]\n"
                " default  minor id of class to which unclassified packets are sent {0}\n"
                " r2q      DRR quantums are computed as rate in Bps/r2q {10}\n"
                " debug    string of 16 numbers each 0-3 {0}\n\n"
+               " direct_qlen  Limit of the direct queue {in packets}\n"
                "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
                "                      [prio P] [slot S] [pslot PS]\n"
                "                      [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
@@ -41,13 +43,13 @@ static void explain(void)
                " burst    max bytes burst which can be accumulated during idle period {computed}\n"
                " mpu      minimum packet size used in rate computations\n"
                " overhead per-packet size overhead used in rate computations\n"
-
+               " linklay  adapting to a linklayer e.g. atm\n"
                " ceil     definite upper class rate (no borrows) {rate}\n"
                " cburst   burst but for ceil {computed}\n"
                " 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
                );
 }
 
@@ -58,34 +60,38 @@ static void explain1(char *arg)
 }
 
 
-#define usage() return(-1)
-
 static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
 {
-       struct tc_htb_glob opt;
+       unsigned int direct_qlen = ~0U;
+       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) {
-                   NEXT_ARG();
-                   if (get_u32(&opt.rate2quantum, *argv, 10)) {
-                       explain1("r2q"); return -1;
-                   }
+                       NEXT_ARG();
+                       if (get_u32(&opt.rate2quantum, *argv, 10)) {
+                               explain1("r2q"); return -1;
+                       }
                } else if (matches(*argv, "default") == 0) {
-                   NEXT_ARG();
-                   if (get_u32(&opt.defcls, *argv, 16)) {
-                       explain1("default"); return -1;
-                   }
+                       NEXT_ARG();
+                       if (get_u32(&opt.defcls, *argv, 16)) {
+                               explain1("default"); return -1;
+                       }
                } else if (matches(*argv, "debug") == 0) {
-                   NEXT_ARG(); p = *argv;
-                   for (i=0; i<16; i++,p++) {
-                       if (*p<'0' || *p>'3') break;
-                       opt.debug |= (*p-'0')<<(2*i);
-                   }
+                       NEXT_ARG(); p = *argv;
+                       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) {
+                       NEXT_ARG();
+                       if (get_u32(&direct_qlen, *argv, 10)) {
+                               explain1("direct_qlen"); return -1;
+                       }
                } else {
                        fprintf(stderr, "What is \"%s\"?\n", *argv);
                        explain();
@@ -93,25 +99,29 @@ static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                }
                argc--; argv++;
        }
-       tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
+       tail = NLMSG_TAIL(n);
        addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
        addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
-       tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
+       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;
        return 0;
 }
 
 static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
 {
-       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, mpu;
-       unsigned char mpu8 = 0, overhead = 0;
+       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 int mtu = 1600; /* eth packet len */
+       unsigned short mpu = 0;
+       unsigned short overhead = 0;
+       unsigned int linklayer  = LINKLAYER_ETHERNET; /* Assume ethernet */
        struct rtattr *tail;
-
-       memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
+       __u64 ceil64 = 0, rate64 = 0;
 
        while (argc > 0) {
                if (matches(*argv, "prio") == 0) {
@@ -127,22 +137,27 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                        }
                } else if (matches(*argv, "mpu") == 0) {
                        NEXT_ARG();
-                       if (get_u8(&mpu8, *argv, 10)) {
+                       if (get_u16(&mpu, *argv, 10)) {
                                explain1("mpu"); return -1;
                        }
                } else if (matches(*argv, "overhead") == 0) {
                        NEXT_ARG();
-                       if (get_u8(&overhead, *argv, 10)) {
+                       if (get_u16(&overhead, *argv, 10)) {
                                explain1("overhead"); return -1;
                        }
+               } else if (matches(*argv, "linklayer") == 0) {
+                       NEXT_ARG();
+                       if (get_linklayer(&linklayer, *argv)) {
+                               explain1("linklayer"); return -1;
+                       }
                } else if (matches(*argv, "quantum") == 0) {
                        NEXT_ARG();
                        if (get_u32(&opt.quantum, *argv, 10)) {
                                explain1("quantum"); return -1;
                        }
                } else if (matches(*argv, "burst") == 0 ||
-                       strcmp(*argv, "buffer") == 0 ||
-                       strcmp(*argv, "maxburst") == 0) {
+                          strcmp(*argv, "buffer") == 0 ||
+                          strcmp(*argv, "maxburst") == 0) {
                        NEXT_ARG();
                        if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
                                explain1("buffer");
@@ -150,8 +165,8 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                        }
                        ok++;
                } else if (matches(*argv, "cburst") == 0 ||
-                       strcmp(*argv, "cbuffer") == 0 ||
-                       strcmp(*argv, "cmaxburst") == 0) {
+                          strcmp(*argv, "cbuffer") == 0 ||
+                          strcmp(*argv, "cmaxburst") == 0) {
                        NEXT_ARG();
                        if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
                                explain1("cbuffer");
@@ -160,22 +175,22 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                        ok++;
                } else if (strcmp(*argv, "ceil") == 0) {
                        NEXT_ARG();
-                       if (opt.ceil.rate) {
+                       if (ceil64) {
                                fprintf(stderr, "Double \"ceil\" spec\n");
                                return -1;
                        }
-                       if (get_rate(&opt.ceil.rate, *argv)) {
+                       if (get_rate64(&ceil64, *argv)) {
                                explain1("ceil");
                                return -1;
                        }
                        ok++;
                } else if (strcmp(*argv, "rate") == 0) {
                        NEXT_ARG();
-                       if (opt.rate.rate) {
+                       if (rate64) {
                                fprintf(stderr, "Double \"rate\" spec\n");
                                return -1;
                        }
-                       if (get_rate(&opt.rate.rate, *argv)) {
+                       if (get_rate64(&rate64, *argv)) {
                                explain1("rate");
                                return -1;
                        }
@@ -191,54 +206,70 @@ static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, str
                argc--; argv++;
        }
 
-/*     if (!ok)
+       /*      if (!ok)
                return 0;*/
 
-       if (opt.rate.rate == 0) {
+       if (!rate64) {
                fprintf(stderr, "\"rate\" is required.\n");
                return -1;
        }
        /* if ceil params are missing, use the same as rate */
-       if (!opt.ceil.rate) opt.ceil = opt.rate;
+       if (!ceil64)
+               ceil64 = rate64;
+
+       opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
+       opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
 
        /* compute minimal allowed burst from rate; mtu is added here to make
           sute that buffer is larger than mtu and to have some safeguard space */
-       if (!buffer) buffer = opt.rate.rate / get_hz() + mtu;
-       if (!cbuffer) cbuffer = opt.ceil.rate / get_hz() + mtu;
+       if (!buffer)
+               buffer = rate64 / get_hz() + mtu;
+       if (!cbuffer)
+               cbuffer = ceil64 / get_hz() + mtu;
+
+       opt.ceil.overhead = overhead;
+       opt.rate.overhead = overhead;
 
-/* encode overhead and mpu, 8 bits each, into lower 16 bits */
-       mpu = (unsigned)mpu8 | (unsigned)overhead << 8;
-       opt.ceil.mpu = mpu; opt.rate.mpu = mpu;
+       opt.ceil.mpu = mpu;
+       opt.rate.mpu = mpu;
 
-       if ((cell_log = tc_calc_rtable(opt.rate.rate, rtab, cell_log, mtu, mpu)) < 0) {
+       if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
                fprintf(stderr, "htb: failed to calculate rate table.\n");
                return -1;
        }
-       opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
-       opt.rate.cell_log = cell_log;
-       
-       if ((ccell_log = tc_calc_rtable(opt.ceil.rate, ctab, cell_log, mtu, mpu)) < 0) {
+       opt.buffer = tc_calc_xmittime(rate64, buffer);
+
+       if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
                fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
                return -1;
        }
-       opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
-       opt.ceil.cell_log = ccell_log;
+       opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
 
-       tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
+       tail = NLMSG_TAIL(n);
        addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+
+       if (rate64 >= (1ULL << 32))
+               addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
+
+       if (ceil64 >= (1ULL << 32))
+               addattr_l(n, 1224, TCA_HTB_CEIL64, &ceil64, sizeof(ceil64));
+
        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*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
+       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
        return 0;
 }
 
 static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-       struct rtattr *tb[TCA_HTB_RTAB+1];
+       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);
@@ -246,51 +277,71 @@ static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        if (opt == NULL)
                return 0;
 
-       memset(tb, 0, sizeof(tb));
-       parse_rtattr(tb, TCA_HTB_RTAB, RTA_DATA(opt), RTA_PAYLOAD(opt));
+       parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
 
        if (tb[TCA_HTB_PARMS]) {
-
-           hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
-           if (RTA_PAYLOAD(tb[TCA_HTB_PARMS])  < sizeof(*hopt)) return -1;
+               hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
+               if (RTA_PAYLOAD(tb[TCA_HTB_PARMS])  < sizeof(*hopt)) return -1;
 
                if (!hopt->level) {
                        fprintf(f, "prio %d ", (int)hopt->prio);
                        if (show_details)
                                fprintf(f, "quantum %d ", (int)hopt->quantum);
                }
-           fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
-           buffer = ((double)hopt->rate.rate*tc_core_tick2usec(hopt->buffer))/1000000;
-           fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
-           cbuffer = ((double)hopt->ceil.rate*tc_core_tick2usec(hopt->cbuffer))/1000000;
-           if (show_details) {
-               fprintf(f, "burst %s/%u mpu %s overhead %s ",
-                       sprint_size(buffer, b1),
-                       1<<hopt->rate.cell_log,
-                       sprint_size(hopt->rate.mpu&0xFF, b2),
-                       sprint_size((hopt->rate.mpu>>8)&0xFF, b3));
-               fprintf(f, "cburst %s/%u mpu %s overhead %s ",
-                       sprint_size(cbuffer, b1),
-                       1<<hopt->ceil.cell_log,
-                       sprint_size(hopt->ceil.mpu&0xFF, b2),
-                       sprint_size((hopt->ceil.mpu>>8)&0xFF, b3));
-               fprintf(f, "level %d ", (int)hopt->level);
-           } else {
-               fprintf(f, "burst %s ", sprint_size(buffer, b1));
-               fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
-           }
-           if (show_raw)
-               fprintf(f, "buffer [%08x] cbuffer [%08x] ", 
-                       hopt->buffer,hopt->cbuffer);
+
+               rate64 = hopt->rate.rate;
+               if (tb[TCA_HTB_RATE64] &&
+                   RTA_PAYLOAD(tb[TCA_HTB_RATE64]) >= sizeof(rate64)) {
+                       rate64 = rta_getattr_u64(tb[TCA_HTB_RATE64]);
+               }
+
+               ceil64 = hopt->ceil.rate;
+               if (tb[TCA_HTB_CEIL64] &&
+                   RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64))
+                       ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]);
+
+               fprintf(f, "rate %s ", sprint_rate(rate64, b1));
+               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));
+               cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
+               linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
+               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<<hopt->rate.cell_log,
+                               sprint_size(hopt->rate.mpu, b2));
+                       fprintf(f, "cburst %s/%u mpu %s ",
+                               sprint_size(cbuffer, b1),
+                               1<<hopt->ceil.cell_log,
+                               sprint_size(hopt->ceil.mpu, b2));
+                       fprintf(f, "level %d ", (int)hopt->level);
+               } else {
+                       fprintf(f, "burst %s ", sprint_size(buffer, b1));
+                       fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
+               }
+               if (show_raw)
+                       fprintf(f, "buffer [%08x] cbuffer [%08x] ",
+                               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;
+               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);
+               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);
+                       fprintf(f, " ver %d.%d", gopt->version >> 16, gopt->version & 0xffff);
+       }
+       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);
        }
        return 0;
 }
@@ -298,6 +349,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;
 
@@ -305,27 +357,17 @@ static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstat
                return -1;
 
        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);
+       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);
        return 0;
 }
 
-struct qdisc_util htb_util = {
-       .id             = "htb",
-       .parse_qopt     = htb_parse_opt,
-       .print_qopt     = htb_print_opt,
-       .print_xstats   = htb_print_xstats,
-       .parse_copt     = htb_parse_class_opt,
-       .print_copt     = htb_print_opt,
-};
-
-/* for testing of old one */
-struct qdisc_util htb2_util = {
-       .id             =  "htb2",
+struct qdisc_util htb_qdisc_util = {
+       .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,
 };