summaryrefslogtreecommitdiffstats
path: root/tc/m_police.c
blob: f38ab90a303963bdbd4713019be7219ad899674e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * m_police.c		Parse/print policing module options.
 *
 *		This program is free software; you can u32istribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 * FIXES:       19990619 - J Hadi Salim (hadi@cyberus.ca)
 *		simple addattr packaging fix.
 *		2002: J Hadi Salim - Add tc action extensions syntax
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "utils.h"
#include "tc_util.h"

static int act_parse_police(struct action_util *a, int *argc_p,
			    char ***argv_p, int tca_id, struct nlmsghdr *n);
static int print_police(struct action_util *a, FILE *f, struct rtattr *tb);

struct action_util police_action_util = {
	.id = "police",
	.parse_aopt = act_parse_police,
	.print_aopt = print_police,
};

static void usage(void)
{
	fprintf(stderr,
		"Usage: ... police [ rate BPS burst BYTES[/BYTES] ] \n"
		"		[ pkts_rate RATE pkts_burst PACKETS ] [ mtu BYTES[/BYTES] ]\n"
		"		[ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n"
		"		[ linklayer TYPE ] [ CONTROL ]\n"
		"Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n"
		"		  Define how to handle packets which exceed (<EXCEEDACT>)\n"
		"		  or conform (<NOTEXCEEDACT>) the configured bandwidth limit.\n"
		"       EXCEEDACT/NOTEXCEEDACT := { pipe | ok | reclassify | drop | continue |\n"
		"				   goto chain <CHAIN_INDEX> }\n");
	exit(-1);
}

static int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
			    int tca_id, struct nlmsghdr *n)
{
	int argc = *argc_p;
	char **argv = *argv_p;
	int res = -1;
	int ok = 0;
	struct tc_police p = { .action = TC_POLICE_RECLASSIFY };
	__u32 rtab[256];
	__u32 ptab[256];
	__u32 avrate = 0;
	int presult = 0;
	unsigned buffer = 0, mtu = 0, mpu = 0;
	unsigned short overhead = 0;
	unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
	int Rcell_log =  -1, Pcell_log = -1;
	struct rtattr *tail;
	__u64 rate64 = 0, prate64 = 0;
	__u64 pps64 = 0, ppsburst64 = 0;

	if (a) /* new way of doing things */
		NEXT_ARG();

	if (argc <= 0)
		return -1;

	while (argc > 0) {

		if (matches(*argv, "index") == 0) {
			NEXT_ARG();
			if (get_u32(&p.index, *argv, 10))
				invarg("index", *argv);
		} else if (matches(*argv, "burst") == 0 ||
			strcmp(*argv, "buffer") == 0 ||
			strcmp(*argv, "maxburst") == 0) {
			NEXT_ARG();
			if (buffer)
				duparg("buffer/burst", *argv);
			if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0)
				invarg("buffer", *argv);
		} else if (strcmp(*argv, "mtu") == 0 ||
			   strcmp(*argv, "minburst") == 0) {
			NEXT_ARG();
			if (mtu)
				duparg("mtu/minburst", *argv);
			if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0)
				invarg("mtu", *argv);
		} else if (strcmp(*argv, "mpu") == 0) {
			NEXT_ARG();
			if (mpu)
				duparg("mpu", *argv);
			if (get_size(&mpu, *argv))
				invarg("mpu", *argv);
		} else if (strcmp(*argv, "rate") == 0) {
			NEXT_ARG();
			if (rate64)
				duparg("rate", *argv);
			if (get_rate64(&rate64, *argv))
				invarg("rate", *argv);
		} else if (strcmp(*argv, "avrate") == 0) {
			NEXT_ARG();
			if (avrate)
				duparg("avrate", *argv);
			if (get_rate(&avrate, *argv))
				invarg("avrate", *argv);
		} else if (matches(*argv, "peakrate") == 0) {
			NEXT_ARG();
			if (prate64)
				duparg("peakrate", *argv);
			if (get_rate64(&prate64, *argv))
				invarg("peakrate", *argv);
		} else if (matches(*argv, "reclassify") == 0 ||
			   matches(*argv, "drop") == 0 ||
			   matches(*argv, "shot") == 0 ||
			   matches(*argv, "continue") == 0 ||
			   matches(*argv, "pass") == 0 ||
			   matches(*argv, "ok") == 0 ||
			   matches(*argv, "pipe") == 0 ||
			   matches(*argv, "goto") == 0) {
			if (!parse_action_control(&argc, &argv, &p.action, false))
				goto action_ctrl_ok;
			return -1;
		} else if (strcmp(*argv, "conform-exceed") == 0) {
			NEXT_ARG();
			if (!parse_action_control_slash(&argc, &argv, &p.action,
							&presult, true))
				goto action_ctrl_ok;
			return -1;
		} else if (matches(*argv, "overhead") == 0) {
			NEXT_ARG();
			if (get_u16(&overhead, *argv, 10))
				invarg("overhead", *argv);
		} else if (matches(*argv, "linklayer") == 0) {
			NEXT_ARG();
			if (get_linklayer(&linklayer, *argv))
				invarg("linklayer", *argv);
		} else if (matches(*argv, "pkts_rate") == 0) {
			NEXT_ARG();
			if (pps64)
				duparg("pkts_rate", *argv);
			if (get_u64(&pps64, *argv, 10))
				invarg("pkts_rate", *argv);
		} else if (matches(*argv, "pkts_burst") == 0) {
			NEXT_ARG();
			if (ppsburst64)
				duparg("pkts_burst", *argv);
			if (get_u64(&ppsburst64, *argv, 10))
				invarg("pkts_burst", *argv);
		} else if (strcmp(*argv, "help") == 0) {
			usage();
		} else {
			break;
		}
		NEXT_ARG_FWD();
