This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / sched / gact.c
1 /*
2  * net/sched/gact.c     Generic actions
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  * copyright    Jamal Hadi Salim (2002-4)
10  *
11  */
12
13 #include <asm/uaccess.h>
14 #include <asm/system.h>
15 #include <asm/bitops.h>
16 #include <linux/config.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/rtnetlink.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/proc_fs.h>
33 #include <net/sock.h>
34 #include <net/pkt_sched.h>
35 #include <linux/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_gact.h>
37
38 /* use generic hash table */
39 #define MY_TAB_SIZE     16
40 #define MY_TAB_MASK     15
41 static u32 idx_gen;
42 static struct tcf_gact *tcf_gact_ht[MY_TAB_SIZE];
43 static rwlock_t gact_lock = RW_LOCK_UNLOCKED;
44
45 /* ovewrride the defaults */
46 #define tcf_st  tcf_gact
47 #define tc_st  tc_gact
48 #define tcf_t_lock   gact_lock
49 #define tcf_ht tcf_gact_ht
50
51 #define CONFIG_NET_ACT_INIT 1
52 #include <net/pkt_act.h>
53
54 #ifdef CONFIG_GACT_PROB
55 typedef int (*g_rand)(struct tcf_gact *p);
56 int
57 gact_net_rand(struct tcf_gact *p) {
58         if (net_random()%p->pval)
59                 return p->action;
60         return p->paction;
61 }
62
63 int
64 gact_determ(struct tcf_gact *p) {
65         if (p->stats.packets%p->pval)
66                 return p->action;
67         return p->paction;
68 }
69
70
71 g_rand gact_rand[MAX_RAND]= { NULL,gact_net_rand, gact_determ};
72
73 #endif
74 int
75 tcf_gact_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,int ovr,int bind)
76 {
77         struct rtattr *tb[TCA_GACT_MAX];
78         struct tc_gact *parm = NULL;
79 #ifdef CONFIG_GACT_PROB
80         struct tc_gact_p *p_parm = NULL;
81 #endif
82         struct tcf_gact *p = NULL;
83         int ret = 0;
84         int size = sizeof (*p);
85
86         if (rtattr_parse(tb, TCA_GACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
87                 return -1;
88
89         if (NULL == a || NULL == tb[TCA_GACT_PARMS - 1]) {
90                 printk("BUG: tcf_gact_init called with NULL params\n");
91                 return -1;
92         }
93
94         parm = RTA_DATA(tb[TCA_GACT_PARMS - 1]);
95 #ifdef CONFIG_GACT_PROB
96         if (NULL != tb[TCA_GACT_PROB - 1]) {
97                 p_parm = RTA_DATA(tb[TCA_GACT_PROB - 1]);
98         }
99 #endif
100
101         p = tcf_hash_check(parm, a, ovr, bind);
102
103         if (NULL == p) {
104                 p = tcf_hash_create(parm,est,a,size,ovr, bind);
105
106                 if (NULL == p) {
107                         return -1;
108                 } else {
109                         p->refcnt = 1;
110                         ret = 1;
111                         goto override;
112                 }
113         }
114
115         if (ovr) {
116 override:
117                 p->action = parm->action;
118 #ifdef CONFIG_GACT_PROB
119                 if (NULL != p_parm) {
120                         p->paction = p_parm->paction;
121                         p->pval = p_parm->pval;
122                         p->ptype = p_parm->ptype;
123                 } else {
124                         p->paction = p->pval = p->ptype = 0;
125                 }
126 #endif
127         }
128
129         return ret;
130 }
131
132 int
133 tcf_gact_cleanup(struct tc_action *a, int bind)
134 {
135         struct tcf_gact *p;
136         p = PRIV(a,gact);
137         if (NULL != p)
138                 return tcf_hash_release(p, bind);
139         return 0;
140 }
141
142 int
143 tcf_gact(struct sk_buff **pskb, struct tc_action *a)
144 {
145         struct tcf_gact *p;
146         struct sk_buff *skb = *pskb;
147         int action = TC_ACT_SHOT;
148
149         p = PRIV(a,gact);
150
151         if (NULL == p) {
152                 if (net_ratelimit())
153                         printk("BUG: tcf_gact called with NULL params\n");
154                 return -1;
155         }
156
157         spin_lock(&p->lock);
158 #ifdef CONFIG_GACT_PROB
159         if (p->ptype && NULL != gact_rand[p->ptype])
160                 action = gact_rand[p->ptype](p);
161         else
162                 action = p->action;
163 #else
164         action = p->action;
165 #endif
166         p->stats.bytes += skb->len;
167         p->stats.packets++;
168         if (TC_ACT_SHOT == action)
169                 p->stats.drops++;
170         p->tm.lastuse = jiffies;
171         spin_unlock(&p->lock);
172
173         return action;
174 }
175
176 int
177 tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
178 {
179         unsigned char *b = skb->tail;
180         struct tc_gact opt;
181 #ifdef CONFIG_GACT_PROB
182         struct tc_gact_p p_opt;
183 #endif
184         struct tcf_gact *p;
185         struct tcf_t t;
186
187         p = PRIV(a,gact);
188         if (NULL == p) {
189                 printk("BUG: tcf_gact_dump called with NULL params\n");
190                 goto rtattr_failure;
191         }
192
193         opt.index = p->index;
194         opt.refcnt = p->refcnt - ref;
195         opt.bindcnt = p->bindcnt - bind;
196         opt.action = p->action;
197         RTA_PUT(skb, TCA_GACT_PARMS, sizeof (opt), &opt);
198 #ifdef CONFIG_GACT_PROB
199         if (p->ptype) {
200                 p_opt.paction = p->paction;
201                 p_opt.pval = p->pval;
202                 p_opt.ptype = p->ptype;
203                 RTA_PUT(skb, TCA_GACT_PROB, sizeof (p_opt), &p_opt);
204         } 
205 #endif
206         t.install = jiffies - p->tm.install;
207         t.lastuse = jiffies - p->tm.lastuse;
208         t.expires = p->tm.expires;
209         RTA_PUT(skb, TCA_GACT_TM, sizeof (t), &t);
210         return skb->len;
211
212       rtattr_failure:
213         skb_trim(skb, b - skb->data);
214         return -1;
215 }
216
217 int
218 tcf_gact_stats(struct sk_buff *skb, struct tc_action *a)
219 {
220         struct tcf_gact *p;
221         p = PRIV(a,gact);
222         if (NULL != p)
223                 return qdisc_copy_stats(skb, &p->stats,p->stats_lock);
224
225         return 1;
226 }
227
228 struct tc_action_ops act_gact_ops = {
229         .next           =       NULL,
230         .kind           =       "gact",
231         .type           =       TCA_ACT_GACT,
232         .capab          =       TCA_CAP_NONE,
233         .owner          =       THIS_MODULE,
234         .act            =       tcf_gact,
235         .get_stats      =       tcf_gact_stats,
236         .dump           =       tcf_gact_dump,
237         .cleanup        =       tcf_gact_cleanup,
238         .lookup         =       tcf_hash_search,
239         .init           =       tcf_gact_init,
240         .walk           =       tcf_generic_walker
241 };
242
243 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
244 MODULE_DESCRIPTION("Generic Classifier actions");
245 MODULE_LICENSE("GPL");
246
247 static int __init
248 gact_init_module(void)
249 {
250 #ifdef CONFIG_GACT_PROB
251         printk("GACT probability on\n");
252 #else
253         printk("GACT probability NOT on\n");
254 #endif
255         return tcf_register_action(&act_gact_ops);
256 }
257
258 static void __exit
259 gact_cleanup_module(void)
260 {
261         tcf_unregister_action(&act_gact_ops);
262 }
263
264 module_init(gact_init_module);
265 module_exit(gact_cleanup_module);