blob: fed47b658837aac39b6d29c4b893490a4ca42e90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/police.c Input police filter.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
11 */
12
13#include <asm/uaccess.h>
14#include <asm/system.h>
15#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
24#include <linux/in.h>
25#include <linux/errno.h>
26#include <linux/interrupt.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
29#include <linux/module.h>
30#include <linux/rtnetlink.h>
31#include <linux/init.h>
32#include <net/sock.h>
33#include <net/act_api.h>
34
David S. Millere9ce1cd2006-08-21 23:54:55 -070035#define L2T(p,L) ((p)->tcfp_R_tab->data[(L)>>(p)->tcfp_R_tab->rate.cell_log])
36#define L2T_P(p,L) ((p)->tcfp_P_tab->data[(L)>>(p)->tcfp_P_tab->rate.cell_log])
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David S. Millere9ce1cd2006-08-21 23:54:55 -070038#define POL_TAB_MASK 15
39static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
40static u32 police_idx_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DEFINE_RWLOCK(police_lock);
42
David S. Millere9ce1cd2006-08-21 23:54:55 -070043static struct tcf_hashinfo police_hash_info = {
44 .htab = tcf_police_ht,
45 .hmask = POL_TAB_MASK,
46 .lock = &police_lock,
47};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Each policer is serialized by its individual spinlock */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_NET_CLS_ACT
Jamal Hadi Salim83b950c2006-04-06 22:24:22 -070052static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int type, struct tc_action *a)
54{
David S. Millere9ce1cd2006-08-21 23:54:55 -070055 struct tcf_common *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
57 struct rtattr *r;
58
59 read_lock(&police_lock);
60
61 s_i = cb->args[0];
62
David S. Millere9ce1cd2006-08-21 23:54:55 -070063 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
64 p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David S. Millere9ce1cd2006-08-21 23:54:55 -070066 for (; p; p = p->tcfc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 index++;
68 if (index < s_i)
69 continue;
70 a->priv = p;
71 a->order = index;
72 r = (struct rtattr*) skb->tail;
73 RTA_PUT(skb, a->order, 0, NULL);
74 if (type == RTM_DELACTION)
75 err = tcf_action_dump_1(skb, a, 0, 1);
76 else
77 err = tcf_action_dump_1(skb, a, 0, 0);
78 if (err < 0) {
79 index--;
80 skb_trim(skb, (u8*)r - skb->data);
81 goto done;
82 }
83 r->rta_len = skb->tail - (u8*)r;
84 n_i++;
85 }
86 }
87done:
88 read_unlock(&police_lock);
89 if (n_i)
90 cb->args[0] += n_i;
91 return n_i;
92
93rtattr_failure:
94 skb_trim(skb, (u8*)r - skb->data);
95 goto done;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099void tcf_police_destroy(struct tcf_police *p)
100{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
102 struct tcf_common **p1p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
105 if (*p1p == &p->common) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 write_lock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107 *p1p = p->tcf_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 write_unlock_bh(&police_lock);
109#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700110 gen_kill_estimator(&p->tcf_bstats,
111 &p->tcf_rate_est);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700113 if (p->tcfp_R_tab)
114 qdisc_put_rtab(p->tcfp_R_tab);
115 if (p->tcfp_P_tab)
116 qdisc_put_rtab(p->tcfp_P_tab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 kfree(p);
118 return;
119 }
120 }
121 BUG_TRAP(0);
122}
123
124#ifdef CONFIG_NET_CLS_ACT
125static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
126 struct tc_action *a, int ovr, int bind)
127{
128 unsigned h;
129 int ret = 0, err;
130 struct rtattr *tb[TCA_POLICE_MAX];
131 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
134
135 if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
136 return -EINVAL;
137
138 if (tb[TCA_POLICE_TBF-1] == NULL ||
139 RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
140 return -EINVAL;
141 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
142
143 if (tb[TCA_POLICE_RESULT-1] != NULL &&
144 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
145 return -EINVAL;
146 if (tb[TCA_POLICE_RESULT-1] != NULL &&
147 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
148 return -EINVAL;
149
David S. Millere9ce1cd2006-08-21 23:54:55 -0700150 if (parm->index) {
151 struct tcf_common *pc;
152
153 pc = tcf_hash_lookup(parm->index, &police_hash_info);
154 if (pc != NULL) {
155 a->priv = pc;
156 police = to_police(pc);
157 if (bind) {
158 police->tcf_bindcnt += 1;
159 police->tcf_refcnt += 1;
160 }
161 if (ovr)
162 goto override;
163 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 police = kzalloc(sizeof(*police), GFP_KERNEL);
168 if (police == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ret = ACT_P_CREATED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 police->tcf_refcnt = 1;
172 spin_lock_init(&police->tcf_lock);
173 police->tcf_stats_lock = &police->tcf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700175 police->tcf_bindcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176override:
177 if (parm->rate.rate) {
178 err = -ENOMEM;
179 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
180 if (R_tab == NULL)
181 goto failure;
182 if (parm->peakrate.rate) {
183 P_tab = qdisc_get_rtab(&parm->peakrate,
184 tb[TCA_POLICE_PEAKRATE-1]);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700185 if (P_tab == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 qdisc_put_rtab(R_tab);
187 goto failure;
188 }
189 }
190 }
191 /* No failure allowed after this point */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192 spin_lock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (R_tab != NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194 qdisc_put_rtab(police->tcfp_R_tab);
195 police->tcfp_R_tab = R_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 if (P_tab != NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700198 qdisc_put_rtab(police->tcfp_P_tab);
199 police->tcfp_P_tab = P_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 if (tb[TCA_POLICE_RESULT-1])
David S. Millere9ce1cd2006-08-21 23:54:55 -0700203 police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
204 police->tcfp_toks = police->tcfp_burst = parm->burst;
205 police->tcfp_mtu = parm->mtu;
206 if (police->tcfp_mtu == 0) {
207 police->tcfp_mtu = ~0;
208 if (police->tcfp_R_tab)
209 police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 if (police->tcfp_P_tab)
212 police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
213 police->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215#ifdef CONFIG_NET_ESTIMATOR
216 if (tb[TCA_POLICE_AVRATE-1])
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217 police->tcfp_ewma_rate =
218 *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (est)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220 gen_replace_estimator(&police->tcf_bstats,
221 &police->tcf_rate_est,
222 police->tcf_stats_lock, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif
224
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (ret != ACT_P_CREATED)
227 return ret;
228
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 PSCHED_GET_TIME(police->tcfp_t_c);
230 police->tcf_index = parm->index ? parm->index :
231 tcf_hash_new_index(&police_idx_gen, &police_hash_info);
232 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 write_lock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 police->tcf_next = tcf_police_ht[h];
235 tcf_police_ht[h] = &police->common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 write_unlock_bh(&police_lock);
237
David S. Millere9ce1cd2006-08-21 23:54:55 -0700238 a->priv = police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240
241failure:
242 if (ret == ACT_P_CREATED)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700243 kfree(police);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return err;
245}
246
247static int tcf_act_police_cleanup(struct tc_action *a, int bind)
248{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700249 struct tcf_police *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (p != NULL)
252 return tcf_police_release(p, bind);
253 return 0;
254}
255
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800256static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
Patrick McHardyabc3bc52005-08-09 19:25:56 -0700257 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259 struct tcf_police *police = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 psched_time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 long toks;
262 long ptoks = 0;
263
David S. Millere9ce1cd2006-08-21 23:54:55 -0700264 spin_lock(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David S. Millere9ce1cd2006-08-21 23:54:55 -0700266 police->tcf_bstats.bytes += skb->len;
267 police->tcf_bstats.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700270 if (police->tcfp_ewma_rate &&
271 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
272 police->tcf_qstats.overlimits++;
273 spin_unlock(&police->tcf_lock);
274 return police->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276#endif
277
David S. Millere9ce1cd2006-08-21 23:54:55 -0700278 if (skb->len <= police->tcfp_mtu) {
279 if (police->tcfp_R_tab == NULL) {
280 spin_unlock(&police->tcf_lock);
281 return police->tcfp_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 PSCHED_GET_TIME(now);
285
David S. Millere9ce1cd2006-08-21 23:54:55 -0700286 toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c,
287 police->tcfp_burst);
288 if (police->tcfp_P_tab) {
289 ptoks = toks + police->tcfp_ptoks;
290 if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
291 ptoks = (long)L2T_P(police, police->tcfp_mtu);
292 ptoks -= L2T_P(police, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700294 toks += police->tcfp_toks;
295 if (toks > (long)police->tcfp_burst)
296 toks = police->tcfp_burst;
297 toks -= L2T(police, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if ((toks|ptoks) >= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700299 police->tcfp_t_c = now;
300 police->tcfp_toks = toks;
301 police->tcfp_ptoks = ptoks;
302 spin_unlock(&police->tcf_lock);
303 return police->tcfp_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 }
306
David S. Millere9ce1cd2006-08-21 23:54:55 -0700307 police->tcf_qstats.overlimits++;
308 spin_unlock(&police->tcf_lock);
309 return police->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static int
313tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
314{
315 unsigned char *b = skb->tail;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700316 struct tcf_police *police = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct tc_police opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
David S. Millere9ce1cd2006-08-21 23:54:55 -0700319 opt.index = police->tcf_index;
320 opt.action = police->tcf_action;
321 opt.mtu = police->tcfp_mtu;
322 opt.burst = police->tcfp_burst;
323 opt.refcnt = police->tcf_refcnt - ref;
324 opt.bindcnt = police->tcf_bindcnt - bind;
325 if (police->tcfp_R_tab)
326 opt.rate = police->tcfp_R_tab->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
328 memset(&opt.rate, 0, sizeof(opt.rate));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700329 if (police->tcfp_P_tab)
330 opt.peakrate = police->tcfp_P_tab->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 else
332 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
333 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700334 if (police->tcfp_result)
335 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
336 &police->tcfp_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700338 if (police->tcfp_ewma_rate)
339 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
341 return skb->len;
342
343rtattr_failure:
344 skb_trim(skb, b - skb->data);
345 return -1;
346}
347
348MODULE_AUTHOR("Alexey Kuznetsov");
349MODULE_DESCRIPTION("Policing actions");
350MODULE_LICENSE("GPL");
351
352static struct tc_action_ops act_police_ops = {
353 .kind = "police",
David S. Millere9ce1cd2006-08-21 23:54:55 -0700354 .hinfo = &police_hash_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .type = TCA_ID_POLICE,
356 .capab = TCA_CAP_NONE,
357 .owner = THIS_MODULE,
358 .act = tcf_act_police,
359 .dump = tcf_act_police_dump,
360 .cleanup = tcf_act_police_cleanup,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700361 .lookup = tcf_hash_search,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .init = tcf_act_police_locate,
Jamal Hadi Salim83b950c2006-04-06 22:24:22 -0700363 .walk = tcf_act_police_walker
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
366static int __init
367police_init_module(void)
368{
369 return tcf_register_action(&act_police_ops);
370}
371
372static void __exit
373police_cleanup_module(void)
374{
375 tcf_unregister_action(&act_police_ops);
376}
377
378module_init(police_init_module);
379module_exit(police_cleanup_module);
380
Patrick McHardy31bd06e2006-01-08 22:16:25 -0800381#else /* CONFIG_NET_CLS_ACT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
David S. Millere9ce1cd2006-08-21 23:54:55 -0700383static struct tcf_common *tcf_police_lookup(u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700385 struct tcf_hashinfo *hinfo = &police_hash_info;
386 struct tcf_common *p;
387
388 read_lock(hinfo->lock);
389 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
390 p = p->tcfc_next) {
391 if (p->tcfc_index == index)
392 break;
393 }
394 read_unlock(hinfo->lock);
395
396 return p;
397}
398
399static u32 tcf_police_new_index(void)
400{
401 u32 *idx_gen = &police_idx_gen;
402 u32 val = *idx_gen;
403
404 do {
405 if (++val == 0)
406 val = 1;
407 } while (tcf_police_lookup(val));
408
409 return (*idx_gen = val);
410}
411
412struct tcf_police *tcf_police_locate(struct rtattr *rta, struct rtattr *est)
413{
414 unsigned int h;
415 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct rtattr *tb[TCA_POLICE_MAX];
417 struct tc_police *parm;
418
419 if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
420 return NULL;
421
422 if (tb[TCA_POLICE_TBF-1] == NULL ||
423 RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
424 return NULL;
425
426 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
427
David S. Millere9ce1cd2006-08-21 23:54:55 -0700428 if (parm->index) {
429 struct tcf_common *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David S. Millere9ce1cd2006-08-21 23:54:55 -0700431 pc = tcf_police_lookup(parm->index);
432 if (pc) {
433 police = to_police(pc);
434 police->tcf_refcnt++;
435 return police;
436 }
437 }
438 police = kzalloc(sizeof(*police), GFP_KERNEL);
439 if (unlikely(!police))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return NULL;
441
David S. Millere9ce1cd2006-08-21 23:54:55 -0700442 police->tcf_refcnt = 1;
443 spin_lock_init(&police->tcf_lock);
444 police->tcf_stats_lock = &police->tcf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (parm->rate.rate) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700446 police->tcfp_R_tab =
447 qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
448 if (police->tcfp_R_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto failure;
450 if (parm->peakrate.rate) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700451 police->tcfp_P_tab =
452 qdisc_get_rtab(&parm->peakrate,
453 tb[TCA_POLICE_PEAKRATE-1]);
454 if (police->tcfp_P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto failure;
456 }
457 }
458 if (tb[TCA_POLICE_RESULT-1]) {
459 if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
460 goto failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700461 police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463#ifdef CONFIG_NET_ESTIMATOR
464 if (tb[TCA_POLICE_AVRATE-1]) {
465 if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32))
466 goto failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700467 police->tcfp_ewma_rate =
468 *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700471 police->tcfp_toks = police->tcfp_burst = parm->burst;
472 police->tcfp_mtu = parm->mtu;
473 if (police->tcfp_mtu == 0) {
474 police->tcfp_mtu = ~0;
475 if (police->tcfp_R_tab)
476 police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700478 if (police->tcfp_P_tab)
479 police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
480 PSCHED_GET_TIME(police->tcfp_t_c);
481 police->tcf_index = parm->index ? parm->index :
482 tcf_police_new_index();
483 police->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#ifdef CONFIG_NET_ESTIMATOR
485 if (est)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700486 gen_new_estimator(&police->tcf_bstats, &police->tcf_rate_est,
487 police->tcf_stats_lock, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700489 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 write_lock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700491 police->tcf_next = tcf_police_ht[h];
492 tcf_police_ht[h] = &police->common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 write_unlock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700494 return police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496failure:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700497 if (police->tcfp_R_tab)
498 qdisc_put_rtab(police->tcfp_R_tab);
499 kfree(police);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return NULL;
501}
502
David S. Millere9ce1cd2006-08-21 23:54:55 -0700503int tcf_police(struct sk_buff *skb, struct tcf_police *police)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 psched_time_t now;
506 long toks;
507 long ptoks = 0;
508
David S. Millere9ce1cd2006-08-21 23:54:55 -0700509 spin_lock(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
David S. Millere9ce1cd2006-08-21 23:54:55 -0700511 police->tcf_bstats.bytes += skb->len;
512 police->tcf_bstats.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700515 if (police->tcfp_ewma_rate &&
516 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
517 police->tcf_qstats.overlimits++;
518 spin_unlock(&police->tcf_lock);
519 return police->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700522 if (skb->len <= police->tcfp_mtu) {
523 if (police->tcfp_R_tab == NULL) {
524 spin_unlock(&police->tcf_lock);
525 return police->tcfp_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 PSCHED_GET_TIME(now);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700529 toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c,
530 police->tcfp_burst);
531 if (police->tcfp_P_tab) {
532 ptoks = toks + police->tcfp_ptoks;
533 if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
534 ptoks = (long)L2T_P(police, police->tcfp_mtu);
535 ptoks -= L2T_P(police, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700537 toks += police->tcfp_toks;
538 if (toks > (long)police->tcfp_burst)
539 toks = police->tcfp_burst;
540 toks -= L2T(police, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if ((toks|ptoks) >= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700542 police->tcfp_t_c = now;
543 police->tcfp_toks = toks;
544 police->tcfp_ptoks = ptoks;
545 spin_unlock(&police->tcf_lock);
546 return police->tcfp_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549
David S. Millere9ce1cd2006-08-21 23:54:55 -0700550 police->tcf_qstats.overlimits++;
551 spin_unlock(&police->tcf_lock);
552 return police->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Patrick McHardy31bd06e2006-01-08 22:16:25 -0800554EXPORT_SYMBOL(tcf_police);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
David S. Millere9ce1cd2006-08-21 23:54:55 -0700556int tcf_police_dump(struct sk_buff *skb, struct tcf_police *police)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700558 unsigned char *b = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct tc_police opt;
560
David S. Millere9ce1cd2006-08-21 23:54:55 -0700561 opt.index = police->tcf_index;
562 opt.action = police->tcf_action;
563 opt.mtu = police->tcfp_mtu;
564 opt.burst = police->tcfp_burst;
565 if (police->tcfp_R_tab)
566 opt.rate = police->tcfp_R_tab->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 else
568 memset(&opt.rate, 0, sizeof(opt.rate));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700569 if (police->tcfp_P_tab)
570 opt.peakrate = police->tcfp_P_tab->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 else
572 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
573 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700574 if (police->tcfp_result)
575 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
576 &police->tcfp_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700578 if (police->tcfp_ewma_rate)
579 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580#endif
581 return skb->len;
582
583rtattr_failure:
584 skb_trim(skb, b - skb->data);
585 return -1;
586}
587
David S. Millere9ce1cd2006-08-21 23:54:55 -0700588int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *police)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct gnet_dump d;
591
592 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700593 TCA_XSTATS, police->tcf_stats_lock,
594 &d) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto errout;
596
David S. Millere9ce1cd2006-08-21 23:54:55 -0700597 if (gnet_stats_copy_basic(&d, &police->tcf_bstats) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598#ifdef CONFIG_NET_ESTIMATOR
David S. Millere9ce1cd2006-08-21 23:54:55 -0700599 gnet_stats_copy_rate_est(&d, &police->tcf_rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700601 gnet_stats_copy_queue(&d, &police->tcf_qstats) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto errout;
603
604 if (gnet_stats_finish_copy(&d) < 0)
605 goto errout;
606
607 return 0;
608
609errout:
610 return -1;
611}
612
Patrick McHardy31bd06e2006-01-08 22:16:25 -0800613#endif /* CONFIG_NET_CLS_ACT */