1 --- iproute2/tc/q_htb.c Sun Oct 21 22:07:29 2001
2 +++ iproute2new/tc/q_htb.c Sun May 12 22:18:27 2002
7 + * This program is free software; you can redistribute it and/or
8 + * modify it under the terms of the GNU General Public License
9 + * as published by the Free Software Foundation; either version
10 + * 2 of the License, or (at your option) any later version.
12 + * Authors: Martin Devera, devik@cdi.cz
21 +#include <sys/socket.h>
22 +#include <netinet/in.h>
23 +#include <arpa/inet.h>
29 +#define HTB_TC_VER 0x30003
30 +#if HTB_TC_VER >> 16 != TC_HTB_PROTOVER
31 +#error "Different kernel and TC HTB versions"
34 +static void explain(void)
36 + fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
37 + " default minor id of class to which unclassified packets are sent {0}\n"
38 + " r2q DRR quantums are computed as rate in Bps/r2q {10}\n"
39 + " debug string of 16 numbers each 0-3 {0}\n\n"
40 + "... class add ... htb rate R1 burst B1 [prio P] [slot S] [pslot PS]\n"
41 + " [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
42 + " rate rate allocated to this class (class can still borrow)\n"
43 + " burst max bytes burst which can be accumulated during idle period {computed}\n"
44 + " ceil definite upper class rate (no borrows) {rate}\n"
45 + " cburst burst but for ceil {computed}\n"
46 + " mtu max packet size we create rate map for {1600}\n"
47 + " prio priority of leaf; lower are served first {0}\n"
48 + " quantum how much bytes to serve from leaf at once {use r2q}\n"
49 + "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
53 +static void explain1(char *arg)
55 + fprintf(stderr, "Illegal \"%s\"\n", arg);
60 +#define usage() return(-1)
62 +static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
64 + struct tc_htb_glob opt;
65 + struct rtattr *tail;
66 + unsigned i; char *p;
67 + memset(&opt,0,sizeof(opt));
68 + opt.rate2quantum = 10;
72 + if (matches(*argv, "r2q") == 0) {
74 + if (get_u32(&opt.rate2quantum, *argv, 10)) {
75 + explain1("r2q"); return -1;
77 + } else if (matches(*argv, "default") == 0) {
79 + if (get_u32(&opt.defcls, *argv, 16)) {
80 + explain1("default"); return -1;
82 + } else if (matches(*argv, "debug") == 0) {
83 + NEXT_ARG(); p = *argv;
84 + for (i=0; i<16; i++,p++) {
85 + if (*p<'0' || *p>'3') break;
86 + opt.debug |= (*p-'0')<<(2*i);
89 + fprintf(stderr, "What is \"%s\"?\n", *argv);
95 + tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
96 + addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
97 + addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
98 + tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
102 +static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
105 + struct tc_htb_opt opt;
106 + __u32 rtab[256],ctab[256];
107 + unsigned buffer=0,cbuffer=0;
108 + int cell_log=-1,ccell_log = -1,mtu;
109 + struct rtattr *tail;
111 + memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
114 + if (matches(*argv, "prio") == 0) {
116 + if (get_u32(&opt.prio, *argv, 10)) {
117 + explain1("prio"); return -1;
120 + } else if (matches(*argv, "mtu") == 0) {
122 + if (get_u32(&mtu, *argv, 10)) {
123 + explain1("mtu"); return -1;
125 + } else if (matches(*argv, "quantum") == 0) {
127 + if (get_u32(&opt.quantum, *argv, 10)) {
128 + explain1("quantum"); return -1;
130 + } else if (matches(*argv, "burst") == 0 ||
131 + strcmp(*argv, "buffer") == 0 ||
132 + strcmp(*argv, "maxburst") == 0) {
134 + if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
135 + explain1("buffer");
139 + } else if (matches(*argv, "cburst") == 0 ||
140 + strcmp(*argv, "cbuffer") == 0 ||
141 + strcmp(*argv, "cmaxburst") == 0) {
143 + if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
144 + explain1("cbuffer");
148 + } else if (strcmp(*argv, "ceil") == 0) {
150 + if (opt.ceil.rate) {
151 + fprintf(stderr, "Double \"ceil\" spec\n");
154 + if (get_rate(&opt.ceil.rate, *argv)) {
159 + } else if (strcmp(*argv, "rate") == 0) {
161 + if (opt.rate.rate) {
162 + fprintf(stderr, "Double \"rate\" spec\n");
165 + if (get_rate(&opt.rate.rate, *argv)) {
170 + } else if (strcmp(*argv, "help") == 0) {
174 + fprintf(stderr, "What is \"%s\"?\n", *argv);
184 + if (opt.rate.rate == 0) {
185 + fprintf(stderr, "\"rate\" is required.\n");
188 + /* if ceil params are missing, use the same as rate */
189 + if (!opt.ceil.rate) opt.ceil = opt.rate;
191 + /* compute minimal allowed burst from rate; mtu is added here to make
192 + sute that buffer is larger than mtu and to have some safeguard space */
193 + if (!buffer) buffer = opt.rate.rate / HZ + mtu;
194 + if (!cbuffer) cbuffer = opt.ceil.rate / HZ + mtu;
196 + if ((cell_log = tc_calc_rtable(opt.rate.rate, rtab, cell_log, mtu, 0)) < 0) {
197 + fprintf(stderr, "htb: failed to calculate rate table.\n");
200 + opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
201 + opt.rate.cell_log = cell_log;
203 + if ((ccell_log = tc_calc_rtable(opt.ceil.rate, ctab, cell_log, mtu, 0)) < 0) {
204 + fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
207 + opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
208 + opt.ceil.cell_log = ccell_log;
210 + tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
211 + addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
212 + addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
213 + addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
214 + addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
215 + tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
219 +static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
221 + struct rtattr *tb[TCA_HTB_RTAB+1];
222 + struct tc_htb_opt *hopt;
223 + struct tc_htb_glob *gopt;
224 + double buffer,cbuffer;
231 + memset(tb, 0, sizeof(tb));
232 + parse_rtattr(tb, TCA_HTB_RTAB, RTA_DATA(opt), RTA_PAYLOAD(opt));
234 + if (tb[TCA_HTB_PARMS]) {
236 + hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
237 + if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
239 + if (!hopt->level) {
240 + fprintf(f, "prio %d ", (int)hopt->prio);
242 + fprintf(f, "quantum %d ", (int)hopt->quantum);
244 + fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
245 + buffer = ((double)hopt->rate.rate*tc_core_tick2usec(hopt->buffer))/1000000;
246 + fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
247 + cbuffer = ((double)hopt->ceil.rate*tc_core_tick2usec(hopt->cbuffer))/1000000;
248 + if (show_details) {
249 + fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
250 + 1<<hopt->rate.cell_log, sprint_size(hopt->rate.mpu, b2));
251 + fprintf(f, "cburst %s/%u mpu %s ", sprint_size(cbuffer, b1),
252 + 1<<hopt->ceil.cell_log, sprint_size(hopt->ceil.mpu, b2));
253 + fprintf(f, "level %d ", (int)hopt->level);
255 + fprintf(f, "burst %s ", sprint_size(buffer, b1));
256 + fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
259 + fprintf(f, "buffer [%08x] cbuffer [%08x] ",
260 + hopt->buffer,hopt->cbuffer);
262 + if (tb[TCA_HTB_INIT]) {
263 + gopt = RTA_DATA(tb[TCA_HTB_INIT]);
264 + if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
266 + fprintf(f, "r2q %d default %x direct_packets_stat %u",
267 + gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
269 + fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
274 +static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
276 + struct tc_htb_xstats *st;
277 + if (xstats == NULL)
280 + if (RTA_PAYLOAD(xstats) < sizeof(*st))
283 + st = RTA_DATA(xstats);
284 + fprintf(f, " lended: %u borrowed: %u giants: %u\n",
285 + st->lends,st->borrows,st->giants);
286 + fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
290 +struct qdisc_util htb_util = {
296 + htb_parse_class_opt,
300 +/* for testing of old one */
301 +struct qdisc_util htb2_util = {
307 + htb_parse_class_opt,
310 --- iproute2/tc/Makefile Tue Jul 6 18:13:07 1999
311 +++ iproute2new/tc/Makefile Thu May 9 12:34:19 2002
312 @@ -21,6 +21,7 @@ ifeq ($(TC_CONFIG_DIFFSERV),y)
315 #TCMODULES += q_csz.o
316 +TCMODULES += q_htb.o
317 #TCMODULES += q_hpfq.o
318 #TCMODULES += q_hfsc.o