act_police.c 10.5 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>
24
#include <net/pkt_cls.h>
25
#include <net/tc_act/tc_police.h>
26

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

29
static unsigned int police_net_id;
30
static struct tc_action_ops act_police_ops;
31

32
static int tcf_police_walker(struct net *net, struct sk_buff *skb,
33
				 struct netlink_callback *cb, int type,
34 35
				 const struct tc_action_ops *ops,
				 struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
36
{
37
	struct tc_action_net *tn = net_generic(net, police_net_id);
L
Linus Torvalds 已提交
38

39
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
L
Linus Torvalds 已提交
40 41
}

42 43 44 45 46 47 48
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 },
};

49
static int tcf_police_init(struct net *net, struct nlattr *nla,
50
			       struct nlattr *est, struct tc_action **a,
51
			       int ovr, int bind, bool rtnl_held,
52
			       struct tcf_proto *tp,
53
			       struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
54
{
55
	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
56
	struct nlattr *tb[TCA_POLICE_MAX + 1];
57
	struct tcf_chain *goto_ch = NULL;
L
Linus Torvalds 已提交
58
	struct tc_police *parm;
59
	struct tcf_police *police;
L
Linus Torvalds 已提交
60
	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
61
	struct tc_action_net *tn = net_generic(net, police_net_id);
62
	struct tcf_police_params *new;
63
	bool exists = false;
L
Linus Torvalds 已提交
64

65
	if (nla == NULL)
L
Linus Torvalds 已提交
66 67
		return -EINVAL;

68 69
	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
					  police_policy, NULL);
70 71 72
	if (err < 0)
		return err;

73
	if (tb[TCA_POLICE_TBF] == NULL)
74
		return -EINVAL;
75
	size = nla_len(tb[TCA_POLICE_TBF]);
76
	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
L
Linus Torvalds 已提交
77
		return -EINVAL;
78

79
	parm = nla_data(tb[TCA_POLICE_TBF]);
80 81 82 83
	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
	if (err < 0)
		return err;
	exists = err;
84 85
	if (exists && bind)
		return 0;
L
Linus Torvalds 已提交
86

87
	if (!exists) {
88
		ret = tcf_idr_create(tn, parm->index, NULL, a,
89
				     &act_police_ops, bind, true);
90 91
		if (ret) {
			tcf_idr_cleanup(tn, parm->index);
92
			return ret;
93
		}
94
		ret = ACT_P_CREATED;
95
		spin_lock_init(&(to_police(*a)->tcfp_lock));
96
	} else if (!ovr) {
97
		tcf_idr_release(*a, bind);
98
		return -EEXIST;
L
Linus Torvalds 已提交
99
	}
100 101 102
	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
	if (err < 0)
		goto release_idr;
L
Linus Torvalds 已提交
103

104
	police = to_police(*a);
L
Linus Torvalds 已提交
105 106
	if (parm->rate.rate) {
		err = -ENOMEM;
107
		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
L
Linus Torvalds 已提交
108 109
		if (R_tab == NULL)
			goto failure;
110

L
Linus Torvalds 已提交
111 112
		if (parm->peakrate.rate) {
			P_tab = qdisc_get_rtab(&parm->peakrate,
113
					       tb[TCA_POLICE_PEAKRATE], NULL);
114
			if (P_tab == NULL)
L
Linus Torvalds 已提交
115 116 117
				goto failure;
		}
	}
118 119

	if (est) {
120 121
		err = gen_replace_estimator(&police->tcf_bstats,
					    police->common.cpu_bstats,
122
					    &police->tcf_rate_est,
123 124
					    &police->tcf_lock,
					    NULL, est);
125
		if (err)
126
			goto failure;
127 128
	} else if (tb[TCA_POLICE_AVRATE] &&
		   (ret == ACT_P_CREATED ||
129
		    !gen_estimator_active(&police->tcf_rate_est))) {
130
		err = -EINVAL;
131
		goto failure;
132 133
	}

134 135 136 137 138 139 140 141 142 143
	if (tb[TCA_POLICE_RESULT]) {
		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
			NL_SET_ERR_MSG(extack,
				       "goto chain not allowed on fallback");
			err = -EINVAL;
			goto failure;
		}
	}

144 145 146 147 148 149
	new = kzalloc(sizeof(*new), GFP_KERNEL);
	if (unlikely(!new)) {
		err = -ENOMEM;
		goto failure;
	}

150
	/* No failure allowed after this point */
151
	new->tcfp_result = tcfp_result;
152 153 154
	new->tcfp_mtu = parm->mtu;
	if (!new->tcfp_mtu) {
		new->tcfp_mtu = ~0;
155
		if (R_tab)
156
			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
157 158
	}
	if (R_tab) {
159 160
		new->rate_present = true;
		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
161 162
		qdisc_put_rtab(R_tab);
	} else {
163
		new->rate_present = false;
L
Linus Torvalds 已提交
164
	}
