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