2 * m_police.c Parse/print policing module options.
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.
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
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
29 struct action_util police_action_util = {
31 .parse_aopt = act_parse_police,
32 .print_aopt = print_police,
35 static void usage(void)
37 fprintf(stderr, "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n");
38 fprintf(stderr, " [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n");
39 fprintf(stderr, " [ linklayer TYPE ] [ ACTIONTERM ]\n");
41 fprintf(stderr, "Old Syntax ACTIONTERM := action <EXCEEDACT>[/NOTEXCEEDACT] \n");
42 fprintf(stderr, "New Syntax ACTIONTERM := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT] \n");
43 fprintf(stderr, "Where: *EXCEEDACT := pipe | ok | reclassify | drop | continue \n");
44 fprintf(stderr, "Where: pipe is only valid for new syntax \n");
48 static void explain1(char *arg)
50 fprintf(stderr, "Illegal \"%s\"\n", arg);
53 char *police_action_n2a(int action, char *buf, int len)
65 case TC_POLICE_RECLASSIFY:
70 snprintf(buf, len, "%d", action);
75 int police_action_a2n(char *arg, int *result)
79 if (matches(arg, "continue") == 0)
81 else if (matches(arg, "drop") == 0)
83 else if (matches(arg, "shot") == 0)
85 else if (matches(arg, "pass") == 0)
87 else if (strcmp(arg, "ok") == 0)
89 else if (matches(arg, "reclassify") == 0)
90 res = TC_POLICE_RECLASSIFY;
91 else if (matches(arg, "pipe") == 0)
95 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
103 int get_police_result(int *action, int *result, char *arg)
105 char *p = strchr(arg, '/');
110 if (police_action_a2n(arg, action)) {
118 if (police_action_a2n(p+1, result))
125 int act_parse_police(struct action_util *a,int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
128 char **argv = *argv_p;
136 unsigned buffer=0, mtu=0, mpu=0;
137 unsigned short overhead=0;
138 unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
139 int Rcell_log=-1, Pcell_log = -1;
142 memset(&p, 0, sizeof(p));
143 p.action = TC_POLICE_RECLASSIFY;
145 if (a) /* new way of doing things */
153 if (matches(*argv, "index") == 0) {
155 if (get_u32(&p.index, *argv, 10)) {
156 fprintf(stderr, "Illegal \"index\"\n");
159 } else if (matches(*argv, "burst") == 0 ||
160 strcmp(*argv, "buffer") == 0 ||
161 strcmp(*argv, "maxburst") == 0) {
164 fprintf(stderr, "Double \"buffer/burst\" spec\n");
167 if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
171 } else if (strcmp(*argv, "mtu") == 0 ||
172 strcmp(*argv, "minburst") == 0) {
175 fprintf(stderr, "Double \"mtu/minburst\" spec\n");
178 if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
182 } else if (strcmp(*argv, "mpu") == 0) {
185 fprintf(stderr, "Double \"mpu\" spec\n");
188 if (get_size(&mpu, *argv)) {
192 } else if (strcmp(*argv, "rate") == 0) {
195 fprintf(stderr, "Double \"rate\" spec\n");
198 if (get_rate(&p.rate.rate, *argv)) {
202 } else if (strcmp(*argv, "avrate") == 0) {
205 fprintf(stderr, "Double \"avrate\" spec\n");
208 if (get_rate(&avrate, *argv)) {
212 } else if (matches(*argv, "peakrate") == 0) {
214 if (p.peakrate.rate) {
215 fprintf(stderr, "Double \"peakrate\" spec\n");
218 if (get_rate(&p.peakrate.rate, *argv)) {
219 explain1("peakrate");
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;
231 } else if (matches(*argv, "pipe") == 0) {
232 p.action = TC_POLICE_PIPE;
233 } else if (strcmp(*argv, "action") == 0 ||
234 strcmp(*argv, "conform-exceed") == 0) {
236 if (get_police_result(&p.action, &presult, *argv)) {
237 fprintf(stderr, "Illegal \"action\"\n");
240 } else if (matches(*argv, "overhead") == 0) {
242 if (get_u16(&overhead, *argv, 10)) {
243 explain1("overhead"); return -1;
245 } else if (matches(*argv, "linklayer") == 0) {
247 if (get_linklayer(&linklayer, *argv)) {
248 explain1("linklayer"); return -1;
250 } else if (strcmp(*argv, "help") == 0) {
262 if (p.rate.rate && !buffer) {
263 fprintf(stderr, "\"burst\" requires \"rate\".\n");
266 if (p.peakrate.rate) {
268 fprintf(stderr, "\"peakrate\" requires \"rate\".\n");
272 fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
279 p.rate.overhead = overhead;
280 if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
281 fprintf(stderr, "TBF: failed to calculate rate table.\n");
284 p.burst = tc_calc_xmittime(p.rate.rate, buffer);
287 if (p.peakrate.rate) {
288 p.peakrate.mpu = mpu;
289 p.peakrate.overhead = overhead;
290 if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
291 fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
296 tail = NLMSG_TAIL(n);
297 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
298 addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p));
300 addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024);
302 addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024);
304 addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate);
306 addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);
308 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
316 int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
318 return act_parse_police(NULL,argc_p,argv_p,tca_id,n);
322 print_police(struct action_util *a, FILE *f, struct rtattr *arg)
326 struct rtattr *tb[TCA_POLICE_MAX+1];
332 parse_rtattr_nested(tb, TCA_POLICE_MAX, arg);
334 if (tb[TCA_POLICE_TBF] == NULL) {
335 fprintf(f, "[NULL police tbf]");
338 #ifndef STOOPID_8BYTE
339 if (RTA_PAYLOAD(tb[TCA_POLICE_TBF]) < sizeof(*p)) {
340 fprintf(f, "[truncated police tbf]");
344 p = RTA_DATA(tb[TCA_POLICE_TBF]);
346 fprintf(f, " police 0x%x ", p->index);
347 fprintf(f, "rate %s ", sprint_rate(p->rate.rate, b1));
348 buffer = tc_calc_xmitsize(p->rate.rate, p->burst);
349 fprintf(f, "burst %s ", sprint_size(buffer, b1));
350 fprintf(f, "mtu %s ", sprint_size(p->mtu, b1));
352 fprintf(f, "[%08x] ", p->burst);
353 if (p->peakrate.rate)
354 fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1));
355 if (tb[TCA_POLICE_AVRATE])
356 fprintf(f, "avrate %s ", sprint_rate(*(__u32*)RTA_DATA(tb[TCA_POLICE_AVRATE]), b1));
357 fprintf(f, "action %s", police_action_n2a(p->action, b1, sizeof(b1)));
358 if (tb[TCA_POLICE_RESULT]) {
359 fprintf(f, "/%s ", police_action_n2a(*(int*)RTA_DATA(tb[TCA_POLICE_RESULT]), b1, sizeof(b1)));
362 fprintf(f, "overhead %ub ", p->rate.overhead);
363 fprintf(f, "\nref %d bind %d\n",p->refcnt, p->bindcnt);
369 tc_print_police(FILE *f, struct rtattr *arg) {
370 return print_police(&police_action_util,f,arg);