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