act_police.c 10.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2
/*
3
 * net/sched/act_police.c	Input police filter
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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>
17
#include <linux/slab.h>
L
Linus Torvalds 已提交
18
#include <net/act_api.h>
19
#include <net/netlink.h>
20
#include <net/pkt_cls.h>
21
#include <net/tc_act/tc_police.h>
22

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

25
static unsigned int police_net_id;
26
static struct tc_action_ops act_police_ops;
27

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

35
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
L
Linus Torvalds 已提交
36 37
}

38 39 40 41 42 43 44
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 },
};

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

62
	if (nla == NULL)
L
Linus Torvalds 已提交
63 64
		return -EINVAL;

65 66
	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
					  police_policy, NULL);
67 68 69
	if (err < 0)
		return err;

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

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

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

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

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

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

132 133 134 135 136 137 138 139 140 141
	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;
		}
	}

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

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

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

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

179
	spin_lock_bh(&police->tcf_lock);
180 181 182 183 184 185
	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);
186
	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
187 188 189
	rcu_swap_protected(police->params,
			   new,
			   lockdep_is_held(&police->tcf_lock));
190
	spin_unlock_bh(&police->tcf_lock);
L
Linus Torvalds 已提交
191

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

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

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

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

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

222 223 224 225
	ret = READ_ONCE(police->tcf_action);
	p = rcu_dereference_bh(police->params);

	if (p->tcfp_ewma_rate) {
226 227 228
		struct gnet_stats_rate_est64 sample;

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

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

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

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

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

283 284 285 286 287 288 289 290 291 292 293 294 295 296
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);
}

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

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

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

L
Linus Torvalds 已提交
337 338
	return skb->len;

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

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

349
	return tcf_idr_search(tn, a, index);
350 351
}

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

static struct tc_action_ops act_police_ops = {
	.kind		=	"police",
358
	.id		=	TCA_ID_POLICE,
L
Linus Torvalds 已提交
359
	.owner		=	THIS_MODULE,
360
	.stats_update	=	tcf_police_stats_update,
361 362 363 364
	.act		=	tcf_police_act,
	.dump		=	tcf_police_dump,
	.init		=	tcf_police_init,
	.walk		=	tcf_police_walker,
365
	.lookup		=	tcf_police_search,
366
	.cleanup	=	tcf_police_cleanup,
367
	.size		=	sizeof(struct tcf_police),
368 369 370 371 372 373
};

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

374
	return tc_action_net_init(net, tn, &act_police_ops);
375 376
}

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

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

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

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

module_init(police_init_module);
module_exit(police_cleanup_module);