Debian Codegraph @

tc/q_sfb.c

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