blob: 97558bd37ed55c0c5702ad3eb7cd80e1b7e699e6 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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>
Stephen Hemmingerae665a52006-12-05 10:10:22 -080010 * FIXES: 19990619 - J Hadi Salim (hadi@cyberus.ca)
11 * simple addattr packaging fix.
osdl.net!shemminger2373fde2004-08-13 23:54:55 +000012 * 2002: J Hadi Salim - Add tc action extensions syntax
13 *
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000014 */
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
net[shemminger]!kaber95812b52004-09-28 18:35:49 +000029struct action_util police_action_util = {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +000030 .id = "police",
31 .parse_aopt = act_parse_police,
32 .print_aopt = print_police,
osdl.net!shemminger2373fde2004-08-13 23:54:55 +000033};
34
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070035static void usage(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036{
37 fprintf(stderr, "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n");
Jesper Dangaard Brouerf71f75f2008-03-23 23:53:57 +010038 fprintf(stderr, " [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n");
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +020039 fprintf(stderr, " [ linklayer TYPE ] [ ACTIONTERM ]\n");
40
Stephen Hemminger32a121c2016-03-21 11:48:36 -070041 fprintf(stderr, "New Syntax ACTIONTERM := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n");
42 fprintf(stderr, "Where: *EXCEEDACT := pipe | ok | reclassify | drop | continue\n");
43 fprintf(stderr, "Where: pipe is only valid for new syntax\n");
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070044 exit(-1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045}
46
47static void explain1(char *arg)
48{
49 fprintf(stderr, "Illegal \"%s\"\n", arg);
50}
51
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080052static const char *police_action_n2a(int action, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000053{
54 switch (action) {
55 case -1:
56 return "continue";
57 break;
58 case TC_POLICE_OK:
59 return "pass";
60 break;
61 case TC_POLICE_SHOT:
62 return "drop";
63 break;
64 case TC_POLICE_RECLASSIFY:
65 return "reclassify";
osdl.net!shemminger2373fde2004-08-13 23:54:55 +000066 case TC_POLICE_PIPE:
67 return "pipe";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000068 default:
69 snprintf(buf, len, "%d", action);
70 return buf;
71 }
72}
73
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080074static int police_action_a2n(const char *arg, int *result)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075{
76 int res;
77
78 if (matches(arg, "continue") == 0)
79 res = -1;
80 else if (matches(arg, "drop") == 0)
81 res = TC_POLICE_SHOT;
82 else if (matches(arg, "shot") == 0)
83 res = TC_POLICE_SHOT;
84 else if (matches(arg, "pass") == 0)
85 res = TC_POLICE_OK;
86 else if (strcmp(arg, "ok") == 0)
87 res = TC_POLICE_OK;
88 else if (matches(arg, "reclassify") == 0)
89 res = TC_POLICE_RECLASSIFY;
osdl.net!shemminger2373fde2004-08-13 23:54:55 +000090 else if (matches(arg, "pipe") == 0)
91 res = TC_POLICE_PIPE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000092 else {
93 char dummy;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070094
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000095 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
96 return -1;
97 }
98 *result = res;
99 return 0;
100}
101
102
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800103static int get_police_result(int *action, int *result, char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104{
105 char *p = strchr(arg, '/');
106
107 if (p)
108 *p = 0;
109
110 if (police_action_a2n(arg, action)) {
111 if (p)
112 *p = '/';
113 return -1;
114 }
115
116 if (p) {
117 *p = '/';
118 if (police_action_a2n(p+1, result))
119 return -1;
120 }
121 return 0;
122}
123
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000124
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700125int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000126{
127 int argc = *argc_p;
128 char **argv = *argv_p;
129 int res = -1;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700130 int ok = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 struct tc_police p;
132 __u32 rtab[256];
133 __u32 ptab[256];
134 __u32 avrate = 0;
135 int presult = 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700136 unsigned buffer = 0, mtu = 0, mpu = 0;
137 unsigned short overhead = 0;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200138 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700139 int Rcell_log = -1, Pcell_log = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000140 struct rtattr *tail;
141
142 memset(&p, 0, sizeof(p));
143 p.action = TC_POLICE_RECLASSIFY;
144
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000145 if (a) /* new way of doing things */
146 NEXT_ARG();
147
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000148 if (argc <= 0)
149 return -1;
150
151 while (argc > 0) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000152
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153 if (matches(*argv, "index") == 0) {
154 NEXT_ARG();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000155 if (get_u32(&p.index, *argv, 10)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000156 fprintf(stderr, "Illegal \"index\"\n");
157 return -1;
158 }
159 } else if (matches(*argv, "burst") == 0 ||
160 strcmp(*argv, "buffer") == 0 ||
161 strcmp(*argv, "maxburst") == 0) {
162 NEXT_ARG();
163 if (buffer) {
164 fprintf(stderr, "Double \"buffer/burst\" spec\n");
165 return -1;
166 }
167 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
168 explain1("buffer");
169 return -1;
170 }
171 } else if (strcmp(*argv, "mtu") == 0 ||
172 strcmp(*argv, "minburst") == 0) {
173 NEXT_ARG();
174 if (mtu) {
175 fprintf(stderr, "Double \"mtu/minburst\" spec\n");
176 return -1;
177 }
178 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
179 explain1("mtu");
180 return -1;
181 }
182 } else if (strcmp(*argv, "mpu") == 0) {
183 NEXT_ARG();
184 if (mpu) {
185 fprintf(stderr, "Double \"mpu\" spec\n");
186 return -1;
187 }
188 if (get_size(&mpu, *argv)) {
189 explain1("mpu");
190 return -1;
191 }
192 } else if (strcmp(*argv, "rate") == 0) {
193 NEXT_ARG();
194 if (p.rate.rate) {
195 fprintf(stderr, "Double \"rate\" spec\n");
196 return -1;
197 }
198 if (get_rate(&p.rate.rate, *argv)) {
199 explain1("rate");
200 return -1;
201 }
202 } else if (strcmp(*argv, "avrate") == 0) {
203 NEXT_ARG();
204 if (avrate) {
205 fprintf(stderr, "Double \"avrate\" spec\n");
206 return -1;
207 }
208 if (get_rate(&avrate, *argv)) {
209 explain1("avrate");
210 return -1;
211 }
212 } else if (matches(*argv, "peakrate") == 0) {
213 NEXT_ARG();
214 if (p.peakrate.rate) {
215 fprintf(stderr, "Double \"peakrate\" spec\n");
216 return -1;
217 }
218 if (get_rate(&p.peakrate.rate, *argv)) {
219 explain1("peakrate");
220 return -1;
221 }
222 } else if (matches(*argv, "reclassify") == 0) {
223 p.action = TC_POLICE_RECLASSIFY;
224 } else if (matches(*argv, "drop") == 0 ||
225 matches(*argv, "shot") == 0) {
226 p.action = TC_POLICE_SHOT;
227 } else if (matches(*argv, "continue") == 0) {
228 p.action = TC_POLICE_UNSPEC;
229 } else if (matches(*argv, "pass") == 0) {
230 p.action = TC_POLICE_OK;
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000231 } else if (matches(*argv, "pipe") == 0) {
232 p.action = TC_POLICE_PIPE;
Jamal Hadi Salim4bfb21c2013-12-21 17:16:53 -0500233 } else if (strcmp(*argv, "conform-exceed") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000234 NEXT_ARG();
235 if (get_police_result(&p.action, &presult, *argv)) {
236 fprintf(stderr, "Illegal \"action\"\n");
237 return -1;
238 }
Jesper Dangaard Brouerf71f75f2008-03-23 23:53:57 +0100239 } else if (matches(*argv, "overhead") == 0) {
240 NEXT_ARG();
241 if (get_u16(&overhead, *argv, 10)) {
242 explain1("overhead"); return -1;
243 }
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200244 } else if (matches(*argv, "linklayer") == 0) {
245 NEXT_ARG();
246 if (get_linklayer(&linklayer, *argv)) {
247 explain1("linklayer"); return -1;
248 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000249 } else if (strcmp(*argv, "help") == 0) {
Jamal Hadi Salimebf32082006-08-08 12:10:08 -0700250 usage();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000251 } else {
Stephen Hemmingeref1d6f92008-01-02 16:34:58 -0800252 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000253 }
254 ok++;
255 argc--; argv++;
256 }
257
258 if (!ok)
259 return -1;
260
261 if (p.rate.rate && !buffer) {
262 fprintf(stderr, "\"burst\" requires \"rate\".\n");
263 return -1;
264 }
265 if (p.peakrate.rate) {
266 if (!p.rate.rate) {
267 fprintf(stderr, "\"peakrate\" requires \"rate\".\n");
268 return -1;
269 }
270 if (!mtu) {
271 fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
272 return -1;
273 }
274 }
275
276 if (p.rate.rate) {
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200277 p.rate.mpu = mpu;
Jesper Dangaard Brouerf71f75f2008-03-23 23:53:57 +0100278 p.rate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200279 if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000280 fprintf(stderr, "TBF: failed to calculate rate table.\n");
281 return -1;
282 }
283 p.burst = tc_calc_xmittime(p.rate.rate, buffer);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000284 }
285 p.mtu = mtu;
286 if (p.peakrate.rate) {
Jesper Dangaard Brouerd5f46f92007-09-05 10:47:47 +0200287 p.peakrate.mpu = mpu;
Jesper Dangaard Brouerf71f75f2008-03-23 23:53:57 +0100288 p.peakrate.overhead = overhead;
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200289 if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290 fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
291 return -1;
292 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293 }
294
6!tgraf1b52a762005-01-18 01:24:18 +0000295 tail = NLMSG_TAIL(n);
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000296 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
297 addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 if (p.rate.rate)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000299 addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 if (p.peakrate.rate)
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700301 addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000302 if (avrate)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000303 addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304 if (presult)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000305 addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000306
6!tgraf1b52a762005-01-18 01:24:18 +0000307 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000308 res = 0;
309
310 *argc_p = argc;
311 *argv_p = argv;
312 return res;
313}
314
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000315int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
316{
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700317 return act_parse_police(NULL, argc_p, argv_p, tca_id, n);
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000318}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000319
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800320int
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000321print_police(struct action_util *a, FILE *f, struct rtattr *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000322{
323 SPRINT_BUF(b1);
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200324 SPRINT_BUF(b2);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000325 struct tc_police *p;
326 struct rtattr *tb[TCA_POLICE_MAX+1];
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700327 unsigned int buffer;
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200328 unsigned int linklayer;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329
330 if (arg == NULL)
331 return 0;
332
6!tgraf14ee9e62005-01-18 22:11:58 +0000333 parse_rtattr_nested(tb, TCA_POLICE_MAX, arg);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000334
335 if (tb[TCA_POLICE_TBF] == NULL) {
336 fprintf(f, "[NULL police tbf]");
337 return 0;
338 }
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000339#ifndef STOOPID_8BYTE
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000340 if (RTA_PAYLOAD(tb[TCA_POLICE_TBF]) < sizeof(*p)) {
341 fprintf(f, "[truncated police tbf]");
342 return -1;
343 }
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000344#endif
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000345 p = RTA_DATA(tb[TCA_POLICE_TBF]);
346
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000347 fprintf(f, " police 0x%x ", p->index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000348 fprintf(f, "rate %s ", sprint_rate(p->rate.rate, b1));
Patrick McHardy76dc0aa2007-03-04 20:14:57 +0100349 buffer = tc_calc_xmitsize(p->rate.rate, p->burst);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000350 fprintf(f, "burst %s ", sprint_size(buffer, b1));
351 fprintf(f, "mtu %s ", sprint_size(p->mtu, b1));
352 if (show_raw)
353 fprintf(f, "[%08x] ", p->burst);
354 if (p->peakrate.rate)
355 fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1));
356 if (tb[TCA_POLICE_AVRATE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700357 fprintf(f, "avrate %s ", sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]), b1));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000358 fprintf(f, "action %s", police_action_n2a(p->action, b1, sizeof(b1)));
359 if (tb[TCA_POLICE_RESULT]) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700360 fprintf(f, "/%s ", police_action_n2a(*(int *)RTA_DATA(tb[TCA_POLICE_RESULT]), b1, sizeof(b1)));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000361 } else
362 fprintf(f, " ");
Jesper Dangaard Brouerf71f75f2008-03-23 23:53:57 +0100363 fprintf(f, "overhead %ub ", p->rate.overhead);
Jesper Dangaard Brouer3e92ff52013-08-30 14:02:10 +0200364 linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK);
365 if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
366 fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700367 fprintf(f, "\nref %d bind %d\n", p->refcnt, p->bindcnt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000368
369 return 0;
370}
371
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800372int
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000373tc_print_police(FILE *f, struct rtattr *arg) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700374 return print_police(&police_action_util, f, arg);
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000375}