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