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