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