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