]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_police.c
lib: introduce print_nl
[mirror_iproute2.git] / tc / m_police.c
1 /*
2 * m_police.c Parse/print policing module options.
3 *
4 * This program is free software; you can u32istribute 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 * FIXES: 19990619 - J Hadi Salim (hadi@cyberus.ca)
11 * simple addattr packaging fix.
12 * 2002: J Hadi Salim - Add tc action extensions syntax
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24
25 #include "utils.h"
26 #include "tc_util.h"
27
28 struct action_util police_action_util = {
29 .id = "police",
30 .parse_aopt = act_parse_police,
31 .print_aopt = print_police,
32 };
33
34 static void usage(void)
35 {
36 fprintf(stderr, "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n");
37 fprintf(stderr, " [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n");
38 fprintf(stderr, " [ linklayer TYPE ] [ CONTROL ]\n");
39
40 fprintf(stderr, "Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n");
41 fprintf(stderr, " Define how to handle packets which exceed (<EXCEEDACT>)\n");
42 fprintf(stderr, " or conform (<NOTEXCEEDACT>) the configured bandwidth limit.\n");
43 fprintf(stderr, " EXCEEDACT/NOTEXCEEDACT := { pipe | ok | reclassify | drop | continue |\n");
44 fprintf(stderr, " goto chain <CHAIN_INDEX> }\n");
45 exit(-1);
46 }
47
48 static void explain1(char *arg)
49 {
50 fprintf(stderr, "Illegal \"%s\"\n", arg);
51 }
52
53 int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
54 int tca_id, struct nlmsghdr *n)
55 {
56 int argc = *argc_p;
57 char **argv = *argv_p;
58 int res = -1;
59 int ok = 0;
60 struct tc_police p = { .action = TC_POLICE_RECLASSIFY };
61 __u32 rtab[256];
62 __u32 ptab[256];
63 __u32 avrate = 0;
64 int presult = 0;
65 unsigned buffer = 0, mtu = 0, mpu = 0;
66 unsigned short overhead = 0;
67 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
68 int Rcell_log = -1, Pcell_log = -1;
69 struct rtattr *tail;
70
71 if (a) /* new way of doing things */
72 NEXT_ARG();
73
74 if (argc <= 0)
75 return -1;
76
77 while (argc > 0) {
78
79 if (matches(*argv, "index") == 0) {
80 NEXT_ARG();
81 if (get_u32(&p.index, *argv, 10)) {
82 fprintf(stderr, "Illegal \"index\"\n");
83 return -1;
84 }
85 } else if (matches(*argv, "burst") == 0 ||
86 strcmp(*argv, "buffer") == 0 ||
87 strcmp(*argv, "maxburst") == 0) {
88 NEXT_ARG();
89 if (buffer) {
90 fprintf(stderr, "Double \"buffer/burst\" spec\n");
91 return -1;
92 }
93 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
94 explain1("buffer");
95 return -1;
96 }
97 } else if (strcmp(*argv, "mtu") == 0 ||
98 strcmp(*argv, "minburst") == 0) {
99 NEXT_ARG();
100 if (mtu) {
101 fprintf(stderr, "Double \"mtu/minburst\" spec\n");
102 return -1;
103 }
104 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
105 explain1("mtu");
106 return -1;
107 }
108 } else if (strcmp(*argv, "mpu") == 0) {
109 NEXT_ARG();
110 if (mpu) {
111 fprintf(stderr, "Double \"mpu\" spec\n");
112 return -1;
113 }
114 if (get_size(&mpu, *argv)) {
115 explain1("mpu");
116 return -1;
117 }
118 } else if (strcmp(*argv, "rate") == 0) {
119 NEXT_ARG();
120 if (p.rate.rate) {
121 fprintf(stderr, "Double \"rate\" spec\n");
122 return -1;
123 }
124 if (get_rate(&p.rate.rate, *argv)) {
125 explain1("rate");
126 return -1;
127 }
128 } else if (strcmp(*argv, "avrate") == 0) {
129 NEXT_ARG();
130 if (avrate) {
131 fprintf(stderr, "Double \"avrate\" spec\n");
132 return -1;
133 }
134 if (get_rate(&avrate, *argv)) {
135 explain1("avrate");
136 return -1;
137 }
138 } else if (matches(*argv, "peakrate") == 0) {
139 NEXT_ARG();
140 if (p.peakrate.rate) {
141 fprintf(stderr, "Double \"peakrate\" spec\n");
142 return -1;
143 }
144 if (get_rate(&p.peakrate.rate, *argv)) {
145 explain1("peakrate");
146 return -1;
147 }
148 } else if (matches(*argv, "reclassify") == 0 ||
149 matches(*argv, "drop") == 0 ||
150 matches(*argv, "shot") == 0 ||
151 matches(*argv, "continue") == 0 ||
152 matches(*argv, "pass") == 0 ||
153 matches(*argv, "ok") == 0 ||
154 matches(*argv, "pipe") == 0 ||
155 matches(*argv, "goto") == 0) {
156 if (!parse_action_control(&argc, &argv, &p.action, false))
157 goto action_ctrl_ok;
158 return -1;
159 } else if (strcmp(*argv, "conform-exceed") == 0) {
160 NEXT_ARG();
161 if (!parse_action_control_slash(&argc, &argv, &p.action,
162 &presult, true))
163 goto action_ctrl_ok;
164 return -1;
165 } else if (matches(*argv, "overhead") == 0) {
166 NEXT_ARG();
167 if (get_u16(&overhead, *argv, 10)) {
168 explain1("overhead"); return -1;
169 }
170 } else if (matches(*argv, "linklayer") == 0) {
171 NEXT_ARG();
172 if (get_linklayer(&linklayer, *argv)) {
173 explain1("linklayer"); return -1;
174 }
175 } else if (strcmp(*argv, "help") == 0) {
176 usage();
177 } else {
178 break;
179 }
180 NEXT_ARG_FWD();
181 action_ctrl_ok:
182 ok++;
183 }
184
185 if (!ok)
186 return -1;
187
188 if (p.rate.rate && avrate)
189 return -1;
190
191 /* Must at least do late binding, use TB or ewma policing */
192 if (!p.rate.rate && !avrate && !p.index) {
193 fprintf(stderr, "\"rate\" or \"avrate\" MUST be specified.\n");
194 return -1;
195 }
196
197 /* When the TB policer is used, burst is required */
198 if (p.rate.rate && !buffer && !avrate) {
199 fprintf(stderr, "\"burst\" requires \"rate\".\n");
200 return -1;
201 }
202
203 if (p.peakrate.rate) {
204 if (!p.rate.rate) {
205 fprintf(stderr, "\"peakrate\" requires \"rate\".\n");
206 return -1;
207 }
208 if (!mtu) {
209 fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
210 return -1;
211 }
212 }
213
214 if (p.rate.rate) {
215 p.rate.mpu = mpu;
216 p.rate.overhead = overhead;
217 if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu,
218 linklayer) < 0) {
219 fprintf(stderr, "POLICE: failed to calculate rate table.\n");
220 return -1;
221 }
222 p.burst = tc_calc_xmittime(p.rate.rate, buffer);
223 }
224 p.mtu = mtu;
225 if (p.peakrate.rate) {
226 p.peakrate.mpu = mpu;
227 p.peakrate.overhead = overhead;
228 if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu,
229 linklayer) < 0) {
230 fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
231 return -1;
232 }
233 }
234
235 tail = addattr_nest(n, MAX_MSG, tca_id);
236 addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p));
237 if (p.rate.rate)
238 addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024);
239 if (p.peakrate.rate)
240 addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024);
241 if (avrate)
242 addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate);
243 if (presult)
244 addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);
245
246 addattr_nest_end(n, tail);
247 res = 0;
248
249 *argc_p = argc;
250 *argv_p = argv;
251 return res;
252 }
253
254 int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
255 {
256 return act_parse_police(NULL, argc_p, argv_p, tca_id, n);
257 }
258
259 int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
260 {
261 SPRINT_BUF(b1);
262 SPRINT_BUF(b2);
263 struct tc_police *p;
264 struct rtattr *tb[TCA_POLICE_MAX+1];
265 unsigned int buffer;
266 unsigned int linklayer;
267
268 if (arg == NULL)
269 return 0;
270
271 parse_rtattr_nested(tb, TCA_POLICE_MAX, arg);
272
273 if (tb[TCA_POLICE_TBF] == NULL) {
274 fprintf(f, "[NULL police tbf]");
275 return 0;
276 }
277 #ifndef STOOPID_8BYTE
278 if (RTA_PAYLOAD(tb[TCA_POLICE_TBF]) < sizeof(*p)) {
279 fprintf(f, "[truncated police tbf]");
280 return -1;
281 }
282 #endif
283 p = RTA_DATA(tb[TCA_POLICE_TBF]);
284
285 fprintf(f, " police 0x%x ", p->index);
286 fprintf(f, "rate %s ", sprint_rate(p->rate.rate, b1));
287 buffer = tc_calc_xmitsize(p->rate.rate, p->burst);
288 fprintf(f, "burst %s ", sprint_size(buffer, b1));
289 fprintf(f, "mtu %s ", sprint_size(p->mtu, b1));
290 if (show_raw)
291 fprintf(f, "[%08x] ", p->burst);
292
293 if (p->peakrate.rate)
294 fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1));
295
296 if (tb[TCA_POLICE_AVRATE])
297 fprintf(f, "avrate %s ",
298 sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]),
299 b1));
300
301 print_action_control(f, "action ", p->action, "");
302
303 if (tb[TCA_POLICE_RESULT]) {
304 __u32 action = rta_getattr_u32(tb[TCA_POLICE_RESULT]);
305
306 print_action_control(f, "/", action, " ");
307 } else
308 fprintf(f, " ");
309
310 fprintf(f, "overhead %ub ", p->rate.overhead);
311 linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK);
312 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
313 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
314 fprintf(f, "\n\tref %d bind %d", p->refcnt, p->bindcnt);
315 if (show_stats) {
316 if (tb[TCA_POLICE_TM]) {
317 struct tcf_t *tm = RTA_DATA(tb[TCA_POLICE_TM]);
318
319 print_tm(f, tm);
320 }
321 }
322 fprintf(f, "\n");
323
324
325 return 0;
326 }
327
328 int tc_print_police(FILE *f, struct rtattr *arg)
329 {
330 return print_police(&police_action_util, f, arg);
331 }