1/*
2 * q_hfsc.c	HFSC.
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:	Patrick McHardy, <kaber@trash.net>
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#include <math.h>
23
24#include "utils.h"
25#include "tc_util.h"
26
27static int hfsc_get_sc(int *, char ***, struct tc_service_curve *);
28
29
30static void
31explain_qdisc(void)
32{
33	fprintf(stderr,
34		"Usage: ... hfsc [ default CLASSID ]\n"
35		"\n"
36		" default: default class for unclassified packets\n"
37	);
38}
39
40static void
41explain_class(void)
42{
43	fprintf(stderr,
44		"Usage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ]\n"
45		"\n"
46		"SC := [ [ m1 BPS ] d SEC ] m2 BPS\n"
47		"\n"
48		" m1 : slope of first segment\n"
49		" d  : x-coordinate of intersection\n"
50		" m2 : slope of second segment\n"
51		"\n"
52		"Alternative format:\n"
53		"\n"
54		"SC := [ [ umax BYTE ] dmax SEC ] rate BPS\n"
55		"\n"
56		" umax : maximum unit of work\n"
57		" dmax : maximum delay\n"
58		" rate : rate\n"
59		"\n"
60		"Remarks:\n"
61		" - at least one of 'rt', 'ls' or 'sc' must be specified\n"
62		" - 'ul' can only be specified with 'ls' or 'sc'\n"
63		"\n"
64	);
65}
66
67static void
68explain1(char *arg)
69{
70	fprintf(stderr, "HFSC: Illegal \"%s\"\n", arg);
71}
72
73static int
74hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
75{
76	struct tc_hfsc_qopt qopt;
77
78	memset(&qopt, 0, sizeof(qopt));
79
80	while (argc > 0) {
81		if (matches(*argv, "default") == 0) {
82			NEXT_ARG();
83			if (qopt.defcls != 0) {
84				fprintf(stderr, "HFSC: Double \"default\"\n");
85				return -1;
86			}
87			if (get_u16(&qopt.defcls, *argv, 16) < 0) {
88				explain1("default");
89				return -1;
90			}
91		} else if (matches(*argv, "help") == 0) {
92			explain_qdisc();
93			return -1;
94		} else {
95			fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
96			explain_qdisc();
97			return -1;
98		}
99		argc--, argv++;
100	}
101
102	addattr_l(n, 1024, TCA_OPTIONS, &qopt, sizeof(qopt));
103	return 0;
104}
105
106static int
107hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
108{
109	struct tc_hfsc_qopt *qopt;
110
111	if (opt == NULL)
112		return 0;
113	if (RTA_PAYLOAD(opt) < sizeof(*qopt))
114		return -1;
115	qopt = RTA_DATA(opt);
116
117	if (qopt->defcls != 0)
118		fprintf(f, "default %x ", qopt->defcls);
119
120	return 0;
121}
122
123static int
124hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
125{
126	struct tc_hfsc_stats *st;
127
128	if (xstats == NULL)
129		return 0;
130	if (RTA_PAYLOAD(xstats) < sizeof(*st))
131		return -1;
132	st = RTA_DATA(xstats);
133
134	fprintf(f, " period %u ", st->period);
135	if (st->work != 0)
136		fprintf(f, "work %llu bytes ", (unsigned long long) st->work);
137	if (st->rtwork != 0)
138		fprintf(f, "rtwork %llu bytes ", (unsigned long long) st->rtwork);
139	fprintf(f, "level %u ", st->level);
140	fprintf(f, "\n");
141
142	return 0;
143}
144
145static int
146hfsc_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
147                     struct nlmsghdr *n)
148{
149	struct tc_service_curve rsc, fsc, usc;
150	int rsc_ok, fsc_ok, usc_ok;
151	struct rtattr *tail;
152
153	memset(&rsc, 0, sizeof(rsc));
154	memset(&fsc, 0, sizeof(fsc));
155	memset(&usc, 0, sizeof(usc));
156	rsc_ok = fsc_ok = usc_ok = 0;
157
158	while (argc > 0) {
159		if (matches(*argv, "rt") == 0) {
160			NEXT_ARG();
161			if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
162				explain1("rt");
163				return -1;
164			}
165			rsc_ok = 1;
166		} else if (matches(*argv, "ls") == 0) {
167			NEXT_ARG();
168			if (hfsc_get_sc(&argc, &argv, &fsc) < 0) {
169				explain1("ls");
170				return -1;
171			}
172			fsc_ok = 1;
173		} else if (matches(*argv, "sc") == 0) {
174			NEXT_ARG();
175			if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
176				explain1("sc");
177				return -1;
178			}
179			memcpy(&fsc, &rsc, sizeof(fsc));
180			rsc_ok = 1;
181			fsc_ok = 1;
182		} else if (matches(*argv, "ul") == 0) {
183			NEXT_ARG();
184			if (hfsc_get_sc(&argc, &argv, &usc) < 0) {
185				explain1("ul");
186				return -1;
187			}
188			usc_ok = 1;
189		} else if (matches(*argv, "help") == 0) {
190			explain_class();
191			return -1;
192		} else {
193			fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
194			explain_class();
195			return -1;
196		}
197		argc--, argv++;
198	}
199
200	if (!(rsc_ok || fsc_ok || usc_ok)) {
201		fprintf(stderr, "HFSC: no parameters given\n");
202		explain_class();
203		return -1;
204	}
205	if (usc_ok && !fsc_ok) {
206		fprintf(stderr, "HFSC: Upper-limit Service Curve without "
207		                "Link-Share Service Curve\n");
208		explain_class();
209		return -1;
210	}
211
212	tail = NLMSG_TAIL(n);
213
214	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
215	if (rsc_ok)
216		addattr_l(n, 1024, TCA_HFSC_RSC, &rsc, sizeof(rsc));
217	if (fsc_ok)
218		addattr_l(n, 1024, TCA_HFSC_FSC, &fsc, sizeof(fsc));
219	if (usc_ok)
220		addattr_l(n, 1024, TCA_HFSC_USC, &usc, sizeof(usc));
221
222	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
223	return 0;
224}
225
226static void
227hfsc_print_sc(FILE *f, char *name, struct tc_service_curve *sc)
228{
229	SPRINT_BUF(b1);
230
231	fprintf(f, "%s ", name);
232	fprintf(f, "m1 %s ", sprint_rate(sc->m1, b1));
233	fprintf(f, "d %s ", sprint_time(tc_core_ktime2time(sc->d), b1));
234	fprintf(f, "m2 %s ", sprint_rate(sc->m2, b1));
235}
236
237static int
238hfsc_print_class_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
239{
240	struct rtattr *tb[TCA_HFSC_MAX+1];
241	struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
242
243	if (opt == NULL)
244		return 0;
245
246	parse_rtattr_nested(tb, TCA_HFSC_MAX, opt);
247
248	if (tb[TCA_HFSC_RSC]) {
249		if (RTA_PAYLOAD(tb[TCA_HFSC_RSC]) < sizeof(*rsc))
250			fprintf(stderr, "HFSC: truncated realtime option\n");
251		else
252			rsc = RTA_DATA(tb[TCA_HFSC_RSC]);
253	}
254	if (tb[TCA_HFSC_FSC]) {
255		if (RTA_PAYLOAD(tb[TCA_HFSC_FSC]) < sizeof(*fsc))
256			fprintf(stderr, "HFSC: truncated linkshare option\n");
257		else
258			fsc = RTA_DATA(tb[TCA_HFSC_FSC]);
259	}
260	if (tb[TCA_HFSC_USC]) {
261		if (RTA_PAYLOAD(tb[TCA_HFSC_USC]) < sizeof(*usc))
262			fprintf(stderr, "HFSC: truncated upperlimit option\n");
263		else
264			usc = RTA_DATA(tb[TCA_HFSC_USC]);
265	}
266
267
268	if (rsc != NULL && fsc != NULL &&
269	    memcmp(rsc, fsc, sizeof(*rsc)) == 0)
270		hfsc_print_sc(f, "sc", rsc);
271	else {
272		if (rsc != NULL)
273			hfsc_print_sc(f, "rt", rsc);
274		if (fsc != NULL)
275			hfsc_print_sc(f, "ls", fsc);
276	}
277	if (usc != NULL)
278		hfsc_print_sc(f, "ul", usc);
279
280	return 0;
281}
282
283struct qdisc_util hfsc_qdisc_util = {
284	.id		= "hfsc",
285	.parse_qopt	= hfsc_parse_opt,
286	.print_qopt	= hfsc_print_opt,
287	.print_xstats	= hfsc_print_xstats,
288	.parse_copt	= hfsc_parse_class_opt,
289	.print_copt	= hfsc_print_class_opt,
290};
291
292static int
293hfsc_get_sc1(int *argcp, char ***argvp, struct tc_service_curve *sc)
294{
295	char **argv = *argvp;
296	int argc = *argcp;
297	unsigned int m1 = 0, d = 0, m2 = 0;
298
299	if (matches(*argv, "m1") == 0) {
300		NEXT_ARG();
301		if (get_rate(&m1, *argv) < 0) {
302			explain1("m1");
303			return -1;
304		}
305		NEXT_ARG();
306	}
307
308	if (matches(*argv, "d") == 0) {
309		NEXT_ARG();
310		if (get_time(&d, *argv) < 0) {
311			explain1("d");
312			return -1;
313		}
314		NEXT_ARG();
315	}
316
317	if (matches(*argv, "m2") == 0) {
318		NEXT_ARG();
319		if (get_rate(&m2, *argv) < 0) {
320			explain1("m2");
321			return -1;
322		}
323	} else
324		return -1;
325
326	sc->m1 = m1;
327	sc->d  = tc_core_time2ktime(d);
328	sc->m2 = m2;
329
330	*argvp = argv;
331	*argcp = argc;
332	return 0;
333}
334
335static int
336hfsc_get_sc2(int *argcp, char ***argvp, struct tc_service_curve *sc)
337{
338	char **argv = *argvp;
339	int argc = *argcp;
340	unsigned int umax = 0, dmax = 0, rate = 0;
341
342	if (matches(*argv, "umax") == 0) {
343		NEXT_ARG();
344		if (get_size(&umax, *argv) < 0) {
345			explain1("umax");
346			return -1;
347		}
348		NEXT_ARG();
349	}
350
351	if (matches(*argv, "dmax") == 0) {
352		NEXT_ARG();
353		if (get_time(&dmax, *argv) < 0) {
354			explain1("dmax");
355			return -1;
356		}
357		NEXT_ARG();
358	}
359
360	if (matches(*argv, "rate") == 0) {
361		NEXT_ARG();
362		if (get_rate(&rate, *argv) < 0) {
363			explain1("rate");
364			return -1;
365		}
366	} else
367		return -1;
368
369	if (umax != 0 && dmax == 0) {
370		fprintf(stderr, "HFSC: umax given but dmax is zero.\n");
371		return -1;
372	}
373
374	if (dmax != 0 && ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax) > rate) {
375		/*
376		 * concave curve, slope of first segment is umax/dmax,
377		 * intersection is at dmax
378		 */
379		sc->m1 = ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax); /* in bps */
380		sc->d  = tc_core_time2ktime(dmax);
381		sc->m2 = rate;
382	} else {
383		/*
384		 * convex curve, slope of first segment is 0, intersection
385		 * is at dmax - umax / rate
386		 */
387		sc->m1 = 0;
388		sc->d  = tc_core_time2ktime(ceil(dmax - umax * TIME_UNITS_PER_SEC / rate));
389		sc->m2 = rate;
390	}
391
392	*argvp = argv;
393	*argcp = argc;
394	return 0;
395}
396
397static int
398hfsc_get_sc(int *argcp, char ***argvp, struct tc_service_curve *sc)
399{
400	if (hfsc_get_sc1(argcp, argvp, sc) < 0 &&
401	    hfsc_get_sc2(argcp, argvp, sc) < 0)
402		return -1;
403
404	if (sc->m1 == 0 && sc->m2 == 0) {
405		fprintf(stderr, "HFSC: Service Curve has two zero slopes\n");
406		return -1;
407	}
408
409	return 0;
410}
411