2 * net/sched/gact.c Generic actions
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.
9 * copyright Jamal Hadi Salim (2002-4)
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>
22 #include <linux/socket.h>
23 #include <linux/sockios.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>
34 #include <net/pkt_sched.h>
35 #include <linux/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_gact.h>
38 /* use generic hash table */
39 #define MY_TAB_SIZE 16
40 #define MY_TAB_MASK 15
42 static struct tcf_gact *tcf_gact_ht[MY_TAB_SIZE];
43 static rwlock_t gact_lock = RW_LOCK_UNLOCKED;
45 /* ovewrride the defaults */
46 #define tcf_st tcf_gact
48 #define tcf_t_lock gact_lock
49 #define tcf_ht tcf_gact_ht
51 #define CONFIG_NET_ACT_INIT 1
52 #include <net/pkt_act.h>
54 #ifdef CONFIG_GACT_PROB
55 typedef int (*g_rand)(struct tcf_gact *p);
57 gact_net_rand(struct tcf_gact *p) {
58 if (net_random()%p->pval)
64 gact_determ(struct tcf_gact *p) {
65 if (p->stats.packets%p->pval)
71 g_rand gact_rand[MAX_RAND]= { NULL,gact_net_rand, gact_determ};
75 tcf_gact_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,int ovr,int bind)
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;
82 struct tcf_gact *p = NULL;
84 int size = sizeof (*p);
86 if (rtattr_parse(tb, TCA_GACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
89 if (NULL == a || NULL == tb[TCA_GACT_PARMS - 1]) {
90 printk("BUG: tcf_gact_init called with NULL params\n");
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]);
101 p = tcf_hash_check(parm, a, ovr, bind);
104 p = tcf_hash_create(parm,est,a,size,ovr, bind);
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;
124 p->paction = p->pval = p->ptype = 0;
133 tcf_gact_cleanup(struct tc_action *a, int bind)
138 return tcf_hash_release(p, bind);
143 tcf_gact(struct sk_buff **pskb, struct tc_action *a)
146 struct sk_buff *skb = *pskb;
147 int action = TC_ACT_SHOT;
153 printk("BUG: tcf_gact called with NULL params\n");
158 #ifdef CONFIG_GACT_PROB
159 if (p->ptype && NULL != gact_rand[p->ptype])
160 action = gact_rand[p->ptype](p);
166 p->stats.bytes += skb->len;
168 if (TC_ACT_SHOT == action)
170 p->tm.lastuse = jiffies;
171 spin_unlock(&p->lock);
177 tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
179 unsigned char *b = skb->tail;
181 #ifdef CONFIG_GACT_PROB
182 struct tc_gact_p p_opt;
189 printk("BUG: tcf_gact_dump called with NULL params\n");
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
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);
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);
213 skb_trim(skb, b - skb->data);
218 tcf_gact_stats(struct sk_buff *skb, struct tc_action *a)
223 return qdisc_copy_stats(skb, &p->stats,p->stats_lock);
228 struct tc_action_ops act_gact_ops = {
231 .type = TCA_ACT_GACT,
232 .capab = TCA_CAP_NONE,
233 .owner = THIS_MODULE,
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
243 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
244 MODULE_DESCRIPTION("Generic Classifier actions");
245 MODULE_LICENSE("GPL");
248 gact_init_module(void)
250 #ifdef CONFIG_GACT_PROB
251 printk("GACT probability on\n");
253 printk("GACT probability NOT on\n");
255 return tcf_register_action(&act_gact_ops);
259 gact_cleanup_module(void)
261 tcf_unregister_action(&act_gact_ops);
264 module_init(gact_init_module);
265 module_exit(gact_cleanup_module);