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