xref: /openbmc/linux/net/sched/act_police.c (revision 27a884dc3cb63b93c2b3b643f5b31eed5f8a4d26)
14bba3925SPatrick McHardy /*
24bba3925SPatrick McHardy  * net/sched/police.c	Input police filter.
34bba3925SPatrick McHardy  *
44bba3925SPatrick McHardy  *		This program is free software; you can redistribute it and/or
54bba3925SPatrick McHardy  *		modify it under the terms of the GNU General Public License
64bba3925SPatrick McHardy  *		as published by the Free Software Foundation; either version
74bba3925SPatrick McHardy  *		2 of the License, or (at your option) any later version.
84bba3925SPatrick McHardy  *
94bba3925SPatrick McHardy  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
104bba3925SPatrick McHardy  * 		J Hadi Salim (action changes)
114bba3925SPatrick McHardy  */
124bba3925SPatrick McHardy 
134bba3925SPatrick McHardy #include <asm/uaccess.h>
144bba3925SPatrick McHardy #include <asm/system.h>
154bba3925SPatrick McHardy #include <linux/bitops.h>
164bba3925SPatrick McHardy #include <linux/module.h>
174bba3925SPatrick McHardy #include <linux/types.h>
184bba3925SPatrick McHardy #include <linux/kernel.h>
194bba3925SPatrick McHardy #include <linux/string.h>
204bba3925SPatrick McHardy #include <linux/mm.h>
214bba3925SPatrick McHardy #include <linux/socket.h>
224bba3925SPatrick McHardy #include <linux/sockios.h>
234bba3925SPatrick McHardy #include <linux/in.h>
244bba3925SPatrick McHardy #include <linux/errno.h>
254bba3925SPatrick McHardy #include <linux/interrupt.h>
264bba3925SPatrick McHardy #include <linux/netdevice.h>
274bba3925SPatrick McHardy #include <linux/skbuff.h>
284bba3925SPatrick McHardy #include <linux/module.h>
294bba3925SPatrick McHardy #include <linux/rtnetlink.h>
304bba3925SPatrick McHardy #include <linux/init.h>
314bba3925SPatrick McHardy #include <net/sock.h>
324bba3925SPatrick McHardy #include <net/act_api.h>
334bba3925SPatrick McHardy 
34e9ce1cd3SDavid S. Miller #define L2T(p,L)   ((p)->tcfp_R_tab->data[(L)>>(p)->tcfp_R_tab->rate.cell_log])
35e9ce1cd3SDavid S. Miller #define L2T_P(p,L) ((p)->tcfp_P_tab->data[(L)>>(p)->tcfp_P_tab->rate.cell_log])
364bba3925SPatrick McHardy 
37e9ce1cd3SDavid S. Miller #define POL_TAB_MASK     15
38e9ce1cd3SDavid S. Miller static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
39e9ce1cd3SDavid S. Miller static u32 police_idx_gen;
404bba3925SPatrick McHardy static DEFINE_RWLOCK(police_lock);
414bba3925SPatrick McHardy 
42e9ce1cd3SDavid S. Miller static struct tcf_hashinfo police_hash_info = {
43e9ce1cd3SDavid S. Miller 	.htab	=	tcf_police_ht,
44e9ce1cd3SDavid S. Miller 	.hmask	=	POL_TAB_MASK,
45e9ce1cd3SDavid S. Miller 	.lock	=	&police_lock,
46e9ce1cd3SDavid S. Miller };
47e9ce1cd3SDavid S. Miller 
481e9b3d53SPatrick McHardy /* old policer structure from before tc actions */
491e9b3d53SPatrick McHardy struct tc_police_compat
501e9b3d53SPatrick McHardy {
511e9b3d53SPatrick McHardy 	u32			index;
521e9b3d53SPatrick McHardy 	int			action;
531e9b3d53SPatrick McHardy 	u32			limit;
541e9b3d53SPatrick McHardy 	u32			burst;
551e9b3d53SPatrick McHardy 	u32			mtu;
561e9b3d53SPatrick McHardy 	struct tc_ratespec	rate;
571e9b3d53SPatrick McHardy 	struct tc_ratespec	peakrate;
581e9b3d53SPatrick McHardy };
591e9b3d53SPatrick McHardy 
604bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
614bba3925SPatrick McHardy 
624bba3925SPatrick McHardy #ifdef CONFIG_NET_CLS_ACT
6383b950c8SJamal Hadi Salim static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
644bba3925SPatrick McHardy 			      int type, struct tc_action *a)
654bba3925SPatrick McHardy {
66e9ce1cd3SDavid S. Miller 	struct tcf_common *p;
674bba3925SPatrick McHardy 	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
684bba3925SPatrick McHardy 	struct rtattr *r;
694bba3925SPatrick McHardy 
704bba3925SPatrick McHardy 	read_lock(&police_lock);
714bba3925SPatrick McHardy 
724bba3925SPatrick McHardy 	s_i = cb->args[0];
734bba3925SPatrick McHardy 
74e9ce1cd3SDavid S. Miller 	for (i = 0; i < (POL_TAB_MASK + 1); i++) {
75e9ce1cd3SDavid S. Miller 		p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
764bba3925SPatrick McHardy 
77e9ce1cd3SDavid S. Miller 		for (; p; p = p->tcfc_next) {
784bba3925SPatrick McHardy 			index++;
794bba3925SPatrick McHardy 			if (index < s_i)
804bba3925SPatrick McHardy 				continue;
814bba3925SPatrick McHardy 			a->priv = p;
824bba3925SPatrick McHardy 			a->order = index;
83*27a884dcSArnaldo Carvalho de Melo 			r = (struct rtattr *)skb_tail_pointer(skb);
844bba3925SPatrick McHardy 			RTA_PUT(skb, a->order, 0, NULL);
854bba3925SPatrick McHardy 			if (type == RTM_DELACTION)
864bba3925SPatrick McHardy 				err = tcf_action_dump_1(skb, a, 0, 1);
874bba3925SPatrick McHardy 			else
884bba3925SPatrick McHardy 				err = tcf_action_dump_1(skb, a, 0, 0);
894bba3925SPatrick McHardy 			if (err < 0) {
904bba3925SPatrick McHardy 				index--;
914bba3925SPatrick McHardy 				skb_trim(skb, (u8*)r - skb->data);
924bba3925SPatrick McHardy 				goto done;
934bba3925SPatrick McHardy 			}
94*27a884dcSArnaldo Carvalho de Melo 			r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
954bba3925SPatrick McHardy 			n_i++;
964bba3925SPatrick McHardy 		}
974bba3925SPatrick McHardy 	}
984bba3925SPatrick McHardy done:
994bba3925SPatrick McHardy 	read_unlock(&police_lock);
1004bba3925SPatrick McHardy 	if (n_i)
1014bba3925SPatrick McHardy 		cb->args[0] += n_i;
1024bba3925SPatrick McHardy 	return n_i;
1034bba3925SPatrick McHardy 
1044bba3925SPatrick McHardy rtattr_failure:
1054bba3925SPatrick McHardy 	skb_trim(skb, (u8*)r - skb->data);
1064bba3925SPatrick McHardy 	goto done;
1074bba3925SPatrick McHardy }
1084bba3925SPatrick McHardy #endif
1094bba3925SPatrick McHardy 
1104bba3925SPatrick McHardy void tcf_police_destroy(struct tcf_police *p)
1114bba3925SPatrick McHardy {
112e9ce1cd3SDavid S. Miller 	unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
113e9ce1cd3SDavid S. Miller 	struct tcf_common **p1p;
1144bba3925SPatrick McHardy 
115e9ce1cd3SDavid S. Miller 	for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
116e9ce1cd3SDavid S. Miller 		if (*p1p == &p->common) {
1174bba3925SPatrick McHardy 			write_lock_bh(&police_lock);
118e9ce1cd3SDavid S. Miller 			*p1p = p->tcf_next;
1194bba3925SPatrick McHardy 			write_unlock_bh(&police_lock);
1204bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
121e9ce1cd3SDavid S. Miller 			gen_kill_estimator(&p->tcf_bstats,
122e9ce1cd3SDavid S. Miller 					   &p->tcf_rate_est);
1234bba3925SPatrick McHardy #endif
124e9ce1cd3SDavid S. Miller 			if (p->tcfp_R_tab)
125e9ce1cd3SDavid S. Miller 				qdisc_put_rtab(p->tcfp_R_tab);
126e9ce1cd3SDavid S. Miller 			if (p->tcfp_P_tab)
127e9ce1cd3SDavid S. Miller 				qdisc_put_rtab(p->tcfp_P_tab);
1284bba3925SPatrick McHardy 			kfree(p);
1294bba3925SPatrick McHardy 			return;
1304bba3925SPatrick McHardy 		}
1314bba3925SPatrick McHardy 	}
1324bba3925SPatrick McHardy 	BUG_TRAP(0);
1334bba3925SPatrick McHardy }
1344bba3925SPatrick McHardy 
1354bba3925SPatrick McHardy #ifdef CONFIG_NET_CLS_ACT
1364bba3925SPatrick McHardy static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
1374bba3925SPatrick McHardy 				 struct tc_action *a, int ovr, int bind)
1384bba3925SPatrick McHardy {
1394bba3925SPatrick McHardy 	unsigned h;
1404bba3925SPatrick McHardy 	int ret = 0, err;
1414bba3925SPatrick McHardy 	struct rtattr *tb[TCA_POLICE_MAX];
1424bba3925SPatrick McHardy 	struct tc_police *parm;
143e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
1444bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
1451e9b3d53SPatrick McHardy 	int size;
1464bba3925SPatrick McHardy 
1474bba3925SPatrick McHardy 	if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
1484bba3925SPatrick McHardy 		return -EINVAL;
1494bba3925SPatrick McHardy 
1501e9b3d53SPatrick McHardy 	if (tb[TCA_POLICE_TBF-1] == NULL)
1511e9b3d53SPatrick McHardy 		return -EINVAL;
1521e9b3d53SPatrick McHardy 	size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
1531e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1544bba3925SPatrick McHardy 		return -EINVAL;
1554bba3925SPatrick McHardy 	parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
1564bba3925SPatrick McHardy 
1574bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1] != NULL &&
1584bba3925SPatrick McHardy 	    RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
1594bba3925SPatrick McHardy 		return -EINVAL;
1604bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1] != NULL &&
1614bba3925SPatrick McHardy 	    RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
1624bba3925SPatrick McHardy 		return -EINVAL;
1634bba3925SPatrick McHardy 
164e9ce1cd3SDavid S. Miller 	if (parm->index) {
165e9ce1cd3SDavid S. Miller 		struct tcf_common *pc;
166e9ce1cd3SDavid S. Miller 
167e9ce1cd3SDavid S. Miller 		pc = tcf_hash_lookup(parm->index, &police_hash_info);
168e9ce1cd3SDavid S. Miller 		if (pc != NULL) {
169e9ce1cd3SDavid S. Miller 			a->priv = pc;
170e9ce1cd3SDavid S. Miller 			police = to_police(pc);
1714bba3925SPatrick McHardy 			if (bind) {
172e9ce1cd3SDavid S. Miller 				police->tcf_bindcnt += 1;
173e9ce1cd3SDavid S. Miller 				police->tcf_refcnt += 1;
1744bba3925SPatrick McHardy 			}
1754bba3925SPatrick McHardy 			if (ovr)
1764bba3925SPatrick McHardy 				goto override;
1774bba3925SPatrick McHardy 			return ret;
1784bba3925SPatrick McHardy 		}
179e9ce1cd3SDavid S. Miller 	}
1804bba3925SPatrick McHardy 
181e9ce1cd3SDavid S. Miller 	police = kzalloc(sizeof(*police), GFP_KERNEL);
182e9ce1cd3SDavid S. Miller 	if (police == NULL)
1834bba3925SPatrick McHardy 		return -ENOMEM;
1844bba3925SPatrick McHardy 	ret = ACT_P_CREATED;
185e9ce1cd3SDavid S. Miller 	police->tcf_refcnt = 1;
186e9ce1cd3SDavid S. Miller 	spin_lock_init(&police->tcf_lock);
187e9ce1cd3SDavid S. Miller 	police->tcf_stats_lock = &police->tcf_lock;
1884bba3925SPatrick McHardy 	if (bind)
189e9ce1cd3SDavid S. Miller 		police->tcf_bindcnt = 1;
1904bba3925SPatrick McHardy override:
1914bba3925SPatrick McHardy 	if (parm->rate.rate) {
1924bba3925SPatrick McHardy 		err = -ENOMEM;
1934bba3925SPatrick McHardy 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
1944bba3925SPatrick McHardy 		if (R_tab == NULL)
1954bba3925SPatrick McHardy 			goto failure;
1964bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1974bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
1984bba3925SPatrick McHardy 					       tb[TCA_POLICE_PEAKRATE-1]);
199e9ce1cd3SDavid S. Miller 			if (P_tab == NULL) {
2004bba3925SPatrick McHardy 				qdisc_put_rtab(R_tab);
2014bba3925SPatrick McHardy 				goto failure;
2024bba3925SPatrick McHardy 			}
2034bba3925SPatrick McHardy 		}
2044bba3925SPatrick McHardy 	}
2054bba3925SPatrick McHardy 	/* No failure allowed after this point */
206e9ce1cd3SDavid S. Miller 	spin_lock_bh(&police->tcf_lock);
2074bba3925SPatrick McHardy 	if (R_tab != NULL) {
208e9ce1cd3SDavid S. Miller 		qdisc_put_rtab(police->tcfp_R_tab);
209e9ce1cd3SDavid S. Miller 		police->tcfp_R_tab = R_tab;
2104bba3925SPatrick McHardy 	}
2114bba3925SPatrick McHardy 	if (P_tab != NULL) {
212e9ce1cd3SDavid S. Miller 		qdisc_put_rtab(police->tcfp_P_tab);
213e9ce1cd3SDavid S. Miller 		police->tcfp_P_tab = P_tab;
2144bba3925SPatrick McHardy 	}
2154bba3925SPatrick McHardy 
2164bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1])
217e9ce1cd3SDavid S. Miller 		police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
218e9ce1cd3SDavid S. Miller 	police->tcfp_toks = police->tcfp_burst = parm->burst;
219e9ce1cd3SDavid S. Miller 	police->tcfp_mtu = parm->mtu;
220e9ce1cd3SDavid S. Miller 	if (police->tcfp_mtu == 0) {
221e9ce1cd3SDavid S. Miller 		police->tcfp_mtu = ~0;
222e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab)
223e9ce1cd3SDavid S. Miller 			police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
2244bba3925SPatrick McHardy 	}
225e9ce1cd3SDavid S. Miller 	if (police->tcfp_P_tab)
226e9ce1cd3SDavid S. Miller 		police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
227e9ce1cd3SDavid S. Miller 	police->tcf_action = parm->action;
2284bba3925SPatrick McHardy 
2294bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
2304bba3925SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE-1])
231e9ce1cd3SDavid S. Miller 		police->tcfp_ewma_rate =
232e9ce1cd3SDavid S. Miller 			*(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
2334bba3925SPatrick McHardy 	if (est)
234e9ce1cd3SDavid S. Miller 		gen_replace_estimator(&police->tcf_bstats,
235e9ce1cd3SDavid S. Miller 				      &police->tcf_rate_est,
236e9ce1cd3SDavid S. Miller 				      police->tcf_stats_lock, est);
2374bba3925SPatrick McHardy #endif
2384bba3925SPatrick McHardy 
239e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
2404bba3925SPatrick McHardy 	if (ret != ACT_P_CREATED)
2414bba3925SPatrick McHardy 		return ret;
2424bba3925SPatrick McHardy 
243e9ce1cd3SDavid S. Miller 	PSCHED_GET_TIME(police->tcfp_t_c);
244e9ce1cd3SDavid S. Miller 	police->tcf_index = parm->index ? parm->index :
245e9ce1cd3SDavid S. Miller 		tcf_hash_new_index(&police_idx_gen, &police_hash_info);
246e9ce1cd3SDavid S. Miller 	h = tcf_hash(police->tcf_index, POL_TAB_MASK);
2474bba3925SPatrick McHardy 	write_lock_bh(&police_lock);
248e9ce1cd3SDavid S. Miller 	police->tcf_next = tcf_police_ht[h];
249e9ce1cd3SDavid S. Miller 	tcf_police_ht[h] = &police->common;
2504bba3925SPatrick McHardy 	write_unlock_bh(&police_lock);
2514bba3925SPatrick McHardy 
252e9ce1cd3SDavid S. Miller 	a->priv = police;
2534bba3925SPatrick McHardy 	return ret;
2544bba3925SPatrick McHardy 
2554bba3925SPatrick McHardy failure:
2564bba3925SPatrick McHardy 	if (ret == ACT_P_CREATED)
257e9ce1cd3SDavid S. Miller 		kfree(police);
2584bba3925SPatrick McHardy 	return err;
2594bba3925SPatrick McHardy }
2604bba3925SPatrick McHardy 
2614bba3925SPatrick McHardy static int tcf_act_police_cleanup(struct tc_action *a, int bind)
2624bba3925SPatrick McHardy {
263e9ce1cd3SDavid S. Miller 	struct tcf_police *p = a->priv;
2644bba3925SPatrick McHardy 
2654bba3925SPatrick McHardy 	if (p != NULL)
2664bba3925SPatrick McHardy 		return tcf_police_release(p, bind);
2674bba3925SPatrick McHardy 	return 0;
2684bba3925SPatrick McHardy }
2694bba3925SPatrick McHardy 
2704bba3925SPatrick McHardy static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
2714bba3925SPatrick McHardy 			  struct tcf_result *res)
2724bba3925SPatrick McHardy {
273e9ce1cd3SDavid S. Miller 	struct tcf_police *police = a->priv;
2744bba3925SPatrick McHardy 	psched_time_t now;
2754bba3925SPatrick McHardy 	long toks;
2764bba3925SPatrick McHardy 	long ptoks = 0;
2774bba3925SPatrick McHardy 
278e9ce1cd3SDavid S. Miller 	spin_lock(&police->tcf_lock);
2794bba3925SPatrick McHardy 
280e9ce1cd3SDavid S. Miller 	police->tcf_bstats.bytes += skb->len;
281e9ce1cd3SDavid S. Miller 	police->tcf_bstats.packets++;
2824bba3925SPatrick McHardy 
2834bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
284e9ce1cd3SDavid S. Miller 	if (police->tcfp_ewma_rate &&
285e9ce1cd3SDavid S. Miller 	    police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
286e9ce1cd3SDavid S. Miller 		police->tcf_qstats.overlimits++;
287e9ce1cd3SDavid S. Miller 		spin_unlock(&police->tcf_lock);
288e9ce1cd3SDavid S. Miller 		return police->tcf_action;
2894bba3925SPatrick McHardy 	}
2904bba3925SPatrick McHardy #endif
2914bba3925SPatrick McHardy 
292e9ce1cd3SDavid S. Miller 	if (skb->len <= police->tcfp_mtu) {
293e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab == NULL) {
294e9ce1cd3SDavid S. Miller 			spin_unlock(&police->tcf_lock);
295e9ce1cd3SDavid S. Miller 			return police->tcfp_result;
2964bba3925SPatrick McHardy 		}
2974bba3925SPatrick McHardy 
2984bba3925SPatrick McHardy 		PSCHED_GET_TIME(now);
2994bba3925SPatrick McHardy 
300e9ce1cd3SDavid S. Miller 		toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c,
301e9ce1cd3SDavid S. Miller 					 police->tcfp_burst);
302e9ce1cd3SDavid S. Miller 		if (police->tcfp_P_tab) {
303e9ce1cd3SDavid S. Miller 			ptoks = toks + police->tcfp_ptoks;
304e9ce1cd3SDavid S. Miller 			if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
305e9ce1cd3SDavid S. Miller 				ptoks = (long)L2T_P(police, police->tcfp_mtu);
306e9ce1cd3SDavid S. Miller 			ptoks -= L2T_P(police, skb->len);
3074bba3925SPatrick McHardy 		}
308e9ce1cd3SDavid S. Miller 		toks += police->tcfp_toks;
309e9ce1cd3SDavid S. Miller 		if (toks > (long)police->tcfp_burst)
310e9ce1cd3SDavid S. Miller 			toks = police->tcfp_burst;
311e9ce1cd3SDavid S. Miller 		toks -= L2T(police, skb->len);
3124bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
313e9ce1cd3SDavid S. Miller 			police->tcfp_t_c = now;
314e9ce1cd3SDavid S. Miller 			police->tcfp_toks = toks;
315e9ce1cd3SDavid S. Miller 			police->tcfp_ptoks = ptoks;
316e9ce1cd3SDavid S. Miller 			spin_unlock(&police->tcf_lock);
317e9ce1cd3SDavid S. Miller 			return police->tcfp_result;
3184bba3925SPatrick McHardy 		}
3194bba3925SPatrick McHardy 	}
3204bba3925SPatrick McHardy 
321e9ce1cd3SDavid S. Miller 	police->tcf_qstats.overlimits++;
322e9ce1cd3SDavid S. Miller 	spin_unlock(&police->tcf_lock);
323e9ce1cd3SDavid S. Miller 	return police->tcf_action;
3244bba3925SPatrick McHardy }
3254bba3925SPatrick McHardy 
3264bba3925SPatrick McHardy static int
3274bba3925SPatrick McHardy tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
3284bba3925SPatrick McHardy {
329*27a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
330e9ce1cd3SDavid S. Miller 	struct tcf_police *police = a->priv;
3314bba3925SPatrick McHardy 	struct tc_police opt;
3324bba3925SPatrick McHardy 
333e9ce1cd3SDavid S. Miller 	opt.index = police->tcf_index;
334e9ce1cd3SDavid S. Miller 	opt.action = police->tcf_action;
335e9ce1cd3SDavid S. Miller 	opt.mtu = police->tcfp_mtu;
336e9ce1cd3SDavid S. Miller 	opt.burst = police->tcfp_burst;
337e9ce1cd3SDavid S. Miller 	opt.refcnt = police->tcf_refcnt - ref;
338e9ce1cd3SDavid S. Miller 	opt.bindcnt = police->tcf_bindcnt - bind;
339e9ce1cd3SDavid S. Miller 	if (police->tcfp_R_tab)
340e9ce1cd3SDavid S. Miller 		opt.rate = police->tcfp_R_tab->rate;
3414bba3925SPatrick McHardy 	else
3424bba3925SPatrick McHardy 		memset(&opt.rate, 0, sizeof(opt.rate));
343e9ce1cd3SDavid S. Miller 	if (police->tcfp_P_tab)
344e9ce1cd3SDavid S. Miller 		opt.peakrate = police->tcfp_P_tab->rate;
3454bba3925SPatrick McHardy 	else
3464bba3925SPatrick McHardy 		memset(&opt.peakrate, 0, sizeof(opt.peakrate));
3474bba3925SPatrick McHardy 	RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
348e9ce1cd3SDavid S. Miller 	if (police->tcfp_result)
349e9ce1cd3SDavid S. Miller 		RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
350e9ce1cd3SDavid S. Miller 			&police->tcfp_result);
3514bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
352e9ce1cd3SDavid S. Miller 	if (police->tcfp_ewma_rate)
353e9ce1cd3SDavid S. Miller 		RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
3544bba3925SPatrick McHardy #endif
3554bba3925SPatrick McHardy 	return skb->len;
3564bba3925SPatrick McHardy 
3574bba3925SPatrick McHardy rtattr_failure:
3584bba3925SPatrick McHardy 	skb_trim(skb, b - skb->data);
3594bba3925SPatrick McHardy 	return -1;
3604bba3925SPatrick McHardy }
3614bba3925SPatrick McHardy 
3624bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3634bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3644bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3654bba3925SPatrick McHardy 
3664bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3674bba3925SPatrick McHardy 	.kind		=	"police",
368e9ce1cd3SDavid S. Miller 	.hinfo		=	&police_hash_info,
3694bba3925SPatrick McHardy 	.type		=	TCA_ID_POLICE,
3704bba3925SPatrick McHardy 	.capab		=	TCA_CAP_NONE,
3714bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
3724bba3925SPatrick McHardy 	.act		=	tcf_act_police,
3734bba3925SPatrick McHardy 	.dump		=	tcf_act_police_dump,
3744bba3925SPatrick McHardy 	.cleanup	=	tcf_act_police_cleanup,
375e9ce1cd3SDavid S. Miller 	.lookup		=	tcf_hash_search,
3764bba3925SPatrick McHardy 	.init		=	tcf_act_police_locate,
37783b950c8SJamal Hadi Salim 	.walk		=	tcf_act_police_walker
3784bba3925SPatrick McHardy };
3794bba3925SPatrick McHardy 
3804bba3925SPatrick McHardy static int __init
3814bba3925SPatrick McHardy police_init_module(void)
3824bba3925SPatrick McHardy {
3834bba3925SPatrick McHardy 	return tcf_register_action(&act_police_ops);
3844bba3925SPatrick McHardy }
3854bba3925SPatrick McHardy 
3864bba3925SPatrick McHardy static void __exit
3874bba3925SPatrick McHardy police_cleanup_module(void)
3884bba3925SPatrick McHardy {
3894bba3925SPatrick McHardy 	tcf_unregister_action(&act_police_ops);
3904bba3925SPatrick McHardy }
3914bba3925SPatrick McHardy 
3924bba3925SPatrick McHardy module_init(police_init_module);
3934bba3925SPatrick McHardy module_exit(police_cleanup_module);
3944bba3925SPatrick McHardy 
3954bba3925SPatrick McHardy #else /* CONFIG_NET_CLS_ACT */
3964bba3925SPatrick McHardy 
397e9ce1cd3SDavid S. Miller static struct tcf_common *tcf_police_lookup(u32 index)
398e9ce1cd3SDavid S. Miller {
399e9ce1cd3SDavid S. Miller 	struct tcf_hashinfo *hinfo = &police_hash_info;
400e9ce1cd3SDavid S. Miller 	struct tcf_common *p;
401e9ce1cd3SDavid S. Miller 
402e9ce1cd3SDavid S. Miller 	read_lock(hinfo->lock);
403e9ce1cd3SDavid S. Miller 	for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
404e9ce1cd3SDavid S. Miller 	     p = p->tcfc_next) {
405e9ce1cd3SDavid S. Miller 		if (p->tcfc_index == index)
406e9ce1cd3SDavid S. Miller 			break;
407e9ce1cd3SDavid S. Miller 	}
408e9ce1cd3SDavid S. Miller 	read_unlock(hinfo->lock);
409e9ce1cd3SDavid S. Miller 
410e9ce1cd3SDavid S. Miller 	return p;
411e9ce1cd3SDavid S. Miller }
412e9ce1cd3SDavid S. Miller 
413e9ce1cd3SDavid S. Miller static u32 tcf_police_new_index(void)
414e9ce1cd3SDavid S. Miller {
415e9ce1cd3SDavid S. Miller 	u32 *idx_gen = &police_idx_gen;
416e9ce1cd3SDavid S. Miller 	u32 val = *idx_gen;
417e9ce1cd3SDavid S. Miller 
418e9ce1cd3SDavid S. Miller 	do {
419e9ce1cd3SDavid S. Miller 		if (++val == 0)
420e9ce1cd3SDavid S. Miller 			val = 1;
421e9ce1cd3SDavid S. Miller 	} while (tcf_police_lookup(val));
422e9ce1cd3SDavid S. Miller 
423e9ce1cd3SDavid S. Miller 	return (*idx_gen = val);
424e9ce1cd3SDavid S. Miller }
425e9ce1cd3SDavid S. Miller 
4264bba3925SPatrick McHardy struct tcf_police *tcf_police_locate(struct rtattr *rta, struct rtattr *est)
4274bba3925SPatrick McHardy {
428e9ce1cd3SDavid S. Miller 	unsigned int h;
429e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
4304bba3925SPatrick McHardy 	struct rtattr *tb[TCA_POLICE_MAX];
4314bba3925SPatrick McHardy 	struct tc_police *parm;
4321e9b3d53SPatrick McHardy 	int size;
4334bba3925SPatrick McHardy 
4344bba3925SPatrick McHardy 	if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
4354bba3925SPatrick McHardy 		return NULL;
4364bba3925SPatrick McHardy 
4371e9b3d53SPatrick McHardy 	if (tb[TCA_POLICE_TBF-1] == NULL)
4381e9b3d53SPatrick McHardy 		return NULL;
4391e9b3d53SPatrick McHardy 	size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
4401e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
4414bba3925SPatrick McHardy 		return NULL;
4424bba3925SPatrick McHardy 
4434bba3925SPatrick McHardy 	parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
4444bba3925SPatrick McHardy 
445e9ce1cd3SDavid S. Miller 	if (parm->index) {
446e9ce1cd3SDavid S. Miller 		struct tcf_common *pc;
4474bba3925SPatrick McHardy 
448e9ce1cd3SDavid S. Miller 		pc = tcf_police_lookup(parm->index);
449e9ce1cd3SDavid S. Miller 		if (pc) {
450e9ce1cd3SDavid S. Miller 			police = to_police(pc);
451e9ce1cd3SDavid S. Miller 			police->tcf_refcnt++;
452e9ce1cd3SDavid S. Miller 			return police;
453e9ce1cd3SDavid S. Miller 		}
454e9ce1cd3SDavid S. Miller 	}
455e9ce1cd3SDavid S. Miller 	police = kzalloc(sizeof(*police), GFP_KERNEL);
456e9ce1cd3SDavid S. Miller 	if (unlikely(!police))
4574bba3925SPatrick McHardy 		return NULL;
4584bba3925SPatrick McHardy 
459e9ce1cd3SDavid S. Miller 	police->tcf_refcnt = 1;
460e9ce1cd3SDavid S. Miller 	spin_lock_init(&police->tcf_lock);
461e9ce1cd3SDavid S. Miller 	police->tcf_stats_lock = &police->tcf_lock;
4624bba3925SPatrick McHardy 	if (parm->rate.rate) {
463e9ce1cd3SDavid S. Miller 		police->tcfp_R_tab =
464e9ce1cd3SDavid S. Miller 			qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
465e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab == NULL)
4664bba3925SPatrick McHardy 			goto failure;
4674bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
468e9ce1cd3SDavid S. Miller 			police->tcfp_P_tab =
469e9ce1cd3SDavid S. Miller 				qdisc_get_rtab(&parm->peakrate,
4704bba3925SPatrick McHardy 					       tb[TCA_POLICE_PEAKRATE-1]);
471e9ce1cd3SDavid S. Miller 			if (police->tcfp_P_tab == NULL)
4724bba3925SPatrick McHardy 				goto failure;
4734bba3925SPatrick McHardy 		}
4744bba3925SPatrick McHardy 	}
4754bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1]) {
4764bba3925SPatrick McHardy 		if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
4774bba3925SPatrick McHardy 			goto failure;
478e9ce1cd3SDavid S. Miller 		police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
4794bba3925SPatrick McHardy 	}
4804bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
4814bba3925SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE-1]) {
4824bba3925SPatrick McHardy 		if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32))
4834bba3925SPatrick McHardy 			goto failure;
484e9ce1cd3SDavid S. Miller 		police->tcfp_ewma_rate =
485e9ce1cd3SDavid S. Miller 			*(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
4864bba3925SPatrick McHardy 	}
4874bba3925SPatrick McHardy #endif
488e9ce1cd3SDavid S. Miller 	police->tcfp_toks = police->tcfp_burst = parm->burst;
489e9ce1cd3SDavid S. Miller 	police->tcfp_mtu = parm->mtu;
490e9ce1cd3SDavid S. Miller 	if (police->tcfp_mtu == 0) {
491e9ce1cd3SDavid S. Miller 		police->tcfp_mtu = ~0;
492e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab)
493e9ce1cd3SDavid S. Miller 			police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
4944bba3925SPatrick McHardy 	}
495e9ce1cd3SDavid S. Miller 	if (police->tcfp_P_tab)
496e9ce1cd3SDavid S. Miller 		police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
497e9ce1cd3SDavid S. Miller 	PSCHED_GET_TIME(police->tcfp_t_c);
498e9ce1cd3SDavid S. Miller 	police->tcf_index = parm->index ? parm->index :
499e9ce1cd3SDavid S. Miller 		tcf_police_new_index();
500e9ce1cd3SDavid S. Miller 	police->tcf_action = parm->action;
5014bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
5024bba3925SPatrick McHardy 	if (est)
503e9ce1cd3SDavid S. Miller 		gen_new_estimator(&police->tcf_bstats, &police->tcf_rate_est,
504e9ce1cd3SDavid S. Miller 				  police->tcf_stats_lock, est);
5054bba3925SPatrick McHardy #endif
506e9ce1cd3SDavid S. Miller 	h = tcf_hash(police->tcf_index, POL_TAB_MASK);
5074bba3925SPatrick McHardy 	write_lock_bh(&police_lock);
508e9ce1cd3SDavid S. Miller 	police->tcf_next = tcf_police_ht[h];
509e9ce1cd3SDavid S. Miller 	tcf_police_ht[h] = &police->common;
5104bba3925SPatrick McHardy 	write_unlock_bh(&police_lock);
511e9ce1cd3SDavid S. Miller 	return police;
5124bba3925SPatrick McHardy 
5134bba3925SPatrick McHardy failure:
514e9ce1cd3SDavid S. Miller 	if (police->tcfp_R_tab)
515e9ce1cd3SDavid S. Miller 		qdisc_put_rtab(police->tcfp_R_tab);
516e9ce1cd3SDavid S. Miller 	kfree(police);
5174bba3925SPatrick McHardy 	return NULL;
5184bba3925SPatrick McHardy }
5194bba3925SPatrick McHardy 
520e9ce1cd3SDavid S. Miller int tcf_police(struct sk_buff *skb, struct tcf_police *police)
5214bba3925SPatrick McHardy {
5224bba3925SPatrick McHardy 	psched_time_t now;
5234bba3925SPatrick McHardy 	long toks;
5244bba3925SPatrick McHardy 	long ptoks = 0;
5254bba3925SPatrick McHardy 
526e9ce1cd3SDavid S. Miller 	spin_lock(&police->tcf_lock);
5274bba3925SPatrick McHardy 
528e9ce1cd3SDavid S. Miller 	police->tcf_bstats.bytes += skb->len;
529e9ce1cd3SDavid S. Miller 	police->tcf_bstats.packets++;
5304bba3925SPatrick McHardy 
5314bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
532e9ce1cd3SDavid S. Miller 	if (police->tcfp_ewma_rate &&
533e9ce1cd3SDavid S. Miller 	    police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
534e9ce1cd3SDavid S. Miller 		police->tcf_qstats.overlimits++;
535e9ce1cd3SDavid S. Miller 		spin_unlock(&police->tcf_lock);
536e9ce1cd3SDavid S. Miller 		return police->tcf_action;
5374bba3925SPatrick McHardy 	}
5384bba3925SPatrick McHardy #endif
539e9ce1cd3SDavid S. Miller 	if (skb->len <= police->tcfp_mtu) {
540e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab == NULL) {
541e9ce1cd3SDavid S. Miller 			spin_unlock(&police->tcf_lock);
542e9ce1cd3SDavid S. Miller 			return police->tcfp_result;
5434bba3925SPatrick McHardy 		}
5444bba3925SPatrick McHardy 
5454bba3925SPatrick McHardy 		PSCHED_GET_TIME(now);
546e9ce1cd3SDavid S. Miller 		toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c,
547e9ce1cd3SDavid S. Miller 					 police->tcfp_burst);
548e9ce1cd3SDavid S. Miller 		if (police->tcfp_P_tab) {
549e9ce1cd3SDavid S. Miller 			ptoks = toks + police->tcfp_ptoks;
550e9ce1cd3SDavid S. Miller 			if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
551e9ce1cd3SDavid S. Miller 				ptoks = (long)L2T_P(police, police->tcfp_mtu);
552e9ce1cd3SDavid S. Miller 			ptoks -= L2T_P(police, skb->len);
5534bba3925SPatrick McHardy 		}
554e9ce1cd3SDavid S. Miller 		toks += police->tcfp_toks;
555e9ce1cd3SDavid S. Miller 		if (toks > (long)police->tcfp_burst)
556e9ce1cd3SDavid S. Miller 			toks = police->tcfp_burst;
557e9ce1cd3SDavid S. Miller 		toks -= L2T(police, skb->len);
5584bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
559e9ce1cd3SDavid S. Miller 			police->tcfp_t_c = now;
560e9ce1cd3SDavid S. Miller 			police->tcfp_toks = toks;
561e9ce1cd3SDavid S. Miller 			police->tcfp_ptoks = ptoks;
562e9ce1cd3SDavid S. Miller 			spin_unlock(&police->tcf_lock);
563e9ce1cd3SDavid S. Miller 			return police->tcfp_result;
5644bba3925SPatrick McHardy 		}
5654bba3925SPatrick McHardy 	}
5664bba3925SPatrick McHardy 
567e9ce1cd3SDavid S. Miller 	police->tcf_qstats.overlimits++;
568e9ce1cd3SDavid S. Miller 	spin_unlock(&police->tcf_lock);
569e9ce1cd3SDavid S. Miller 	return police->tcf_action;
5704bba3925SPatrick McHardy }
5714bba3925SPatrick McHardy EXPORT_SYMBOL(tcf_police);
5724bba3925SPatrick McHardy 
573e9ce1cd3SDavid S. Miller int tcf_police_dump(struct sk_buff *skb, struct tcf_police *police)
5744bba3925SPatrick McHardy {
575*27a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
5764bba3925SPatrick McHardy 	struct tc_police opt;
5774bba3925SPatrick McHardy 
578e9ce1cd3SDavid S. Miller 	opt.index = police->tcf_index;
579e9ce1cd3SDavid S. Miller 	opt.action = police->tcf_action;
580e9ce1cd3SDavid S. Miller 	opt.mtu = police->tcfp_mtu;
581e9ce1cd3SDavid S. Miller 	opt.burst = police->tcfp_burst;
582e9ce1cd3SDavid S. Miller 	if (police->tcfp_R_tab)
583e9ce1cd3SDavid S. Miller 		opt.rate = police->tcfp_R_tab->rate;
5844bba3925SPatrick McHardy 	else
5854bba3925SPatrick McHardy 		memset(&opt.rate, 0, sizeof(opt.rate));
586e9ce1cd3SDavid S. Miller 	if (police->tcfp_P_tab)
587e9ce1cd3SDavid S. Miller 		opt.peakrate = police->tcfp_P_tab->rate;
5884bba3925SPatrick McHardy 	else
5894bba3925SPatrick McHardy 		memset(&opt.peakrate, 0, sizeof(opt.peakrate));
5904bba3925SPatrick McHardy 	RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
591e9ce1cd3SDavid S. Miller 	if (police->tcfp_result)
592e9ce1cd3SDavid S. Miller 		RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
593e9ce1cd3SDavid S. Miller 			&police->tcfp_result);
5944bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
595e9ce1cd3SDavid S. Miller 	if (police->tcfp_ewma_rate)
596e9ce1cd3SDavid S. Miller 		RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
5974bba3925SPatrick McHardy #endif
5984bba3925SPatrick McHardy 	return skb->len;
5994bba3925SPatrick McHardy 
6004bba3925SPatrick McHardy rtattr_failure:
6014bba3925SPatrick McHardy 	skb_trim(skb, b - skb->data);
6024bba3925SPatrick McHardy 	return -1;
6034bba3925SPatrick McHardy }
6044bba3925SPatrick McHardy 
605e9ce1cd3SDavid S. Miller int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *police)
6064bba3925SPatrick McHardy {
6074bba3925SPatrick McHardy 	struct gnet_dump d;
6084bba3925SPatrick McHardy 
6094bba3925SPatrick McHardy 	if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
610e9ce1cd3SDavid S. Miller 					 TCA_XSTATS, police->tcf_stats_lock,
611e9ce1cd3SDavid S. Miller 					 &d) < 0)
6124bba3925SPatrick McHardy 		goto errout;
6134bba3925SPatrick McHardy 
614e9ce1cd3SDavid S. Miller 	if (gnet_stats_copy_basic(&d, &police->tcf_bstats) < 0 ||
6154bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
616e9ce1cd3SDavid S. Miller 	    gnet_stats_copy_rate_est(&d, &police->tcf_rate_est) < 0 ||
6174bba3925SPatrick McHardy #endif
618e9ce1cd3SDavid S. Miller 	    gnet_stats_copy_queue(&d, &police->tcf_qstats) < 0)
6194bba3925SPatrick McHardy 		goto errout;
6204bba3925SPatrick McHardy 
6214bba3925SPatrick McHardy 	if (gnet_stats_finish_copy(&d) < 0)
6224bba3925SPatrick McHardy 		goto errout;
6234bba3925SPatrick McHardy 
6244bba3925SPatrick McHardy 	return 0;
6254bba3925SPatrick McHardy 
6264bba3925SPatrick McHardy errout:
6274bba3925SPatrick McHardy 	return -1;
6284bba3925SPatrick McHardy }
6294bba3925SPatrick McHardy 
6304bba3925SPatrick McHardy #endif /* CONFIG_NET_CLS_ACT */
631