Merge /spare/repo/linux-2.6/
[linux-2.6] / 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 <linux/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
42 static u32 idx_gen;
43 static struct tcf_gact *tcf_gact_ht[MY_TAB_SIZE];
44 static DEFINE_RWLOCK(gact_lock);
45
46 /* ovewrride the defaults */
47 #define tcf_st          tcf_gact
48 #define tc_st           tc_gact
49 #define tcf_t_lock      gact_lock
50 #define tcf_ht          tcf_gact_ht
51
52 #define CONFIG_NET_ACT_INIT 1
53 #include <net/pkt_act.h>
54
55 #ifdef CONFIG_GACT_PROB
56 static int gact_net_rand(struct tcf_gact *p)
57 {
58         if (net_random()%p->pval)
59                 return p->action;
60         return p->paction;
61 }
62
63 static int gact_determ(struct tcf_gact *p)
64 {
65         if (p->bstats.packets%p->pval)
66                 return p->action;
67         return p->paction;
68 }
69
70 typedef int (*g_rand)(struct tcf_gact *p);
71 static g_rand gact_rand[MAX_RAND]= { NULL, gact_net_rand, gact_determ };
72 #endif
73
74 static int tcf_gact_init(struct rtattr *rta, struct rtattr *est,
75                          struct tc_action *a, int ovr, int bind)
76 {
77         struct rtattr *tb[TCA_GACT_MAX];
78         struct tc_gact *parm;
79         struct tcf_gact *p;
80         int ret = 0;
81
82         if (rta == NULL || rtattr_parse_nested(tb, TCA_GACT_MAX, rta) < 0)
83                 return -EINVAL;
84
85         if (tb[TCA_GACT_PARMS - 1] == NULL ||
86             RTA_PAYLOAD(tb[TCA_GACT_PARMS - 1]) < sizeof(*parm))
87                 return -EINVAL;
88         parm = RTA_DATA(tb[TCA_GACT_PARMS - 1]);
89
90         if (tb[TCA_GACT_PROB-1] != NULL)
91 #ifdef CONFIG_GACT_PROB
92                 if (RTA_PAYLOAD(tb[TCA_GACT_PROB-1]) < sizeof(struct tc_gact_p))
93                         return -EINVAL;
94 #else
95                 return -EOPNOTSUPP;
96 #endif
97
98         p = tcf_hash_check(parm->index, a, ovr, bind);
99         if (p == NULL) {
100                 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
101                 if (p == NULL)
102                         return -ENOMEM;
103                 ret = ACT_P_CREATED;
104         } else {
105                 if (!ovr) {
106                         tcf_hash_release(p, bind);
107                         return -EEXIST;
108                 }
109         }
110
111         spin_lock_bh(&p->lock);
112         p->action = parm->action;
113 #ifdef CONFIG_GACT_PROB
114         if (tb[TCA_GACT_PROB-1] != NULL) {
115                 struct tc_gact_p *p_parm = RTA_DATA(tb[TCA_GACT_PROB-1]);
116                 p->paction = p_parm->paction;
117                 p->pval    = p_parm->pval;
118                 p->ptype   = p_parm->ptype;
119         }
120 #endif
121         spin_unlock_bh(&p->lock);
122         if (ret == ACT_P_CREATED)
123                 tcf_hash_insert(p);
124         return ret;
125 }
126
127 static int
128 tcf_gact_cleanup(struct tc_action *a, int bind)
129 {
130         struct tcf_gact *p = PRIV(a, gact);
131
132         if (p != NULL)
133                 return tcf_hash_release(p, bind);
134         return 0;
135 }
136
137 static int
138 tcf_gact(struct sk_buff **pskb, struct tc_action *a, struct tcf_result *res)
139 {
140         struct tcf_gact *p = PRIV(a, gact);
141         struct sk_buff *skb = *pskb;
142         int action = TC_ACT_SHOT;
143
144         spin_lock(&p->lock);
145 #ifdef CONFIG_GACT_PROB
146         if (p->ptype && gact_rand[p->ptype] != NULL)
147                 action = gact_rand[p->ptype](p);
148         else
149                 action = p->action;
150 #else
151         action = p->action;
152 #endif
153         p->bstats.bytes += skb->len;
154         p->bstats.packets++;
155         if (action == TC_ACT_SHOT)
156                 p->qstats.drops++;
157         p->tm.lastuse = jiffies;
158         spin_unlock(&p->lock);
159
160         return action;
161 }
162
163 static int
164 tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
165 {
166         unsigned char *b = skb->tail;
167         struct tc_gact opt;
168         struct tcf_gact *p = PRIV(a, gact);
169         struct tcf_t t;
170
171         opt.index = p->index;
172         opt.refcnt = p->refcnt - ref;
173         opt.bindcnt = p->bindcnt - bind;
174         opt.action = p->action;
175         RTA_PUT(skb, TCA_GACT_PARMS, sizeof(opt), &opt);
176 #ifdef CONFIG_GACT_PROB
177         if (p->ptype) {
178                 struct tc_gact_p p_opt;
179                 p_opt.paction = p->paction;
180                 p_opt.pval = p->pval;
181                 p_opt.ptype = p->ptype;
182                 RTA_PUT(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt);
183         }
184 #endif
185         t.install = jiffies_to_clock_t(jiffies - p->tm.install);
186         t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
187         t.expires = jiffies_to_clock_t(p->tm.expires);
188         RTA_PUT(skb, TCA_GACT_TM, sizeof(t), &t);
189         return skb->len;
190
191       rtattr_failure:
192         skb_trim(skb, b - skb->data);
193         return -1;
194 }
195
196 static struct tc_action_ops act_gact_ops = {
197         .kind           =       "gact",
198         .type           =       TCA_ACT_GACT,
199         .capab          =       TCA_CAP_NONE,
200         .owner          =       THIS_MODULE,
201         .act            =       tcf_gact,
202         .dump           =       tcf_gact_dump,
203         .cleanup        =       tcf_gact_cleanup,
204         .lookup         =       tcf_hash_search,
205         .init           =       tcf_gact_init,
206         .walk           =       tcf_generic_walker
207 };
208
209 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
210 MODULE_DESCRIPTION("Generic Classifier actions");
211 MODULE_LICENSE("GPL");
212
213 static int __init
214 gact_init_module(void)
215 {
216 #ifdef CONFIG_GACT_PROB
217         printk("GACT probability on\n");
218 #else
219         printk("GACT probability NOT on\n");
220 #endif
221         return tcf_register_action(&act_gact_ops);
222 }
223
224 static void __exit
225 gact_cleanup_module(void)
226 {
227         tcf_unregister_action(&act_gact_ops);
228 }
229
230 module_init(gact_init_module);
231 module_exit(gact_cleanup_module);