]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_htb.c
linklayer interface between kernel and tc/userspace
[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 <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
31 static void explain(void)
32 {
33 fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\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 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
38 " [prio P] [slot S] [pslot PS]\n"
39 " [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
40 " rate rate allocated to this class (class can still borrow)\n"
41 " burst max bytes burst which can be accumulated during idle period {computed}\n"
42 " mpu minimum packet size used in rate computations\n"
43 " overhead per-packet size overhead used in rate computations\n"
44 " linklay adapting to a linklayer e.g. atm\n"
45 " ceil definite upper class rate (no borrows) {rate}\n"
46 " cburst burst but for ceil {computed}\n"
47 " mtu max packet size we create rate map for {1600}\n"
48 " prio priority of leaf; lower are served first {0}\n"
49 " quantum how much bytes to serve from leaf at once {use r2q}\n"
50 "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
51 );
52 }
53
54 static void explain1(char *arg)
55 {
56 fprintf(stderr, "Illegal \"%s\"\n", arg);
57 explain();
58 }
59
60
61 static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
62 {
63 struct tc_htb_glob opt;
64 struct rtattr *tail;
65 unsigned i; char *p;
66 memset(&opt,0,sizeof(opt));
67 opt.rate2quantum = 10;
68 opt.version = 3;
69
70 while (argc > 0) {
71 if (matches(*argv, "r2q") == 0) {
72 NEXT_ARG();
73 if (get_u32(&opt.rate2quantum, *argv, 10)) {
74 explain1("r2q"); return -1;
75 }
76 } else if (matches(*argv, "default") == 0) {
77 NEXT_ARG();
78 if (get_u32(&opt.defcls, *argv, 16)) {
79 explain1("default"); return -1;
80 }
81 } else if (matches(*argv, "debug") == 0) {
82 NEXT_ARG(); p = *argv;
83 for (i=0; i<16; i++,p++) {
84 if (*p<'0' || *p>'3') break;
85 opt.debug |= (*p-'0')<<(2*i);
86 }
87 } else {
88 fprintf(stderr, "What is \"%s\"?\n", *argv);
89 explain();
90 return -1;
91 }
92 argc--; argv++;
93 }
94 tail = NLMSG_TAIL(n);
95 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
96 addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
97 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
98 return 0;
99 }
100
101 static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
102 {
103 int ok=0;
104 struct tc_htb_opt opt;
105 __u32 rtab[256],ctab[256];
106 unsigned buffer=0,cbuffer=0;
107 int cell_log=-1,ccell_log = -1;
108 unsigned mtu;
109 unsigned short mpu = 0;
110 unsigned short overhead = 0;
111 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
112 struct rtattr *tail;
113
114 memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
115
116 while (argc > 0) {
117 if (matches(*argv, "prio") == 0) {
118 NEXT_ARG();
119 if (get_u32(&opt.prio, *argv, 10)) {
120 explain1("prio"); return -1;
121 }
122 ok++;
123 } else if (matches(*argv, "mtu") == 0) {
124 NEXT_ARG();
125 if (get_u32(&mtu, *argv, 10)) {
126 explain1("mtu"); return -1;
127 }
128 } else if (matches(*argv, "mpu") == 0) {
129 NEXT_ARG();
130 if (get_u16(&mpu, *argv, 10)) {
131 explain1("mpu"); return -1;
132 }
133 } else if (matches(*argv, "overhead") == 0) {
134 NEXT_ARG();
135 if (get_u16(&overhead, *argv, 10)) {
136 explain1("overhead"); return -1;
137 }
138 } else if (matches(*argv, "linklayer") == 0) {
139 NEXT_ARG();
140 if (get_linklayer(&linklayer, *argv)) {
141 explain1("linklayer"); return -1;
142 }
143 } else if (matches(*argv, "quantum") == 0) {
144 NEXT_ARG();
145 if (get_u32(&opt.quantum, *argv, 10)) {
146 explain1("quantum"); return -1;
147 }
148 } else if (matches(*argv, "burst") == 0 ||
149 strcmp(*argv, "buffer") == 0 ||
150 strcmp(*argv, "maxburst") == 0) {
151 NEXT_ARG();
152 if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
153 explain1("buffer");
154 return -1;
155 }
156 ok++;
157 } else if (matches(*argv, "cburst") == 0 ||
158 strcmp(*argv, "cbuffer") == 0 ||
159 strcmp(*argv, "cmaxburst") == 0) {
160 NEXT_ARG();
161 if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
162 explain1("cbuffer");
163 return -1;
164 }
165 ok++;
166 } else if (strcmp(*argv, "ceil") == 0) {
167 NEXT_ARG();
168 if (opt.ceil.rate) {
169 fprintf(stderr, "Double \"ceil\" spec\n");
170 return -1;
171 }
172 if (get_rate(&opt.ceil.rate, *argv)) {
173 explain1("ceil");
174 return -1;
175 }
176 ok++;
177 } else if (strcmp(*argv, "rate") == 0) {
178 NEXT_ARG();
179 if (opt.rate.rate) {
180 fprintf(stderr, "Double \"rate\" spec\n");
181 return -1;
182 }
183 if (get_rate(&opt.rate.rate, *argv)) {
184 explain1("rate");
185 return -1;
186 }
187 ok++;
188 } else if (strcmp(*argv, "help") == 0) {
189 explain();
190 return -1;
191 } else {
192 fprintf(stderr, "What is \"%s\"?\n", *argv);
193 explain();
194 return -1;
195 }
196 argc--; argv++;
197 }
198
199 /* if (!ok)
200 return 0;*/
201
202 if (opt.rate.rate == 0) {
203 fprintf(stderr, "\"rate\" is required.\n");
204 return -1;
205 }
206 /* if ceil params are missing, use the same as rate */
207 if (!opt.ceil.rate) opt.ceil = opt.rate;
208
209 /* compute minimal allowed burst from rate; mtu is added here to make
210 sute that buffer is larger than mtu and to have some safeguard space */
211 if (!buffer) buffer = opt.rate.rate / get_hz() + mtu;
212 if (!cbuffer) cbuffer = opt.ceil.rate / get_hz() + mtu;
213
214 opt.ceil.overhead = overhead;
215 opt.rate.overhead = overhead;
216
217 opt.ceil.mpu = mpu;
218 opt.rate.mpu = mpu;
219
220 if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
221 fprintf(stderr, "htb: failed to calculate rate table.\n");
222 return -1;
223 }
224 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
225
226 if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
227 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
228 return -1;
229 }
230 opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
231
232 tail = NLMSG_TAIL(n);
233 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
234 addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
235 addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
236 addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
237 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
238 return 0;
239 }
240
241 static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
242 {
243 struct rtattr *tb[TCA_HTB_RTAB+1];
244 struct tc_htb_opt *hopt;
245 struct tc_htb_glob *gopt;
246 double buffer,cbuffer;
247 unsigned int linklayer;
248 SPRINT_BUF(b1);
249 SPRINT_BUF(b2);
250 SPRINT_BUF(b3);
251 SPRINT_BUF(b4);
252
253 if (opt == NULL)
254 return 0;
255
256 parse_rtattr_nested(tb, TCA_HTB_RTAB, opt);
257
258 if (tb[TCA_HTB_PARMS]) {
259 hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
260 if (RTA_PAYLOAD(tb[TCA_HTB_PARMS]) < sizeof(*hopt)) return -1;
261
262 if (!hopt->level) {
263 fprintf(f, "prio %d ", (int)hopt->prio);
264 if (show_details)
265 fprintf(f, "quantum %d ", (int)hopt->quantum);
266 }
267 fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
268 if (hopt->rate.overhead)
269 fprintf(f, "overhead %u ", hopt->rate.overhead);
270 buffer = tc_calc_xmitsize(hopt->rate.rate, hopt->buffer);
271 fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
272 cbuffer = tc_calc_xmitsize(hopt->ceil.rate, hopt->cbuffer);
273 linklayer = (hopt->rate.linklayer & TC_LINKLAYER_MASK);
274 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
275 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b4));
276 if (show_details) {
277 fprintf(f, "burst %s/%u mpu %s overhead %s ",
278 sprint_size(buffer, b1),
279 1<<hopt->rate.cell_log,
280 sprint_size(hopt->rate.mpu&0xFF, b2),
281 sprint_size((hopt->rate.mpu>>8)&0xFF, b3));
282 fprintf(f, "cburst %s/%u mpu %s overhead %s ",
283 sprint_size(cbuffer, b1),
284 1<<hopt->ceil.cell_log,
285 sprint_size(hopt->ceil.mpu&0xFF, b2),
286 sprint_size((hopt->ceil.mpu>>8)&0xFF, b3));
287 fprintf(f, "level %d ", (int)hopt->level);
288 } else {
289 fprintf(f, "burst %s ", sprint_size(buffer, b1));
290 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
291 }
292 if (show_raw)
293 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
294 hopt->buffer,hopt->cbuffer);
295 }
296 if (tb[TCA_HTB_INIT]) {
297 gopt = RTA_DATA(tb[TCA_HTB_INIT]);
298 if (RTA_PAYLOAD(tb[TCA_HTB_INIT]) < sizeof(*gopt)) return -1;
299
300 fprintf(f, "r2q %d default %x direct_packets_stat %u",
301 gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
302 if (show_details)
303 fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
304 }
305 return 0;
306 }
307
308 static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
309 {
310 struct tc_htb_xstats *st;
311 if (xstats == NULL)
312 return 0;
313
314 if (RTA_PAYLOAD(xstats) < sizeof(*st))
315 return -1;
316
317 st = RTA_DATA(xstats);
318 fprintf(f, " lended: %u borrowed: %u giants: %u\n",
319 st->lends,st->borrows,st->giants);
320 fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
321 return 0;
322 }
323
324 struct qdisc_util htb_qdisc_util = {
325 .id = "htb",
326 .parse_qopt = htb_parse_opt,
327 .print_qopt = htb_print_opt,
328 .print_xstats = htb_print_xstats,
329 .parse_copt = htb_parse_class_opt,
330 .print_copt = htb_print_opt,
331 };
332
333 /* for testing of old one */
334 struct qdisc_util htb2_qdisc_util = {
335 .id = "htb2",
336 .parse_qopt = htb_parse_opt,
337 .print_qopt = htb_print_opt,
338 .print_xstats = htb_print_xstats,
339 .parse_copt = htb_parse_class_opt,
340 .print_copt = htb_print_opt,
341 };