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
31static 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 [prio P] [slot S] [pslot PS]\n"
38		"                      [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
39		" rate     rate allocated to this class (class can still borrow)\n"
40		" burst    max bytes burst which can be accumulated during idle period {computed}\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
50static void explain1(char *arg)
51{
52    fprintf(stderr, "Illegal \"%s\"\n", arg);
53    explain();
54}
55
56
57#define usage() return(-1)
58
59static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
60{
61	struct tc_htb_glob opt;
62	struct rtattr *tail;
63	unsigned i; char *p;
64	memset(&opt,0,sizeof(opt));
65	opt.rate2quantum = 10;
66	opt.version = 3;
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 {
86			fprintf(stderr, "What is \"%s\"?\n", *argv);
87			explain();
88			return -1;
89		}
90		argc--; argv++;
91	}
92	tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
93	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
94	addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
95	tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
96	return 0;
97}
98
99static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
100{
101	int ok=0;
102	struct tc_htb_opt opt;
103	__u32 rtab[256],ctab[256];
104	unsigned buffer=0,cbuffer=0;
105	int cell_log=-1,ccell_log = -1,mtu;
106	struct rtattr *tail;
107
108	memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
109
110	while (argc > 0) {
111		if (matches(*argv, "prio") == 0) {
112			NEXT_ARG();
113			if (get_u32(&opt.prio, *argv, 10)) {
114				explain1("prio"); return -1;
115			}
116			ok++;
117		} else if (matches(*argv, "mtu") == 0) {
118			NEXT_ARG();
119			if (get_u32(&mtu, *argv, 10)) {
120				explain1("mtu"); return -1;
121			}
122		} else if (matches(*argv, "quantum") == 0) {
123			NEXT_ARG();
124			if (get_u32(&opt.quantum, *argv, 10)) {
125				explain1("quantum"); return -1;
126			}
127		} else if (matches(*argv, "burst") == 0 ||
128			strcmp(*argv, "buffer") == 0 ||
129			strcmp(*argv, "maxburst") == 0) {
130			NEXT_ARG();
131			if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
132				explain1("buffer");
133				return -1;
134			}
135			ok++;
136		} else if (matches(*argv, "cburst") == 0 ||
137			strcmp(*argv, "cbuffer") == 0 ||
138			strcmp(*argv, "cmaxburst") == 0) {
139			NEXT_ARG();
140			if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
141				explain1("cbuffer");
142				return -1;
143			}
144			ok++;
145		} else if (strcmp(*argv, "ceil") == 0) {
146			NEXT_ARG();
147			if (opt.ceil.rate) {
148				fprintf(stderr, "Double \"ceil\" spec\n");
149				return -1;
150			}
151			if (get_rate(&opt.ceil.rate, *argv)) {
152				explain1("ceil");
153				return -1;
154			}
155			ok++;
156		} else if (strcmp(*argv, "rate") == 0) {
157			NEXT_ARG();
158			if (opt.rate.rate) {
159				fprintf(stderr, "Double \"rate\" spec\n");
160				return -1;
161			}
162			if (get_rate(&opt.rate.rate, *argv)) {
163				explain1("rate");
164				return -1;
165			}
166			ok++;
167		} else if (strcmp(*argv, "help") == 0) {
168			explain();
169			return -1;
170		} else {
171			fprintf(stderr, "What is \"%s\"?\n", *argv);
172			explain();
173			return -1;
174		}
175		argc--; argv++;
176	}
177
178/*	if (!ok)
179		return 0;*/
180
181	if (opt.rate.rate == 0) {
182		fprintf(stderr, "\"rate\" is required.\n");
183		return -1;
184	}
185	/* if ceil params are missing, use the same as rate */
186	if (!opt.ceil.rate) opt.ceil = opt.rate;
187
188	/* compute minimal allowed burst from rate; mtu is added here to make
189	   sute that buffer is larger than mtu and to have some safeguard space */
190	if (!buffer) buffer = opt.rate.rate / HZ + mtu;
191	if (!cbuffer) cbuffer = opt.ceil.rate / HZ + mtu;
192
193	if ((cell_log = tc_calc_rtable(opt.rate.rate, rtab, cell_log, mtu, 0)) < 0) {
194		fprintf(stderr, "htb: failed to calculate rate table.\n");
195		return -1;
196	}
197	opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
198	opt.rate.cell_log = cell_log;
199
200	if ((ccell_log = tc_calc_rtable(opt.ceil.rate, ctab, cell_log, mtu, 0)) < 0) {
201		fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
202		return -1;
203	}
204	opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
205	opt.ceil.cell_log = ccell_log;
206
207	tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
208	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
209	addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
210	addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
211	addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
212	tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
213	return 0;
214}
215
216static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
217{
218	struct rtattr *tb[TCA_HTB_RTAB+1];
219	struct tc_htb_opt *hopt;
220	struct tc_htb_glob *gopt;
221	double buffer,cbuffer;
222	SPRINT_BUF(b1);
223	SPRINT_BUF(b2);
224
225	if (opt == NULL)
226		return 0;
227
228	memset(tb, 0, sizeof(tb));
229	parse_rtattr(tb, TCA_HTB_RTAB, RTA_DATA(opt), RTA_PAYLOAD(opt));
230
231	if (tb[TCA_HTB_PARMS]) {
232
233	    hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
234	    if (RTA_PAYLOAD(tb[TCA_HTB_PARMS])  < sizeof(*hopt)) return -1;
235
236		if (!hopt->level) {
237			fprintf(f, "prio %d ", (int)hopt->prio);
238			if (show_details)
239				fprintf(f, "quantum %d ", (int)hopt->quantum);
240		}
241	    fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
242	    buffer = ((double)hopt->rate.rate*tc_core_tick2usec(hopt->buffer))/1000000;
243	    fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
244	    cbuffer = ((double)hopt->ceil.rate*tc_core_tick2usec(hopt->cbuffer))/1000000;
245	    if (show_details) {
246		fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
247			1<<hopt->rate.cell_log, sprint_size(hopt->rate.mpu, b2));
248		fprintf(f, "cburst %s/%u mpu %s ", sprint_size(cbuffer, b1),
249			1<<hopt->ceil.cell_log, sprint_size(hopt->ceil.mpu, b2));
250		fprintf(f, "level %d ", (int)hopt->level);
251	    } else {
252		fprintf(f, "burst %s ", sprint_size(buffer, b1));
253		fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
254	    }
255	    if (show_raw)
256		fprintf(f, "buffer [%08x] cbuffer [%08x] ",
257			hopt->buffer,hopt->cbuffer);
258	}
259	if (tb[TCA_HTB_INIT]) {
260	    gopt = RTA_DATA(tb[TCA_HTB_INIT]);
261	    if (RTA_PAYLOAD(tb[TCA_HTB_INIT])  < sizeof(*gopt)) return -1;
262
263	    fprintf(f, "r2q %d default %x direct_packets_stat %u",
264		    gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
265		if (show_details)
266			fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
267	}
268	return 0;
269}
270
271static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
272{
273	struct tc_htb_xstats *st;
274	if (xstats == NULL)
275		return 0;
276
277	if (RTA_PAYLOAD(xstats) < sizeof(*st))
278		return -1;
279
280	st = RTA_DATA(xstats);
281	fprintf(f, " lended: %u borrowed: %u giants: %u\n",
282		st->lends,st->borrows,st->giants);
283	fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
284	return 0;
285}
286
287struct qdisc_util htb_util = {
288	NULL,
289	"htb",
290	htb_parse_opt,
291	htb_print_opt,
292	htb_print_xstats,
293	htb_parse_class_opt,
294	htb_print_opt,
295};
296
297/* for testing of old one */
298struct qdisc_util htb2_util = {
299	NULL,
300	"htb2",
301	htb_parse_opt,
302	htb_print_opt,
303	htb_print_xstats,
304	htb_parse_class_opt,
305	htb_print_opt,
306};
307