blob: 7075a4c00c22e5578be541c2d384282673c98fe8 [file] [log] [blame]
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +00001/*
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>
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>
22
23#include "utils.h"
24#include "tc_util.h"
25
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
30
31static void explain(void)
32{
33 fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
Eric Dumazetb43f3312013-09-13 09:10:01 -070034 " [direct_qlen P]\n"
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000035 " default minor id of class to which unclassified packets are sent {0}\n"
36 " r2q DRR quantums are computed as rate in Bps/r2q {10}\n"
37 " debug string of 16 numbers each 0-3 {0}\n\n"
Eric Dumazetb43f3312013-09-13 09:10:01 -070038 " direct_qlen Limit of the direct queue {in packets}\n"
osdl.net!shemmingera166d242004-07-30 20:26:15 +000039 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
40 " [prio P] [slot S] [pslot PS]\n"
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000041 " [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"
osdl.net!shemmingera166d242004-07-30 20:26:15 +000044 " mpu minimum packet size used in rate computations\n"
45 " overhead per-packet size overhead used in rate computations\n"
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020046 " linklay adapting to a linklayer e.g. atm\n"
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000047 " ceil definite upper class rate (no borrows) {rate}\n"
48 " cburst burst but for ceil {computed}\n"
49 " mtu max packet size we create rate map for {1600}\n"
50 " prio priority of leaf; lower are served first {0}\n"
51 " quantum how much bytes to serve from leaf at once {use r2q}\n"
52 "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
53 );
54}
55
56static void explain1(char *arg)
57{
58 fprintf(stderr, "Illegal \"%s\"\n", arg);
59 explain();
60}
61
62
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000063static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
64{
Hiroaki SHIMODA4d4da092014-03-08 22:10:03 +090065 unsigned int direct_qlen = ~0U;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000066 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;
72
73 while (argc > 0) {
74 if (matches(*argv, "r2q") == 0) {
Stephen Hemminger22fa92e2013-06-07 08:54:45 -070075 NEXT_ARG();
76 if (get_u32(&opt.rate2quantum, *argv, 10)) {
77 explain1("r2q"); return -1;
78 }
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000079 } else if (matches(*argv, "default") == 0) {
Stephen Hemminger22fa92e2013-06-07 08:54:45 -070080 NEXT_ARG();
81 if (get_u32(&opt.defcls, *argv, 16)) {
82 explain1("default"); return -1;
83 }
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000084 } else if (matches(*argv, "debug") == 0) {
Stephen Hemminger22fa92e2013-06-07 08:54:45 -070085 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);
89 }
Hiroaki SHIMODA4d4da092014-03-08 22:10:03 +090090 } else if (matches(*argv, "direct_qlen") == 0) {
91 NEXT_ARG();
92 if (get_u32(&direct_qlen, *argv, 10)) {
93 explain1("direct_qlen"); return -1;
94 }
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +000095 } else {
96 fprintf(stderr, "What is \"%s\"?\n", *argv);
97 explain();
98 return -1;
99 }
100 argc--; argv++;
101 }
6!tgraf1b52a762005-01-18 01:24:18 +0000102 tail = NLMSG_TAIL(n);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000103 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
104 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
Hiroaki SHIMODA4d4da092014-03-08 22:10:03 +0900105 if (direct_qlen != ~0U)
106 addattr_l(n, 2024, TCA_HTB_DIRECT_QLEN,
107 &direct_qlen, sizeof(direct_qlen));
6!tgraf1b52a762005-01-18 01:24:18 +0000108 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000109 return 0;
110}
111
112static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
113{
114 int ok=0;
115 struct tc_htb_opt opt;
116 __u32 rtab[256],ctab[256];
117 unsigned buffer=0,cbuffer=0;
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000118 int cell_log=-1,ccell_log = -1;
Jesper Dangaard Brouerbccd0142007-09-11 16:59:58 +0200119 unsigned mtu;
120 unsigned short mpu = 0;
121 unsigned short overhead = 0;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200122 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000123 struct rtattr *tail;
Eric Dumazet8334bb32013-11-12 14:34:07 -0800124 __u64 ceil64 = 0, rate64 = 0;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000125
126 memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
127
128 while (argc > 0) {
129 if (matches(*argv, "prio") == 0) {
130 NEXT_ARG();
131 if (get_u32(&opt.prio, *argv, 10)) {
132 explain1("prio"); return -1;
133 }
134 ok++;
135 } else if (matches(*argv, "mtu") == 0) {
136 NEXT_ARG();
137 if (get_u32(&mtu, *argv, 10)) {
138 explain1("mtu"); return -1;
139 }
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000140 } else if (matches(*argv, "mpu") == 0) {
141 NEXT_ARG();
Jesper Dangaard Brouerbccd0142007-09-11 16:59:58 +0200142 if (get_u16(&mpu, *argv, 10)) {
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000143 explain1("mpu"); return -1;
144 }
145 } else if (matches(*argv, "overhead") == 0) {
146 NEXT_ARG();
Jesper Dangaard Brouerbccd0142007-09-11 16:59:58 +0200147 if (get_u16(&overhead, *argv, 10)) {
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000148 explain1("overhead"); return -1;
149 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200150 } else if (matches(*argv, "linklayer") == 0) {
151 NEXT_ARG();
152 if (get_linklayer(&linklayer, *argv)) {
153 explain1("linklayer"); return -1;
154 }
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000155 } else if (matches(*argv, "quantum") == 0) {
156 NEXT_ARG();
157 if (get_u32(&opt.quantum, *argv, 10)) {
158 explain1("quantum"); return -1;
159 }
160 } else if (matches(*argv, "burst") == 0 ||
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700161 strcmp(*argv, "buffer") == 0 ||
162 strcmp(*argv, "maxburst") == 0) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000163 NEXT_ARG();
164 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
165 explain1("buffer");
166 return -1;
167 }
168 ok++;
169 } else if (matches(*argv, "cburst") == 0 ||
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700170 strcmp(*argv, "cbuffer") == 0 ||
171 strcmp(*argv, "cmaxburst") == 0) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000172 NEXT_ARG();
173 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
174 explain1("cbuffer");
175 return -1;
176 }
177 ok++;
178 } else if (strcmp(*argv, "ceil") == 0) {
179 NEXT_ARG();
Eric Dumazet8334bb32013-11-12 14:34:07 -0800180 if (ceil64) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000181 fprintf(stderr, "Double \"ceil\" spec\n");
182 return -1;
183 }
Eric Dumazet8334bb32013-11-12 14:34:07 -0800184 if (get_rate64(&ceil64, *argv)) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000185 explain1("ceil");
186 return -1;
187 }
188 ok++;
189 } else if (strcmp(*argv, "rate") == 0) {
190 NEXT_ARG();
Eric Dumazet8334bb32013-11-12 14:34:07 -0800191 if (rate64) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000192 fprintf(stderr, "Double \"rate\" spec\n");
193 return -1;
194 }
Eric Dumazet8334bb32013-11-12 14:34:07 -0800195 if (get_rate64(&rate64, *argv)) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000196 explain1("rate");
197 return -1;
198 }
199 ok++;
200 } else if (strcmp(*argv, "help") == 0) {
201 explain();
202 return -1;
203 } else {
204 fprintf(stderr, "What is \"%s\"?\n", *argv);
205 explain();
206 return -1;
207 }
208 argc--; argv++;
209 }
210
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700211 /* if (!ok)
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000212 return 0;*/
213
Eric Dumazet8334bb32013-11-12 14:34:07 -0800214 if (!rate64) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000215 fprintf(stderr, "\"rate\" is required.\n");
216 return -1;
217 }
218 /* if ceil params are missing, use the same as rate */
Eric Dumazet8334bb32013-11-12 14:34:07 -0800219 if (!ceil64)
220 ceil64 = rate64;
221
222 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
223 opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000224
225 /* compute minimal allowed burst from rate; mtu is added here to make
226 sute that buffer is larger than mtu and to have some safeguard space */
Eric Dumazet8334bb32013-11-12 14:34:07 -0800227 if (!buffer)
228 buffer = rate64 / get_hz() + mtu;
229 if (!cbuffer)
230 cbuffer = ceil64 / get_hz() + mtu;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000231
Jesper Dangaard Brouerbccd0142007-09-11 16:59:58 +0200232 opt.ceil.overhead = overhead;
233 opt.rate.overhead = overhead;
234
235 opt.ceil.mpu = mpu;
236 opt.rate.mpu = mpu;
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000237
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200238 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000239 fprintf(stderr, "htb: failed to calculate rate table.\n");
240 return -1;
241 }
Eric Dumazet8334bb32013-11-12 14:34:07 -0800242 opt.buffer = tc_calc_xmittime(rate64, buffer);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800243
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200244 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000245 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
246 return -1;
247 }
Eric Dumazet8334bb32013-11-12 14:34:07 -0800248 opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000249
6!tgraf1b52a762005-01-18 01:24:18 +0000250 tail = NLMSG_TAIL(n);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000251 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
Eric Dumazet8334bb32013-11-12 14:34:07 -0800252
253 if (rate64 >= (1ULL << 32))
254 addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
255
256 if (ceil64 >= (1ULL << 32))
257 addattr_l(n, 1224, TCA_HTB_CEIL64, &ceil64, sizeof(ceil64));
258
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000259 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
260 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
261 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
6!tgraf1b52a762005-01-18 01:24:18 +0000262 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000263 return 0;
264}
265
266static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
267{
Eric Dumazetb43f3312013-09-13 09:10:01 -0700268 struct rtattr *tb[TCA_HTB_MAX + 1];
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000269 struct tc_htb_opt *hopt;
270 struct tc_htb_glob *gopt;
271 double buffer,cbuffer;
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200272 unsigned int linklayer;
Eric Dumazet8334bb32013-11-12 14:34:07 -0800273 __u64 rate64, ceil64;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000274 SPRINT_BUF(b1);
275 SPRINT_BUF(b2);
osdl.net!shemmingera166d242004-07-30 20:26:15 +0000276 SPRINT_BUF(b3);
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200277 SPRINT_BUF(b4);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000278
279 if (opt == NULL)
280 return 0;
281
Eric Dumazetb43f3312013-09-13 09:10:01 -0700282 parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000283
284 if (tb[TCA_HTB_PARMS]) {
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700285 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
286 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000287
288 if (!hopt->level) {
289 fprintf(f, "prio %d ", (int)hopt->prio);
290 if (show_details)
291 fprintf(f, "quantum %d ", (int)hopt->quantum);
292 }
Eric Dumazet8334bb32013-11-12 14:34:07 -0800293
294 rate64 = hopt->rate.rate;
295 if (tb[TCA_HTB_RATE64] &&
296 RTA_PAYLOAD(tb[TCA_HTB_RATE64]) >= sizeof(rate64)) {
297 rate64 = rta_getattr_u64(tb[TCA_HTB_RATE64]);
298 }
299
300 ceil64 = hopt->ceil.rate;
301 if (tb[TCA_HTB_CEIL64] &&
302 RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64))
303 ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]);
304
305 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700306 if (hopt->rate.overhead)
307 fprintf(f, "overhead %u ", hopt->rate.overhead);
Eric Dumazet8334bb32013-11-12 14:34:07 -0800308 buffer = tc_calc_xmitsize(rate64, hopt->buffer);
309
310 fprintf(f, "ceil %s ", sprint_rate(ceil64, b1));
311 cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200312 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
313 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
314 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b4));
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700315 if (show_details) {
316 fprintf(f, "burst %s/%u mpu %s overhead %s ",
317 sprint_size(buffer, b1),
318 1<<hopt->rate.cell_log,
319 sprint_size(hopt->rate.mpu&0xFF, b2),
320 sprint_size((hopt->rate.mpu>>8)&0xFF, b3));
321 fprintf(f, "cburst %s/%u mpu %s overhead %s ",
322 sprint_size(cbuffer, b1),
323 1<<hopt->ceil.cell_log,
324 sprint_size(hopt->ceil.mpu&0xFF, b2),
325 sprint_size((hopt->ceil.mpu>>8)&0xFF, b3));
326 fprintf(f, "level %d ", (int)hopt->level);
327 } else {
328 fprintf(f, "burst %s ", sprint_size(buffer, b1));
329 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
330 }
331 if (show_raw)
332 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
333 hopt->buffer,hopt->cbuffer);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000334 }
335 if (tb[TCA_HTB_INIT]) {
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700336 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
337 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000338
Stephen Hemminger22fa92e2013-06-07 08:54:45 -0700339 fprintf(f, "r2q %d default %x direct_packets_stat %u",
340 gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000341 if (show_details)
342 fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
343 }
Eric Dumazetb43f3312013-09-13 09:10:01 -0700344 if (tb[TCA_HTB_DIRECT_QLEN] &&
345 RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
346 __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
347
348 fprintf(f, " direct_qlen %u", direct_qlen);
349 }
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000350 return 0;
351}
352
353static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
354{
355 struct tc_htb_xstats *st;
356 if (xstats == NULL)
357 return 0;
358
359 if (RTA_PAYLOAD(xstats) < sizeof(*st))
360 return -1;
361
362 st = RTA_DATA(xstats);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800363 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000364 st->lends,st->borrows,st->giants);
365 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
366 return 0;
367}
368
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000369struct qdisc_util htb_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000370 .id = "htb",
371 .parse_qopt = htb_parse_opt,
372 .print_qopt = htb_print_opt,
373 .print_xstats = htb_print_xstats,
374 .parse_copt = htb_parse_class_opt,
375 .print_copt = htb_print_opt,
org[shemminger]!shemminger9e9d6152004-06-07 22:04:51 +0000376};