]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_tbf.c
Do not strip binaries with `install`
[mirror_iproute2.git] / tc / q_tbf.c
CommitLineData
aba5acdf
SH
1/*
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");
29 fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ]\n");
30}
31
32static void explain1(char *arg)
33{
34 fprintf(stderr, "Illegal \"%s\"\n", arg);
35}
36
37
38#define usage() return(-1)
39
40static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
41{
42 int ok=0;
43 struct tc_tbf_qopt opt;
44 __u32 rtab[256];
45 __u32 ptab[256];
46 unsigned buffer=0, mtu=0, mpu=0, latency=0;
ae665a52 47 int Rcell_log=-1, Pcell_log = -1;
aba5acdf
SH
48 struct rtattr *tail;
49
50 memset(&opt, 0, sizeof(opt));
51
52 while (argc > 0) {
53 if (matches(*argv, "limit") == 0) {
54 NEXT_ARG();
55 if (opt.limit || latency) {
56 fprintf(stderr, "Double \"limit/latency\" spec\n");
57 return -1;
58 }
59 if (get_size(&opt.limit, *argv)) {
60 explain1("limit");
61 return -1;
62 }
63 ok++;
64 } else if (matches(*argv, "latency") == 0) {
65 NEXT_ARG();
66 if (opt.limit || latency) {
67 fprintf(stderr, "Double \"limit/latency\" spec\n");
68 return -1;
69 }
8f34caaf 70 if (get_time(&latency, *argv)) {
aba5acdf
SH
71 explain1("latency");
72 return -1;
73 }
74 ok++;
75 } else if (matches(*argv, "burst") == 0 ||
76 strcmp(*argv, "buffer") == 0 ||
77 strcmp(*argv, "maxburst") == 0) {
78 NEXT_ARG();
79 if (buffer) {
80 fprintf(stderr, "Double \"buffer/burst\" spec\n");
81 return -1;
82 }
83 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
84 explain1("buffer");
85 return -1;
86 }
87 ok++;
88 } else if (strcmp(*argv, "mtu") == 0 ||
89 strcmp(*argv, "minburst") == 0) {
90 NEXT_ARG();
91 if (mtu) {
92 fprintf(stderr, "Double \"mtu/minburst\" spec\n");
93 return -1;
94 }
95 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
96 explain1("mtu");
97 return -1;
98 }
99 ok++;
100 } else if (strcmp(*argv, "mpu") == 0) {
101 NEXT_ARG();
102 if (mpu) {
103 fprintf(stderr, "Double \"mpu\" spec\n");
104 return -1;
105 }
106 if (get_size(&mpu, *argv)) {
107 explain1("mpu");
108 return -1;
109 }
110 ok++;
111 } else if (strcmp(*argv, "rate") == 0) {
112 NEXT_ARG();
113 if (opt.rate.rate) {
114 fprintf(stderr, "Double \"rate\" spec\n");
115 return -1;
116 }
117 if (get_rate(&opt.rate.rate, *argv)) {
118 explain1("rate");
119 return -1;
120 }
121 ok++;
122 } else if (matches(*argv, "peakrate") == 0) {
123 NEXT_ARG();
124 if (opt.peakrate.rate) {
125 fprintf(stderr, "Double \"peakrate\" spec\n");
126 return -1;
127 }
128 if (get_rate(&opt.peakrate.rate, *argv)) {
129 explain1("peakrate");
130 return -1;
131 }
132 ok++;
133 } else if (strcmp(*argv, "help") == 0) {
134 explain();
135 return -1;
136 } else {
137 fprintf(stderr, "What is \"%s\"?\n", *argv);
138 explain();
139 return -1;
140 }
141 argc--; argv++;
142 }
143
144 if (!ok)
145 return 0;
146
147 if (opt.rate.rate == 0 || !buffer) {
148 fprintf(stderr, "Both \"rate\" and \"burst\" are required.\n");
149 return -1;
150 }
151 if (opt.peakrate.rate) {
152 if (!mtu) {
153 fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
154 return -1;
155 }
156 }
157
158 if (opt.limit == 0 && latency == 0) {
159 fprintf(stderr, "Either \"limit\" or \"latency\" are required.\n");
160 return -1;
161 }
162
163 if (opt.limit == 0) {
f0bda7e5 164 double lim = opt.rate.rate*(double)latency/TIME_UNITS_PER_SEC + buffer;
aba5acdf 165 if (opt.peakrate.rate) {
f0bda7e5 166 double lim2 = opt.peakrate.rate*(double)latency/TIME_UNITS_PER_SEC + mtu;
aba5acdf
SH
167 if (lim2 < lim)
168 lim = lim2;
169 }
170 opt.limit = lim;
171 }
172
d5f46f9c
JDB
173 opt.rate.mpu = mpu;
174 if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu) < 0) {
aba5acdf
SH
175 fprintf(stderr, "TBF: failed to calculate rate table.\n");
176 return -1;
177 }
178 opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
d5f46f9c 179
aba5acdf 180 if (opt.peakrate.rate) {
d5f46f9c
JDB
181 opt.peakrate.mpu = mpu;
182 if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu) < 0) {
aba5acdf
SH
183 fprintf(stderr, "TBF: failed to calculate peak rate table.\n");
184 return -1;
185 }
186 opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
aba5acdf
SH
187 }
188
228569c3 189 tail = NLMSG_TAIL(n);
aba5acdf
SH
190 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
191 addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
192 addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
193 if (opt.peakrate.rate)
194 addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
228569c3 195 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
196 return 0;
197}
198
199static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
200{
201 struct rtattr *tb[TCA_TBF_PTAB+1];
202 struct tc_tbf_qopt *qopt;
203 double buffer, mtu;
204 double latency;
205 SPRINT_BUF(b1);
206 SPRINT_BUF(b2);
207
208 if (opt == NULL)
209 return 0;
210
3b3ecd31 211 parse_rtattr_nested(tb, TCA_TBF_PTAB, opt);
aba5acdf
SH
212
213 if (tb[TCA_TBF_PARMS] == NULL)
214 return -1;
215
216 qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
217 if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
218 return -1;
219 fprintf(f, "rate %s ", sprint_rate(qopt->rate.rate, b1));
76dc0aa2 220 buffer = tc_calc_xmitsize(qopt->rate.rate, qopt->buffer);
aba5acdf
SH
221 if (show_details) {
222 fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
223 1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
224 } else {
225 fprintf(f, "burst %s ", sprint_size(buffer, b1));
226 }
227 if (show_raw)
228 fprintf(f, "[%08x] ", qopt->buffer);
229 if (qopt->peakrate.rate) {
230 fprintf(f, "peakrate %s ", sprint_rate(qopt->peakrate.rate, b1));
231 if (qopt->mtu || qopt->peakrate.mpu) {
76dc0aa2 232 mtu = tc_calc_xmitsize(qopt->peakrate.rate, qopt->mtu);
aba5acdf
SH
233 if (show_details) {
234 fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
235 1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
236 } else {
237 fprintf(f, "minburst %s ", sprint_size(mtu, b1));
238 }
239 if (show_raw)
240 fprintf(f, "[%08x] ", qopt->mtu);
241 }
242 }
243
244 if (show_raw)
245 fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
246
8f34caaf 247 latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)qopt->rate.rate) - tc_core_tick2time(qopt->buffer);
aba5acdf 248 if (qopt->peakrate.rate) {
8f34caaf 249 double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)qopt->peakrate.rate) - tc_core_tick2time(qopt->mtu);
aba5acdf
SH
250 if (lat2 > latency)
251 latency = lat2;
252 }
8f34caaf 253 fprintf(f, "lat %s ", sprint_time(latency, b1));
aba5acdf
SH
254
255 return 0;
256}
257
95812b56 258struct qdisc_util tbf_qdisc_util = {
f2f99e2e
SH
259 .id = "tbf",
260 .parse_qopt = tbf_parse_opt,
261 .print_qopt = tbf_print_opt,
aba5acdf
SH
262};
263