act_police.c 10.7 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>
L
Linus Torvalds 已提交
25

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

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

	spinlock_t		tcfp_lock ____cacheline_aligned_in_smp;
	s64			tcfp_toks;
	s64			tcfp_ptoks;
	s64			tcfp_t_c;
47
};
48 49

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

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

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

64
static unsigned int police_net_id;
65
static struct tc_action_ops act_police_ops;
66

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

74
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
L
Linus Torvalds 已提交
75 76
}

77 78 79 80 81 82 83
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 },
};

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

100
	if (nla == NULL)
L
Linus Torvalds 已提交
101 102
		return -EINVAL;

103
	err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
104 105 106
	if (err < 0)
		return err;

107
	if (tb[TCA_POLICE_TBF] == NULL)
108
		return -EINVAL;
109
	size = nla_len(tb[TCA_POLICE_TBF]);
110
	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
L
Linus Torvalds 已提交
111
		return -EINVAL;
112

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

121
	if (!exists) {
122
		ret = tcf_idr_create(tn, parm->index, NULL, a,
123
				     &act_police_ops, bind, true);
124 125
		if (ret) {
			tcf_idr_cleanup(tn, parm->index);
126
			return ret;
127
		}
128
		ret = ACT_P_CREATED;
129
		spin_lock_init(&(to_police(*a)->tcfp_lock));
130
	} else if (!ovr) {
131
		tcf_idr_release(*a, bind);
132
		return -EEXIST;
L
Linus Torvalds 已提交
133
	}
134 135 136
	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
	if (err < 0)
		goto release_idr;
L
Linus Torvalds 已提交
137

138
	police = to_police(*a);
L
Linus Torvalds 已提交
139 140
	if (parm->rate.rate) {
		err = -ENOMEM;
141
		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
L
Linus Torvalds 已提交
142 143
		if (R_tab == NULL)
			goto failure;
144

L
Linus Torvalds 已提交
145 146
		if (parm->peakrate.rate) {
			P_tab = qdisc_get_rtab(&parm->peakrate,
147
					       tb[TCA_POLICE_PEAKRATE], NULL);
148
			if (P_tab == NULL)
L
Linus Torvalds 已提交
149 150 151
				goto failure;
		}
	}
152 153

	if (est) {
154 155
		err = gen_replace_estimator(&police->tcf_bstats,
					    police->common.cpu_bstats,
156
					    &police->tcf_rate_est,
157 158
					    &police->tcf_lock,
					    NULL, est);
159
		if (err)
160
			goto failure;
161 162
	} else if (tb[TCA_POLICE_AVRATE] &&
		   (ret == ACT_P_CREATED ||
163
		    !gen_estimator_active(&police->tcf_rate_est))) {
164
		err = -EINVAL;
165
		goto failure;
166 167
	}

168 169 170 171 172 173 174 175 176 177
	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;
		}
	}

178 179 180 181 182 183
	new = kzalloc(sizeof(*new), GFP_KERNEL);
	if (unlikely(!new)) {
		err = -ENOMEM;
		goto failure;
	}

184
	/* No failure allowed after this point */
185
	new->tcfp_result = tcfp_result;
186 187 188
	new->tcfp_mtu = parm->mtu;
	if (!new->tcfp_mtu) {
		new->tcfp_mtu = ~0;
189
		if (R_tab)
190
			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
191 192
	}
	if (R_tab) {
193 194
		new->rate_present = true;
		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
195 196
		qdisc_put_rtab(R_tab);
	} else {
197
		new->rate_present = false;
L
Linus Torvalds 已提交
198
	}
199
	if (P_tab) {
200 201
		new->peak_present = true;
		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
202 203
		qdisc_put_rtab(P_tab);
	} else {
204
		new->peak_present = false;
L
Linus Torvalds 已提交
205 206
	}

207
	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
208
	if (new->peak_present)
209 210
		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
							 new->tcfp_mtu);
L
Linus Torvalds 已提交
211

212
	if (tb[TCA_POLICE_AVRATE])
213
		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
L
Linus Torvalds 已提交
214

215
	spin_lock_bh(&police->tcf_lock);
216 217 218 219 220 221
	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);
222
	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
223 224 225
	rcu_swap_protected(police->params,
			   new,
			   lockdep_is_held(&police->tcf_lock));
226
	spin_unlock_bh(&police->tcf_lock);
L
Linus Torvalds 已提交
227

228 229
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
230 231
	if (new)
		kfree_rcu(new, rcu);
L
Linus Torvalds 已提交
232

233 234
	if (ret == ACT_P_CREATED)
		tcf_idr_insert(tn, *a);
L
Linus Torvalds 已提交
235 236 237
	return ret;

failure:
238 239
	qdisc_put_rtab(P_tab);
	qdisc_put_rtab(R_tab);
240 241 242
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
release_idr:
243
	tcf_idr_release(*a, bind);
L
Linus Torvalds 已提交
244 245 246
	return err;
}

247
static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
248
			  struct tcf_result *res)
