]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_htb.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_htb.c
CommitLineData
9e9d615e 1/*
2 * q_htb.c HTB.
3 *
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.
8 *
9 * Authors: Martin Devera, devik@cdi.cz
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
9e9d615e 16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21
22#include "utils.h"
23#include "tc_util.h"
24
25#define HTB_TC_VER 0x30003
26#if HTB_TC_VER >> 16 != TC_HTB_PROTOVER
27#error "Different kernel and TC HTB versions"
28#endif
29
30static void explain(void)
31{
32 fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
b43f3318 33 " [direct_qlen P]\n"
9e9d615e 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"
b43f3318 37 " direct_qlen Limit of the direct queue {in packets}\n"
a166d246
SH
38 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
39 " [prio P] [slot S] [pslot PS]\n"
9e9d615e 40 " [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
41 " rate rate allocated to this class (class can still borrow)\n"
42 " burst max bytes burst which can be accumulated during idle period {computed}\n"
a166d246
SH
43 " mpu minimum packet size used in rate computations\n"
44 " overhead per-packet size overhead used in rate computations\n"
292f29b4 45 " linklay adapting to a linklayer e.g. atm\n"
9e9d615e 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"
32a121cb 51 "\nTC HTB version %d.%d\n", HTB_TC_VER>>16, HTB_TC_VER&0xffff
9e9d615e 52 );
53}
54
55static void explain1(char *arg)
56{
57 fprintf(stderr, "Illegal \"%s\"\n", arg);
58 explain();
59}
60
61
927e3cfb 62static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
9e9d615e 63{
4d4da09e 64 unsigned int direct_qlen = ~0U;
d17b136f
PS
65 struct tc_htb_glob opt = {
66 .rate2quantum = 10,
67 .version = 3,
68 };
9e9d615e 69 struct rtattr *tail;
32a121cb
SH
70 unsigned int i; char *p;
71
9e9d615e 72 while (argc > 0) {
73 if (matches(*argv, "r2q") == 0) {
22fa92e3
SH
74 NEXT_ARG();
75 if (get_u32(&opt.rate2quantum, *argv, 10)) {
76 explain1("r2q"); return -1;
77 }
9e9d615e 78 } else if (matches(*argv, "default") == 0) {
22fa92e3
SH
79 NEXT_ARG();
80 if (get_u32(&opt.defcls, *argv, 16)) {
81 explain1("default"); return -1;
82 }
9e9d615e 83 } else if (matches(*argv, "debug") == 0) {
22fa92e3 84 NEXT_ARG(); p = *argv;
32a121cb
SH
85 for (i = 0; i < 16; i++, p++) {
86 if (*p < '0' || *p > '3') break;
22fa92e3
SH
87 opt.debug |= (*p-'0')<<(2*i);
88 }
4d4da09e
HS
89 } else if (matches(*argv, "direct_qlen") == 0) {
90 NEXT_ARG();
91 if (get_u32(&direct_qlen, *argv, 10)) {
92 explain1("direct_qlen"); return -1;
93 }
9e9d615e 94 } else {
95 fprintf(stderr, "What is \"%s\"?\n", *argv);
96 explain();
97 return -1;
98 }
99 argc--; argv++;
100 }
1b52a762 101 tail = NLMSG_TAIL(n);
9e9d615e 102 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
103 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
4d4da09e
HS
104 if (direct_qlen != ~0U)
105 addattr_l(n, 2024, TCA_HTB_DIRECT_QLEN,
106 &direct_qlen, sizeof(direct_qlen));
1b52a762 107 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
9e9d615e 108 return 0;
109}
110
927e3cfb 111static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
9e9d615e 112{
32a121cb 113 int ok = 0;
d17b136f 114 struct tc_htb_opt opt = {};
32a121cb
SH
115 __u32 rtab[256], ctab[256];
116 unsigned buffer = 0, cbuffer = 0;
117 int cell_log = -1, ccell_log = -1;
d17b136f 118 unsigned int mtu = 1600; /* eth packet len */
bccd014b
JDB
119 unsigned short mpu = 0;
120 unsigned short overhead = 0;
292f29b4 121 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
9e9d615e 122 struct rtattr *tail;
8334bb32 123 __u64 ceil64 = 0, rate64 = 0;
9e9d615e 124
9e9d615e 125 while (argc > 0) {
126 if (matches(*argv, "prio") == 0) {
127 NEXT_ARG();
128 if (get_u32(&opt.prio, *argv, 10)) {
129 explain1("prio"); return -1;
130 }
131 ok++;
132 } else if (matches(*argv, "mtu") == 0) {
133 NEXT_ARG();
134 if (get_u32(&mtu, *argv, 10)) {
135 explain1("mtu"); return -1;
136 }
a166d246
SH
137 } else if (matches(*argv, "mpu") == 0) {
138 NEXT_ARG();
bccd014b 139 if (get_u16(&mpu, *argv, 10)) {
a166d246
SH
140 explain1("mpu"); return -1;
141 }
142 } else if (matches(*argv, "overhead") == 0) {
143 NEXT_ARG();
bccd014b 144 if (get_u16(&overhead, *argv, 10)) {
a166d246
SH
145 explain1("overhead"); return -1;
146 }
292f29b4
JDB
147 } else if (matches(*argv, "linklayer") == 0) {
148 NEXT_ARG();
149 if (get_linklayer(&linklayer, *argv)) {
150 explain1("linklayer"); return -1;
151 }
9e9d615e 152 } else if (matches(*argv, "quantum") == 0) {
153 NEXT_ARG();
154 if (get_u32(&opt.quantum, *argv, 10)) {
155 explain1("quantum"); return -1;
156 }
157 } else if (matches(*argv, "burst") == 0 ||
22fa92e3
SH
158 strcmp(*argv, "buffer") == 0 ||
159 strcmp(*argv, "maxburst") == 0) {
9e9d615e 160 NEXT_ARG();
161 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
162 explain1("buffer");
163 return -1;
164 }
165 ok++;
166 } else if (matches(*argv, "cburst") == 0 ||
22fa92e3
SH
167 strcmp(*argv, "cbuffer") == 0 ||
168 strcmp(*argv, "cmaxburst") == 0) {
9e9d615e 169 NEXT_ARG();
170 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
171 explain1("cbuffer");
172 return -1;
173 }
174 ok++;
175 } else if (strcmp(*argv, "ceil") == 0) {
176 NEXT_ARG();
8334bb32 177 if (ceil64) {
9e9d615e 178 fprintf(stderr, "Double \"ceil\" spec\n");
179 return -1;
180 }
927e3cfb
ND
181 if (strchr(*argv, '%')) {
182 if (get_percent_rate64(&ceil64, *argv, dev)) {
183 explain1("ceil");
184 return -1;
185 }
186 } else if (get_rate64(&ceil64, *argv)) {
9e9d615e 187 explain1("ceil");
188 return -1;
189 }
190 ok++;
191 } else if (strcmp(*argv, "rate") == 0) {
192 NEXT_ARG();
8334bb32 193 if (rate64) {
9e9d615e 194 fprintf(stderr, "Double \"rate\" spec\n");
195 return -1;
196 }
927e3cfb
ND
197 if (strchr(*argv, '%')) {
198 if (get_percent_rate64(&rate64, *argv, dev)) {
199 explain1("rate");
200 return -1;
201 }
202 } else if (get_rate64(&rate64, *argv)) {
9e9d615e 203 explain1("rate");
204 return -1;
205 }
206 ok++;
207 } else if (strcmp(*argv, "help") == 0) {
208 explain();
209 return -1;
210 } else {
211 fprintf(stderr, "What is \"%s\"?\n", *argv);
212 explain();
213 return -1;
214 }
215 argc--; argv++;
216 }
217
22fa92e3 218 /* if (!ok)
9e9d615e 219 return 0;*/
220
8334bb32 221 if (!rate64) {
9e9d615e 222 fprintf(stderr, "\"rate\" is required.\n");
223 return -1;
224 }
225 /* if ceil params are missing, use the same as rate */
8334bb32
ED
226 if (!ceil64)
227 ceil64 = rate64;
228
229 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
230 opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
9e9d615e 231
232 /* compute minimal allowed burst from rate; mtu is added here to make
233 sute that buffer is larger than mtu and to have some safeguard space */
8334bb32
ED
234 if (!buffer)
235 buffer = rate64 / get_hz() + mtu;
236 if (!cbuffer)
237 cbuffer = ceil64 / get_hz() + mtu;
9e9d615e 238
bccd014b
JDB
239 opt.ceil.overhead = overhead;
240 opt.rate.overhead = overhead;
241
242 opt.ceil.mpu = mpu;
243 opt.rate.mpu = mpu;
a166d246 244
292f29b4 245 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
9e9d615e 246 fprintf(stderr, "htb: failed to calculate rate table.\n");
247 return -1;
248 }
8334bb32 249 opt.buffer = tc_calc_xmittime(rate64, buffer);
ae665a52 250
292f29b4 251 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
9e9d615e 252 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
253 return -1;
254 }
8334bb32 255 opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
9e9d615e 256
1b52a762 257 tail = NLMSG_TAIL(n);
9e9d615e 258 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
8334bb32
ED
259
260 if (rate64 >= (1ULL << 32))
261 addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
262
263 if (ceil64 >= (1ULL << 32))
264 addattr_l(n, 1224, TCA_HTB_CEIL64, &ceil64, sizeof(ceil64));
265
9e9d615e 266 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
267 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
268 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
1b52a762 269 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
9e9d615e 270 return 0;
271}
272
273static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
274{
b43f3318 275 struct rtattr *tb[TCA_HTB_MAX + 1];
9e9d615e 276 struct tc_htb_opt *hopt;
277 struct tc_htb_glob *gopt;
32a121cb 278 double buffer, cbuffer;
3e92ff52 279 unsigned int linklayer;
8334bb32 280 __u64 rate64, ceil64;
32a121cb 281
9e9d615e 282 SPRINT_BUF(b1);
283 SPRINT_BUF(b2);
467f9fce 284 SPRINT_BUF(b3);
9e9d615e 285
286 if (opt == NULL)
287 return 0;
288
b43f3318 289 parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
9e9d615e 290
291 if (tb[TCA_HTB_PARMS]) {
22fa92e3
SH
292 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
293 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
9e9d615e 294
295 if (!hopt->level) {
296 fprintf(f, "prio %d ", (int)hopt->prio);
297 if (show_details)
298 fprintf(f, "quantum %d ", (int)hopt->quantum);
299 }
8334bb32
ED
300
301 rate64 = hopt->rate.rate;
302 if (tb[TCA_HTB_RATE64] &&
303 RTA_PAYLOAD(tb[TCA_HTB_RATE64]) >= sizeof(rate64)) {
304 rate64 = rta_getattr_u64(tb[TCA_HTB_RATE64]);
305 }
306
307 ceil64 = hopt->ceil.rate;
308 if (tb[TCA_HTB_CEIL64] &&
309 RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64))
310 ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]);
311
312 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
22fa92e3
SH
313 if (hopt->rate.overhead)
314 fprintf(f, "overhead %u ", hopt->rate.overhead);
8334bb32
ED
315 buffer = tc_calc_xmitsize(rate64, hopt->buffer);
316
317 fprintf(f, "ceil %s ", sprint_rate(ceil64, b1));
318 cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
3e92ff52
JDB
319 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
320 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
467f9fce 321 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
22fa92e3 322 if (show_details) {
1aea7fea 323 fprintf(f, "burst %s/%u mpu %s ",
22fa92e3
SH
324 sprint_size(buffer, b1),
325 1<<hopt->rate.cell_log,
1aea7fea
DS
326 sprint_size(hopt->rate.mpu, b2));
327 fprintf(f, "cburst %s/%u mpu %s ",
22fa92e3
SH
328 sprint_size(cbuffer, b1),
329 1<<hopt->ceil.cell_log,
1aea7fea 330 sprint_size(hopt->ceil.mpu, b2));
22fa92e3
SH
331 fprintf(f, "level %d ", (int)hopt->level);
332 } else {
333 fprintf(f, "burst %s ", sprint_size(buffer, b1));
334 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
335 }
336 if (show_raw)
337 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
32a121cb 338 hopt->buffer, hopt->cbuffer);
9e9d615e 339 }
340 if (tb[TCA_HTB_INIT]) {
22fa92e3
SH
341 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
342 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
9e9d615e 343
22fa92e3 344 fprintf(f, "r2q %d default %x direct_packets_stat %u",
32a121cb 345 gopt->rate2quantum, gopt->defcls, gopt->direct_pkts);
9e9d615e 346 if (show_details)
32a121cb 347 fprintf(f, " ver %d.%d", gopt->version >> 16, gopt->version & 0xffff);
9e9d615e 348 }
b43f3318
ED
349 if (tb[TCA_HTB_DIRECT_QLEN] &&
350 RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
351 __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
352
353 fprintf(f, " direct_qlen %u", direct_qlen);
354 }
9e9d615e 355 return 0;
356}
357
358static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
359{
360 struct tc_htb_xstats *st;
32a121cb 361
9e9d615e 362 if (xstats == NULL)
363 return 0;
364
365 if (RTA_PAYLOAD(xstats) < sizeof(*st))
366 return -1;
367
368 st = RTA_DATA(xstats);
ae665a52 369 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
32a121cb
SH
370 st->lends, st->borrows, st->giants);
371 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens, st->ctokens);
9e9d615e 372 return 0;
373}
374
95812b56 375struct qdisc_util htb_qdisc_util = {
32a121cb 376 .id = "htb",
f2f99e2e
SH
377 .parse_qopt = htb_parse_opt,
378 .print_qopt = htb_print_opt,
32a121cb 379 .print_xstats = htb_print_xstats,
f2f99e2e
SH
380 .parse_copt = htb_parse_class_opt,
381 .print_copt = htb_print_opt,
9e9d615e 382};