Changes to update Tomato RAF.
[tomato.git] / release / src / router / iproute2 / tc / q_htb.c
blob957185b56584354b7fc0787b4cc31d2c216391c3
1 /*
2 * q_htb.c HTB.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Martin Devera, devik@cdi.cz
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
23 #include "utils.h"
24 #include "tc_util.h"
26 #define HTB_TC_VER 0x30003
27 #if HTB_TC_VER >> 16 != TC_HTB_PROTOVER
28 #error "Different kernel and TC HTB versions"
29 #endif
31 static void explain(void)
33 fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
34 " default minor id of class to which unclassified packets are sent {0}\n"
35 " r2q DRR quantums are computed as rate in Bps/r2q {10}\n"
36 " debug string of 16 numbers each 0-3 {0}\n\n"
37 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O] [atm]\n"
38 " [prio P] [slot S] [pslot PS]\n"
39 " [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
40 " rate rate allocated to this class (class can still borrow)\n"
41 " burst max bytes burst which can be accumulated during idle period {computed}\n"
42 " mpu minimum packet size used in rate computations\n"
43 " overhead per-packet size overhead used in rate computations\n"
45 " atm include atm cell tax in rate computations\n"
46 " ceil definite upper class rate (no borrows) {rate}\n"
47 " cburst burst but for ceil {computed}\n"
48 " mtu max packet size we create rate map for {1600}\n"
49 " prio priority of leaf; lower are served first {0}\n"
50 " quantum how much bytes to serve from leaf at once {use r2q}\n"
51 "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
55 static void explain1(char *arg)
57 fprintf(stderr, "Illegal \"%s\"\n", arg);
58 explain();
62 #define usage() return(-1)
64 static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
66 struct tc_htb_glob opt;
67 struct rtattr *tail;
68 unsigned i; char *p;
69 memset(&opt,0,sizeof(opt));
70 opt.rate2quantum = 10;
71 opt.version = 3;
73 while (argc > 0) {
74 if (matches(*argv, "r2q") == 0) {
75 NEXT_ARG();
76 if (get_u32(&opt.rate2quantum, *argv, 10)) {
77 explain1("r2q"); return -1;
79 } else if (matches(*argv, "default") == 0) {
80 NEXT_ARG();
81 if (get_u32(&opt.defcls, *argv, 16)) {
82 explain1("default"); return -1;
84 } else if (matches(*argv, "debug") == 0) {
85 NEXT_ARG(); p = *argv;
86 for (i=0; i<16; i++,p++) {
87 if (*p<'0' || *p>'3') break;
88 opt.debug |= (*p-'0')<<(2*i);
90 } else {
91 fprintf(stderr, "What is \"%s\"?\n", *argv);
92 explain();
93 return -1;
95 argc--; argv++;
97 tail = NLMSG_TAIL(n);
98 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
99 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
100 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
101 return 0;
104 static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
106 int ok=0;
107 struct tc_htb_opt opt;
108 __u32 rtab[256],ctab[256];
109 unsigned buffer=0,cbuffer=0;
110 int cell_log=-1,ccell_log = -1;
111 unsigned mtu;
112 __u8 mpu8=0;
113 __s8 overhead=0;
114 int atm=0;
115 struct rtattr *tail;
117 memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
119 while (argc > 0) {
120 if (matches(*argv, "prio") == 0) {
121 NEXT_ARG();
122 if (get_u32(&opt.prio, *argv, 10)) {
123 explain1("prio"); return -1;
125 ok++;
126 } else if (matches(*argv, "mtu") == 0) {
127 NEXT_ARG();
128 if (get_u32(&mtu, *argv, 10)) {
129 explain1("mtu"); return -1;
131 } else if (matches(*argv, "mpu") == 0) {
132 NEXT_ARG();
133 if (get_u8(&mpu8, *argv, 10)) {
134 explain1("mpu"); return -1;
136 } else if (matches(*argv, "overhead") == 0) {
137 NEXT_ARG();
138 if (get_s8(&overhead, *argv, 10)) {
139 explain1("overhead"); return -1;
141 } else if (matches(*argv, "atm") == 0) {
142 atm = 1;
143 } else if (matches(*argv, "quantum") == 0) {
144 NEXT_ARG();
145 if (get_u32(&opt.quantum, *argv, 10)) {
146 explain1("quantum"); return -1;
148 } else if (matches(*argv, "burst") == 0 ||
149 strcmp(*argv, "buffer") == 0 ||
150 strcmp(*argv, "maxburst") == 0) {
151 NEXT_ARG();
152 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
153 explain1("buffer");
154 return -1;
156 ok++;
157 } else if (matches(*argv, "cburst") == 0 ||
158 strcmp(*argv, "cbuffer") == 0 ||
159 strcmp(*argv, "cmaxburst") == 0) {
160 NEXT_ARG();
161 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
162 explain1("cbuffer");
163 return -1;
165 ok++;
166 } else if (strcmp(*argv, "ceil") == 0) {
167 NEXT_ARG();
168 if (opt.ceil.rate) {
169 fprintf(stderr, "Double \"ceil\" spec\n");
170 return -1;
172 if (get_rate(&opt.ceil.rate, *argv)) {
173 explain1("ceil");
174 return -1;
176 ok++;
177 } else if (strcmp(*argv, "rate") == 0) {
178 NEXT_ARG();
179 if (opt.rate.rate) {
180 fprintf(stderr, "Double \"rate\" spec\n");
181 return -1;
183 if (get_rate(&opt.rate.rate, *argv)) {
184 explain1("rate");
185 return -1;
187 ok++;
188 } else if (strcmp(*argv, "help") == 0) {
189 explain();
190 return -1;
191 } else {
192 fprintf(stderr, "What is \"%s\"?\n", *argv);
193 explain();
194 return -1;
196 argc--; argv++;
199 /* if (!ok)
200 return 0;*/
202 if (opt.rate.rate == 0) {
203 fprintf(stderr, "\"rate\" is required.\n");
204 return -1;
206 /* if ceil params are missing, use the same as rate */
207 if (!opt.ceil.rate) opt.ceil = opt.rate;
209 /* compute minimal allowed burst from rate; mtu is added here to make
210 sute that buffer is larger than mtu and to have some safeguard space */
211 if (!buffer) buffer = opt.rate.rate / get_hz() + mtu;
212 if (!cbuffer) cbuffer = opt.ceil.rate / get_hz() + mtu;
214 /* encode overhead and mpu, 8 bits each, into lower 16 bits */
215 tc_calc_ratespec(&opt.rate, rtab, opt.rate.rate, cell_log, mtu, mpu8, atm, overhead);
216 tc_calc_ratespec(&opt.ceil, ctab, opt.ceil.rate, cell_log, mtu, mpu8, atm, overhead);
217 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
218 opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
220 tail = NLMSG_TAIL(n);
221 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
222 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
223 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
224 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
225 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
226 return 0;
229 static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
231 struct rtattr *tb[TCA_HTB_RTAB+1];
232 struct tc_htb_opt *hopt;
233 struct tc_htb_glob *gopt;
234 double buffer,cbuffer;
235 SPRINT_BUF(b1);
236 SPRINT_BUF(b2);
237 SPRINT_BUF(b3);
239 if (opt == NULL)
240 return 0;
242 parse_rtattr_nested(tb, TCA_HTB_RTAB, opt);
244 if (tb[TCA_HTB_PARMS]) {
246 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
247 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
249 if (!hopt->level) {
250 fprintf(f, "prio %d ", (int)hopt->prio);
251 if (show_details)
252 fprintf(f, "quantum %d ", (int)hopt->quantum);
254 fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
255 buffer = ((double)hopt->rate.rate*tc_core_tick2usec(hopt->buffer))/1000000;
256 fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
257 cbuffer = ((double)hopt->ceil.rate*tc_core_tick2usec(hopt->cbuffer))/1000000;
258 if (show_details) {
259 fprintf(f, "burst %s/%u mpu %s overhead %s ",
260 sprint_size(buffer, b1),
261 1<<hopt->rate.cell_log,
262 sprint_size(hopt->rate.mpu&0xFF, b2),
263 sprint_size((__s8)(hopt->rate.mpu>>8), b3));
264 if (hopt->rate.feature & 0x0001)
265 fprintf(f, "atm ");
266 fprintf(f, "cburst %s/%u mpu %s overhead %s ",
267 sprint_size(cbuffer, b1),
268 1<<hopt->ceil.cell_log,
269 sprint_size(hopt->ceil.mpu&0xFF, b2),
270 sprint_size((__s8)(hopt->ceil.mpu>>8), b3));
271 if (hopt->ceil.feature & 0x0001)
272 fprintf(f, "atm ");
273 fprintf(f, "level %d ", (int)hopt->level);
274 } else {
275 fprintf(f, "burst %s ", sprint_size(buffer, b1));
276 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
278 if (show_raw)
279 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
280 hopt->buffer,hopt->cbuffer);
282 if (tb[TCA_HTB_INIT]) {
283 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
284 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
286 fprintf(f, "r2q %d default %x direct_packets_stat %u",
287 gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
288 if (show_details)
289 fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
291 return 0;
294 static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
296 struct tc_htb_xstats *st;
297 if (xstats == NULL)
298 return 0;
300 if (RTA_PAYLOAD(xstats) < sizeof(*st))
301 return -1;
303 st = RTA_DATA(xstats);
304 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
305 st->lends,st->borrows,st->giants);
306 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
307 return 0;
310 struct qdisc_util htb_qdisc_util = {
311 .id = "htb",
312 .parse_qopt = htb_parse_opt,
313 .print_qopt = htb_print_opt,
314 .print_xstats = htb_print_xstats,
315 .parse_copt = htb_parse_class_opt,
316 .print_copt = htb_print_opt,
319 /* for testing of old one */
320 struct qdisc_util htb2_qdisc_util = {
321 .id = "htb2",
322 .parse_qopt = htb_parse_opt,
323 .print_qopt = htb_print_opt,
324 .print_xstats = htb_print_xstats,
325 .parse_copt = htb_parse_class_opt,
326 .print_copt = htb_print_opt,