blob: 2d56331100d87bbaaca60d8f2b0ff631212f60cc [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;
Yang Yingliangddc62432013-11-26 09:55:35 +080050 __u64 rate64 = 0, prate64 = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000051
52 memset(&opt, 0, sizeof(opt));
53
54 while (argc > 0) {
55 if (matches(*argv, "limit") == 0) {
56 NEXT_ARG();
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000057 if (opt.limit) {
58 fprintf(stderr, "tbf: duplicate \"limit\" specification\n");
59 return -1;
60 }
61 if (latency) {
62 fprintf(stderr, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000063 return -1;
64 }
65 if (get_size(&opt.limit, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000066 explain1("limit", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000067 return -1;
68 }
69 ok++;
70 } else if (matches(*argv, "latency") == 0) {
71 NEXT_ARG();
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000072 if (latency) {
73 fprintf(stderr, "tbf: duplicate \"latency\" specification\n");
74 return -1;
75 }
76 if (opt.limit) {
77 fprintf(stderr, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000078 return -1;
79 }
Patrick McHardy8f34caa2007-03-04 20:15:00 +010080 if (get_time(&latency, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000081 explain1("latency", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082 return -1;
83 }
84 ok++;
85 } else if (matches(*argv, "burst") == 0 ||
86 strcmp(*argv, "buffer") == 0 ||
87 strcmp(*argv, "maxburst") == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000088 const char *parm_name = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000089 NEXT_ARG();
90 if (buffer) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000091 fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000092 return -1;
93 }
94 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +000095 explain1(parm_name, *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000096 return -1;
97 }
98 ok++;
99 } else if (strcmp(*argv, "mtu") == 0 ||
100 strcmp(*argv, "minburst") == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000101 const char *parm_name = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000102 NEXT_ARG();
103 if (mtu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000104 fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000105 return -1;
106 }
107 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000108 explain1(parm_name, *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000109 return -1;
110 }
111 ok++;
112 } else if (strcmp(*argv, "mpu") == 0) {
113 NEXT_ARG();
114 if (mpu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000115 fprintf(stderr, "tbf: duplicate \"mpu\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116 return -1;
117 }
118 if (get_size(&mpu, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000119 explain1("mpu", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000120 return -1;
121 }
122 ok++;
123 } else if (strcmp(*argv, "rate") == 0) {
124 NEXT_ARG();
Yang Yingliangddc62432013-11-26 09:55:35 +0800125 if (rate64) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000126 fprintf(stderr, "tbf: duplicate \"rate\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127 return -1;
128 }
Yang Yingliangddc62432013-11-26 09:55:35 +0800129 if (get_rate64(&rate64, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000130 explain1("rate", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 return -1;
132 }
133 ok++;
134 } else if (matches(*argv, "peakrate") == 0) {
135 NEXT_ARG();
Yang Yingliangddc62432013-11-26 09:55:35 +0800136 if (prate64) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000137 fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000138 return -1;
139 }
Yang Yingliangddc62432013-11-26 09:55:35 +0800140 if (get_rate64(&prate64, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000141 explain1("peakrate", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142 return -1;
143 }
144 ok++;
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100145 } else if (matches(*argv, "overhead") == 0) {
146 NEXT_ARG();
147 if (overhead) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000148 fprintf(stderr, "tbf: duplicate \"overhead\" specification\n");
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100149 return -1;
150 }
151 if (get_u16(&overhead, *argv, 10)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000152 explain1("overhead", *argv); return -1;
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100153 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200154 } else if (matches(*argv, "linklayer") == 0) {
155 NEXT_ARG();
156 if (get_linklayer(&linklayer, *argv)) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000157 explain1("linklayer", *argv); return -1;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200158 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000159 } else if (strcmp(*argv, "help") == 0) {
160 explain();
161 return -1;
162 } else {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000163 fprintf(stderr, "tbf: unknown parameter \"%s\"\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000164 explain();
165 return -1;
166 }
167 argc--; argv++;
168 }
169
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000170 int verdict = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000171
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000172 /* Be nice to the user: try to emit all error messages in
173 * one go rather than reveal one more problem when a
174 * previous one has been fixed.
175 */
Yang Yingliangddc62432013-11-26 09:55:35 +0800176 if (rate64 == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000177 fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n");
178 verdict = -1;
179 }
180 if (!buffer) {
181 fprintf(stderr, "tbf: the \"burst\" parameter is mandatory.\n");
182 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000183 }
Yang Yingliangddc62432013-11-26 09:55:35 +0800184 if (prate64) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000185 if (!mtu) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000186 fprintf(stderr, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n");
187 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188 }
189 }
190
191 if (opt.limit == 0 && latency == 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000192 fprintf(stderr, "tbf: either \"limit\" or \"latency\" is required.\n");
193 verdict = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000194 }
195
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000196 if (verdict != 0) {
197 explain();
198 return verdict;
199 }
200
Yang Yingliangddc62432013-11-26 09:55:35 +0800201 opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
202 opt.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
203
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 if (opt.limit == 0) {
Yang Yingliangddc62432013-11-26 09:55:35 +0800205 double lim = rate64*(double)latency/TIME_UNITS_PER_SEC + buffer;
206 if (prate64) {
207 double lim2 = prate64*(double)latency/TIME_UNITS_PER_SEC + mtu;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000208 if (lim2 < lim)
209 lim = lim2;
210 }
211 opt.limit = lim;
212 }
213
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100214 opt.rate.mpu = mpu;
215 opt.rate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200216 if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000217 fprintf(stderr, "tbf: failed to calculate rate table.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218 return -1;
219 }
220 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200221
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000222 if (opt.peakrate.rate) {
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100223 opt.peakrate.mpu = mpu;
224 opt.peakrate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200225 if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
Kees van Reeuwijk3bed7bb2013-02-19 07:46:04 +0000226 fprintf(stderr, "tbf: failed to calculate peak rate table.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000227 return -1;
228 }
229 opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000230 }
231
4!tgraf228569c2005-01-18 01:24:18 +0000232 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000233 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
234 addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
Yang Yinglianga01de0a2014-01-16 11:09:13 +0800235 addattr_l(n, 2124, TCA_TBF_BURST, &buffer, sizeof(buffer));
Yang Yingliangddc62432013-11-26 09:55:35 +0800236 if (rate64 >= (1ULL << 32))
237 addattr_l(n, 2124, TCA_TBF_RATE64, &rate64, sizeof(rate64));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000238 addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
Yang Yingliangddc62432013-11-26 09:55:35 +0800239 if (opt.peakrate.rate) {
240 if (prate64 >= (1ULL << 32))
241 addattr_l(n, 3124, TCA_TBF_PRATE64, &prate64, sizeof(prate64));
Yang Yinglianga01de0a2014-01-16 11:09:13 +0800242 addattr_l(n, 3224, TCA_TBF_PBURST, &mtu, sizeof(mtu));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000243 addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
Yang Yingliangddc62432013-11-26 09:55:35 +0800244 }
4!tgraf228569c2005-01-18 01:24:18 +0000245 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000246 return 0;
247}
248
249static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
250{
Yang Yingliangddc62432013-11-26 09:55:35 +0800251 struct rtattr *tb[TCA_TBF_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000252 struct tc_tbf_qopt *qopt;
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200253 unsigned int linklayer;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254 double buffer, mtu;
255 double latency;
Yang Yingliangddc62432013-11-26 09:55:35 +0800256 __u64 rate64 = 0, prate64 = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257 SPRINT_BUF(b1);
258 SPRINT_BUF(b2);
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200259 SPRINT_BUF(b3);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260
261 if (opt == NULL)
262 return 0;
263
Yang Yingliangddc62432013-11-26 09:55:35 +0800264 parse_rtattr_nested(tb, TCA_TBF_MAX, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000265
266 if (tb[TCA_TBF_PARMS] == NULL)
267 return -1;
268
269 qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
270 if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
271 return -1;
Yang Yingliangddc62432013-11-26 09:55:35 +0800272 rate64 = qopt->rate.rate;
273 if (tb[TCA_TBF_RATE64] &&
274 RTA_PAYLOAD(tb[TCA_TBF_RATE64]) >= sizeof(rate64))
275 rate64 = rta_getattr_u64(tb[TCA_TBF_RATE64]);
276 fprintf(f, "rate %s ", sprint_rate(rate64, b1));
277 buffer = tc_calc_xmitsize(rate64, qopt->buffer);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278 if (show_details) {
279 fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
280 1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
281 } else {
282 fprintf(f, "burst %s ", sprint_size(buffer, b1));
283 }
284 if (show_raw)
285 fprintf(f, "[%08x] ", qopt->buffer);
Yang Yingliangddc62432013-11-26 09:55:35 +0800286 prate64 = qopt->peakrate.rate;
287 if (tb[TCA_TBF_PRATE64] &&
288 RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64))
289 prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]);
290 if (prate64) {
291 fprintf(f, "peakrate %s ", sprint_rate(prate64, b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000292 if (qopt->mtu || qopt->peakrate.mpu) {
Yang Yingliangddc62432013-11-26 09:55:35 +0800293 mtu = tc_calc_xmitsize(prate64, qopt->mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294 if (show_details) {
295 fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
296 1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
297 } else {
298 fprintf(f, "minburst %s ", sprint_size(mtu, b1));
299 }
300 if (show_raw)
301 fprintf(f, "[%08x] ", qopt->mtu);
302 }
303 }
304
Yang Yingliangddc62432013-11-26 09:55:35 +0800305 latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)rate64) - tc_core_tick2time(qopt->buffer);
306 if (prate64) {
307 double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)prate64) - tc_core_tick2time(qopt->mtu);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000308 if (lat2 > latency)
309 latency = lat2;
310 }
Sergey V. Lobanov3ff10e82014-05-11 03:01:14 +0400311 if (latency >= 0.0)
312 fprintf(f, "lat %s ", sprint_time(latency, b1));
313 if (show_raw || latency < 0.0)
314 fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100316 if (qopt->rate.overhead) {
317 fprintf(f, "overhead %d", qopt->rate.overhead);
318 }
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200319 linklayer = (qopt->rate.linklayer & TC_LINKLAYER_MASK);
320 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
321 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
Jesper Dangaard Brouer2c425792008-03-23 23:47:49 +0100322
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000323 return 0;
324}
325
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000326struct qdisc_util tbf_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000327 .id = "tbf",
328 .parse_qopt = tbf_parse_opt,
329 .print_qopt = tbf_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000330};
331