blob: 72cfff669faa4b4a387d296c2f23be42e1bc5b96 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * q_tbf.c TBF.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
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>
22
23#include "utils.h"
24#include "tc_util.h"
25
26static void explain(void)
27{
28 fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +010029 fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ] ");
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020030 fprintf(stderr, "[ overhead BYTES ] [ linklayer TYPE ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000031}
32
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000033static void explain1(const char *arg, const char *val)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034{
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000035 fprintf(stderr, "tbf: illegal value for \"%s\": \"%s\"\n", arg, val);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036}
37
38
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000039static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
40{
41 int ok=0;
42 struct tc_tbf_qopt opt;
43 __u32 rtab[256];
44 __u32 ptab[256];
45 unsigned buffer=0, mtu=0, mpu=0, latency=0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -080046 int Rcell_log=-1, Pcell_log = -1;
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +010047 unsigned short overhead=0;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020048 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000049 struct rtattr *tail;
50
51 memset(&opt, 0, sizeof(opt));
52
53 while (argc > 0) {
54 if (matches(*argv, "limit") == 0) {
55 NEXT_ARG();
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000056 if (opt.limit) {
57 fprintf(stderr, "tbf: duplicate \"limit\" specification\n");
58 return -1;
59 }
60 if (latency) {
61 fprintf(stderr, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 return -1;
63 }
64 if (get_size(&opt.limit, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000065 explain1("limit", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000066 return -1;
67 }
68 ok++;
69 } else if (matches(*argv, "latency") == 0) {
70 NEXT_ARG();
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000071 if (latency) {
72 fprintf(stderr, "tbf: duplicate \"latency\" specification\n");
73 return -1;
74 }
75 if (opt.limit) {
76 fprintf(stderr, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000077 return -1;
78 }
Patrick McHardy8f34caa2007-03-04 20:15:00 +010079 if (get_time(&latency, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000080 explain1("latency", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 return -1;
82 }
83 ok++;
84 } else if (matches(*argv, "burst") == 0 ||
85 strcmp(*argv, "buffer") == 0 ||
86 strcmp(*argv, "maxburst") == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000087 const char *parm_name = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000088 NEXT_ARG();
89 if (buffer) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000090 fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000091 return -1;
92 }
93 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000094 explain1(parm_name, *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000095 return -1;
96 }
97 ok++;
98 } else if (strcmp(*argv, "mtu") == 0 ||
99 strcmp(*argv, "minburst") == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000100 const char *parm_name = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101 NEXT_ARG();
102 if (mtu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000103 fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104 return -1;
105 }
106 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000107 explain1(parm_name, *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 return -1;
109 }
110 ok++;
111 } else if (strcmp(*argv, "mpu") == 0) {
112 NEXT_ARG();
113 if (mpu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000114 fprintf(stderr, "tbf: duplicate \"mpu\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000115 return -1;
116 }
117 if (get_size(&mpu, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000118 explain1("mpu", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000119 return -1;
120 }
121 ok++;
122 } else if (strcmp(*argv, "rate") == 0) {
123 NEXT_ARG();
124 if (opt.rate.rate) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000125 fprintf(stderr, "tbf: duplicate \"rate\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000126 return -1;
127 }
128 if (get_rate(&opt.rate.rate, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000129 explain1("rate", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130 return -1;
131 }
132 ok++;
133 } else if (matches(*argv, "peakrate") == 0) {
134 NEXT_ARG();
135 if (opt.peakrate.rate) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000136 fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000137 return -1;
138 }
139 if (get_rate(&opt.peakrate.rate, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000140 explain1("peakrate", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000141 return -1;
142 }
143 ok++;
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100144 } else if (matches(*argv, "overhead") == 0) {
145 NEXT_ARG();
146 if (overhead) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000147 fprintf(stderr, "tbf: duplicate \"overhead\" specification\n");
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100148 return -1;
149 }
150 if (get_u16(&overhead, *argv, 10)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000151 explain1("overhead", *argv); return -1;
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100152 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200153 } else if (matches(*argv, "linklayer") == 0) {
154 NEXT_ARG();
155 if (get_linklayer(&linklayer, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000156 explain1("linklayer", *argv); return -1;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200157 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158 } else if (strcmp(*argv, "help") == 0) {
159 explain();
160 return -1;
161 } else {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000162 fprintf(stderr, "tbf: unknown parameter \"%s\"\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163 explain();
164 return -1;
165 }
166 argc--; argv++;
167 }
168
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000169 int verdict = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000171 /* Be nice to the user: try to emit all error messages in
172 * one go rather than reveal one more problem when a
173 * previous one has been fixed.
174 */
175 if (opt.rate.rate == 0) {
176 fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n");
177 verdict = -1;
178 }
179 if (!buffer) {
180 fprintf(stderr, "tbf: the \"burst\" parameter is mandatory.\n");
181 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000182 }
183 if (opt.peakrate.rate) {
184 if (!mtu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000185 fprintf(stderr, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n");
186 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000187 }
188 }
189
190 if (opt.limit == 0 && latency == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000191 fprintf(stderr, "tbf: either \"limit\" or \"latency\" is required.\n");
192 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000193 }
194
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000195 if (verdict != 0) {
196 explain();
197 return verdict;
198 }
199
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200 if (opt.limit == 0) {
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100201 double lim = opt.rate.rate*(double)latency/TIME_UNITS_PER_SEC + buffer;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000202 if (opt.peakrate.rate) {
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100203 double lim2 = opt.peakrate.rate*(double)latency/TIME_UNITS_PER_SEC + mtu;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 if (lim2 < lim)
205 lim = lim2;
206 }
207 opt.limit = lim;
208 }
209
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100210 opt.rate.mpu = mpu;
211 opt.rate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200212 if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000213 fprintf(stderr, "tbf: failed to calculate rate table.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214 return -1;
215 }
216 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200217
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218 if (opt.peakrate.rate) {
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100219 opt.peakrate.mpu = mpu;
220 opt.peakrate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200221 if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000222 fprintf(stderr, "tbf: failed to calculate peak rate table.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000223 return -1;
224 }
225 opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226 }
227
4!tgraf228569c2005-01-18 01:24:18 +0000228 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
230 addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
231 addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
232 if (opt.peakrate.rate)
233 addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
4!tgraf228569c2005-01-18 01:24:18 +0000234 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000235 return 0;
236}
237
238static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
239{
240 struct rtattr *tb[TCA_TBF_PTAB+1];
241 struct tc_tbf_qopt *qopt;
242 double buffer, mtu;
243 double latency;
244 SPRINT_BUF(b1);
245 SPRINT_BUF(b2);
246
247 if (opt == NULL)
248 return 0;
249
4!tgraf3b3ecd32005-01-18 22:11:58 +0000250 parse_rtattr_nested(tb, TCA_TBF_PTAB, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000251
252 if (tb[TCA_TBF_PARMS] == NULL)
253 return -1;
254
255 qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
256 if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
257 return -1;
258 fprintf(f, "rate %s ", sprint_rate(qopt->rate.rate, b1));
Patrick McHardy76dc0aa2007-03-04 20:14:57 +0100259 buffer = tc_calc_xmitsize(qopt->rate.rate, qopt->buffer);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260 if (show_details) {
261 fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
262 1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
263 } else {
264 fprintf(f, "burst %s ", sprint_size(buffer, b1));
265 }
266 if (show_raw)
267 fprintf(f, "[%08x] ", qopt->buffer);
268 if (qopt->peakrate.rate) {
269 fprintf(f, "peakrate %s ", sprint_rate(qopt->peakrate.rate, b1));
270 if (qopt->mtu || qopt->peakrate.mpu) {
Patrick McHardy76dc0aa2007-03-04 20:14:57 +0100271 mtu = tc_calc_xmitsize(qopt->peakrate.rate, qopt->mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000272 if (show_details) {
273 fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
274 1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
275 } else {
276 fprintf(f, "minburst %s ", sprint_size(mtu, b1));
277 }
278 if (show_raw)
279 fprintf(f, "[%08x] ", qopt->mtu);
280 }
281 }
282
283 if (show_raw)
284 fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
285
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100286 latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)qopt->rate.rate) - tc_core_tick2time(qopt->buffer);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000287 if (qopt->peakrate.rate) {
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100288 double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)qopt->peakrate.rate) - tc_core_tick2time(qopt->mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000289 if (lat2 > latency)
290 latency = lat2;
291 }
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100292 fprintf(f, "lat %s ", sprint_time(latency, b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100294 if (qopt->rate.overhead) {
295 fprintf(f, "overhead %d", qopt->rate.overhead);
296 }
297
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 return 0;
299}
300
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000301struct qdisc_util tbf_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000302 .id = "tbf",
303 .parse_qopt = tbf_parse_opt,
304 .print_qopt = tbf_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305};
306