]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_htb.c
treewide: Use addattr_nest()/addattr_nest_end() to handle nested attributes
[mirror_iproute2.git] / tc / q_htb.c
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>
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
30 static 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
55 static void explain1(char *arg)
56 {
57 fprintf(stderr, "Illegal \"%s\"\n", arg);
58 explain();
59 }
60
61 static 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
110 static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
111 {
112 int ok = 0;
113 struct tc_htb_opt opt = {};
114 __u32 rtab[256], ctab[256];
115 unsigned buffer = 0, cbuffer = 0;
116 int cell_log = -1, ccell_log = -1;
117 unsigned int mtu = 1600; /* eth packet len */
118 unsigned short mpu = 0;
119 unsigned short overhead = 0;
120 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
121 struct rtattr *tail;
122 __u64 ceil64 = 0, rate64 = 0;
123
124 while (argc > 0) {
125 if (matches(*argv, "prio") == 0) {
126 NEXT_ARG();
127 if (get_u32(&opt.prio, *argv, 10)) {
128 explain1("prio"); return -1;
129 }
130 ok++;
131 } else if (matches(*argv, "mtu") == 0) {
132 NEXT_ARG();
133 if (get_u32(&mtu, *argv, 10)) {
134 explain1("mtu"); return -1;
135 }
136 } else if (matches(*argv, "mpu") == 0) {
137 NEXT_ARG();
138 if (get_u16(&mpu, *argv, 10)) {
139 explain1("mpu"); return -1;
140 }
141 } else if (matches(*argv, "overhead") == 0) {
142 NEXT_ARG();
143 if (get_u16(&overhead, *argv, 10)) {
144 explain1("overhead"); return -1;
145 }
146 } else if (matches(*argv, "linklayer") == 0) {
147 NEXT_ARG();
148 if (get_linklayer(&linklayer, *argv)) {
149 explain1("linklayer"); return -1;
150 }
151 } else if (matches(*argv, "quantum") == 0) {
152 NEXT_ARG();
153 if (get_u32(&opt.quantum, *argv, 10)) {
154 explain1("quantum"); return -1;
155 }
156 } else if (matches(*argv, "burst") == 0 ||
157 strcmp(*argv, "buffer") == 0 ||
158 strcmp(*argv, "maxburst") == 0) {
159 NEXT_ARG();
160 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
161 explain1("buffer");
162 return -1;
163 }
164 ok++;
165 } else if (matches(*argv, "cburst") == 0 ||
166 strcmp(*argv, "cbuffer") == 0 ||
167 strcmp(*argv, "cmaxburst") == 0) {
168 NEXT_ARG();
169 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
170 explain1("cbuffer");
171 return -1;
172 }
173 ok++;
174 } else if (strcmp(*argv, "ceil") == 0) {
175 NEXT_ARG();
176 if (ceil64) {
177 fprintf(stderr, "Double \"ceil\" spec\n");
178 return -1;
179 }
180 if (strchr(*argv, '%')) {
181 if (get_percent_rate64(&ceil64, *argv, dev)) {
182 explain1("ceil");
183 return -1;
184 }
185 } else if (get_rate64(&ceil64, *argv)) {
186 explain1("ceil");
187 return -1;
188 }
189 ok++;
190 } else if (strcmp(*argv, "rate") == 0) {
191 NEXT_ARG();
192 if (rate64) {
193 fprintf(stderr, "Double \"rate\" spec\n");
194 return -1;
195 }
196 if (strchr(*argv, '%')) {
197 if (get_percent_rate64(&rate64, *argv, dev)) {
198 explain1("rate");
199 return -1;
200 }
201 } else if (get_rate64(&rate64, *argv)) {
202 explain1("rate");
203 return -1;
204 }
205 ok++;
206 } else if (strcmp(*argv, "help") == 0) {
207 explain();
208 return -1;
209 } else {
210 fprintf(stderr, "What is \"%s\"?\n", *argv);
211 explain();
212 return -1;
213 }
214 argc--; argv++;
215 }
216
217 /* if (!ok)
218 return 0;*/
219
220 if (!rate64) {
221 fprintf(stderr, "\"rate\" is required.\n");
222 return -1;
223 }
224 /* if ceil params are missing, use the same as rate */
225 if (!ceil64)
226 ceil64 = rate64;
227
228 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
229 opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
230
231 /* compute minimal allowed burst from rate; mtu is added here to make
232 sute that buffer is larger than mtu and to have some safeguard space */
233 if (!buffer)
234 buffer = rate64 / get_hz() + mtu;
235 if (!cbuffer)
236 cbuffer = ceil64 / get_hz() + mtu;
237
238 opt.ceil.overhead = overhead;
239 opt.rate.overhead = overhead;
240
241 opt.ceil.mpu = mpu;
242 opt.rate.mpu = mpu;
243
244 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
245 fprintf(stderr, "htb: failed to calculate rate table.\n");
246 return -1;
247 }
248 opt.buffer = tc_calc_xmittime(rate64, buffer);
249
250 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
251 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
252 return -1;
253 }
254 opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
255
256 tail = addattr_nest(n, 1024, TCA_OPTIONS);
257
258 if (rate64 >= (1ULL << 32))
259 addattr_l(n, 1124, TCA_HTB_RATE64, &rate64, sizeof(rate64));
260
261 if (ceil64 >= (1ULL << 32))
262 addattr_l(n, 1224, TCA_HTB_CEIL64, &ceil64, sizeof(ceil64));
263
264 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
265 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
266 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
267 addattr_nest_end(n, tail);
268 return 0;
269 }
270
271 static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
272 {
273 struct rtattr *tb[TCA_HTB_MAX + 1];
274 struct tc_htb_opt *hopt;
275 struct tc_htb_glob *gopt;
276 double buffer, cbuffer;
277 unsigned int linklayer;
278 __u64 rate64, ceil64;
279
280 SPRINT_BUF(b1);
281 SPRINT_BUF(b2);
282 SPRINT_BUF(b3);
283
284 if (opt == NULL)
285 return 0;
286
287 parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
288
289 if (tb[TCA_HTB_PARMS]) {
290 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
291 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
292
293 if (!hopt->level) {
294 print_int(PRINT_ANY, "prio", "prio ", (int)hopt->prio);
295 if (show_details)
296 print_int(PRINT_ANY, "quantum", "quantum ",
297 (int)hopt->quantum);
298 }
299
300 rate64 = hopt->rate.rate;
301 if (tb[TCA_HTB_RATE64] &&
302 RTA_PAYLOAD(tb[TCA_HTB_RATE64]) >= sizeof(rate64)) {
303 rate64 = rta_getattr_u64(tb[TCA_HTB_RATE64]);
304 }
305
306 ceil64 = hopt->ceil.rate;
307 if (tb[TCA_HTB_CEIL64] &&
308 RTA_PAYLOAD(tb[TCA_HTB_CEIL64]) >= sizeof(ceil64))
309 ceil64 = rta_getattr_u64(tb[TCA_HTB_CEIL64]);
310
311 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
312 if (hopt->rate.overhead)
313 fprintf(f, "overhead %u ", hopt->rate.overhead);
314 buffer = tc_calc_xmitsize(rate64, hopt->buffer);
315
316 fprintf(f, "ceil %s ", sprint_rate(ceil64, b1));
317 cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
318 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
319 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
320 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
321 if (show_details) {
322 fprintf(f, "burst %s/%u mpu %s ",
323 sprint_size(buffer, b1),
324 1<<hopt->rate.cell_log,
325 sprint_size(hopt->rate.mpu, b2));
326 fprintf(f, "cburst %s/%u mpu %s ",
327 sprint_size(cbuffer, b1),
328 1<<hopt->ceil.cell_log,
329 sprint_size(hopt->ceil.mpu, b2));
330 fprintf(f, "level %d ", (int)hopt->level);
331 } else {
332 fprintf(f, "burst %s ", sprint_size(buffer, b1));
333 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
334 }
335 if (show_raw)
336 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
337 hopt->buffer, hopt->cbuffer);
338 }
339 if (tb[TCA_HTB_INIT]) {
340 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
341 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
342
343 print_int(PRINT_ANY, "r2q", "r2q %d", gopt->rate2quantum);
344 print_uint(PRINT_ANY, "default", " default %u", gopt->defcls);
345 print_uint(PRINT_ANY, "direct_packets_stat",
346 " direct_packets_stat %u", gopt->direct_pkts);
347 if (show_details) {
348 sprintf(b1, "%d.%d", gopt->version >> 16, gopt->version & 0xffff);
349 print_string(PRINT_ANY, "ver", " ver %s", b1);
350 }
351 }
352 if (tb[TCA_HTB_DIRECT_QLEN] &&
353 RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
354 __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
355
356 print_uint(PRINT_ANY, "direct_qlen", " direct_qlen %u",
357 direct_qlen);
358 }
359 return 0;
360 }
361
362 static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
363 {
364 struct tc_htb_xstats *st;
365
366 if (xstats == NULL)
367 return 0;
368
369 if (RTA_PAYLOAD(xstats) < sizeof(*st))
370 return -1;
371
372 st = RTA_DATA(xstats);
373 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
374 st->lends, st->borrows, st->giants);
375 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens, st->ctokens);
376 return 0;
377 }
378
379 struct qdisc_util htb_qdisc_util = {
380 .id = "htb",
381 .parse_qopt = htb_parse_opt,
382 .print_qopt = htb_print_opt,
383 .print_xstats = htb_print_xstats,
384 .parse_copt = htb_parse_class_opt,
385 .print_copt = htb_print_opt,
386 };