]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_fq_codel.c
tc class: Show classes as ASCII graph
[mirror_iproute2.git] / tc / q_fq_codel.c
CommitLineData
c3524efc
ED
1/*
2 * Fair Queue Codel
3 *
4 * Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * Alternatively, provided that this notice is retained in full, this
19 * software may be distributed under the terms of the GNU General
20 * Public License ("GPL") version 2, in which case the provisions of the
21 * GPL apply INSTEAD OF those given above.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34 * DAMAGE.
35 *
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <syslog.h>
42#include <fcntl.h>
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46#include <string.h>
47
48#include "utils.h"
49#include "tc_util.h"
50
51static void explain(void)
52{
53 fprintf(stderr, "Usage: ... fq_codel [ limit PACKETS ] [ flows NUMBER ]\n");
54 fprintf(stderr, " [ target TIME] [ interval TIME ]\n");
55 fprintf(stderr, " [ quantum BYTES ] [ [no]ecn ]\n");
56}
57
58static int fq_codel_parse_opt(struct qdisc_util *qu, int argc, char **argv,
59 struct nlmsghdr *n)
60{
61 unsigned limit = 0;
62 unsigned flows = 0;
63 unsigned target = 0;
64 unsigned interval = 0;
65 unsigned quantum = 0;
66 int ecn = -1;
67 struct rtattr *tail;
68
69 while (argc > 0) {
70 if (strcmp(*argv, "limit") == 0) {
71 NEXT_ARG();
72 if (get_unsigned(&limit, *argv, 0)) {
73 fprintf(stderr, "Illegal \"limit\"\n");
74 return -1;
75 }
76 } else if (strcmp(*argv, "flows") == 0) {
77 NEXT_ARG();
78 if (get_unsigned(&flows, *argv, 0)) {
79 fprintf(stderr, "Illegal \"flows\"\n");
80 return -1;
81 }
82 } else if (strcmp(*argv, "quantum") == 0) {
83 NEXT_ARG();
84 if (get_unsigned(&quantum, *argv, 0)) {
85 fprintf(stderr, "Illegal \"quantum\"\n");
86 return -1;
87 }
88 } else if (strcmp(*argv, "target") == 0) {
89 NEXT_ARG();
90 if (get_time(&target, *argv)) {
91 fprintf(stderr, "Illegal \"target\"\n");
92 return -1;
93 }
94 } else if (strcmp(*argv, "interval") == 0) {
95 NEXT_ARG();
96 if (get_time(&interval, *argv)) {
97 fprintf(stderr, "Illegal \"interval\"\n");
98 return -1;
99 }
100 } else if (strcmp(*argv, "ecn") == 0) {
101 ecn = 1;
102 } else if (strcmp(*argv, "noecn") == 0) {
103 ecn = 0;
104 } else if (strcmp(*argv, "help") == 0) {
105 explain();
106 return -1;
107 } else {
108 fprintf(stderr, "What is \"%s\"?\n", *argv);
109 explain();
110 return -1;
111 }
112 argc--; argv++;
113 }
114
115 tail = NLMSG_TAIL(n);
116 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
117 if (limit)
118 addattr_l(n, 1024, TCA_FQ_CODEL_LIMIT, &limit, sizeof(limit));
119 if (flows)
120 addattr_l(n, 1024, TCA_FQ_CODEL_FLOWS, &flows, sizeof(flows));
121 if (quantum)
122 addattr_l(n, 1024, TCA_FQ_CODEL_QUANTUM, &quantum, sizeof(quantum));
123 if (interval)
124 addattr_l(n, 1024, TCA_FQ_CODEL_INTERVAL, &interval, sizeof(interval));
125 if (target)
126 addattr_l(n, 1024, TCA_FQ_CODEL_TARGET, &target, sizeof(target));
127 if (ecn != -1)
128 addattr_l(n, 1024, TCA_FQ_CODEL_ECN, &ecn, sizeof(ecn));
129 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
130 return 0;
131}
132
133static int fq_codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
134{
135 struct rtattr *tb[TCA_FQ_CODEL_MAX + 1];
136 unsigned limit;
137 unsigned flows;
138 unsigned interval;
139 unsigned target;
140 unsigned ecn;
141 unsigned quantum;
142 SPRINT_BUF(b1);
143
144 if (opt == NULL)
145 return 0;
146
147 parse_rtattr_nested(tb, TCA_FQ_CODEL_MAX, opt);
148
149 if (tb[TCA_FQ_CODEL_LIMIT] &&
150 RTA_PAYLOAD(tb[TCA_FQ_CODEL_LIMIT]) >= sizeof(__u32)) {
151 limit = rta_getattr_u32(tb[TCA_FQ_CODEL_LIMIT]);
152 fprintf(f, "limit %up ", limit);
153 }
154 if (tb[TCA_FQ_CODEL_FLOWS] &&
155 RTA_PAYLOAD(tb[TCA_FQ_CODEL_FLOWS]) >= sizeof(__u32)) {
156 flows = rta_getattr_u32(tb[TCA_FQ_CODEL_FLOWS]);
157 fprintf(f, "flows %u ", flows);
158 }
159 if (tb[TCA_FQ_CODEL_QUANTUM] &&
160 RTA_PAYLOAD(tb[TCA_FQ_CODEL_QUANTUM]) >= sizeof(__u32)) {
161 quantum = rta_getattr_u32(tb[TCA_FQ_CODEL_QUANTUM]);
162 fprintf(f, "quantum %u ", quantum);
163 }
164 if (tb[TCA_FQ_CODEL_TARGET] &&
165 RTA_PAYLOAD(tb[TCA_FQ_CODEL_TARGET]) >= sizeof(__u32)) {
166 target = rta_getattr_u32(tb[TCA_FQ_CODEL_TARGET]);
167 fprintf(f, "target %s ", sprint_time(target, b1));
168 }
169 if (tb[TCA_FQ_CODEL_INTERVAL] &&
170 RTA_PAYLOAD(tb[TCA_FQ_CODEL_INTERVAL]) >= sizeof(__u32)) {
171 interval = rta_getattr_u32(tb[TCA_FQ_CODEL_INTERVAL]);
172 fprintf(f, "interval %s ", sprint_time(interval, b1));
173 }
174 if (tb[TCA_FQ_CODEL_ECN] &&
175 RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) {
176 ecn = rta_getattr_u32(tb[TCA_FQ_CODEL_ECN]);
177 if (ecn)
178 fprintf(f, "ecn ");
179 }
180
181 return 0;
182}
183
184static int fq_codel_print_xstats(struct qdisc_util *qu, FILE *f,
185 struct rtattr *xstats)
186{
187 struct tc_fq_codel_xstats *st;
188 SPRINT_BUF(b1);
189
190 if (xstats == NULL)
191 return 0;
192
193 if (RTA_PAYLOAD(xstats) < sizeof(*st))
194 return -1;
195
196 st = RTA_DATA(xstats);
197 if (st->type == TCA_FQ_CODEL_XSTATS_QDISC) {
198 fprintf(f, " maxpacket %u drop_overlimit %u new_flow_count %u ecn_mark %u",
199 st->qdisc_stats.maxpacket,
200 st->qdisc_stats.drop_overlimit,
201 st->qdisc_stats.new_flow_count,
202 st->qdisc_stats.ecn_mark);
203 fprintf(f, "\n new_flows_len %u old_flows_len %u",
204 st->qdisc_stats.new_flows_len,
205 st->qdisc_stats.old_flows_len);
206 }
207 if (st->type == TCA_FQ_CODEL_XSTATS_CLASS) {
208 fprintf(f, " deficit %d count %u lastcount %u ldelay %s",
209 st->class_stats.deficit,
210 st->class_stats.count,
211 st->class_stats.lastcount,
212 sprint_time(st->class_stats.ldelay, b1));
213 if (st->class_stats.dropping) {
214 fprintf(f, " dropping");
215 if (st->class_stats.drop_next < 0)
216 fprintf(f, " drop_next -%s",
217 sprint_time(-st->class_stats.drop_next, b1));
218 else
219 fprintf(f, " drop_next %s",
220 sprint_time(st->class_stats.drop_next, b1));
221 }
222 }
223 return 0;
224
225}
226
227struct qdisc_util fq_codel_qdisc_util = {
228 .id = "fq_codel",
229 .parse_qopt = fq_codel_parse_opt,
230 .print_qopt = fq_codel_print_opt,
231 .print_xstats = fq_codel_print_xstats,
232};