1/*
2 * q_sfq.c		SFQ.
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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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
26static void explain(void)
27{
28	fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
29	fprintf(stderr, "               [ divisor NUMBER ]\n");
30}
31
32static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
33{
34	int ok=0;
35	struct tc_sfq_qopt opt;
36
37	memset(&opt, 0, sizeof(opt));
38
39	while (argc > 0) {
40		if (strcmp(*argv, "quantum") == 0) {
41			NEXT_ARG();
42			if (get_size(&opt.quantum, *argv)) {
43				fprintf(stderr, "Illegal \"limit\"\n");
44				return -1;
45			}
46			ok++;
47		} else if (strcmp(*argv, "perturb") == 0) {
48			NEXT_ARG();
49			if (get_integer(&opt.perturb_period, *argv, 0)) {
50				fprintf(stderr, "Illegal \"perturb\"\n");
51				return -1;
52			}
53			ok++;
54		} else if (strcmp(*argv, "limit") == 0) {
55			NEXT_ARG();
56			if (get_u32(&opt.limit, *argv, 0)) {
57				fprintf(stderr, "Illegal \"limit\"\n");
58				return -1;
59			}
60			if (opt.limit < 2) {
61				fprintf(stderr, "Illegal \"limit\", must be > 1\n");
62				return -1;
63			}
64			ok++;
65		} else if (strcmp(*argv, "divisor") == 0) {
66			NEXT_ARG();
67			if (get_u32(&opt.divisor, *argv, 0)) {
68				fprintf(stderr, "Illegal \"divisor\"\n");
69				return -1;
70			}
71			ok++;
72		} else if (strcmp(*argv, "help") == 0) {
73			explain();
74			return -1;
75		} else {
76			fprintf(stderr, "What is \"%s\"?\n", *argv);
77			explain();
78			return -1;
79		}
80		argc--; argv++;
81	}
82
83	if (ok)
84		addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
85	return 0;
86}
87
88static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
89{
90	struct tc_sfq_qopt *qopt;
91	SPRINT_BUF(b1);
92
93	if (opt == NULL)
94		return 0;
95
96	if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
97		return -1;
98	qopt = RTA_DATA(opt);
99	fprintf(f, "limit %up ", qopt->limit);
100	fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
101	if (show_details) {
102		fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
103	}
104	fprintf(f, "divisor %u ", qopt->divisor);
105	if (qopt->perturb_period)
106		fprintf(f, "perturb %dsec ", qopt->perturb_period);
107	return 0;
108}
109
110static int sfq_print_xstats(struct qdisc_util *qu, FILE *f,
111			    struct rtattr *xstats)
112{
113	struct tc_sfq_xstats *st;
114
115	if (xstats == NULL)
116		return 0;
117	if (RTA_PAYLOAD(xstats) < sizeof(*st))
118		return -1;
119	st = RTA_DATA(xstats);
120
121	fprintf(f, " allot %d ", st->allot);
122	fprintf(f, "\n");
123	return 0;
124}
125
126struct qdisc_util sfq_qdisc_util = {
127	.id		= "sfq",
128	.parse_qopt	= sfq_parse_opt,
129	.print_qopt	= sfq_print_opt,
130	.print_xstats	= sfq_print_xstats,
131};
132