Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iproute2 / tc / q_tbf.c
blobb9f54b22aad7263f335999b2a2a4c993b3bc2311
1 /*
2 * q_tbf.c TBF.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
23 #include "utils.h"
24 #include "tc_util.h"
26 static void explain(void)
28 fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
29 fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ]\n");
32 static void explain1(char *arg)
34 fprintf(stderr, "Illegal \"%s\"\n", arg);
38 #define usage() return(-1)
40 static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
42 int ok=0;
43 struct tc_tbf_qopt opt;
44 __u32 rtab[256];
45 __u32 ptab[256];
46 unsigned buffer=0, mtu=0, latency=0;
47 __u8 mpu=0;
48 __s8 overhead=0;
49 int atm=0;
50 int Rcell_log=-1, Pcell_log = -1;
51 struct rtattr *tail;
53 memset(&opt, 0, sizeof(opt));
55 while (argc > 0) {
56 if (matches(*argv, "limit") == 0) {
57 NEXT_ARG();
58 if (opt.limit || latency) {
59 fprintf(stderr, "Double \"limit/latency\" spec\n");
60 return -1;
62 if (get_size(&opt.limit, *argv)) {
63 explain1("limit");
64 return -1;
66 ok++;
67 } else if (matches(*argv, "latency") == 0) {
68 NEXT_ARG();
69 if (opt.limit || latency) {
70 fprintf(stderr, "Double \"limit/latency\" spec\n");
71 return -1;
73 if (get_usecs(&latency, *argv)) {
74 explain1("latency");
75 return -1;
77 ok++;
78 } else if (matches(*argv, "burst") == 0 ||
79 strcmp(*argv, "buffer") == 0 ||
80 strcmp(*argv, "maxburst") == 0) {
81 NEXT_ARG();
82 if (buffer) {
83 fprintf(stderr, "Double \"buffer/burst\" spec\n");
84 return -1;
86 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
87 explain1("buffer");
88 return -1;
90 ok++;
91 } else if (strcmp(*argv, "mtu") == 0 ||
92 strcmp(*argv, "minburst") == 0) {
93 NEXT_ARG();
94 if (mtu) {
95 fprintf(stderr, "Double \"mtu/minburst\" spec\n");
96 return -1;
98 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
99 explain1("mtu");
100 return -1;
102 ok++;
103 } else if (strcmp(*argv, "mpu") == 0) {
104 NEXT_ARG();
105 if (mpu) {
106 fprintf(stderr, "Double \"mpu\" spec\n");
107 return -1;
109 if (get_u8(&mpu, *argv, 10)) {
110 explain1("mpu");
111 return -1;
113 ok++;
114 } else if (strcmp(*argv, "rate") == 0) {
115 NEXT_ARG();
116 if (opt.rate.rate) {
117 fprintf(stderr, "Double \"rate\" spec\n");
118 return -1;
120 if (get_rate(&opt.rate.rate, *argv)) {
121 explain1("rate");
122 return -1;
124 ok++;
125 } else if (strcmp(*argv, "overhead") == 0) {
126 NEXT_ARG();
127 if (overhead) {
128 fprintf(stderr, "Double \"overhead\" spec\n");
129 return -1;
131 if (get_s8(&overhead, *argv, 10)) {
132 explain1("overhead");
133 return -1;
135 ok++;
136 } else if (strcmp(*argv, "atm") == 0) {
137 atm = 1;
138 ok++;
139 } else if (matches(*argv, "peakrate") == 0) {
140 NEXT_ARG();
141 if (opt.peakrate.rate) {
142 fprintf(stderr, "Double \"peakrate\" spec\n");
143 return -1;
145 if (get_rate(&opt.peakrate.rate, *argv)) {
146 explain1("peakrate");
147 return -1;
149 ok++;
150 } else if (strcmp(*argv, "help") == 0) {
151 explain();
152 return -1;
153 } else {
154 fprintf(stderr, "What is \"%s\"?\n", *argv);
155 explain();
156 return -1;
158 argc--; argv++;
161 if (!ok)
162 return 0;
164 if (opt.rate.rate == 0 || !buffer) {
165 fprintf(stderr, "Both \"rate\" and \"burst\" are required.\n");
166 return -1;
168 if (opt.peakrate.rate) {
169 if (!mtu) {
170 fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
171 return -1;
175 if (opt.limit == 0 && latency == 0) {
176 fprintf(stderr, "Either \"limit\" or \"latency\" are required.\n");
177 return -1;
180 if (opt.limit == 0) {
181 double lim = opt.rate.rate*(double)latency/1000000 + buffer;
182 if (opt.peakrate.rate) {
183 double lim2 = opt.peakrate.rate*(double)latency/1000000 + mtu;
184 if (lim2 < lim)
185 lim = lim2;
187 opt.limit = lim;
190 tc_calc_ratespec(&opt.rate, rtab, opt.rate.rate, Rcell_log, mtu, mpu, atm, overhead);
191 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
192 if (opt.peakrate.rate) {
193 tc_calc_ratespec(&opt.peakrate, ptab, opt.peakrate.rate, Pcell_log, mtu, mpu, atm, overhead);
194 opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
197 tail = NLMSG_TAIL(n);
198 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
199 addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
200 addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
201 if (opt.peakrate.rate)
202 addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
203 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
204 return 0;
207 static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
209 struct rtattr *tb[TCA_TBF_PTAB+1];
210 struct tc_tbf_qopt *qopt;
211 double buffer, mtu;
212 double latency;
213 SPRINT_BUF(b1);
214 SPRINT_BUF(b2);
216 if (opt == NULL)
217 return 0;
219 parse_rtattr_nested(tb, TCA_TBF_PTAB, opt);
221 if (tb[TCA_TBF_PARMS] == NULL)
222 return -1;
224 qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
225 if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
226 return -1;
227 fprintf(f, "rate %s ", sprint_rate(qopt->rate.rate, b1));
228 buffer = ((double)qopt->rate.rate*tc_core_tick2usec(qopt->buffer))/1000000;
229 if (show_details) {
230 fprintf(f, "burst %s/%u mpu %s overhead %d ", sprint_size(buffer, b1),
231 1<<qopt->rate.cell_log,
232 sprint_size(qopt->rate.mpu & 0xFF, b2),
233 (__s8)(qopt->rate.mpu >> 8));
234 if (qopt->rate.feature & 0x0001)
235 fprintf(f, "atm ");
236 } else {
237 fprintf(f, "burst %s ", sprint_size(buffer, b1));
239 if (show_raw)
240 fprintf(f, "[%08x] ", qopt->buffer);
241 if (qopt->peakrate.rate) {
242 fprintf(f, "peakrate %s ", sprint_rate(qopt->peakrate.rate, b1));
243 if (qopt->mtu || qopt->peakrate.mpu) {
244 mtu = ((double)qopt->peakrate.rate*tc_core_tick2usec(qopt->mtu))/1000000;
245 if (show_details) {
246 fprintf(f, "mtu %s/%u mpu %s overhead %d ", sprint_size(mtu, b1),
247 1<<qopt->peakrate.cell_log,
248 sprint_size(qopt->peakrate.mpu & 0xFF, b2),
249 (__s8)(qopt->peakrate.mpu >> 8));
250 if (qopt->peakrate.feature & 0x0001)
251 fprintf(f, "atm ");
252 } else {
253 fprintf(f, "minburst %s ", sprint_size(mtu, b1));
255 if (show_raw)
256 fprintf(f, "[%08x] ", qopt->mtu);
260 if (show_raw)
261 fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
263 latency = 1000000*(qopt->limit/(double)qopt->rate.rate) - tc_core_tick2usec(qopt->buffer);
264 if (qopt->peakrate.rate) {
265 double lat2 = 1000000*(qopt->limit/(double)qopt->peakrate.rate) - tc_core_tick2usec(qopt->mtu);
266 if (lat2 > latency)
267 latency = lat2;
269 fprintf(f, "lat %s ", sprint_usecs(tc_core_tick2usec(latency), b1));
271 return 0;
274 struct qdisc_util tbf_qdisc_util = {
275 .id = "tbf",
276 .parse_qopt = tbf_parse_opt,
277 .print_qopt = tbf_print_opt,