]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_pie.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_pie.c
1 /* Copyright (C) 2013 Cisco Systems, Inc, 2013.
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * Author: Vijay Subramanian <vijaynsu@cisco.com>
14 * Author: Mythili Prabhu <mysuryan@cisco.com>
15 *
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include "utils.h"
29 #include "tc_util.h"
30
31 static void explain(void)
32 {
33 fprintf(stderr, "Usage: ... pie [ limit PACKETS ][ target TIME us]\n");
34 fprintf(stderr, " [ tupdate TIME us][ alpha ALPHA ]");
35 fprintf(stderr, "[beta BETA ][bytemode | nobytemode][ecn | noecn ]\n");
36 }
37
38 #define ALPHA_MAX 32
39 #define BETA_MAX 32
40
41 static int pie_parse_opt(struct qdisc_util *qu, int argc, char **argv,
42 struct nlmsghdr *n, const char *dev)
43 {
44 unsigned int limit = 0;
45 unsigned int target = 0;
46 unsigned int tupdate = 0;
47 unsigned int alpha = 0;
48 unsigned int beta = 0;
49 int ecn = -1;
50 int bytemode = -1;
51 struct rtattr *tail;
52
53 while (argc > 0) {
54 if (strcmp(*argv, "limit") == 0) {
55 NEXT_ARG();
56 if (get_unsigned(&limit, *argv, 0)) {
57 fprintf(stderr, "Illegal \"limit\"\n");
58 return -1;
59 }
60 } else if (strcmp(*argv, "target") == 0) {
61 NEXT_ARG();
62 if (get_time(&target, *argv)) {
63 fprintf(stderr, "Illegal \"target\"\n");
64 return -1;
65 }
66 } else if (strcmp(*argv, "tupdate") == 0) {
67 NEXT_ARG();
68 if (get_time(&tupdate, *argv)) {
69 fprintf(stderr, "Illegal \"tupdate\"\n");
70 return -1;
71 }
72 } else if (strcmp(*argv, "alpha") == 0) {
73 NEXT_ARG();
74 if (get_unsigned(&alpha, *argv, 0) ||
75 (alpha > ALPHA_MAX)) {
76 fprintf(stderr, "Illegal \"alpha\"\n");
77 return -1;
78 }
79 } else if (strcmp(*argv, "beta") == 0) {
80 NEXT_ARG();
81 if (get_unsigned(&beta, *argv, 0) ||
82 (beta > BETA_MAX)) {
83 fprintf(stderr, "Illegal \"beta\"\n");
84 return -1;
85 }
86 } else if (strcmp(*argv, "ecn") == 0) {
87 ecn = 1;
88 } else if (strcmp(*argv, "noecn") == 0) {
89 ecn = 0;
90 } else if (strcmp(*argv, "bytemode") == 0) {
91 bytemode = 1;
92 } else if (strcmp(*argv, "nobytemode") == 0) {
93 bytemode = 0;
94 } else if (strcmp(*argv, "help") == 0) {
95 explain();
96 return -1;
97 } else {
98 fprintf(stderr, "What is \"%s\"?\n", *argv);
99 explain();
100 return -1;
101 }
102 argc--;
103 argv++;
104 }
105
106 tail = NLMSG_TAIL(n);
107 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
108 if (limit)
109 addattr_l(n, 1024, TCA_PIE_LIMIT, &limit, sizeof(limit));
110 if (tupdate)
111 addattr_l(n, 1024, TCA_PIE_TUPDATE, &tupdate, sizeof(tupdate));
112 if (target)
113 addattr_l(n, 1024, TCA_PIE_TARGET, &target, sizeof(target));
114 if (alpha)
115 addattr_l(n, 1024, TCA_PIE_ALPHA, &alpha, sizeof(alpha));
116 if (beta)
117 addattr_l(n, 1024, TCA_PIE_BETA, &beta, sizeof(beta));
118 if (ecn != -1)
119 addattr_l(n, 1024, TCA_PIE_ECN, &ecn, sizeof(ecn));
120 if (bytemode != -1)
121 addattr_l(n, 1024, TCA_PIE_BYTEMODE, &bytemode,
122 sizeof(bytemode));
123
124 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
125 return 0;
126 }
127
128 static int pie_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
129 {
130 struct rtattr *tb[TCA_PIE_MAX + 1];
131 unsigned int limit;
132 unsigned int tupdate;
133 unsigned int target;
134 unsigned int alpha;
135 unsigned int beta;
136 unsigned int ecn;
137 unsigned int bytemode;
138
139 SPRINT_BUF(b1);
140
141 if (opt == NULL)
142 return 0;
143
144 parse_rtattr_nested(tb, TCA_PIE_MAX, opt);
145
146 if (tb[TCA_PIE_LIMIT] &&
147 RTA_PAYLOAD(tb[TCA_PIE_LIMIT]) >= sizeof(__u32)) {
148 limit = rta_getattr_u32(tb[TCA_PIE_LIMIT]);
149 fprintf(f, "limit %up ", limit);
150 }
151 if (tb[TCA_PIE_TARGET] &&
152 RTA_PAYLOAD(tb[TCA_PIE_TARGET]) >= sizeof(__u32)) {
153 target = rta_getattr_u32(tb[TCA_PIE_TARGET]);
154 fprintf(f, "target %s ", sprint_time(target, b1));
155 }
156 if (tb[TCA_PIE_TUPDATE] &&
157 RTA_PAYLOAD(tb[TCA_PIE_TUPDATE]) >= sizeof(__u32)) {
158 tupdate = rta_getattr_u32(tb[TCA_PIE_TUPDATE]);
159 fprintf(f, "tupdate %s ", sprint_time(tupdate, b1));
160 }
161 if (tb[TCA_PIE_ALPHA] &&
162 RTA_PAYLOAD(tb[TCA_PIE_ALPHA]) >= sizeof(__u32)) {
163 alpha = rta_getattr_u32(tb[TCA_PIE_ALPHA]);
164 fprintf(f, "alpha %u ", alpha);
165 }
166 if (tb[TCA_PIE_BETA] &&
167 RTA_PAYLOAD(tb[TCA_PIE_BETA]) >= sizeof(__u32)) {
168 beta = rta_getattr_u32(tb[TCA_PIE_BETA]);
169 fprintf(f, "beta %u ", beta);
170 }
171
172 if (tb[TCA_PIE_ECN] && RTA_PAYLOAD(tb[TCA_PIE_ECN]) >= sizeof(__u32)) {
173 ecn = rta_getattr_u32(tb[TCA_PIE_ECN]);
174 if (ecn)
175 fprintf(f, "ecn ");
176 }
177
178 if (tb[TCA_PIE_BYTEMODE] &&
179 RTA_PAYLOAD(tb[TCA_PIE_BYTEMODE]) >= sizeof(__u32)) {
180 bytemode = rta_getattr_u32(tb[TCA_PIE_BYTEMODE]);
181 if (bytemode)
182 fprintf(f, "bytemode ");
183 }
184
185 return 0;
186 }
187
188 static int pie_print_xstats(struct qdisc_util *qu, FILE *f,
189 struct rtattr *xstats)
190 {
191 struct tc_pie_xstats *st;
192
193 if (xstats == NULL)
194 return 0;
195
196 if (RTA_PAYLOAD(xstats) < sizeof(*st))
197 return -1;
198
199 st = RTA_DATA(xstats);
200 /*prob is returned as a fracion of maximum integer value */
201 fprintf(f, "prob %f delay %uus avg_dq_rate %u\n",
202 (double)st->prob / (double)0xffffffff, st->delay,
203 st->avg_dq_rate);
204 fprintf(f, "pkts_in %u overlimit %u dropped %u maxq %u ecn_mark %u\n",
205 st->packets_in, st->overlimit, st->dropped, st->maxq,
206 st->ecn_mark);
207 return 0;
208
209 }
210
211 struct qdisc_util pie_qdisc_util = {
212 .id = "pie",
213 .parse_qopt = pie_parse_opt,
214 .print_qopt = pie_print_opt,
215 .print_xstats = pie_print_xstats,
216 };