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