1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
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"
33 " [direct_qlen P]\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 " direct_qlen Limit of the direct queue {in packets}\n"
38 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
39 " [prio P] [slot S] [pslot PS]\n"
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"
43 " mpu minimum packet size used in rate computations\n"
44 " overhead per-packet size overhead used in rate computations\n"
45 " linklay adapting to a linklayer e.g. atm\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
52 );
53}
54
55static void explain1(char *arg)
56{
57 fprintf(stderr, "Illegal \"%s\"\n", arg);
58 explain();
59}
60
61static int htb_parse_opt(struct qdisc_util *qu, int argc,
62 char **argv, struct nlmsghdr *n, const char *dev)
63{
64 unsigned int direct_qlen = ~0U;
65 struct tc_htb_glob opt = {
66 .rate2quantum = 10,
67 .version = 3,
68 };
69 struct rtattr *tail;
70 unsigned int i; char *p;
71
72 while (argc > 0) {
73 if (matches(*argv, "r2q") == 0) {
74 NEXT_ARG();
75 if (get_u32(&opt.rate2quantum, *argv, 10)) {
76 explain1("r2q"); return -1;
77 }
78 } else if (matches(*argv, "default") == 0) {
79 NEXT_ARG();
80 if (get_u32(&opt.defcls, *argv, 16)) {
81 explain1("default"); return -1;
82 }
83 } else if (matches(*argv, "debug") == 0) {
84 NEXT_ARG(); p = *argv;
85 for (i = 0; i < 16; i++, p++) {
86 if (*p < '0' || *p > '3') break;
87 opt.debug |= (*p-'0')<<(2*i);
88 }
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 }
94 } else {
95 fprintf(stderr, "What is \"%s\"?\n", *argv);
96 explain();
97 return -1;
98 }
99 argc--; argv++;
100 }
101 tail = addattr_nest(n, 1024, TCA_OPTIONS);
102 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
103 if (direct_qlen != ~0U)
104 addattr_l(n, 2024, TCA_HTB_DIRECT_QLEN,
105 &direct_qlen, sizeof(direct_qlen));
106 addattr_nest_end(n, tail);
107 return 0;
108}
109
110static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
111{
112 struct tc_htb_opt opt = {};
113 __u32 rtab[256], ctab[256];
114 unsigned buffer = 0, cbuffer = 0;
115 int cell_log = -1, ccell_log = -1;
116 unsigned int mtu = 1600;
117 unsigned short mpu = 0;
118 unsigned short overhead = 0;
119 unsigned int linklayer = LINKLAYER_ETHERNET;
120 struct rtattr *tail;
121 __u64 ceil64 = 0, rate64 = 0;
122
123 while (argc > 0) {
124 if (matches(*argv, "prio") == 0) {
125 NEXT_ARG();
126 if (get_u32(&opt.prio, *argv, 10)) {
127 explain1("prio"); return -1;
128 }
129 } else if (matches(*argv, "mtu") == 0) {
130 NEXT_ARG();
131 if (get_u32(&mtu, *argv, 10)) {
132 explain1("mtu"); return -1;
133 }
134 } else if (matches(*argv, "mpu") == 0) {
135 NEXT_ARG();
136 if (get_u16(&mpu, *argv, 10)) {
137 explain1("mpu"); return -1;
138 }
139 } else if (matches(*argv, "overhead") == 0) {
140 NEXT_ARG();
141 if (get_u16(&overhead, *argv, 10)) {
142 explain1("overhead"); return -1;
143 }
144 } else if (matches(*argv, "linklayer") == 0) {
145 NEXT_ARG();
146 if (get_linklayer(&linklayer, *argv)) {
147 explain1("linklayer"); return -1;
148 }
149 } else if (matches(*argv, "quantum") == 0) {
150 NEXT_ARG();
151 if (get_u32(&opt.quantum, *argv, 10)) {
152 explain1("quantum"); return -1;
153 }
154 } else if (matches(*argv, "burst") == 0 ||
155 strcmp(*argv, "buffer") == 0 ||
156 strcmp(*argv, "maxburst") == 0) {
157 NEXT_ARG();
158 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
159 explain1("buffer");
160 return -1;
161 }
162 } else if (matches(*argv, "cburst") == 0 ||
163 strcmp(*argv, "cbuffer") == 0 ||
164 strcmp(*argv, "cmaxburst") == 0) {
165 NEXT_ARG();
166 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
167 explain1("cbuffer");
168 return -1;
169 }
170 } else if (strcmp(*argv, "ceil") == 0) {
171 NEXT_ARG();
172 if (ceil64) {
173 fprintf(stderr, "Double \"ceil\" spec\n");
174 return -1;
175 }
176 if (strchr(*argv, '%')) {
177 if (get_percent_rate64(&ceil64, *argv, dev)) {
178 explain1("ceil");
179 return -1;
180 }
181 } else if (get_rate64(&ceil64, *argv)) {
182 explain1("ceil");
183 return -1;
184 }
185 } else if (strcmp(*argv, "rate") == 0) {
186 NEXT_ARG();
187 if (rate64) {
188 fprintf(stderr, "Double \"rate\" spec\n");
189 return -1;
190 }
191 if (strchr(*argv, '%')) {
192 if (get_percent_rate64(&rate64, *argv, dev)) {
193 explain1("rate");
194 return -1;
195 }
196 } else if (get_rate64(&rate64, *argv)) {
197 explain1("rate");
198 return -1;
199 }
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
211 if (!rate64) {
212 fprintf(stderr, "\"rate\" is required.\n");
213 return -1;
214 }
215
216 if (!ceil64)
217 ceil64 = rate64;
218
219 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
220 opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
221
222
223
224 if (!buffer)
225 buffer = rate64 / get_hz() + mtu;
226 if (!cbuffer)
227 cbuffer = ceil64 / get_hz() + mtu;
228
229 opt.ceil.overhead = overhead;
230 opt.rate.overhead = overhead;
231
232 opt.ceil.mpu = mpu;
233 opt.rate.mpu = mpu;
234
235 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
236 fprintf(stderr, "htb: failed to calculate rate table.\n");
237 return -1;
238 }
239 opt.buffer = tc_calc_xmittime(rate64, buffer);
240
241 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
242 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
243 return -1;
244 }
245 opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
246
247 tail = addattr_nest(n, 1024, TCA_OPTIONS);
248
249 if (rate64 >= (1ULL << 32))
250 addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
251
252 if (ceil64 >= (1ULL << 32))
253 addattr_l(n, 1224, TCA_HTB_CEIL64, &ceil64, sizeof(ceil64));
254
255 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
256 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
257 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
258 addattr_nest_end(n, tail);
259 return 0;
260}
261
262static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
263{
264 struct rtattr *tb[TCA_HTB_MAX + 1];
265 struct tc_htb_opt *hopt;
266 struct tc_htb_glob *gopt;
267 double buffer, cbuffer;
268 unsigned int linklayer;
269 __u64 rate64, ceil64;
270
271 SPRINT_BUF(b1);
272 SPRINT_BUF(b2);
273 SPRINT_BUF(b3);
274
275 if (opt == NULL)
276 return 0;
277
278 parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
279
280 if (tb[TCA_HTB_PARMS]) {
281 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
282 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
283
284 if (!hopt->level) {
285 print_int(PRINT_ANY, "prio", "prio %d ", (int)hopt->prio);
286 if (show_details)
287 print_int(PRINT_ANY, "quantum", "quantum %d ",
288 (int)hopt->quantum);
289 }
290
291 rate64 = hopt->rate.rate;
292 if (tb[TCA_HTB_RATE64] &&
293 RTA_PAYLOAD(tb[TCA_HTB_RATE64]) >= sizeof(rate64)) {
294 rate64 = rta_getattr_u64(tb[TCA_HTB_RATE64]);
295 }
296
297 ceil64 = hopt->ceil.rate;
298 if (tb[TCA_HTB_CEIL64] &&
299 RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64))
300 ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]);
301
302 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
303 if (hopt->rate.overhead)
304 fprintf(f, "overhead %u ", hopt->rate.overhead);
305 buffer = tc_calc_xmitsize(rate64, hopt->buffer);
306
307 fprintf(f, "ceil %s ", sprint_rate(ceil64, b1));
308 cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
309 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
310 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
311 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
312 if (show_details) {
313 fprintf(f, "burst %s/%u mpu %s ",
314 sprint_size(buffer, b1),
315 1<<hopt->rate.cell_log,
316 sprint_size(hopt->rate.mpu, b2));
317 fprintf(f, "cburst %s/%u mpu %s ",
318 sprint_size(cbuffer, b1),
319 1<<hopt->ceil.cell_log,
320 sprint_size(hopt->ceil.mpu, b2));
321 fprintf(f, "level %d ", (int)hopt->level);
322 } else {
323 fprintf(f, "burst %s ", sprint_size(buffer, b1));
324 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
325 }
326 if (show_raw)
327 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
328 hopt->buffer, hopt->cbuffer);
329 }
330 if (tb[TCA_HTB_INIT]) {
331 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
332 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
333
334 print_int(PRINT_ANY, "r2q", "r2q %d", gopt->rate2quantum);
335 print_0xhex(PRINT_ANY, "default", " default %#llx", gopt->defcls);
336 print_uint(PRINT_ANY, "direct_packets_stat",
337 " direct_packets_stat %u", gopt->direct_pkts);
338 if (show_details) {
339 sprintf(b1, "%d.%d", gopt->version >> 16, gopt->version & 0xffff);
340 print_string(PRINT_ANY, "ver", " ver %s", b1);
341 }
342 }
343 if (tb[TCA_HTB_DIRECT_QLEN] &&
344 RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
345 __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
346
347 print_uint(PRINT_ANY, "direct_qlen", " direct_qlen %u",
348 direct_qlen);
349 }
350 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
357 if (xstats == NULL)
358 return 0;
359
360 if (RTA_PAYLOAD(xstats) < sizeof(*st))
361 return -1;
362
363 st = RTA_DATA(xstats);
364 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
365 st->lends, st->borrows, st->giants);
366 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens, st->ctokens);
367 return 0;
368}
369
370struct qdisc_util htb_qdisc_util = {
371 .id = "htb",
372 .parse_qopt = htb_parse_opt,
373 .print_qopt = htb_print_opt,
374 .print_xstats = htb_print_xstats,
375 .parse_copt = htb_parse_class_opt,
376 .print_copt = htb_print_opt,
377};
378