act_police.c 10.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * net/sched/act_police.c	Input police filter
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *		This program is free software; you can redistribute 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>
 * 		J Hadi Salim (action changes)
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/init.h>
21
#include <linux/slab.h>
L
Linus Torvalds 已提交
22
#include <net/act_api.h>
23
#include <net/netlink.h>
L
Linus Torvalds 已提交
24

25
struct tcf_police_params {
26 27
	int			tcfp_result;
	u32			tcfp_ewma_rate;
28
	s64			tcfp_burst;
29
	u32			tcfp_mtu;
30 31 32 33 34 35 36 37
	s64			tcfp_toks;
	s64			tcfp_ptoks;
	s64			tcfp_mtu_ptoks;
	s64			tcfp_t_c;
	struct psched_ratecfg	rate;
	bool			rate_present;
	struct psched_ratecfg	peak;
	bool			peak_present;
38 39 40 41 42 43
	struct rcu_head rcu;
};

struct tcf_police {
	struct tc_action	common;
	struct tcf_police_params __rcu *params;
44
};
45 46

#define to_police(pc) ((struct tcf_police *)pc)
47

48
/* old policer structure from before tc actions */
E
Eric Dumazet 已提交
49
struct tc_police_compat {
50 51 52 53 54 55 56 57 58
	u32			index;
	int			action;
	u32			limit;
	u32			burst;
	u32			mtu;
	struct tc_ratespec	rate;
	struct tc_ratespec	peakrate;
};

59
/* Each policer is serialized by its individual spinlock */
L
Linus Torvalds 已提交
60

61
static unsigned int police_net_id;
62
static struct tc_action_ops act_police_ops;
63

64
static int tcf_police_walker(struct net *net, struct sk_buff *skb,
65
				 struct netlink_callback *cb, int type,
66 67
				 const struct tc_action_ops *ops,
				 struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
68
{
69
	struct tc_action_net *tn = net_generic(net, police_net_id);
L
Linus Torvalds 已提交
70

71
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
L
Linus Torvalds 已提交
72 73
}

74 75 76 77 78 79 80
static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
	[TCA_POLICE_RATE]	= { .len = TC_RTAB_SIZE },
	[TCA_POLICE_PEAKRATE]	= { .len = TC_RTAB_SIZE },
	[TCA_POLICE_AVRATE]	= { .type = NLA_U32 },
	[TCA_POLICE_RESULT]	= { .type = NLA_U32 },
};