L
Linus Torvalds 已提交
249
{
250
	struct tcf_police *police = to_police(a);
251
	struct tcf_police_params *p;
252 253
	s64 now, toks, ptoks = 0;
	int ret;
L
Linus Torvalds 已提交
254

255
	tcf_lastuse_update(&police->tcf_tm);
256
	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
L
Linus Torvalds 已提交
257

258 259 260 261
	ret = READ_ONCE(police->tcf_action);
	p = rcu_dereference_bh(police->params);

	if (p->tcfp_ewma_rate) {
262 263 264
		struct gnet_stats_rate_est64 sample;

		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
265
		    sample.bps >= p->tcfp_ewma_rate)
266
			goto inc_overlimits;
L
Linus Torvalds 已提交
267 268
	}

269 270 271 272
	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
		if (!p->rate_present) {
			ret = p->tcfp_result;
			goto end;
L
Linus Torvalds 已提交
273 274
		}

275
		now = ktime_get_ns();
276 277
		spin_lock_bh(&police->tcfp_lock);
		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
278
		if (p->peak_present) {
279
			ptoks = toks + police->tcfp_ptoks;
280 281 282 283
			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 已提交
284
		}
285
		toks += police->tcfp_toks;
286 287 288
		if (toks > p->tcfp_burst)
			toks = p->tcfp_burst;
		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
L
Linus Torvalds 已提交
289
		if ((toks|ptoks) >= 0) {
290 291 292 293
			police->tcfp_t_c = now;
			police->tcfp_toks = toks;
			police->tcfp_ptoks = ptoks;
			spin_unlock_bh(&police->tcfp_lock);
294
			ret = p->tcfp_result;
295
			goto inc_drops;
L
Linus Torvalds 已提交
296
		}
297
		spin_unlock_bh(&police->tcfp_lock);
L
Linus Torvalds 已提交
298
	}
299 300 301 302 303 304

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));
305
end:
306
	return ret;
L
Linus Torvalds 已提交
307 308
}

309 310 311 312 313 314 315 316 317 318
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);
}

319
static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
J
Jamal Hadi Salim 已提交
320
			       int bind, int ref)
L
Linus Torvalds 已提交
321
{
322
	unsigned char *b = skb_tail_pointer(skb);
323
	struct tcf_police *police = to_police(a);
324
	struct tcf_police_params *p;
325 326
	struct tc_police opt = {
		.index = police->tcf_index,
327 328
		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
329
	};
330
	struct tcf_t t;
331

332 333
	spin_lock_bh(&police->tcf_lock);
	opt.action = police->tcf_action;
334 335 336 337 338 339 340 341
	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);
342 343
	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
		goto nla_put_failure;
344 345
	if (p->tcfp_result &&
	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
346
		goto nla_put_failure;
347 348
	if (p->tcfp_ewma_rate &&
	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
349
		goto nla_put_failure;
350 351 352

	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
353
	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
354 355 356
	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;
357
	spin_unlock_bh(&police->tcf_lock);
358

L
Linus Torvalds 已提交
359 360
	return skb->len;

361
nla_put_failure:
362
	spin_unlock_bh(&police->tcf_lock);
363
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
364 365 366
	return -1;
}

367
static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
368 369 370
{
	struct tc_action_net *tn = net_generic(net, police_net_id);

371
	return tcf_idr_search(tn, a, index);
372 373
}

L
Linus Torvalds 已提交
374 375 376 377 378 379
MODULE_AUTHOR("Alexey Kuznetsov");
MODULE_DESCRIPTION("Policing actions");
MODULE_LICENSE("GPL");

static struct tc_action_ops act_police_ops = {
	.kind		=	"police",
380
	.id		=	TCA_ID_POLICE,
L
Linus Torvalds 已提交
381
	.owner		=	THIS_MODULE,
382 383 384 385
	.act		=	tcf_police_act,
	.dump		=	tcf_police_dump,
	.init		=	tcf_police_init,
	.walk		=	tcf_police_walker,
386
	.lookup		=	tcf_police_search,
387
	.cleanup	=	tcf_police_cleanup,
388
	.size		=	sizeof(struct tcf_police),
389 390 391 392 393 394
};

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

395
	return tc_action_net_init(tn, &act_police_ops);
396 397
}

398
static void __net_exit police_exit_net(struct list_head *net_list)
399
{
400
	tc_action_net_exit(net_list, police_net_id);
401 402 403 404
}

static struct pernet_operations police_net_ops = {
	.init = police_init_net,
405
	.exit_batch = police_exit_net,
406 407
	.id   = &police_net_id,
	.size = sizeof(struct tc_action_net),
L
Linus Torvalds 已提交
408 409
};

J
Jamal Hadi Salim 已提交
410
static int __init police_init_module(void)
L
Linus Torvalds 已提交
411
{
412
	return tcf_register_action(&act_police_ops, &police_net_ops);
L
Linus Torvalds 已提交
413 414
}

J
Jamal Hadi Salim 已提交
415
static void __exit police_cleanup_module(void)
L
Linus Torvalds 已提交
416
{
417
	tcf_unregister_action(&act_police_ops, &police_net_ops);
L
Linus Torvalds 已提交
418 419 420 421
}

module_init(police_init_module);
module_exit(police_cleanup_module);