This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / sched / police.c
1 /*
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>
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/in.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/module.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/init.h>
33 #include <net/sock.h>
34 #include <net/act_api.h>
35
36 #define L2T(p,L)   ((p)->R_tab->data[(L)>>(p)->R_tab->rate.cell_log])
37 #define L2T_P(p,L) ((p)->P_tab->data[(L)>>(p)->P_tab->rate.cell_log])
38 #define PRIV(a) ((struct tcf_police *) (a)->priv)
39
40 /* use generic hash table */
41 #define MY_TAB_SIZE     16
42 #define MY_TAB_MASK     15
43 static u32 idx_gen;
44 static struct tcf_police *tcf_police_ht[MY_TAB_SIZE];
45 /* Policer hash table lock */
46 static rwlock_t police_lock = RW_LOCK_UNLOCKED;
47
48 /* Each policer is serialized by its individual spinlock */
49
50 static __inline__ unsigned tcf_police_hash(u32 index)
51 {
52         return index&0xF;
53 }
54
55 static __inline__ struct tcf_police * tcf_police_lookup(u32 index)
56 {
57         struct tcf_police *p;
58
59         read_lock(&police_lock);
60         for (p = tcf_police_ht[tcf_police_hash(index)]; p; p = p->next) {
61                 if (p->index == index)
62                         break;
63         }
64         read_unlock(&police_lock);
65         return p;
66 }
67
68 #ifdef CONFIG_NET_CLS_ACT
69 static __inline__ int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb, int type, struct tc_action *a)
70 {
71         struct tcf_police *p;
72         int err =0, index =  -1,i= 0, s_i = 0, n_i = 0;
73         struct rtattr *r ;
74
75         read_lock(&police_lock);
76
77         s_i = cb->args[0];
78
79         for (i = 0; i < MY_TAB_SIZE; i++) {
80                 p = tcf_police_ht[tcf_police_hash(i)];
81
82                 for (; p; p = p->next) {
83                         index++;
84                         if (index < s_i)
85                                 continue;
86                         a->priv = p;
87                         a->order = index;
88                         r = (struct rtattr*) skb->tail;
89                         RTA_PUT(skb, a->order, 0, NULL);
90                         if (type == RTM_DELACTION)
91                                 err = tcf_action_dump_1(skb, a, 0, 1);
92                         else
93                                 err = tcf_action_dump_1(skb, a, 0, 0);
94                         if (0 > err) {
95                                 index--;
96                                 skb_trim(skb, (u8*)r - skb->data);
97                                 goto done;
98                         }
99                         r->rta_len = skb->tail - (u8*)r;
100                         n_i++;
101                 }
102         }
103 done:
104         read_unlock(&police_lock);
105         if (n_i)
106                 cb->args[0] += n_i;
107         return n_i;
108
109 rtattr_failure:
110         skb_trim(skb, (u8*)r - skb->data);
111         goto done;
112 }
113
114 static inline int
115 tcf_hash_search(struct tc_action *a, u32 index)
116 {
117         struct tcf_police *p = tcf_police_lookup(index);
118
119         if (p != NULL) {
120                 a->priv = p;
121                 return 1;
122         } else {
123                 return 0;
124         }
125 }
126
127
128 #endif
129
130 static __inline__ u32 tcf_police_new_index(void)
131 {
132         do {
133                 if (++idx_gen == 0)
134                         idx_gen = 1;
135         } while (tcf_police_lookup(idx_gen));
136
137         return idx_gen;
138 }
139
140
141 void tcf_police_destroy(struct tcf_police *p)
142 {
143         unsigned h = tcf_police_hash(p->index);
144         struct tcf_police **p1p;
145         
146         for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->next) {
147                 if (*p1p == p) {
148                         write_lock_bh(&police_lock);
149                         *p1p = p->next;
150                         write_unlock_bh(&police_lock);
151 #ifdef CONFIG_NET_ESTIMATOR
152                         gen_kill_estimator(&p->bstats, &p->rate_est);
153 #endif
154                         if (p->R_tab)
155                                 qdisc_put_rtab(p->R_tab);
156                         if (p->P_tab)
157                                 qdisc_put_rtab(p->P_tab);
158                         kfree(p);
159                         return;
160                 }
161         }
162         BUG_TRAP(0);
163 }
164
165 #ifdef CONFIG_NET_CLS_ACT
166 int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,struct tc_action *a, int ovr, int bind)
167 {
168         unsigned h;
169         int ret = 0;
170         struct rtattr *tb[TCA_POLICE_MAX];
171         struct tc_police *parm;
172         struct tcf_police *p;
173
174         if (NULL == a) {
175                 if (net_ratelimit())
176                         printk("BUG: tcf_police_locate called with NULL params\n");
177                 return -1;  
178         }
179
180         if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
181                 return -1;
182
183         if (tb[TCA_POLICE_TBF-1] == NULL)
184                 return -1;
185
186         parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
187
188         if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
189                 a->priv = (void *)p;
190                 spin_lock(&p->lock);
191                 if (bind) {
192                         p->bindcnt += 1;
193                         p->refcnt += 1;
194                 }
195                 if (ovr) {
196                         goto override;
197                 }
198                 spin_unlock(&p->lock);
199                 return ret; 
200         }
201
202         p = kmalloc(sizeof(*p), GFP_KERNEL);
203         if (p == NULL)
204                 return -1;
205
206         memset(p, 0, sizeof(*p));
207         ret = 1;
208         p->refcnt = 1;
209         spin_lock_init(&p->lock);
210         p->stats_lock = &p->lock;
211         if (bind)
212                 p->bindcnt = 1;
213 override:
214         if (parm->rate.rate) {
215                 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL) {
216                         goto failure;
217                 }
218                 if (parm->peakrate.rate &&
219                     (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL) {
220                         goto failure;
221                 }
222         }
223         if (tb[TCA_POLICE_RESULT-1])
224                 p->result = *(int*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
225 #ifdef CONFIG_NET_ESTIMATOR
226         if (tb[TCA_POLICE_AVRATE-1])
227                 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
228 #endif
229         p->toks = p->burst = parm->burst;
230         p->mtu = parm->mtu;
231         if (p->mtu == 0) {
232                 p->mtu = ~0;
233                 if (p->R_tab)
234                         p->mtu = 255<<p->R_tab->rate.cell_log;
235         }
236         if (p->P_tab)
237                 p->ptoks = L2T_P(p, p->mtu);
238         p->action = parm->action;
239
240         if (ovr) {
241                 spin_unlock(&p->lock);
242                 return ret;
243         }
244         PSCHED_GET_TIME(p->t_c);
245         p->index = parm->index ? : tcf_police_new_index();
246 #ifdef CONFIG_NET_ESTIMATOR
247         if (est)
248                 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
249 #endif
250         h = tcf_police_hash(p->index);
251         write_lock_bh(&police_lock);
252         p->next = tcf_police_ht[h];
253         tcf_police_ht[h] = p;
254         write_unlock_bh(&police_lock);
255
256         a->priv = (void *)p;
257         return ret;  
258
259 failure:
260         if (p->R_tab)
261                 qdisc_put_rtab(p->R_tab);
262         if (ovr)
263                 spin_unlock(&p->lock);
264         kfree(p);
265         return -1;
266 }
267
268 int tcf_act_police_cleanup(struct tc_action *a, int bind)
269 {
270         struct tcf_police *p;
271         p = PRIV(a);
272         if (NULL != p) 
273                 return tcf_police_release(p, bind);
274
275         return 0;
276 }
277
278 int tcf_act_police(struct sk_buff **pskb, struct tc_action *a)
279 {
280         psched_time_t now;
281         struct sk_buff *skb = *pskb;
282         struct tcf_police *p;
283         long toks;
284         long ptoks = 0;
285
286         p = PRIV(a);
287
288         if (NULL == p) {
289                 printk("BUG: tcf_police called with NULL params\n");
290                 return -1;  
291         }
292
293         spin_lock(&p->lock);
294
295         p->bstats.bytes += skb->len;
296         p->bstats.packets++;
297
298 #ifdef CONFIG_NET_ESTIMATOR
299         if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
300                 p->qstats.overlimits++;
301                 spin_unlock(&p->lock);
302                 return p->action;
303         }
304 #endif
305
306         if (skb->len <= p->mtu) {
307                 if (p->R_tab == NULL) {
308                         spin_unlock(&p->lock);
309                         return p->result;
310                 }
311
312                 PSCHED_GET_TIME(now);
313
314                 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
315
316                 if (p->P_tab) {
317                         ptoks = toks + p->ptoks;
318                         if (ptoks > (long)L2T_P(p, p->mtu))
319                                 ptoks = (long)L2T_P(p, p->mtu);
320                         ptoks -= L2T_P(p, skb->len);
321                 }
322                 toks += p->toks;
323                 if (toks > (long)p->burst)
324                         toks = p->burst;
325                 toks -= L2T(p, skb->len);
326
327                 if ((toks|ptoks) >= 0) {
328                         p->t_c = now;
329                         p->toks = toks;
330                         p->ptoks = ptoks;
331                         spin_unlock(&p->lock);
332                         return p->result;
333                 }
334         }
335
336         p->qstats.overlimits++;
337         spin_unlock(&p->lock);
338         return p->action;
339 }
340
341 int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
342 {
343         unsigned char    *b = skb->tail;
344         struct tc_police opt;
345         struct tcf_police *p;
346
347         p = PRIV(a);
348         if (NULL == p) {
349                 printk("BUG: tcf_police_dump called with NULL params\n");
350                 goto rtattr_failure;
351         }
352
353         opt.index = p->index;
354         opt.action = p->action;
355         opt.mtu = p->mtu;
356         opt.burst = p->burst;
357         opt.refcnt = p->refcnt - ref;
358         opt.bindcnt = p->bindcnt - bind;
359         if (p->R_tab)
360                 opt.rate = p->R_tab->rate;
361         else
362                 memset(&opt.rate, 0, sizeof(opt.rate));
363         if (p->P_tab)
364                 opt.peakrate = p->P_tab->rate;
365         else
366                 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
367         RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
368         if (p->result)
369                 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
370 #ifdef CONFIG_NET_ESTIMATOR
371         if (p->ewma_rate)
372                 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
373 #endif
374         return skb->len;
375
376 rtattr_failure:
377         skb_trim(skb, b - skb->data);
378         return -1;
379 }
380
381 MODULE_AUTHOR("Alexey Kuznetsov");
382 MODULE_DESCRIPTION("Policing actions");
383 MODULE_LICENSE("GPL");
384
385
386 static struct tc_action_ops act_police_ops = {
387         .next           =       NULL,
388         .kind           =       "police",
389         .type           =       TCA_ID_POLICE, 
390         .capab          =       TCA_CAP_NONE, 
391         .owner          =       THIS_MODULE,
392         .act            =       tcf_act_police,
393         .dump           =       tcf_act_police_dump,
394         .cleanup        =       tcf_act_police_cleanup,
395         .lookup         =       tcf_hash_search,
396         .init           =       tcf_act_police_locate,
397         .walk           =       tcf_generic_walker
398 };
399
400 static int __init
401 police_init_module(void)
402 {
403         return tcf_register_action(&act_police_ops);
404 }
405
406 static void __exit
407 police_cleanup_module(void)
408 {
409         tcf_unregister_action(&act_police_ops);
410 }
411
412 module_init(police_init_module);
413 module_exit(police_cleanup_module);
414
415 #endif
416
417 struct tcf_police * tcf_police_locate(struct rtattr *rta, struct rtattr *est)
418 {
419         unsigned h;
420         struct tcf_police *p;
421         struct rtattr *tb[TCA_POLICE_MAX];
422         struct tc_police *parm;
423
424         if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
425                 return NULL;
426
427         if (tb[TCA_POLICE_TBF-1] == NULL)
428                 return NULL;
429
430         parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
431
432         if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
433                 p->refcnt++;
434                 return p;
435         }
436
437         p = kmalloc(sizeof(*p), GFP_KERNEL);
438         if (p == NULL)
439                 return NULL;
440
441         memset(p, 0, sizeof(*p));
442         p->refcnt = 1;
443         spin_lock_init(&p->lock);
444         p->stats_lock = &p->lock;
445         if (parm->rate.rate) {
446                 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL)
447                         goto failure;
448                 if (parm->peakrate.rate &&
449                     (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL)
450                         goto failure;
451         }
452         if (tb[TCA_POLICE_RESULT-1])
453                 p->result = *(int*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
454 #ifdef CONFIG_NET_ESTIMATOR
455         if (tb[TCA_POLICE_AVRATE-1])
456                 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
457 #endif
458         p->toks = p->burst = parm->burst;
459         p->mtu = parm->mtu;
460         if (p->mtu == 0) {
461                 p->mtu = ~0;
462                 if (p->R_tab)
463                         p->mtu = 255<<p->R_tab->rate.cell_log;
464         }
465         if (p->P_tab)
466                 p->ptoks = L2T_P(p, p->mtu);
467         PSCHED_GET_TIME(p->t_c);
468         p->index = parm->index ? : tcf_police_new_index();
469         p->action = parm->action;
470 #ifdef CONFIG_NET_ESTIMATOR
471         if (est)
472                 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
473 #endif
474         h = tcf_police_hash(p->index);
475         write_lock_bh(&police_lock);
476         p->next = tcf_police_ht[h];
477         tcf_police_ht[h] = p;
478         write_unlock_bh(&police_lock);
479         return p;
480
481 failure:
482         if (p->R_tab)
483                 qdisc_put_rtab(p->R_tab);
484         kfree(p);
485         return NULL;
486 }
487
488 int tcf_police(struct sk_buff *skb, struct tcf_police *p)
489 {
490         psched_time_t now;
491         long toks;
492         long ptoks = 0;
493
494         spin_lock(&p->lock);
495
496         p->bstats.bytes += skb->len;
497         p->bstats.packets++;
498
499 #ifdef CONFIG_NET_ESTIMATOR
500         if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
501                 p->qstats.overlimits++;
502                 spin_unlock(&p->lock);
503                 return p->action;
504         }
505 #endif
506
507         if (skb->len <= p->mtu) {
508                 if (p->R_tab == NULL) {
509                         spin_unlock(&p->lock);
510                         return p->result;
511                 }
512
513                 PSCHED_GET_TIME(now);
514
515                 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
516
517                 if (p->P_tab) {
518                         ptoks = toks + p->ptoks;
519                         if (ptoks > (long)L2T_P(p, p->mtu))
520                                 ptoks = (long)L2T_P(p, p->mtu);
521                         ptoks -= L2T_P(p, skb->len);
522                 }
523                 toks += p->toks;
524                 if (toks > (long)p->burst)
525                         toks = p->burst;
526                 toks -= L2T(p, skb->len);
527
528                 if ((toks|ptoks) >= 0) {
529                         p->t_c = now;
530                         p->toks = toks;
531                         p->ptoks = ptoks;
532                         spin_unlock(&p->lock);
533                         return p->result;
534                 }
535         }
536
537         p->qstats.overlimits++;
538         spin_unlock(&p->lock);
539         return p->action;
540 }
541
542 int tcf_police_dump(struct sk_buff *skb, struct tcf_police *p)
543 {
544         unsigned char    *b = skb->tail;
545         struct tc_police opt;
546
547         opt.index = p->index;
548         opt.action = p->action;
549         opt.mtu = p->mtu;
550         opt.burst = p->burst;
551         if (p->R_tab)
552                 opt.rate = p->R_tab->rate;
553         else
554                 memset(&opt.rate, 0, sizeof(opt.rate));
555         if (p->P_tab)
556                 opt.peakrate = p->P_tab->rate;
557         else
558                 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
559         RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
560         if (p->result)
561                 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
562 #ifdef CONFIG_NET_ESTIMATOR
563         if (p->ewma_rate)
564                 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
565 #endif
566         return skb->len;
567
568 rtattr_failure:
569         skb_trim(skb, b - skb->data);
570         return -1;
571 }
572
573 int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *p)
574 {
575         struct gnet_dump d;
576         
577         if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
578                         TCA_XSTATS, p->stats_lock, &d) < 0)
579                 goto errout;
580         
581         if (gnet_stats_copy_basic(&d, &p->bstats) < 0 ||
582 #ifdef CONFIG_NET_ESTIMATOR
583             gnet_stats_copy_rate_est(&d, &p->rate_est) < 0 ||
584 #endif
585             gnet_stats_copy_queue(&d, &p->qstats) < 0)
586                 goto errout;
587
588         if (gnet_stats_finish_copy(&d) < 0)
589                 goto errout;
590
591         return 0;
592
593 errout:
594         return -1;
595 }
596
597
598 EXPORT_SYMBOL(tcf_police);
599 EXPORT_SYMBOL(tcf_police_destroy);
600 EXPORT_SYMBOL(tcf_police_dump);
601 EXPORT_SYMBOL(tcf_police_dump_stats);
602 EXPORT_SYMBOL(tcf_police_hash);
603 EXPORT_SYMBOL(tcf_police_ht);
604 EXPORT_SYMBOL(tcf_police_locate);
605 EXPORT_SYMBOL(tcf_police_lookup);
606 EXPORT_SYMBOL(tcf_police_new_index);