81
static int tcf_police_init(struct net *net, struct nlattr *nla,
82
			       struct nlattr *est, struct tc_action **a,
83
			       int ovr, int bind, bool rtnl_held,
84
			       struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
85 86
{
	int ret = 0, err;
87
	struct nlattr *tb[TCA_POLICE_MAX + 1];
L
Linus Torvalds 已提交
88
	struct tc_police *parm;
89
	struct tcf_police *police;
L
Linus Torvalds 已提交
90
	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
91
	struct tc_action_net *tn = net_generic(net, police_net_id);
92
	struct tcf_police_params *new;
93
	bool exists = false;
94
	int size;
L
Linus Torvalds 已提交
95

96
	if (nla == NULL)
L
Linus Torvalds 已提交
97 98
		return -EINVAL;

99
	err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
100 101 102
	if (err < 0)
		return err;

103
	if (tb[TCA_POLICE_TBF] == NULL)
104
		return -EINVAL;
105
	size = nla_len(tb[TCA_POLICE_TBF]);
106
	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
L
Linus Torvalds 已提交
107
		return -EINVAL;
108

109
	parm = nla_data(tb[TCA_POLICE_TBF]);
110 111 112 113
	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
	if (err < 0)
		return err;
	exists = err;
114 115
	if (exists && bind)
		return 0;
L
Linus Torvalds 已提交
116

117
	if (!exists) {
118
		ret = tcf_idr_create(tn, parm->index, NULL, a,
119
				     &act_police_ops, bind, true);
120 121
		if (ret) {
			tcf_idr_cleanup(tn, parm->index);
122
			return ret;
123
		}
124
		ret = ACT_P_CREATED;
125
	} else if (!ovr) {
126
		tcf_idr_release(*a, bind);
127
		return -EEXIST;
L
Linus Torvalds 已提交
128 129
	}

130
	police = to_police(*a);
L
Linus Torvalds 已提交
131 132
	if (parm->rate.rate) {
		err = -ENOMEM;
133
		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
L
Linus Torvalds 已提交
134 135
		if (R_tab == NULL)
			goto failure;
136

L
Linus Torvalds 已提交
137 138
		if (parm->peakrate.rate) {
			P_tab = qdisc_get_rtab(&parm->peakrate,
139
					       tb[TCA_POLICE_PEAKRATE], NULL);
140
			if (P_tab == NULL)
L
Linus Torvalds 已提交
141 142 143
				goto failure;
		}
	}
144 145

	if (est) {
146 147
		err = gen_replace_estimator(&police->tcf_bstats,
					    police->common.cpu_bstats,
148
					    &police->tcf_rate_est,
149 150
					    &police->tcf_lock,
					    NULL, est);
151
		if (err)
152
			goto failure;
153 154
	} else if (tb[TCA_POLICE_AVRATE] &&
		   (ret == ACT_P_CREATED ||
155
		    !gen_estimator_active(&police->tcf_rate_est))) {
156
		err = -EINVAL;
157
		goto failure;
158 159
	}

160 161 162 163 164 165
	new = kzalloc(sizeof(*new), GFP_KERNEL);
	if (unlikely(!new)) {
		err = -ENOMEM;
		goto failure;
	}

166
	/* No failure allowed after this point */
167 168 169
	new->tcfp_mtu = parm->mtu;
	if (!new->tcfp_mtu) {
		new->tcfp_mtu = ~0;
170
		if (R_tab)
171
			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
172 173
	}
	if (R_tab) {
174 175
		new->rate_present = true;
		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
176 177
		qdisc_put_rtab(R_tab);
	} else {
178
		new->rate_present = false;
L
Linus Torvalds 已提交
179
	}
180
	if (P_tab) {
181 182
		new->peak_present = true;
		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
183 184
		qdisc_put_rtab(P_tab);
	} else {
185
		new->peak_present = false;
L
Linus Torvalds 已提交
186 187
	}

188 189 190 191 192 193
	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
	new->tcfp_toks = new->tcfp_burst;
	if (new->peak_present) {
		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
							 new->tcfp_mtu);
		new->tcfp_ptoks = new->tcfp_mtu_ptoks;
L
Linus Torvalds 已提交
194 195
	}

196
	if (tb[TCA_POLICE_AVRATE])
197
		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
L
Linus Torvalds 已提交
198

199 200 201 202 203 204 205 206 207 208
	if (tb[TCA_POLICE_RESULT]) {
		new->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
		if (TC_ACT_EXT_CMP(new->tcfp_result, TC_ACT_GOTO_CHAIN)) {
			NL_SET_ERR_MSG(extack,
				       "goto chain not allowed on fallback");
			err = -EINVAL;
			goto failure;
		}
	}

209 210 211 212 213 214
	spin_lock_bh(&police->tcf_lock);
	new->tcfp_t_c = ktime_get_ns();
	police->tcf_action = parm->action;
	rcu_swap_protected(police->params,
			   new,
			   lockdep_is_held(&police->tcf_lock));
215
	spin_unlock_bh(&police->tcf_lock);
L
Linus Torvalds 已提交
216

217 218
	if (new)
		kfree_rcu(new, rcu);
L
Linus Torvalds 已提交
219

220 221
	if (ret == ACT_P_CREATED)
		tcf_idr_insert(tn, *a);
L
Linus Torvalds 已提交
222 223 224
	return ret;

failure:
225 226
	qdisc_put_rtab(P_tab);
	qdisc_put_rtab(R_tab);
227
	tcf_idr_release(*a, bind);
L
Linus Torvalds 已提交
228 229 230
	return err;
}

231
static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
232
			  struct tcf_result *res)
L
Linus Torvalds 已提交
233
{
234
	struct tcf_police *police = to_police(a);
235
	struct tcf_police_params *p;
236 237
	s64 now, toks, ptoks = 0;
	int ret;
L
Linus Torvalds 已提交
238

239
	tcf_lastuse_update(&police->tcf_tm);
240
	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
L
Linus Torvalds 已提交
241

242 243 244 245
	ret = READ_ONCE(police->tcf_action);
	p = rcu_dereference_bh(police->params);

	if (p->tcfp_ewma_rate) {
246 247 248
		struct gnet_stats_rate_est64 sample;

		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
249
		    sample.bps >= p->tcfp_ewma_rate)
250
			goto inc_overlimits;
L
Linus Torvalds 已提交
251 252
	}

253 254 255 256
	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
		if (!p->rate_present) {
			ret = p->tcfp_result;
			goto end;
L
Linus Torvalds 已提交
257 258
		}

259
		now = ktime_get_ns();
260 261 262 263 264 265 266
		toks = min_t(s64, now - p->tcfp_t_c, p->tcfp_burst);
		if (p->peak_present) {
			ptoks = toks + p->tcfp_ptoks;
			if (ptoks > p->tcfp_mtu_ptoks)
				ptoks = p->tcfp_mtu_ptoks;
			ptoks -= (s64)psched_l2t_ns(&p->peak,
						    qdisc_pkt_len(skb));
L
Linus Torvalds 已提交
267
		}
