• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * q_sfb.c	Stochastic Fair Blue.
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:	Juliusz Chroboczek <jch@pps.jussieu.fr>
10  *
11  */
12 
13 
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24 
25 #include "utils.h"
26 #include "tc_util.h"
27 
explain(void)28 static void explain(void)
29 {
30 	fprintf(stderr,
31 		"Usage: ... sfb [ rehash SECS ] [ db SECS ]\n"
32 		"	    [ limit PACKETS ] [ max PACKETS ] [ target PACKETS ]\n"
33 		"	    [ increment FLOAT ] [ decrement FLOAT ]\n"
34 		"	    [ penalty_rate PPS ] [ penalty_burst PACKETS ]\n");
35 }
36 
get_prob(__u32 * val,const char * arg)37 static int get_prob(__u32 *val, const char *arg)
38 {
39 	double d;
40 	char *ptr;
41 
42 	if (!arg || !*arg)
43 		return -1;
44 	d = strtod(arg, &ptr);
45 	if (!ptr || ptr == arg || d < 0.0 || d > 1.0)
46 		return -1;
47 	*val = (__u32)(d * SFB_MAX_PROB + 0.5);
48 	return 0;
49 }
50 
sfb_parse_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)51 static int sfb_parse_opt(struct qdisc_util *qu, int argc, char **argv,
52 			 struct nlmsghdr *n)
53 {
54 	struct tc_sfb_qopt opt = {
55 		.rehash_interval = 600*1000,
56 		.warmup_time = 60*1000,
57 		.penalty_rate = 10,
58 		.penalty_burst = 20,
59 		.increment = (SFB_MAX_PROB + 1000) / 2000,
60 		.decrement = (SFB_MAX_PROB + 10000) / 20000,
61 	};
62 	struct rtattr *tail;
63 
64 	while (argc > 0) {
65 	    if (strcmp(*argv, "rehash") == 0) {
66 			NEXT_ARG();
67 			if (get_u32(&opt.rehash_interval, *argv, 0)) {
68 				fprintf(stderr, "Illegal \"rehash\"\n");
69 				return -1;
70 			}
71 		} else if (strcmp(*argv, "db") == 0) {
72 			NEXT_ARG();
73 			if (get_u32(&opt.warmup_time, *argv, 0)) {
74 				fprintf(stderr, "Illegal \"db\"\n");
75 				return -1;
76 			}
77 		} else if (strcmp(*argv, "limit") == 0) {
78 			NEXT_ARG();
79 			if (get_u32(&opt.limit, *argv, 0)) {
80 				fprintf(stderr, "Illegal \"limit\"\n");
81 				return -1;
82 			}
83 		} else if (strcmp(*argv, "max") == 0) {
84 			NEXT_ARG();
85 			if (get_u32(&opt.max, *argv, 0)) {
86 				fprintf(stderr, "Illegal \"max\"\n");
87 				return -1;
88 			}
89 		} else if (strcmp(*argv, "target") == 0) {
90 			NEXT_ARG();
91 			if (get_u32(&opt.bin_size, *argv, 0)) {
92 				fprintf(stderr, "Illegal \"target\"\n");
93 				return -1;
94 			}
95 		} else if (strcmp(*argv, "increment") == 0) {
96 			NEXT_ARG();
97 			if (get_prob(&opt.increment, *argv)) {
98 				fprintf(stderr, "Illegal \"increment\"\n");
99 				return -1;
100 			}
101 		} else if (strcmp(*argv, "decrement") == 0) {
102 			NEXT_ARG();
103 			if (get_prob(&opt.decrement, *argv)) {
104 				fprintf(stderr, "Illegal \"decrement\"\n");
105 				return -1;
106 			}
107 		} else if (strcmp(*argv, "penalty_rate") == 0) {
108 			NEXT_ARG();
109 			if (get_u32(&opt.penalty_rate, *argv, 0)) {
110 				fprintf(stderr, "Illegal \"penalty_rate\"\n");
111 				return -1;
112 			}
113 		} else if (strcmp(*argv, "penalty_burst") == 0) {
114 			NEXT_ARG();
115 			if (get_u32(&opt.penalty_burst, *argv, 0)) {
116 				fprintf(stderr, "Illegal \"penalty_burst\"\n");
117 				return -1;
118 			}
119 		} else {
120 			fprintf(stderr, "What is \"%s\"?\n", *argv);
121 			explain();
122 			return -1;
123 		}
124 		argc--; argv++;
125 	}
126 
127 	if (opt.max == 0) {
128 		if (opt.bin_size >= 1)
129 			opt.max = (opt.bin_size * 5 + 1) / 4;
130 		else
131 			opt.max = 25;
132 	}
133 	if (opt.bin_size == 0)
134 		opt.bin_size = (opt.max * 4 + 3) / 5;
135 
136 	tail = NLMSG_TAIL(n);
137 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
138 	addattr_l(n, 1024, TCA_SFB_PARMS, &opt, sizeof(opt));
139 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
140 	return 0;
141 }
142 
sfb_print_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)143 static int sfb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
144 {
145 	struct rtattr *tb[__TCA_SFB_MAX];
146 	struct tc_sfb_qopt *qopt;
147 
148 	if (opt == NULL)
149 		return 0;
150 
151 	parse_rtattr_nested(tb, TCA_SFB_MAX, opt);
152 	if (tb[TCA_SFB_PARMS] == NULL)
153 		return -1;
154 	qopt = RTA_DATA(tb[TCA_SFB_PARMS]);
155 	if (RTA_PAYLOAD(tb[TCA_SFB_PARMS]) < sizeof(*qopt))
156 		return -1;
157 
158 	fprintf(f,
159 		"limit %d max %d target %d\n"
160 		"  increment %.5f decrement %.5f penalty rate %d burst %d (%ums %ums)",
161 		qopt->limit, qopt->max, qopt->bin_size,
162 		(double)qopt->increment / SFB_MAX_PROB,
163 		(double)qopt->decrement / SFB_MAX_PROB,
164 		qopt->penalty_rate, qopt->penalty_burst,
165 		qopt->rehash_interval, qopt->warmup_time);
166 
167 	return 0;
168 }
169 
sfb_print_xstats(struct qdisc_util * qu,FILE * f,struct rtattr * xstats)170 static int sfb_print_xstats(struct qdisc_util *qu, FILE *f,
171 			    struct rtattr *xstats)
172 {
173     struct tc_sfb_xstats *st;
174 
175     if (xstats == NULL)
176 	    return 0;
177 
178     if (RTA_PAYLOAD(xstats) < sizeof(*st))
179 	    return -1;
180 
181     st = RTA_DATA(xstats);
182     fprintf(f,
183 	    "  earlydrop %u penaltydrop %u bucketdrop %u queuedrop %u childdrop %u marked %u\n"
184 	    "  maxqlen %u maxprob %.5f avgprob %.5f ",
185 	    st->earlydrop, st->penaltydrop, st->bucketdrop, st->queuedrop, st->childdrop,
186 	    st->marked,
187 	    st->maxqlen, (double)st->maxprob / SFB_MAX_PROB,
188 		(double)st->avgprob / SFB_MAX_PROB);
189 
190     return 0;
191 }
192 
193 struct qdisc_util sfb_qdisc_util = {
194 	.id		= "sfb",
195 	.parse_qopt	= sfb_parse_opt,
196 	.print_qopt	= sfb_print_opt,
197 	.print_xstats	= sfb_print_xstats,
198 };
199