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 struct rtattr *tail;
56
57 memset(&opt, 0, sizeof(opt));
58 opt.rehash_interval = 600*1000;
59 opt.warmup_time = 60*1000;
60 opt.penalty_rate = 10;
61 opt.penalty_burst = 20;
62 opt.increment = (SFB_MAX_PROB + 1000) / 2000;
63 opt.decrement = (SFB_MAX_PROB + 10000) / 20000;
64
65 while (argc > 0) {
66 if (strcmp(*argv, "rehash") == 0) {
67 NEXT_ARG();
68 if (get_u32(&opt.rehash_interval, *argv, 0)) {
69 fprintf(stderr, "Illegal \"rehash\"\n");
70 return -1;
71 }
72 } else if (strcmp(*argv, "db") == 0) {
73 NEXT_ARG();
74 if (get_u32(&opt.warmup_time, *argv, 0)) {
75 fprintf(stderr, "Illegal \"db\"\n");
76 return -1;
77 }
78 } else if (strcmp(*argv, "limit") == 0) {
79 NEXT_ARG();
80 if (get_u32(&opt.limit, *argv, 0)) {
81 fprintf(stderr, "Illegal \"limit\"\n");
82 return -1;
83 }
84 } else if (strcmp(*argv, "max") == 0) {
85 NEXT_ARG();
86 if (get_u32(&opt.max, *argv, 0)) {
87 fprintf(stderr, "Illegal \"max\"\n");
88 return -1;
89 }
90 } else if (strcmp(*argv, "target") == 0) {
91 NEXT_ARG();
92 if (get_u32(&opt.bin_size, *argv, 0)) {
93 fprintf(stderr, "Illegal \"target\"\n");
94 return -1;
95 }
96 } else if (strcmp(*argv, "increment") == 0) {
97 NEXT_ARG();
98 if (get_prob(&opt.increment, *argv)) {
99 fprintf(stderr, "Illegal \"increment\"\n");
100 return -1;
101 }
102 } else if (strcmp(*argv, "decrement") == 0) {
103 NEXT_ARG();
104 if (get_prob(&opt.decrement, *argv)) {
105 fprintf(stderr, "Illegal \"decrement\"\n");
106 return -1;
107 }
108 } else if (strcmp(*argv, "penalty_rate") == 0) {
109 NEXT_ARG();
110 if (get_u32(&opt.penalty_rate, *argv, 0)) {
111 fprintf(stderr, "Illegal \"penalty_rate\"\n");
112 return -1;
113 }
114 } else if (strcmp(*argv, "penalty_burst") == 0) {
115 NEXT_ARG();
116 if (get_u32(&opt.penalty_burst, *argv, 0)) {
117 fprintf(stderr, "Illegal \"penalty_burst\"\n");
118 return -1;
119 }
120 } else {
121 fprintf(stderr, "What is \"%s\"?\n", *argv);
122 explain();
123 return -1;
124 }
125 argc--; argv++;
126 }
127
128 if (opt.max == 0) {
129 if (opt.bin_size >= 1)
130 opt.max = (opt.bin_size * 5 + 1) / 4;
131 else
132 opt.max = 25;
133 }
134 if (opt.bin_size == 0)
135 opt.bin_size = (opt.max * 4 + 3) / 5;
136
137 tail = NLMSG_TAIL(n);
138 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
139 addattr_l(n, 1024, TCA_SFB_PARMS, &opt, sizeof(opt));
140 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
141 return 0;
142 }
143
sfb_print_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)144 static int sfb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
145 {
146 struct rtattr *tb[__TCA_SFB_MAX];
147 struct tc_sfb_qopt *qopt;
148
149 if (opt == NULL)
150 return 0;
151
152 parse_rtattr_nested(tb, TCA_SFB_MAX, opt);
153 if (tb[TCA_SFB_PARMS] == NULL)
154 return -1;
155 qopt = RTA_DATA(tb[TCA_SFB_PARMS]);
156 if (RTA_PAYLOAD(tb[TCA_SFB_PARMS]) < sizeof(*qopt))
157 return -1;
158
159 fprintf(f,
160 "limit %d max %d target %d\n"
161 " increment %.5f decrement %.5f penalty rate %d burst %d "
162 "(%ums %ums)",
163 qopt->limit, qopt->max, qopt->bin_size,
164 (double)qopt->increment / SFB_MAX_PROB,
165 (double)qopt->decrement / SFB_MAX_PROB,
166 qopt->penalty_rate, qopt->penalty_burst,
167 qopt->rehash_interval, qopt->warmup_time);
168
169 return 0;
170 }
171
sfb_print_xstats(struct qdisc_util * qu,FILE * f,struct rtattr * xstats)172 static int sfb_print_xstats(struct qdisc_util *qu, FILE *f,
173 struct rtattr *xstats)
174 {
175 struct tc_sfb_xstats *st;
176
177 if (xstats == NULL)
178 return 0;
179
180 if (RTA_PAYLOAD(xstats) < sizeof(*st))
181 return -1;
182
183 st = RTA_DATA(xstats);
184 fprintf(f,
185 " earlydrop %u penaltydrop %u bucketdrop %u queuedrop %u childdrop %u marked %u\n"
186 " maxqlen %u maxprob %.5f avgprob %.5f ",
187 st->earlydrop, st->penaltydrop, st->bucketdrop, st->queuedrop, st->childdrop,
188 st->marked,
189 st->maxqlen, (double)st->maxprob / SFB_MAX_PROB,
190 (double)st->avgprob / SFB_MAX_PROB);
191
192 return 0;
193 }
194
195 struct qdisc_util sfb_qdisc_util = {
196 .id = "sfb",
197 .parse_qopt = sfb_parse_opt,
198 .print_qopt = sfb_print_opt,
199 .print_xstats = sfb_print_xstats,
200 };
201