action_ctrl_ok:
		ok++;
	}

	if (!ok)
		return -1;

	if (rate64 && avrate)
		return -1;

	/* Must at least do late binding, use TB or ewma policing */
	if (!rate64 && !avrate && !p.index && !mtu && !pps64) {
		fprintf(stderr, "'rate' or 'avrate' or 'mtu' or 'pkts_rate' MUST be specified.\n");
		return -1;
	}

	/* When the TB policer is used, burst is required */
	if (rate64 && !buffer && !avrate) {
		fprintf(stderr, "'burst' requires 'rate'.\n");
		return -1;
	}

	/* When the packets TB policer is used, pkts_burst is required */
	if (pps64 && !ppsburst64) {
		fprintf(stderr, "'pkts_burst' requires 'pkts_rate'.\n");
		return -1;
	}

	/* forbid rate and pkts_rate in same action */
	if (pps64 && rate64) {
		fprintf(stderr, "'rate' and 'pkts_rate' are not allowed in same action.\n");
		return -1;
	}

	if (prate64) {
		if (!rate64) {
			fprintf(stderr, "'peakrate' requires 'rate'.\n");
			return -1;
		}
		if (!mtu) {
			fprintf(stderr, "'mtu' is required, if 'peakrate' is requested.\n");
			return -1;
		}
	}

	if (rate64) {
		p.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
		p.rate.mpu = mpu;
		p.rate.overhead = overhead;
		if (tc_calc_rtable_64(&p.rate, rtab, Rcell_log, mtu,
				   linklayer, rate64) < 0) {
			fprintf(stderr, "POLICE: failed to calculate rate table.\n");
			return -1;
		}
		p.burst = tc_calc_xmittime(rate64, buffer);
	}
	p.mtu = mtu;
	if (prate64) {
		p.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
		p.peakrate.mpu = mpu;
		p.peakrate.overhead = overhead;
		if (tc_calc_rtable_64(&p.peakrate, ptab, Pcell_log, mtu,
				   linklayer, prate64) < 0) {
			fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
			return -1;
		}
	}

	tail = addattr_nest(n, MAX_MSG, tca_id);
	addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p));
	if (rate64) {
		addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024);
		if (rate64 >= (1ULL << 32))
			addattr64(n, MAX_MSG, TCA_POLICE_RATE64, rate64);
	}
	if (prate64) {
		addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024);
		if (prate64 >= (1ULL << 32))
			addattr64(n, MAX_MSG, TCA_POLICE_PEAKRATE64, prate64);
	}
	if (avrate)
		addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate);
	if (presult)
		addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);

	if (pps64) {
		addattr64(n, MAX_MSG, TCA_POLICE_PKTRATE64, pps64);
		ppsburst64 = tc_calc_xmittime(pps64, ppsburst64);
		addattr64(n, MAX_MSG, TCA_POLICE_PKTBURST64, ppsburst64);
	}

	addattr_nest_end(n, tail);
	res = 0;

	*argc_p = argc;
	*argv_p = argv;
	return res;
}