165
	if (P_tab) {
166 167
		new->peak_present = true;
		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
168 169
		qdisc_put_rtab(P_tab);
	} else {
170
		new->peak_present = false;
L
Linus Torvalds 已提交
171 172
	}

173
	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
174
	if (new->peak_present)
175 176
		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
							 new->tcfp_mtu);
L
Linus Torvalds 已提交
177

178
	if (tb[TCA_POLICE_AVRATE])
179
		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
L
Linus Torvalds 已提交
180

181
	spin_lock_bh(&police->tcf_lock);
182 183 184 185 186 187
	spin_lock_bh(&police->tcfp_lock);
	police->tcfp_t_c = ktime_get_ns();
	police->tcfp_toks = new->tcfp_burst;
	if (new->peak_present)
		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
	spin_unlock_bh(&police->tcfp_lock);
188
	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
189 190 191
	rcu_swap_protected(police->params,
			   new,
			   lockdep_is_held(&police->tcf_lock));
192
	spin_unlock_bh(&police->tcf_lock);
L
Linus Torvalds 已提交
193

194 195
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
196 197
	if (new)
		kfree_rcu(new, rcu);
L
Linus Torvalds 已提交
198

199 200
	if (ret == ACT_P_CREATED)
		tcf_idr_insert(tn, *a);
L
Linus Torvalds 已提交
201 202 203
	return ret;

failure:
204 205
	qdisc_put_rtab(P_tab);
	qdisc_put_rtab(R_tab);
206 207 208
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
release_idr:
209
	tcf_idr_release(*a, bind);
L
Linus Torvalds 已提交
210 211 212
	return err;
}

213
static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
214
			  struct tcf_result *res)
L
Linus Torvalds 已提交
215
{
216
	struct tcf_police *police = to_police(a);
217
	struct tcf_police_params *p;
218 219
	s64 now, toks, ptoks = 0;
	int ret;
L
Linus Torvalds 已提交
220

221
	tcf_lastuse_update(&police->tcf_tm);
222
	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
L
Linus Torvalds 已提交
223

224 225 226 227
	ret = READ_ONCE(police->tcf_action);
	p = rcu_dereference_bh(police->params);

	if (p->tcfp_ewma_rate) {
228 229 230
		struct gnet_stats_rate_est64 sample;

		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
231
		    sample.bps >= p->tcfp_ewma_rate)
232
			goto inc_overlimits;
L
Linus Torvalds 已提交
233 234
	}

235 236 237 238
	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
		if (!p->rate_present) {
			ret = p->tcfp_result;
			goto end;
L
Linus Torvalds 已提交
239 240
		}

241
		now = ktime_get_ns();
242 243
		spin_lock_bh(&police->tcfp_lock);
		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
244
		if (p->peak_present) {
245
			ptoks = toks + police->tcfp_ptoks;
246 247 248 249
			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 已提交
250
		}
251
		toks += police->tcfp_toks;
252 253 254
		if (toks > p->tcfp_burst)
			toks = p->tcfp_burst;
		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
L
Linus Torvalds 已提交
255
		if ((toks|ptoks) >= 0) {
256 257 258 259
			police->tcfp_t_c = now;
			police->tcfp_toks = toks;
			police->tcfp_ptoks = ptoks;
			spin_unlock_bh(&police->tcfp_lock);
260
			ret = p->tcfp_result;
261
			goto inc_drops;
L
Linus Torvalds 已提交
262
		}
263
		spin_unlock_bh(&police->tcfp_lock);
L
Linus Torvalds 已提交
264
	}
265 266 267 268 269 270

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));
271
end:
272
	return ret;
L
Linus Torvalds 已提交
273 274
}

275 276 277 278 279 280 281 282 283 284
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);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298
static void tcf_police_stats_update(struct tc_action *a,
				    u64 bytes, u32 packets,
				    u64 lastuse, bool hw)
{
	struct tcf_police *police = to_police(a);
	struct tcf_t *tm = &police->tcf_tm;

	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
	if (hw)
		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
				   bytes, packets);
	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
}

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

312 313
	spin_lock_bh(&police->tcf_lock);
	opt.action = police->tcf_action;
314 315 316 317 318 319 320 321
	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);
322 323
	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
		goto nla_put_failure;
324 325
	if (p->tcfp_result &&
	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
326
		goto nla_put_failure;
327 328
	if (p->tcfp_ewma_rate &&
	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
329
		goto nla_put_failure;
330 331 332

	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
333
	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
334 335 336
	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;
337
	spin_unlock_bh(&police->tcf_lock);
338

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

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

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

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

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

static struct tc_action_ops act_police_ops = {
	.kind		=	"police",
360
	.id		=	TCA_ID_POLICE,
L
Linus Torvalds 已提交
361
	.owner		=	THIS_MODULE,
362
	.stats_update	=	tcf_police_stats_update,
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);