Debian Codegraph @

tc/q_htb.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* q_htb.c Hierarchical Token Bucket
3 *
4 * Author: Martin Devera, [email protected]
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <string.h>
15
16 #include "utils.h"
17 #include "tc_util.h"
18
19 #define HTB_TC_VER 0x30003
20 #if HTB_TC_VER >> 16 != TC_HTB_PROTOVER
21 #error "Different kernel and TC HTB versions"
22 #endif
23
24 static void explain(void)
25 {
26 fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
27 " [direct_qlen P] [offload]\n"
28 " default minor id of class to which unclassified packets are sent {0}\n"
29 " r2q DRR quantums are computed as rate in Bps/r2q {10}\n"
30 " debug string of 16 numbers each 0-3 {0}\n\n"
31 " direct_qlen Limit of the direct queue {in packets}\n"
32 " offload enable hardware offload\n"
33 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
34 " [prio P] [slot S] [pslot PS]\n"
35 " [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
36 " rate rate allocated to this class (class can still borrow)\n"
37 " burst max bytes burst which can be accumulated during idle period {computed}\n"
38 " mpu minimum packet size used in rate computations\n"
39 " overhead per-packet size overhead used in rate computations\n"
40 " linklay adapting to a linklayer e.g. atm\n"
41 " ceil definite upper class rate (no borrows) {rate}\n"
42 " cburst burst but for ceil {computed}\n"
43 " mtu max packet size we create rate map for {1600}\n"
44 " prio priority of leaf; lower are served first {0}\n"
45 " quantum how much bytes to serve from leaf at once {use r2q}\n"
46 "\nTC HTB version %d.%d\n", HTB_TC_VER>>16, HTB_TC_VER&0xffff
47 );
48 }
49
50 static void explain1(char *arg)
51 {
52 fprintf(stderr, "Illegal \"%s\"\n", arg);
53 explain();
54 }
55
56 static int htb_parse_opt(const struct qdisc_util *qu, int argc,
57 char **argv, struct nlmsghdr *n, const char *dev)
58 {
59 unsigned int direct_qlen = ~0U;
60 struct tc_htb_glob opt = {
61 .rate2quantum = 10,
62 .version = 3,
63 };
64 struct rtattr *tail;
65 unsigned int i; char *p;
66 bool offload = false;
67
68 while (argc > 0) {
69 if (matches(*argv, "r2q") == 0) {
70 NEXT_ARG();
71 if (get_u32(&opt.rate2quantum, *argv, 10)) {
72 explain1("r2q"); return -1;
73 }
74 } else if (matches(*argv, "default") == 0) {
75 NEXT_ARG();
76 if (get_u32(&opt.defcls, *argv, 16)) {
77 explain1("default"); return -1;
78 }
79 } else if (matches(*argv, "debug") == 0) {
80 NEXT_ARG(); p = *argv;
81 for (i = 0; i < 16; i++, p++) {
82 if (*p < '0' || *p > '3') break;
83 opt.debug |= (*p-'0')<<(2*i);
84 }
85 } else if (matches(*argv, "direct_qlen") == 0) {
86 NEXT_ARG();
87 if (get_u32(&direct_qlen, *argv, 10)) {
88 explain1("direct_qlen"); return -1;
89 }
90 } else if (matches(*argv, "offload") == 0) {
91 offload = true;
92 } else {
93 fprintf(stderr, "What is \"%s\"?\n", *argv);
94 explain();
95 return -1;
96 }
97 argc--; argv++;
98 }
99 tail = addattr_nest(n, 1024, TCA_OPTIONS);
100 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
101 if (direct_qlen != ~0U)
102 addattr_l(n, 2024, TCA_HTB_DIRECT_QLEN,
103 &direct_qlen, sizeof(direct_qlen));
104 if (offload)
105 addattr(n, 2024, TCA_HTB_OFFLOAD);
106 addattr_nest_end(n, tail);
107 return 0;
108 }
109
110 static int htb_parse_class_opt(const struct qdisc_util *qu, int argc, char **argv,
111 struct nlmsghdr *n, const char *dev)
112 {
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 char *param;
124
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 } 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 param = *argv;
160 NEXT_ARG();
161 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
162 explain1(param);
163 return -1;
164 }
165 } else if (matches(*argv, "cburst") == 0 ||
166 strcmp(*argv, "cbuffer") == 0 ||
167 strcmp(*argv, "cmaxburst") == 0) {
168 param = *argv;
169 NEXT_ARG();
170 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
171 explain1(param);
172 return -1;
173 }
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 } else if (strcmp(*argv, "rate") == 0) {
190 NEXT_ARG();
191 if (rate64) {
192 fprintf(stderr, "Double \"rate\" spec\n");
193 return -1;
194 }
195 if (strchr(*argv, '%')) {
196 if (get_percent_rate64(&rate64, *argv, dev)) {
197 explain1("rate");
198 return -1;
199 }
200 } else if (get_rate64(&rate64, *argv)) {
201 explain1("rate");
202 return -1;
203 }
204 } else if (strcmp(*argv, "help") == 0) {
205 explain();
206 return -1;
207 } else {
208 fprintf(stderr, "What is \"%s\"?\n", *argv);
209 explain();
210 return -1;
211 }
212 argc--; argv++;
213 }
214
215 if (!rate64) {
216 fprintf(stderr, "\"rate\" is required.\n");
217 return -1;
218 }
219 /* if ceil params are missing, use the same as rate */
220 if (!ceil64)
221 ceil64 = rate64;
222
223 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
224 opt.ceil.rate = (ceil64 >= (1ULL << 32)) ? ~0U : ceil64;
225
226 /* compute minimal allowed burst from rate; mtu is added here to make
227 sure that buffer is larger than mtu and to have some safeguard space */
228 if (!buffer)
229 buffer = rate64 / get_hz() + mtu;
230 if (!cbuffer)
231 cbuffer = ceil64 / get_hz() + mtu;
232
233 opt.ceil.overhead = overhead;
234 opt.rate.overhead = overhead;
235
236 opt.ceil.mpu = mpu;
237 opt.rate.mpu = mpu;
238
239 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
240 fprintf(stderr, "htb: failed to calculate rate table.\n");
241 return -1;
242 }
243 opt.buffer = tc_calc_xmittime(rate64, buffer);
244
245 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
246 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
247 return -1;
248 }
249 opt.cbuffer = tc_calc_xmittime(ceil64, cbuffer);
250
251 tail = addattr_nest(n, 1024, TCA_OPTIONS);
252
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
259 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);
262 addattr_nest_end(n, tail);
263 return 0;
264 }
265
266 static int htb_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
267 {
268 struct rtattr *tb[TCA_HTB_MAX + 1];
269 struct tc_htb_opt *hopt;
270 struct tc_htb_glob *gopt;
271 double buffer, cbuffer;
272 unsigned int linklayer;
273 __u64 rate64, ceil64;
274
275 SPRINT_BUF(b1);
276 SPRINT_BUF(b3);
277
278 if (opt == NULL)
279 return 0;
280
281 parse_rtattr_nested(tb, TCA_HTB_MAX, opt);
282
283 if (tb[TCA_HTB_PARMS]) {
284 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
285 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
286
287 if (!hopt->level) {
288 print_int(PRINT_ANY, "prio", "prio %d ", (int)hopt->prio);
289 if (show_details)
290 print_int(PRINT_ANY, "quantum", "quantum %d ",
291 (int)hopt->quantum);
292 }
293
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 tc_print_rate(PRINT_ANY, "rate", "rate %s ", rate64);
306 if (hopt->rate.overhead)
307 print_uint(PRINT_ANY, "overhead", "overhead %u ", hopt->rate.overhead);
308 buffer = tc_calc_xmitsize(rate64, hopt->buffer);
309
310 tc_print_rate(PRINT_ANY, "ceil", "ceil %s ", ceil64);
311 cbuffer = tc_calc_xmitsize(ceil64, hopt->cbuffer);
312 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
313 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
314 print_string(PRINT_ANY, "linklayer", "linklayer %s ",
315 sprint_linklayer(linklayer, b3));
316 if (show_details) {
317 print_size(PRINT_ANY, "burst", "burst %s/", buffer);
318 print_uint(PRINT_ANY, "burst_cell", "%u", 1<<hopt->rate.cell_log);
319 print_size(PRINT_ANY, "mpu_rate", "mpu %s ", hopt->rate.mpu);
320 print_size(PRINT_ANY, "cburst", "cburst %s/", cbuffer);
321 print_uint(PRINT_ANY, "cburst_cell", "%u", 1<<hopt->ceil.cell_log);
322 print_size(PRINT_ANY, "mpu_ceil", "mpu %s ", hopt->ceil.mpu);
323 print_int(PRINT_ANY, "level", "level %d ", (int)hopt->level);
324 } else {
325 print_size(PRINT_ANY, "burst", "burst %s ", buffer);
326 print_size(PRINT_ANY, "cburst", "cburst %s", cbuffer);
327 }
328 if (show_raw)
329 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
330 hopt->buffer, hopt->cbuffer);
331 }
332 if (tb[TCA_HTB_INIT]) {
333 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
334 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
335
336 print_int(PRINT_ANY, "r2q", "r2q %d", gopt->rate2quantum);
337 print_0xhex(PRINT_ANY, "default", " default %#llx", gopt->defcls);
338 print_uint(PRINT_ANY, "direct_packets_stat",
339 " direct_packets_stat %u", gopt->direct_pkts);
340 if (show_details) {
341 sprintf(b1, "%d.%d", gopt->version >> 16, gopt->version & 0xffff);
342 print_string(PRINT_ANY, "ver", " ver %s", b1);
343 }
344 }
345 if (tb[TCA_HTB_DIRECT_QLEN] &&
346 RTA_PAYLOAD(tb[TCA_HTB_DIRECT_QLEN]) >= sizeof(__u32)) {
347 __u32 direct_qlen = rta_getattr_u32(tb[TCA_HTB_DIRECT_QLEN]);
348
349 print_uint(PRINT_ANY, "direct_qlen", " direct_qlen %u",
350 direct_qlen);
351 }
352 if (tb[TCA_HTB_OFFLOAD])
353 print_null(PRINT_ANY, "offload", " offload", NULL);
354 return 0;
355 }
356
357 static int htb_print_xstats(const struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
358 {
359 struct tc_htb_xstats *st;
360
361 if (xstats == NULL)
362 return 0;
363
364 if (RTA_PAYLOAD(xstats) < sizeof(*st))
365 return -1;
366
367 st = RTA_DATA(xstats);
368 print_uint(PRINT_ANY, "lended", " lended: %u ", st->lends);
369 print_uint(PRINT_ANY, "borrowed", "borrowed: %u ", st->borrows);
370 print_uint(PRINT_ANY, "giants", "giants: %u", st->giants);
371 print_nl();
372 print_int(PRINT_ANY, "tokens", " tokens: %d ", st->tokens);
373 print_int(PRINT_ANY, "ctokens", "ctokens: %d", st->ctokens);
374 print_nl();
375 return 0;
376 }
377
378 struct qdisc_util htb_qdisc_util = {
379 .id = "htb",
380 .parse_qopt = htb_parse_opt,
381 .print_qopt = htb_print_opt,
382 .print_xstats = htb_print_xstats,
383 .parse_copt = htb_parse_class_opt,
384 .print_copt = htb_print_opt,
385 };