268 269 270 271
		toks += p->tcfp_toks;
		if (toks > p->tcfp_burst)
			toks = p->tcfp_burst;
		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
L
Linus Torvalds 已提交
272
		if ((toks|ptoks) >= 0) {
273 274 275 276
			p->tcfp_t_c = now;
			p->tcfp_toks = toks;
			p->tcfp_ptoks = ptoks;
			ret = p->tcfp_result;
277
			goto inc_drops;
L
Linus Torvalds 已提交
278 279
		}
	}
280 281 282 283 284 285

inc_overlimits:
	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
inc_drops:
	if (ret == TC_ACT_SHOT)
		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
286
end:
287
	return ret;
L
Linus Torvalds 已提交
288 289
}

290 291 292 293 294 295 296 297 298 299
static void tcf_police_cleanup(struct tc_action *a)
{
	struct tcf_police *police = to_police(a);
	struct tcf_police_params *p;

	p = rcu_dereference_protected(police->params, 1);
	if (p)
		kfree_rcu(p, rcu);
}

300
static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
J
Jamal Hadi Salim 已提交
301
			       int bind, int ref)
L
Linus Torvalds 已提交
302
{
303
	unsigned char *b = skb_tail_pointer(skb);
304
	struct tcf_police *police = to_police(a);
305
	struct tcf_police_params *p;
306 307
	struct tc_police opt = {
		.index = police->tcf_index,
308 309
		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
310
	};
311
	struct tcf_t t;
312

313 314
	spin_lock_bh(&police->tcf_lock);
	opt.action = police->tcf_action;
315 316 317 318 319 320 321 322
	p = rcu_dereference_protected(police->params,
				      lockdep_is_held(&police->tcf_lock));
	opt.mtu = p->tcfp_mtu;
	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
	if (p->rate_present)
		psched_ratecfg_getrate(&opt.rate, &p->rate);
	if (p->peak_present)
		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
323 324
	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
		goto nla_put_failure;
325 326
	if (p->tcfp_result &&
	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
327
		goto nla_put_failure;
328 329
	if (p->tcfp_ewma_rate &&
	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
330
		goto nla_put_failure;
331 332 333

	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
334
	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
335 336 337
	t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
		goto nla_put_failure;
338
	spin_unlock_bh(&police->tcf_lock);
339

L
Linus Torvalds 已提交
340 341
	return skb->len;

342
nla_put_failure:
343
	spin_unlock_bh(&police->tcf_lock);
344
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
345 346 347
	return -1;
}

348
static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
349 350 351
{
	struct tc_action_net *tn = net_generic(net, police_net_id);

352
	return tcf_idr_search(tn, a, index);
353 354
}

L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362
MODULE_AUTHOR("Alexey Kuznetsov");
MODULE_DESCRIPTION("Policing actions");
MODULE_LICENSE("GPL");

static struct tc_action_ops act_police_ops = {
	.kind		=	"police",
	.type		=	TCA_ID_POLICE,
	.owner		=	THIS_MODULE,
363 364 365 366
	.act		=	tcf_police_act,
	.dump		=	tcf_police_dump,
	.init		=	tcf_police_init,
	.walk		=	tcf_police_walker,
367
	.lookup		=	tcf_police_search,
368
	.cleanup	=	tcf_police_cleanup,
369
	.size		=	sizeof(struct tcf_police),
370 371 372 373 374 375
};

static __net_init int police_init_net(struct net *net)
{
	struct tc_action_net *tn = net_generic(net, police_net_id);

376
	return tc_action_net_init(tn, &act_police_ops);
377 378
}

379
static void __net_exit police_exit_net(struct list_head *net_list)
380
{
381
	tc_action_net_exit(net_list, police_net_id);
382 383 384 385
}

static struct pernet_operations police_net_ops = {
	.init = police_init_net,
386
	.exit_batch = police_exit_net,
387 388
	.id   = &police_net_id,
	.size = sizeof(struct tc_action_net),
L
Linus Torvalds 已提交
389 390
};

J
Jamal Hadi Salim 已提交
391
static int __init police_init_module(void)
L
Linus Torvalds 已提交
392
{
393
	return tcf_register_action(&act_police_ops, &police_net_ops);
L
Linus Torvalds 已提交
394 395
}

J
Jamal Hadi Salim 已提交
396
static void __exit police_cleanup_module(void)
L
Linus Torvalds 已提交
397
{
398
	tcf_unregister_action(&act_police_ops, &police_net_ops);
L
Linus Torvalds 已提交
399 400 401 402
}

module_init(police_init_module);
module_exit(police_cleanup_module);