xref: /openbmc/linux/net/sched/act_police.c (revision 3bebcda28077375470dd60545b71bba2f83335fd)
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>
33dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
344bba3925SPatrick McHardy 
35e9ce1cd3SDavid S. Miller #define L2T(p,L)   ((p)->tcfp_R_tab->data[(L)>>(p)->tcfp_R_tab->rate.cell_log])
36e9ce1cd3SDavid S. Miller #define L2T_P(p,L) ((p)->tcfp_P_tab->data[(L)>>(p)->tcfp_P_tab->rate.cell_log])
374bba3925SPatrick McHardy 
38e9ce1cd3SDavid S. Miller #define POL_TAB_MASK     15
39e9ce1cd3SDavid S. Miller static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
40e9ce1cd3SDavid S. Miller static u32 police_idx_gen;
414bba3925SPatrick McHardy static DEFINE_RWLOCK(police_lock);
424bba3925SPatrick McHardy 
43e9ce1cd3SDavid S. Miller static struct tcf_hashinfo police_hash_info = {
44e9ce1cd3SDavid S. Miller 	.htab	=	tcf_police_ht,
45e9ce1cd3SDavid S. Miller 	.hmask	=	POL_TAB_MASK,
46e9ce1cd3SDavid S. Miller 	.lock	=	&police_lock,
47e9ce1cd3SDavid S. Miller };
48e9ce1cd3SDavid S. Miller 
491e9b3d53SPatrick McHardy /* old policer structure from before tc actions */
501e9b3d53SPatrick McHardy struct tc_police_compat
511e9b3d53SPatrick McHardy {
521e9b3d53SPatrick McHardy 	u32			index;
531e9b3d53SPatrick McHardy 	int			action;
541e9b3d53SPatrick McHardy 	u32			limit;
551e9b3d53SPatrick McHardy 	u32			burst;
561e9b3d53SPatrick McHardy 	u32			mtu;
571e9b3d53SPatrick McHardy 	struct tc_ratespec	rate;
581e9b3d53SPatrick McHardy 	struct tc_ratespec	peakrate;
591e9b3d53SPatrick McHardy };
601e9b3d53SPatrick McHardy 
614bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
624bba3925SPatrick McHardy 
634bba3925SPatrick McHardy #ifdef CONFIG_NET_CLS_ACT
6483b950c8SJamal Hadi Salim static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
654bba3925SPatrick McHardy 			      int type, struct tc_action *a)
664bba3925SPatrick McHardy {
67e9ce1cd3SDavid S. Miller 	struct tcf_common *p;
684bba3925SPatrick McHardy 	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
694bba3925SPatrick McHardy 	struct rtattr *r;
704bba3925SPatrick McHardy 
714bba3925SPatrick McHardy 	read_lock(&police_lock);
724bba3925SPatrick McHardy 
734bba3925SPatrick McHardy 	s_i = cb->args[0];
744bba3925SPatrick McHardy 
75e9ce1cd3SDavid S. Miller 	for (i = 0; i < (POL_TAB_MASK + 1); i++) {
76e9ce1cd3SDavid S. Miller 		p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
774bba3925SPatrick McHardy 
78e9ce1cd3SDavid S. Miller 		for (; p; p = p->tcfc_next) {
794bba3925SPatrick McHardy 			index++;
804bba3925SPatrick McHardy 			if (index < s_i)
814bba3925SPatrick McHardy 				continue;
824bba3925SPatrick McHardy 			a->priv = p;
834bba3925SPatrick McHardy 			a->order = index;
8427a884dcSArnaldo Carvalho de Melo 			r = (struct rtattr *)skb_tail_pointer(skb);
854bba3925SPatrick McHardy 			RTA_PUT(skb, a->order, 0, NULL);
864bba3925SPatrick McHardy 			if (type == RTM_DELACTION)
874bba3925SPatrick McHardy 				err = tcf_action_dump_1(skb, a, 0, 1);
884bba3925SPatrick McHardy 			else
894bba3925SPatrick McHardy 				err = tcf_action_dump_1(skb, a, 0, 0);
904bba3925SPatrick McHardy 			if (err < 0) {
914bba3925SPatrick McHardy 				index--;
92dc5fc579SArnaldo Carvalho de Melo 				nlmsg_trim(skb, r);
934bba3925SPatrick McHardy 				goto done;
944bba3925SPatrick McHardy 			}
9527a884dcSArnaldo Carvalho de Melo 			r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
964bba3925SPatrick McHardy 			n_i++;
974bba3925SPatrick McHardy 		}
984bba3925SPatrick McHardy 	}
994bba3925SPatrick McHardy done:
1004bba3925SPatrick McHardy 	read_unlock(&police_lock);
1014bba3925SPatrick McHardy 	if (n_i)
1024bba3925SPatrick McHardy 		cb->args[0] += n_i;
1034bba3925SPatrick McHardy 	return n_i;
1044bba3925SPatrick McHardy 
1054bba3925SPatrick McHardy rtattr_failure:
106dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, r);
1074bba3925SPatrick McHardy 	goto done;
1084bba3925SPatrick McHardy }
1094bba3925SPatrick McHardy #endif
1104bba3925SPatrick McHardy 
1114bba3925SPatrick McHardy void tcf_police_destroy(struct tcf_police *p)
1124bba3925SPatrick McHardy {
113e9ce1cd3SDavid S. Miller 	unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
114e9ce1cd3SDavid S. Miller 	struct tcf_common **p1p;
1154bba3925SPatrick McHardy 
116e9ce1cd3SDavid S. Miller 	for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
117e9ce1cd3SDavid S. Miller 		if (*p1p == &p->common) {
1184bba3925SPatrick McHardy 			write_lock_bh(&police_lock);
119e9ce1cd3SDavid S. Miller 			*p1p = p->tcf_next;
1204bba3925SPatrick McHardy 			write_unlock_bh(&police_lock);
1214bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
122e9ce1cd3SDavid S. Miller 			gen_kill_estimator(&p->tcf_bstats,
123e9ce1cd3SDavid S. Miller 					   &p->tcf_rate_est);
1244bba3925SPatrick McHardy #endif
125e9ce1cd3SDavid S. Miller 			if (p->tcfp_R_tab)
126e9ce1cd3SDavid S. Miller 				qdisc_put_rtab(p->tcfp_R_tab);
127e9ce1cd3SDavid S. Miller 			if (p->tcfp_P_tab)
128e9ce1cd3SDavid S. Miller 				qdisc_put_rtab(p->tcfp_P_tab);
1294bba3925SPatrick McHardy 			kfree(p);
1304bba3925SPatrick McHardy 			return;
1314bba3925SPatrick McHardy 		}
1324bba3925SPatrick McHardy 	}
1334bba3925SPatrick McHardy 	BUG_TRAP(0);
1344bba3925SPatrick McHardy }
1354bba3925SPatrick McHardy 
1364bba3925SPatrick McHardy #ifdef CONFIG_NET_CLS_ACT
1374bba3925SPatrick McHardy static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
1384bba3925SPatrick McHardy 				 struct tc_action *a, int ovr, int bind)
1394bba3925SPatrick McHardy {
1404bba3925SPatrick McHardy 	unsigned h;
1414bba3925SPatrick McHardy 	int ret = 0, err;
1424bba3925SPatrick McHardy 	struct rtattr *tb[TCA_POLICE_MAX];
1434bba3925SPatrick McHardy 	struct tc_police *parm;
144e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
1454bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
1461e9b3d53SPatrick McHardy 	int size;
1474bba3925SPatrick McHardy 
1484bba3925SPatrick McHardy 	if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
1494bba3925SPatrick McHardy 		return -EINVAL;
1504bba3925SPatrick McHardy 
1511e9b3d53SPatrick McHardy 	if (tb[TCA_POLICE_TBF-1] == NULL)
1521e9b3d53SPatrick McHardy 		return -EINVAL;
1531e9b3d53SPatrick McHardy 	size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
1541e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1554bba3925SPatrick McHardy 		return -EINVAL;
1564bba3925SPatrick McHardy 	parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
1574bba3925SPatrick McHardy 
1584bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1] != NULL &&
1594bba3925SPatrick McHardy 	    RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
1604bba3925SPatrick McHardy 		return -EINVAL;
1614bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1] != NULL &&
1624bba3925SPatrick McHardy 	    RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
1634bba3925SPatrick McHardy 		return -EINVAL;
1644bba3925SPatrick McHardy 
165e9ce1cd3SDavid S. Miller 	if (parm->index) {
166e9ce1cd3SDavid S. Miller 		struct tcf_common *pc;
167e9ce1cd3SDavid S. Miller 
168e9ce1cd3SDavid S. Miller 		pc = tcf_hash_lookup(parm->index, &police_hash_info);
169e9ce1cd3SDavid S. Miller 		if (pc != NULL) {
170e9ce1cd3SDavid S. Miller 			a->priv = pc;
171e9ce1cd3SDavid S. Miller 			police = to_police(pc);
1724bba3925SPatrick McHardy 			if (bind) {
173e9ce1cd3SDavid S. Miller 				police->tcf_bindcnt += 1;
174e9ce1cd3SDavid S. Miller 				police->tcf_refcnt += 1;
1754bba3925SPatrick McHardy 			}
1764bba3925SPatrick McHardy 			if (ovr)
1774bba3925SPatrick McHardy 				goto override;
1784bba3925SPatrick McHardy 			return ret;
1794bba3925SPatrick McHardy 		}
180e9ce1cd3SDavid S. Miller 	}
1814bba3925SPatrick McHardy 
182e9ce1cd3SDavid S. Miller 	police = kzalloc(sizeof(*police), GFP_KERNEL);
183e9ce1cd3SDavid S. Miller 	if (police == NULL)
1844bba3925SPatrick McHardy 		return -ENOMEM;
1854bba3925SPatrick McHardy 	ret = ACT_P_CREATED;
186e9ce1cd3SDavid S. Miller 	police->tcf_refcnt = 1;
187e9ce1cd3SDavid S. Miller 	spin_lock_init(&police->tcf_lock);
188e9ce1cd3SDavid S. Miller 	police->tcf_stats_lock = &police->tcf_lock;
1894bba3925SPatrick McHardy 	if (bind)
190e9ce1cd3SDavid S. Miller 		police->tcf_bindcnt = 1;
1914bba3925SPatrick McHardy override:
1924bba3925SPatrick McHardy 	if (parm->rate.rate) {
1934bba3925SPatrick McHardy 		err = -ENOMEM;
1944bba3925SPatrick McHardy 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
1954bba3925SPatrick McHardy 		if (R_tab == NULL)
1964bba3925SPatrick McHardy 			goto failure;
1974bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1984bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
1994bba3925SPatrick McHardy 					       tb[TCA_POLICE_PEAKRATE-1]);
200e9ce1cd3SDavid S. Miller 			if (P_tab == NULL) {
2014bba3925SPatrick McHardy 				qdisc_put_rtab(R_tab);
2024bba3925SPatrick McHardy 				goto failure;
2034bba3925SPatrick McHardy 			}
2044bba3925SPatrick McHardy 		}
2054bba3925SPatrick McHardy 	}
2064bba3925SPatrick McHardy 	/* No failure allowed after this point */
207e9ce1cd3SDavid S. Miller 	spin_lock_bh(&police->tcf_lock);
2084bba3925SPatrick McHardy 	if (R_tab != NULL) {
209e9ce1cd3SDavid S. Miller 		qdisc_put_rtab(police->tcfp_R_tab);
210e9ce1cd3SDavid S. Miller 		police->tcfp_R_tab = R_tab;
2114bba3925SPatrick McHardy 	}
2124bba3925SPatrick McHardy 	if (P_tab != NULL) {
213e9ce1cd3SDavid S. Miller 		qdisc_put_rtab(police->tcfp_P_tab);
214e9ce1cd3SDavid S. Miller 		police->tcfp_P_tab = P_tab;
2154bba3925SPatrick McHardy 	}
2164bba3925SPatrick McHardy 
2174bba3925SPatrick McHardy 	if (tb[TCA_POLICE_RESULT-1])
218e9ce1cd3SDavid S. Miller 		police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
219e9ce1cd3SDavid S. Miller 	police->tcfp_toks = police->tcfp_burst = parm->burst;
220e9ce1cd3SDavid S. Miller 	police->tcfp_mtu = parm->mtu;
221e9ce1cd3SDavid S. Miller 	if (police->tcfp_mtu == 0) {
222e9ce1cd3SDavid S. Miller 		police->tcfp_mtu = ~0;
223e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab)
224e9ce1cd3SDavid S. Miller 			police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
2254bba3925SPatrick McHardy 	}
226e9ce1cd3SDavid S. Miller 	if (police->tcfp_P_tab)
227e9ce1cd3SDavid S. Miller 		police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
228e9ce1cd3SDavid S. Miller 	police->tcf_action = parm->action;
2294bba3925SPatrick McHardy 
2304bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
2314bba3925SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE-1])
232e9ce1cd3SDavid S. Miller 		police->tcfp_ewma_rate =
233e9ce1cd3SDavid S. Miller 			*(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
2344bba3925SPatrick McHardy 	if (est)
235e9ce1cd3SDavid S. Miller 		gen_replace_estimator(&police->tcf_bstats,
236e9ce1cd3SDavid S. Miller 				      &police->tcf_rate_est,
237e9ce1cd3SDavid S. Miller 				      police->tcf_stats_lock, est);
2384bba3925SPatrick McHardy #endif
2394bba3925SPatrick McHardy 
240e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
2414bba3925SPatrick McHardy 	if (ret != ACT_P_CREATED)
2424bba3925SPatrick McHardy 		return ret;
2434bba3925SPatrick McHardy 
244*3bebcda2SPatrick McHardy 	police->tcfp_t_c = psched_get_time();
245e9ce1cd3SDavid S. Miller 	police->tcf_index = parm->index ? parm->index :
246e9ce1cd3SDavid S. Miller 		tcf_hash_new_index(&police_idx_gen, &police_hash_info);
247e9ce1cd3SDavid S. Miller 	h = tcf_hash(police->tcf_index, POL_TAB_MASK);
2484bba3925SPatrick McHardy 	write_lock_bh(&police_lock);
249e9ce1cd3SDavid S. Miller 	police->tcf_next = tcf_police_ht[h];
250e9ce1cd3SDavid S. Miller 	tcf_police_ht[h] = &police->common;
2514bba3925SPatrick McHardy 	write_unlock_bh(&police_lock);
2524bba3925SPatrick McHardy 
253e9ce1cd3SDavid S. Miller 	a->priv = police;
2544bba3925SPatrick McHardy 	return ret;
2554bba3925SPatrick McHardy 
2564bba3925SPatrick McHardy failure:
2574bba3925SPatrick McHardy 	if (ret == ACT_P_CREATED)
258e9ce1cd3SDavid S. Miller 		kfree(police);
2594bba3925SPatrick McHardy 	return err;
2604bba3925SPatrick McHardy }
2614bba3925SPatrick McHardy 
2624bba3925SPatrick McHardy static int tcf_act_police_cleanup(struct tc_action *a, int bind)
2634bba3925SPatrick McHardy {
264e9ce1cd3SDavid S. Miller 	struct tcf_police *p = a->priv;
2654bba3925SPatrick McHardy 
2664bba3925SPatrick McHardy 	if (p != NULL)
2674bba3925SPatrick McHardy 		return tcf_police_release(p, bind);
2684bba3925SPatrick McHardy 	return 0;
2694bba3925SPatrick McHardy }
2704bba3925SPatrick McHardy 
2714bba3925SPatrick McHardy static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
2724bba3925SPatrick McHardy 			  struct tcf_result *res)
2734bba3925SPatrick McHardy {
274e9ce1cd3SDavid S. Miller 	struct tcf_police *police = a->priv;
2754bba3925SPatrick McHardy 	psched_time_t now;
2764bba3925SPatrick McHardy 	long toks;
2774bba3925SPatrick McHardy 	long ptoks = 0;
2784bba3925SPatrick McHardy 
279e9ce1cd3SDavid S. Miller 	spin_lock(&police->tcf_lock);
2804bba3925SPatrick McHardy 
281e9ce1cd3SDavid S. Miller 	police->tcf_bstats.bytes += skb->len;
282e9ce1cd3SDavid S. Miller 	police->tcf_bstats.packets++;
2834bba3925SPatrick McHardy 
2844bba3925SPatrick McHardy #ifdef CONFIG_NET_ESTIMATOR
285e9ce1cd3SDavid S. Miller 	if (police->tcfp_ewma_rate &&
286e9ce1cd3SDavid S. Miller 	    police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
287e9ce1cd3SDavid S. Miller 		police->tcf_qstats.overlimits++;
288e9ce1cd3SDavid S. Miller 		spin_unlock(&police->tcf_lock);
289e9ce1cd3SDavid S. Miller 		return police->tcf_action;
2904bba3925SPatrick McHardy 	}
2914bba3925SPatrick McHardy #endif
2924bba3925SPatrick McHardy 
293e9ce1cd3SDavid S. Miller 	if (skb->len <= police->tcfp_mtu) {
294e9ce1cd3SDavid S. Miller 		if (police->tcfp_R_tab == NULL) {
295e9ce1cd3SDavid S. Miller 			spin_unlock(&police->tcf_lock);
296e9ce1cd3SDavid S. Miller 			return police->tcfp_result;
2974bba3925SPatrick McHardy 		}
2984bba3925SPatrick McHardy 
299*3bebcda2SPatrick McHardy 		now = psched_get_time();
30003cc45c0SPatrick McHardy 		toks = psched_tdiff_bounded(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 {
32927a884dcSArnaldo 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:
358dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
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);
497*3bebcda2SPatrick McHardy 	police->tcfp_t_c = psched_get_time();
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 
545*3bebcda2SPatrick McHardy 		now = psched_get_time();
54603cc45c0SPatrick McHardy 		toks = psched_tdiff_bounded(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 {
57527a884dcSArnaldo 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:
601dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
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