1aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger/*
2aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger * q_sfq.c		SFQ.
3aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
4aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		This program is free software; you can redistribute it and/or
5aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		modify it under the terms of the GNU General Public License
6aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		as published by the Free Software Foundation; either version
7aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *		2 of the License, or (at your option) any later version.
8aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
9aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger *
11aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger */
12aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
13aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <stdio.h>
14aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <stdlib.h>
15aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <unistd.h>
16aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <syslog.h>
17aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <fcntl.h>
18aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <sys/socket.h>
19aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <netinet/in.h>
20aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <arpa/inet.h>
21aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include <string.h>
226987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet#include <math.h>
23aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
24aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include "utils.h"
25aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger#include "tc_util.h"
266987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet#include "tc_red.h"
27aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
28aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerstatic void explain(void)
29aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger{
30aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
316987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	fprintf(stderr, "               [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n");
326987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	fprintf(stderr, "               [ headdrop ]\n");
336987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	fprintf(stderr, "               [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n");
346987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	fprintf(stderr, "               [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n");
356987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	fprintf(stderr, "               [ ecn ] [ harddrop ]\n");
36aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger}
37aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
38aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerstatic int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
39aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger{
406987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	int ok = 0, red = 0;
416987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	struct tc_sfq_qopt_v1 opt;
426987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	unsigned int burst = 0;
436987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	int wlog;
446987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	unsigned int avpkt = 1000;
456987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	double probability = 0.02;
46aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
47aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	memset(&opt, 0, sizeof(opt));
48aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
49aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	while (argc > 0) {
50aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		if (strcmp(*argv, "quantum") == 0) {
51aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			NEXT_ARG();
526987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_size(&opt.v0.quantum, *argv)) {
53aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				fprintf(stderr, "Illegal \"limit\"\n");
54aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				return -1;
55aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			}
56aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			ok++;
57aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		} else if (strcmp(*argv, "perturb") == 0) {
58aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			NEXT_ARG();
596987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_integer(&opt.v0.perturb_period, *argv, 0)) {
60aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				fprintf(stderr, "Illegal \"perturb\"\n");
61aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				return -1;
62aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			}
63aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			ok++;
64aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		} else if (strcmp(*argv, "limit") == 0) {
65aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			NEXT_ARG();
666987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.v0.limit, *argv, 0)) {
67aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				fprintf(stderr, "Illegal \"limit\"\n");
68aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				return -1;
69aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			}
706987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (opt.v0.limit < 2) {
71aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				fprintf(stderr, "Illegal \"limit\", must be > 1\n");
72aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger				return -1;
73aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			}
74aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			ok++;
75f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet		} else if (strcmp(*argv, "divisor") == 0) {
76f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet			NEXT_ARG();
776987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.v0.divisor, *argv, 0)) {
78f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet				fprintf(stderr, "Illegal \"divisor\"\n");
79f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet				return -1;
80f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet			}
81f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet			ok++;
826987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "flows") == 0) {
836987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
846987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.v0.flows, *argv, 0)) {
856987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"flows\"\n");
866987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
876987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
886987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			ok++;
896987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "depth") == 0) {
906987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
916987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.depth, *argv, 0)) {
926987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"flows\"\n");
936987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
946987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
956987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			ok++;
966987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "headdrop") == 0) {
976987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			opt.headdrop = 1;
986987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			ok++;
996987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "redflowlimit") == 0) {
1006987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1016987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.limit, *argv, 0)) {
1026987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"redflowlimit\"\n");
1036987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1046987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1056987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1066987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "min") == 0) {
1076987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1086987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.qth_min, *argv, 0)) {
1096987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"min\"\n");
1106987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1116987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1126987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1136987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "max") == 0) {
1146987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1156987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_u32(&opt.qth_max, *argv, 0)) {
1166987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"max\"\n");
1176987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1186987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1196987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1206987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "burst") == 0) {
1216987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1226987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_unsigned(&burst, *argv, 0)) {
1236987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"burst\"\n");
1246987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1256987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1266987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1276987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "avpkt") == 0) {
1286987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1296987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (get_size(&avpkt, *argv)) {
1306987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"avpkt\"\n");
1316987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1326987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1336987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1346987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "probability") == 0) {
1356987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			NEXT_ARG();
1366987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			if (sscanf(*argv, "%lg", &probability) != 1) {
1376987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				fprintf(stderr, "Illegal \"probability\"\n");
1386987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				return -1;
1396987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			}
1406987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1416987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "ecn") == 0) {
1426987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			opt.flags |= TC_RED_ECN;
1436987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
1446987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		} else if (strcmp(*argv, "harddrop") == 0) {
1456987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			opt.flags |= TC_RED_HARDDROP;
1466987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			red++;
147aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		} else if (strcmp(*argv, "help") == 0) {
148aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			explain();
149aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			return -1;
150aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		} else {
151aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			fprintf(stderr, "What is \"%s\"?\n", *argv);
152aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			explain();
153aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger			return -1;
154aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		}
155aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		argc--; argv++;
156aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	}
1576987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (red) {
1586987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (!opt.limit) {
1596987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "Required parameter (redflowlimit) is missing\n");
1606987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			return -1;
1616987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
1626987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		/* Compute default min/max thresholds based on
1636987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		   Sally Floyd's recommendations:
1646987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		   http://www.icir.org/floyd/REDparameters.txt
1656987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		*/
1666987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (!opt.qth_max)
1676987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			opt.qth_max = opt.limit / 4;
1686987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (!opt.qth_min)
1696987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			opt.qth_min = opt.qth_max / 3;
1706987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (!burst)
1716987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
1726987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet
1736987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (opt.qth_max > opt.limit) {
1746987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "\"max\" is larger than \"limit\"\n");
1756987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			return -1;
1766987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
1776987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet
1786987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (opt.qth_min >= opt.qth_max) {
1796987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
1806987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			return -1;
1816987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
1826987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet
1836987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt);
1846987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (wlog < 0) {
1856987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n");
1866987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			return -1;
1876987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
1886987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (wlog >= 10)
1896987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst);
1906987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		opt.Wlog = wlog;
1916987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet
1926987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability);
1936987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (wlog < 0) {
1946987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(stderr, "SFQ: failed to calculate probability.\n");
1956987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			return -1;
1966987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
1976987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		opt.Plog = wlog;
1986987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		opt.max_P = probability * pow(2, 32);
1996987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	}
200aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
2016987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (ok || red)
202aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
203aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	return 0;
204aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger}
205aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
206aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemmingerstatic int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
207aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger{
208aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	struct tc_sfq_qopt *qopt;
2096987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	struct tc_sfq_qopt_v1 *qopt_ext = NULL;
210aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	SPRINT_BUF(b1);
2116987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	SPRINT_BUF(b2);
2126987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	SPRINT_BUF(b3);
213aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (opt == NULL)
214aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		return 0;
215aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
216aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
217aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		return -1;
2186987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
2196987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		qopt_ext = RTA_DATA(opt);
220aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	qopt = RTA_DATA(opt);
221aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	fprintf(f, "limit %up ", qopt->limit);
222aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
2236987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (qopt_ext && qopt_ext->depth)
2246987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		fprintf(f, "depth %u ", qopt_ext->depth);
2256987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (qopt_ext && qopt_ext->headdrop)
2266987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		fprintf(f, "headdrop ");
2276987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet
228aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (show_details) {
229aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
230aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	}
231f3f28c2126343fea70a697fc61392c433e0dc9e1Eric Dumazet	fprintf(f, "divisor %u ", qopt->divisor);
232aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	if (qopt->perturb_period)
233aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger		fprintf(f, "perturb %dsec ", qopt->perturb_period);
2346987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	if (qopt_ext && qopt_ext->qth_min) {
2356987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		fprintf(f, "\n ewma %u ", qopt_ext->Wlog);
2366987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		fprintf(f, "min %s max %s probability %g ",
2376987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			sprint_size(qopt_ext->qth_min, b2),
2386987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			sprint_size(qopt_ext->qth_max, b3),
2396987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			qopt_ext->max_P / pow(2, 32));
2406987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (qopt_ext->flags & TC_RED_ECN)
2416987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(f, "ecn ");
2426987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		if (show_stats) {
2436987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(f, "\n prob_mark %u prob_mark_head %u prob_drop %u",
2446987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.prob_mark,
2456987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.prob_mark_head,
2466987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.prob_drop);
2476987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet			fprintf(f, "\n forced_mark %u forced_mark_head %u forced_drop %u",
2486987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.forced_mark,
2496987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.forced_mark_head,
2506987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet				qopt_ext->stats.forced_drop);
2516987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet		}
2526987ecf0832d62350ea10432f3f1f7a84c457b81Eric Dumazet	}
253aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger	return 0;
254aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger}
255aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger
2565626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardystatic int sfq_print_xstats(struct qdisc_util *qu, FILE *f,
2575626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy			    struct rtattr *xstats)
2585626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy{
2595626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	struct tc_sfq_xstats *st;
2605626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy
2615626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	if (xstats == NULL)
2625626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy		return 0;
2635626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	if (RTA_PAYLOAD(xstats) < sizeof(*st))
2645626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy		return -1;
2655626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	st = RTA_DATA(xstats);
2665626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy
2675626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	fprintf(f, " allot %d ", st->allot);
2685626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	fprintf(f, "\n");
2695626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	return 0;
2705626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy}
2715626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy
27295812b56a5a66e7e9a21744cfe8bc0bb9791ea98net[shemminger]!kaberstruct qdisc_util sfq_qdisc_util = {
273f2f99e2eefdbd9cb6a750b19a7b3036db351b983osdl.net!shemminger	.id		= "sfq",
274f2f99e2eefdbd9cb6a750b19a7b3036db351b983osdl.net!shemminger	.parse_qopt	= sfq_parse_opt,
275f2f99e2eefdbd9cb6a750b19a7b3036db351b983osdl.net!shemminger	.print_qopt	= sfq_print_opt,
2765626a24a8bcbbd149718a5bf148eb18b61d46538Patrick McHardy	.print_xstats	= sfq_print_xstats,
277aba5acdfdb347d2c21fc67d613d83d4430ca3937osdl.org!shemminger};
278