]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_codel.c
tc_codel: Controlled Delay AQM
[lisovros/iproute2_canprio.git] / tc / q_codel.c
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>
7  *  Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
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>
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
54 static void explain(void)
55 {
56         fprintf(stderr, "Usage: ... codel [ limit PACKETS ] [ target TIME]\n");
57         fprintf(stderr, "                 [ interval TIME ] [ ecn ]\n");
58 }
59
60 static int codel_parse_opt(struct qdisc_util *qu, int argc, char **argv,
61                            struct nlmsghdr *n)
62 {
63         unsigned limit = 0;
64         unsigned target = 0;
65         unsigned interval = 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, "target") == 0) {
77                         NEXT_ARG();
78                         if (get_time(&target, *argv)) {
79                                 fprintf(stderr, "Illegal \"target\"\n");
80                                 return -1;
81                         }
82                 } else if (strcmp(*argv, "interval") == 0) {
83                         NEXT_ARG();
84                         if (get_time(&interval, *argv)) {
85                                 fprintf(stderr, "Illegal \"interval\"\n");
86                                 return -1;
87                         }
88                 } else if (strcmp(*argv, "ecn") == 0) {
89                         ecn = 1;
90                 } else if (strcmp(*argv, "noecn") == 0) {
91                         ecn = 0;
92                 } else if (strcmp(*argv, "help") == 0) {
93                         explain();
94                         return -1;
95                 } else {
96                         fprintf(stderr, "What is \"%s\"?\n", *argv);
97                         explain();
98                         return -1;
99                 }
100                 argc--; argv++;
101         }
102
103         tail = NLMSG_TAIL(n);
104         addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
105         if (limit)
106                 addattr_l(n, 1024, TCA_CODEL_LIMIT, &limit, sizeof(limit));
107         if (interval)
108                 addattr_l(n, 1024, TCA_CODEL_INTERVAL, &interval, sizeof(interval));
109         if (target)
110                 addattr_l(n, 1024, TCA_CODEL_TARGET, &target, sizeof(target));
111         if (ecn != -1)
112                 addattr_l(n, 1024, TCA_CODEL_ECN, &ecn, sizeof(ecn));
113         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
114         return 0;
115 }
116
117 static int codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
118 {
119         struct rtattr *tb[TCA_CODEL_MAX + 1];
120         unsigned limit;
121         unsigned interval;
122         unsigned target;
123         unsigned ecn;
124         SPRINT_BUF(b1);
125
126         if (opt == NULL)
127                 return 0;
128
129         parse_rtattr_nested(tb, TCA_CODEL_MAX, opt);
130
131         if (tb[TCA_CODEL_LIMIT] &&
132             RTA_PAYLOAD(tb[TCA_CODEL_LIMIT]) >= sizeof(__u32)) {
133                 limit = rta_getattr_u32(tb[TCA_CODEL_LIMIT]);
134                 fprintf(f, "limit %up ", limit);
135         }
136         if (tb[TCA_CODEL_TARGET] &&
137             RTA_PAYLOAD(tb[TCA_CODEL_TARGET]) >= sizeof(__u32)) {
138                 target = rta_getattr_u32(tb[TCA_CODEL_TARGET]);
139                 fprintf(f, "target %s ", sprint_time(target, b1));
140         }
141         if (tb[TCA_CODEL_INTERVAL] &&
142             RTA_PAYLOAD(tb[TCA_CODEL_INTERVAL]) >= sizeof(__u32)) {
143                 interval = rta_getattr_u32(tb[TCA_CODEL_INTERVAL]);
144                 fprintf(f, "interval %s ", sprint_time(interval, b1));
145         }
146         if (tb[TCA_CODEL_ECN] &&
147             RTA_PAYLOAD(tb[TCA_CODEL_ECN]) >= sizeof(__u32)) {
148                 ecn = rta_getattr_u32(tb[TCA_CODEL_ECN]);
149                 if (ecn)
150                         fprintf(f, "ecn ");
151         }
152
153         return 0;
154 }
155
156 static int codel_print_xstats(struct qdisc_util *qu, FILE *f,
157                               struct rtattr *xstats)
158 {
159         struct tc_codel_xstats *st;
160         SPRINT_BUF(b1);
161
162         if (xstats == NULL)
163                 return 0;
164
165         if (RTA_PAYLOAD(xstats) < sizeof(*st))
166                 return -1;
167
168         st = RTA_DATA(xstats);
169         fprintf(f, "  count %u lastcount %u ldelay %s",
170                 st->count, st->lastcount, sprint_time(st->ldelay, b1));
171         if (st->dropping)
172                 fprintf(f, " dropping");
173         if (st->drop_next < 0)
174                 fprintf(f, " drop_next -%s", sprint_time(-st->drop_next, b1));
175         else
176                 fprintf(f, " drop_next %s", sprint_time(st->drop_next, b1));
177         fprintf(f, "\n  maxpacket %u ecn_mark %u drop_overlimit %u",
178                 st->maxpacket, st->ecn_mark, st->drop_overlimit);
179         return 0;
180
181 }
182
183 struct qdisc_util codel_qdisc_util = {
184         .id             = "codel",
185         .parse_qopt     = codel_parse_opt,
186         .print_qopt     = codel_print_opt,
187         .print_xstats   = codel_print_xstats,
188 };