int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
{
	return act_parse_police(NULL, argc_p, argv_p, tca_id, n);
}

static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
{
	SPRINT_BUF(b2);
	struct tc_police *p;
	struct rtattr *tb[TCA_POLICE_MAX+1];
	unsigned int buffer;
	unsigned int linklayer;
	__u64 rate64, prate64;
	__u64 pps64, ppsburst64;

	print_string(PRINT_JSON, "kind", "%s", "police");
	if (arg == NULL)
		return 0;

	parse_rtattr_nested(tb, TCA_POLICE_MAX, arg);

	if (tb[TCA_POLICE_TBF] == NULL) {
		fprintf(stderr, "[NULL police tbf]");
		return -1;
	}
#ifndef STOOPID_8BYTE
	if (RTA_PAYLOAD(tb[TCA_POLICE_TBF])  < sizeof(*p)) {
		fprintf(stderr, "[truncated police tbf]");
		return -1;
	}
#endif
	p = RTA_DATA(tb[TCA_POLICE_TBF]);

	rate64 = p->rate.rate;
	if (tb[TCA_POLICE_RATE64] &&
	    RTA_PAYLOAD(tb[TCA_POLICE_RATE64]) >= sizeof(rate64))
		rate64 = rta_getattr_u64(tb[TCA_POLICE_RATE64]);

	print_hex(PRINT_FP, NULL, " police 0x%x ", p->index);
	print_uint(PRINT_JSON, "index", NULL, p->index);
	tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64);
	buffer = tc_calc_xmitsize(rate64, p->burst);
	print_size(PRINT_FP, NULL, "burst %s ", buffer);
	print_size(PRINT_FP, NULL, "mtu %s ", p->mtu);
	if (show_raw)
		print_hex(PRINT_FP, NULL, "[%08x] ", p->burst);

	prate64 = p->peakrate.rate;
	if (tb[TCA_POLICE_PEAKRATE64] &&
	    RTA_PAYLOAD(tb[TCA_POLICE_PEAKRATE64]) >= sizeof(prate64))
		prate64 = rta_getattr_u64(tb[TCA_POLICE_PEAKRATE64]);

	if (prate64)
		tc_print_rate(PRINT_FP, NULL, "peakrate %s ", prate64);

	if (tb[TCA_POLICE_AVRATE])
		tc_print_rate(PRINT_FP, NULL, "avrate %s ",
			      rta_getattr_u32(tb[TCA_POLICE_AVRATE]));

	if ((tb[TCA_POLICE_PKTRATE64] &&
	     RTA_PAYLOAD(tb[TCA_POLICE_PKTRATE64]) >= sizeof(pps64)) &&
	     (tb[TCA_POLICE_PKTBURST64] &&
	      RTA_PAYLOAD(tb[TCA_POLICE_PKTBURST64]) >= sizeof(ppsburst64))) {
		pps64 = rta_getattr_u64(tb[TCA_POLICE_PKTRATE64]);
		ppsburst64 = rta_getattr_u64(tb[TCA_POLICE_PKTBURST64]);
		ppsburst64 = tc_calc_xmitsize(pps64, ppsburst64);
		print_u64(PRINT_ANY, "pkts_rate", "pkts_rate %llu ", pps64);
		print_u64(PRINT_ANY, "pkts_burst", "pkts_burst %llu ", ppsburst64);
	}

	print_action_control(f, "action ", p->action, "");

	if (tb[TCA_POLICE_RESULT]) {
		__u32 action = rta_getattr_u32(tb[TCA_POLICE_RESULT]);

		print_action_control(f, "/", action, " ");
	} else {
		print_string(PRINT_FP, NULL, " ", NULL);
	}

	print_size(PRINT_ANY, "overhead", "overhead %s ", p->rate.overhead);
	linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK);
	if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
		print_string(PRINT_ANY, "linklayer", "linklayer %s ",
			     sprint_linklayer(linklayer, b2));
	print_nl();
	print_int(PRINT_ANY, "ref", "\tref %d ", p->refcnt);
	print_int(PRINT_ANY, "bind", "bind %d ", p->bindcnt);
	if (show_stats) {
		if (tb[TCA_POLICE_TM]) {
			struct tcf_t *tm = RTA_DATA(tb[TCA_POLICE_TM]);

			print_tm(f, tm);
		}
	}
	print_nl();


	return 0;
}

int tc_print_police(FILE *f, struct rtattr *arg)
{
	return print_police(&police_action_util, f, arg);
}