]>
Commit | Line | Data |
---|---|---|
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 | 29 | struct 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 | 35 | static void usage(void) |
aba5acdf SH |
36 | { |
37 | fprintf(stderr, "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n"); | |
38 | fprintf(stderr, " [ peakrate BPS ] [ avrate BPS ]\n"); | |
2373fde9 | 39 | fprintf(stderr, " [ ACTIONTERM ]\n"); |
ae665a52 SH |
40 | fprintf(stderr, "Old Syntax ACTIONTERM := action <EXCEEDACT>[/NOTEXCEEDACT] \n"); |
41 | fprintf(stderr, "New Syntax ACTIONTERM := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT] \n"); | |
2373fde9 SH |
42 | fprintf(stderr, "Where: *EXCEEDACT := pipe | ok | reclassify | drop | continue \n"); |
43 | fprintf(stderr, "Where: pipe is only valid for new syntax \n"); | |
ebf32083 | 44 | exit(-1); |
aba5acdf SH |
45 | } |
46 | ||
47 | static void explain1(char *arg) | |
48 | { | |
49 | fprintf(stderr, "Illegal \"%s\"\n", arg); | |
50 | } | |
51 | ||
aba5acdf SH |
52 | char *police_action_n2a(int action, char *buf, int len) |
53 | { | |
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"; | |
2373fde9 SH |
66 | case TC_POLICE_PIPE: |
67 | return "pipe"; | |
aba5acdf SH |
68 | default: |
69 | snprintf(buf, len, "%d", action); | |
70 | return buf; | |
71 | } | |
72 | } | |
73 | ||
74 | int police_action_a2n(char *arg, int *result) | |
75 | { | |
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; | |
2373fde9 SH |
90 | else if (matches(arg, "pipe") == 0) |
91 | res = TC_POLICE_PIPE; | |
aba5acdf SH |
92 | else { |
93 | char dummy; | |
94 | if (sscanf(arg, "%d%c", &res, &dummy) != 1) | |
95 | return -1; | |
96 | } | |
97 | *result = res; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | ||
102 | int get_police_result(int *action, int *result, char *arg) | |
103 | { | |
104 | char *p = strchr(arg, '/'); | |
105 | ||
106 | if (p) | |
107 | *p = 0; | |
108 | ||
109 | if (police_action_a2n(arg, action)) { | |
110 | if (p) | |
111 | *p = '/'; | |
112 | return -1; | |
113 | } | |
114 | ||
115 | if (p) { | |
116 | *p = '/'; | |
117 | if (police_action_a2n(p+1, result)) | |
118 | return -1; | |
119 | } | |
120 | return 0; | |
121 | } | |
122 | ||
2373fde9 SH |
123 | |
124 | int act_parse_police(struct action_util *a,int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) | |
aba5acdf SH |
125 | { |
126 | int argc = *argc_p; | |
127 | char **argv = *argv_p; | |
128 | int res = -1; | |
129 | int ok=0; | |
130 | struct tc_police p; | |
131 | __u32 rtab[256]; | |
132 | __u32 ptab[256]; | |
133 | __u32 avrate = 0; | |
134 | int presult = 0; | |
135 | unsigned buffer=0, mtu=0, mpu=0; | |
ae665a52 | 136 | int Rcell_log=-1, Pcell_log = -1; |
aba5acdf SH |
137 | struct rtattr *tail; |
138 | ||
139 | memset(&p, 0, sizeof(p)); | |
140 | p.action = TC_POLICE_RECLASSIFY; | |
141 | ||
2373fde9 SH |
142 | if (a) /* new way of doing things */ |
143 | NEXT_ARG(); | |
144 | ||
aba5acdf SH |
145 | if (argc <= 0) |
146 | return -1; | |
147 | ||
148 | while (argc > 0) { | |
2373fde9 | 149 | |
aba5acdf SH |
150 | if (matches(*argv, "index") == 0) { |
151 | NEXT_ARG(); | |
2373fde9 | 152 | if (get_u32(&p.index, *argv, 10)) { |
aba5acdf SH |
153 | fprintf(stderr, "Illegal \"index\"\n"); |
154 | return -1; | |
155 | } | |
156 | } else if (matches(*argv, "burst") == 0 || | |
157 | strcmp(*argv, "buffer") == 0 || | |
158 | strcmp(*argv, "maxburst") == 0) { | |
159 | NEXT_ARG(); | |
160 | if (buffer) { | |
161 | fprintf(stderr, "Double \"buffer/burst\" spec\n"); | |
162 | return -1; | |
163 | } | |
164 | if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) { | |
165 | explain1("buffer"); | |
166 | return -1; | |
167 | } | |
168 | } else if (strcmp(*argv, "mtu") == 0 || | |
169 | strcmp(*argv, "minburst") == 0) { | |
170 | NEXT_ARG(); | |
171 | if (mtu) { | |
172 | fprintf(stderr, "Double \"mtu/minburst\" spec\n"); | |
173 | return -1; | |
174 | } | |
175 | if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) { | |
176 | explain1("mtu"); | |
177 | return -1; | |
178 | } | |
179 | } else if (strcmp(*argv, "mpu") == 0) { | |
180 | NEXT_ARG(); | |
181 | if (mpu) { | |
182 | fprintf(stderr, "Double \"mpu\" spec\n"); | |
183 | return -1; | |
184 | } | |
185 | if (get_size(&mpu, *argv)) { | |
186 | explain1("mpu"); | |
187 | return -1; | |
188 | } | |
189 | } else if (strcmp(*argv, "rate") == 0) { | |
190 | NEXT_ARG(); | |
191 | if (p.rate.rate) { | |
192 | fprintf(stderr, "Double \"rate\" spec\n"); | |
193 | return -1; | |
194 | } | |
195 | if (get_rate(&p.rate.rate, *argv)) { | |
196 | explain1("rate"); | |
197 | return -1; | |
198 | } | |
199 | } else if (strcmp(*argv, "avrate") == 0) { | |
200 | NEXT_ARG(); | |
201 | if (avrate) { | |
202 | fprintf(stderr, "Double \"avrate\" spec\n"); | |
203 | return -1; | |
204 | } | |
205 | if (get_rate(&avrate, *argv)) { | |
206 | explain1("avrate"); | |
207 | return -1; | |
208 | } | |
209 | } else if (matches(*argv, "peakrate") == 0) { | |
210 | NEXT_ARG(); | |
211 | if (p.peakrate.rate) { | |
212 | fprintf(stderr, "Double \"peakrate\" spec\n"); | |
213 | return -1; | |
214 | } | |
215 | if (get_rate(&p.peakrate.rate, *argv)) { | |
216 | explain1("peakrate"); | |
217 | return -1; | |
218 | } | |
219 | } else if (matches(*argv, "reclassify") == 0) { | |
220 | p.action = TC_POLICE_RECLASSIFY; | |
221 | } else if (matches(*argv, "drop") == 0 || | |
222 | matches(*argv, "shot") == 0) { | |
223 | p.action = TC_POLICE_SHOT; | |
224 | } else if (matches(*argv, "continue") == 0) { | |
225 | p.action = TC_POLICE_UNSPEC; | |
226 | } else if (matches(*argv, "pass") == 0) { | |
227 | p.action = TC_POLICE_OK; | |
2373fde9 SH |
228 | } else if (matches(*argv, "pipe") == 0) { |
229 | p.action = TC_POLICE_PIPE; | |
230 | } else if (strcmp(*argv, "conform-exceed") == 0) { | |
aba5acdf SH |
231 | NEXT_ARG(); |
232 | if (get_police_result(&p.action, &presult, *argv)) { | |
233 | fprintf(stderr, "Illegal \"action\"\n"); | |
234 | return -1; | |
235 | } | |
236 | } else if (strcmp(*argv, "help") == 0) { | |
ebf32083 | 237 | usage(); |
aba5acdf SH |
238 | } else { |
239 | break; | |
240 | } | |
241 | ok++; | |
242 | argc--; argv++; | |
243 | } | |
244 | ||
245 | if (!ok) | |
246 | return -1; | |
247 | ||
248 | if (p.rate.rate && !buffer) { | |
249 | fprintf(stderr, "\"burst\" requires \"rate\".\n"); | |
250 | return -1; | |
251 | } | |
252 | if (p.peakrate.rate) { | |
253 | if (!p.rate.rate) { | |
254 | fprintf(stderr, "\"peakrate\" requires \"rate\".\n"); | |
255 | return -1; | |
256 | } | |
257 | if (!mtu) { | |
258 | fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n"); | |
259 | return -1; | |
260 | } | |
261 | } | |
262 | ||
263 | if (p.rate.rate) { | |
264 | if ((Rcell_log = tc_calc_rtable(p.rate.rate, rtab, Rcell_log, mtu, mpu)) < 0) { | |
265 | fprintf(stderr, "TBF: failed to calculate rate table.\n"); | |
266 | return -1; | |
267 | } | |
268 | p.burst = tc_calc_xmittime(p.rate.rate, buffer); | |
269 | p.rate.cell_log = Rcell_log; | |
270 | p.rate.mpu = mpu; | |
271 | } | |
272 | p.mtu = mtu; | |
273 | if (p.peakrate.rate) { | |
274 | if ((Pcell_log = tc_calc_rtable(p.peakrate.rate, ptab, Pcell_log, mtu, mpu)) < 0) { | |
275 | fprintf(stderr, "POLICE: failed to calculate peak rate table.\n"); | |
276 | return -1; | |
277 | } | |
278 | p.peakrate.cell_log = Pcell_log; | |
279 | p.peakrate.mpu = mpu; | |
280 | } | |
281 | ||
1b52a762 | 282 | tail = NLMSG_TAIL(n); |
2373fde9 SH |
283 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
284 | addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p)); | |
aba5acdf | 285 | if (p.rate.rate) |
2373fde9 | 286 | addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024); |
aba5acdf | 287 | if (p.peakrate.rate) |
2373fde9 | 288 | addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024); |
aba5acdf | 289 | if (avrate) |
2373fde9 | 290 | addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate); |
aba5acdf | 291 | if (presult) |
2373fde9 | 292 | addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult); |
aba5acdf | 293 | |
1b52a762 | 294 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
aba5acdf SH |
295 | res = 0; |
296 | ||
297 | *argc_p = argc; | |
298 | *argv_p = argv; | |
299 | return res; | |
300 | } | |
301 | ||
2373fde9 SH |
302 | int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) |
303 | { | |
304 | return act_parse_police(NULL,argc_p,argv_p,tca_id,n); | |
305 | } | |
aba5acdf | 306 | |
ae665a52 | 307 | int |
f2f99e2e | 308 | print_police(struct action_util *a, FILE *f, struct rtattr *arg) |
aba5acdf SH |
309 | { |
310 | SPRINT_BUF(b1); | |
311 | struct tc_police *p; | |
312 | struct rtattr *tb[TCA_POLICE_MAX+1]; | |
313 | unsigned buffer; | |
314 | ||
315 | if (arg == NULL) | |
316 | return 0; | |
317 | ||
14ee9e61 | 318 | parse_rtattr_nested(tb, TCA_POLICE_MAX, arg); |
aba5acdf SH |
319 | |
320 | if (tb[TCA_POLICE_TBF] == NULL) { | |
321 | fprintf(f, "[NULL police tbf]"); | |
322 | return 0; | |
323 | } | |
2373fde9 | 324 | #ifndef STOOPID_8BYTE |
aba5acdf SH |
325 | if (RTA_PAYLOAD(tb[TCA_POLICE_TBF]) < sizeof(*p)) { |
326 | fprintf(f, "[truncated police tbf]"); | |
327 | return -1; | |
328 | } | |
2373fde9 | 329 | #endif |
aba5acdf SH |
330 | p = RTA_DATA(tb[TCA_POLICE_TBF]); |
331 | ||
2373fde9 | 332 | fprintf(f, " police 0x%x ", p->index); |
aba5acdf SH |
333 | fprintf(f, "rate %s ", sprint_rate(p->rate.rate, b1)); |
334 | buffer = ((double)p->rate.rate*tc_core_tick2usec(p->burst))/1000000; | |
335 | fprintf(f, "burst %s ", sprint_size(buffer, b1)); | |
336 | fprintf(f, "mtu %s ", sprint_size(p->mtu, b1)); | |
337 | if (show_raw) | |
338 | fprintf(f, "[%08x] ", p->burst); | |
339 | if (p->peakrate.rate) | |
340 | fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1)); | |
341 | if (tb[TCA_POLICE_AVRATE]) | |
342 | fprintf(f, "avrate %s ", sprint_rate(*(__u32*)RTA_DATA(tb[TCA_POLICE_AVRATE]), b1)); | |
2373fde9 SH |
343 | fprintf(f, "action %s", police_action_n2a(p->action, b1, sizeof(b1))); |
344 | if (tb[TCA_POLICE_RESULT]) { | |
345 | fprintf(f, "/%s ", police_action_n2a(*(int*)RTA_DATA(tb[TCA_POLICE_RESULT]), b1, sizeof(b1))); | |
346 | } else | |
347 | fprintf(f, " "); | |
348 | fprintf(f, "\nref %d bind %d\n",p->refcnt, p->bindcnt); | |
aba5acdf SH |
349 | |
350 | return 0; | |
351 | } | |
352 | ||
ae665a52 | 353 | int |
2373fde9 | 354 | tc_print_police(FILE *f, struct rtattr *arg) { |
95812b56 | 355 | return print_police(&police_action_util,f,arg); |
2373fde9 | 356 | } |