blob: 9221b48b6e01b1fd542d1d9312300df1c7c0f79d [file] [log] [blame]
Eric Dumazet185d88f2012-05-10 20:22:35 +00001/*
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>
Eric Dumazetdf1c7d92015-05-11 10:44:55 -07007 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
Eric Dumazet185d88f2012-05-10 20:22:35 +00008 *
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>
44#include <syslog.h>
45#include <fcntl.h>
46#include <sys/socket.h>
47#include <netinet/in.h>
48#include <arpa/inet.h>
49#include <string.h>
50
51#include "utils.h"
52#include "tc_util.h"
53
54static void explain(void)
55{
Luca Lemmo4733b182016-03-16 17:56:14 +010056 fprintf(stderr, "Usage: ... codel [ limit PACKETS ] [ target TIME ]\n");
Vijay Subramanian50a3ec32012-05-24 11:48:07 -070057 fprintf(stderr, " [ interval TIME ] [ ecn | noecn ]\n");
Eric Dumazetdf1c7d92015-05-11 10:44:55 -070058 fprintf(stderr, " [ ce_threshold TIME ]\n");
Eric Dumazet185d88f2012-05-10 20:22:35 +000059}
60
61static int codel_parse_opt(struct qdisc_util *qu, int argc, char **argv,
62 struct nlmsghdr *n)
63{
Stephen Hemminger32a121c2016-03-21 11:48:36 -070064 unsigned int limit = 0;
65 unsigned int target = 0;
66 unsigned int interval = 0;
67 unsigned int ce_threshold = ~0U;
Eric Dumazet185d88f2012-05-10 20:22:35 +000068 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 }
Eric Dumazetdf1c7d92015-05-11 10:44:55 -070084 } 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 }
Eric Dumazet185d88f2012-05-10 20:22:35 +000090 } 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
111 tail = NLMSG_TAIL(n);
112 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
113 if (limit)
114 addattr_l(n, 1024, TCA_CODEL_LIMIT, &limit, sizeof(limit));
115 if (interval)
116 addattr_l(n, 1024, TCA_CODEL_INTERVAL, &interval, sizeof(interval));
117 if (target)
118 addattr_l(n, 1024, TCA_CODEL_TARGET, &target, sizeof(target));
119 if (ecn != -1)
120 addattr_l(n, 1024, TCA_CODEL_ECN, &ecn, sizeof(ecn));
Eric Dumazetdf1c7d92015-05-11 10:44:55 -0700121 if (ce_threshold != ~0U)
122 addattr_l(n, 1024, TCA_CODEL_CE_THRESHOLD,
123 &ce_threshold, sizeof(ce_threshold));
124
Eric Dumazet185d88f2012-05-10 20:22:35 +0000125 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
126 return 0;
127}
128
129static int codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
130{
131 struct rtattr *tb[TCA_CODEL_MAX + 1];
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700132 unsigned int limit;
133 unsigned int interval;
134 unsigned int target;
135 unsigned int ecn;
136 unsigned int ce_threshold;
137
Eric Dumazet185d88f2012-05-10 20:22:35 +0000138 SPRINT_BUF(b1);
139
140 if (opt == NULL)
141 return 0;
142
143 parse_rtattr_nested(tb, TCA_CODEL_MAX, opt);
144
145 if (tb[TCA_CODEL_LIMIT] &&
146 RTA_PAYLOAD(tb[TCA_CODEL_LIMIT]) >= sizeof(__u32)) {
147 limit = rta_getattr_u32(tb[TCA_CODEL_LIMIT]);
148 fprintf(f, "limit %up ", limit);
149 }
150 if (tb[TCA_CODEL_TARGET] &&
151 RTA_PAYLOAD(tb[TCA_CODEL_TARGET]) >= sizeof(__u32)) {
152 target = rta_getattr_u32(tb[TCA_CODEL_TARGET]);
153 fprintf(f, "target %s ", sprint_time(target, b1));
154 }
Eric Dumazetdf1c7d92015-05-11 10:44:55 -0700155 if (tb[TCA_CODEL_CE_THRESHOLD] &&
156 RTA_PAYLOAD(tb[TCA_CODEL_CE_THRESHOLD]) >= sizeof(__u32)) {
157 ce_threshold = rta_getattr_u32(tb[TCA_CODEL_CE_THRESHOLD]);
158 fprintf(f, "ce_threshold %s ", sprint_time(ce_threshold, b1));
159 }
Eric Dumazet185d88f2012-05-10 20:22:35 +0000160 if (tb[TCA_CODEL_INTERVAL] &&
161 RTA_PAYLOAD(tb[TCA_CODEL_INTERVAL]) >= sizeof(__u32)) {
162 interval = rta_getattr_u32(tb[TCA_CODEL_INTERVAL]);
163 fprintf(f, "interval %s ", sprint_time(interval, b1));
164 }
165 if (tb[TCA_CODEL_ECN] &&
166 RTA_PAYLOAD(tb[TCA_CODEL_ECN]) >= sizeof(__u32)) {
167 ecn = rta_getattr_u32(tb[TCA_CODEL_ECN]);
168 if (ecn)
169 fprintf(f, "ecn ");
170 }
171
172 return 0;
173}
174
175static int codel_print_xstats(struct qdisc_util *qu, FILE *f,
176 struct rtattr *xstats)
177{
Eric Dumazetdf1c7d92015-05-11 10:44:55 -0700178 struct tc_codel_xstats _st, *st;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700179
Eric Dumazet185d88f2012-05-10 20:22:35 +0000180 SPRINT_BUF(b1);
181
182 if (xstats == NULL)
183 return 0;
184
Eric Dumazet185d88f2012-05-10 20:22:35 +0000185 st = RTA_DATA(xstats);
Eric Dumazetdf1c7d92015-05-11 10:44:55 -0700186 if (RTA_PAYLOAD(xstats) < sizeof(*st)) {
187 memset(&_st, 0, sizeof(_st));
188 memcpy(&_st, st, RTA_PAYLOAD(xstats));
189 st = &_st;
190 }
191
Eric Dumazet185d88f2012-05-10 20:22:35 +0000192 fprintf(f, " count %u lastcount %u ldelay %s",
193 st->count, st->lastcount, sprint_time(st->ldelay, b1));
194 if (st->dropping)
195 fprintf(f, " dropping");
196 if (st->drop_next < 0)
197 fprintf(f, " drop_next -%s", sprint_time(-st->drop_next, b1));
198 else
199 fprintf(f, " drop_next %s", sprint_time(st->drop_next, b1));
200 fprintf(f, "\n maxpacket %u ecn_mark %u drop_overlimit %u",
201 st->maxpacket, st->ecn_mark, st->drop_overlimit);
Eric Dumazetdf1c7d92015-05-11 10:44:55 -0700202 if (st->ce_mark)
203 fprintf(f, " ce_mark %u", st->ce_mark);
Eric Dumazet185d88f2012-05-10 20:22:35 +0000204 return 0;
205
206}
207
208struct qdisc_util codel_qdisc_util = {
209 .id = "codel",
210 .parse_qopt = codel_parse_opt,
211 .print_qopt = codel_print_opt,
212 .print_xstats = codel_print_xstats,
213};