aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJamal Hadi Salim <hadi@cyberus.ca>2005-04-24 23:10:16 -0400
committerDavid S. Miller <davem@davemloft.net>2005-04-24 23:10:16 -0400
commitdb7530797992bb2be703f9f3cc69b1a578f812f8 (patch)
tree741ca281d2acb1f5a62df20ccda4ad294b57347b
parentac6910e189471e6b46ecea59e7620b083329ad4f (diff)
[PKT_SCHED]: Introduce simple actions.
And provide an example simply action in order to demonstrate usage. Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/tc_act/tc_defact.h21
-rw-r--r--include/net/act_generic.h142
-rw-r--r--include/net/tc_act/tc_defact.h13
-rw-r--r--net/sched/Kconfig10
-rw-r--r--net/sched/Makefile11
-rw-r--r--net/sched/simple.c107
6 files changed, 299 insertions, 5 deletions
diff --git a/include/linux/tc_act/tc_defact.h b/include/linux/tc_act/tc_defact.h
new file mode 100644
index 000000000000..964f473af0f0
--- /dev/null
+++ b/include/linux/tc_act/tc_defact.h
@@ -0,0 +1,21 @@
1#ifndef __LINUX_TC_DEF_H
2#define __LINUX_TC_DEF_H
3
4#include <linux/pkt_cls.h>
5
6struct tc_defact
7{
8 tc_gen;
9};
10
11enum
12{
13 TCA_DEF_UNSPEC,
14 TCA_DEF_TM,
15 TCA_DEF_PARMS,
16 TCA_DEF_DATA,
17 __TCA_DEF_MAX
18};
19#define TCA_DEF_MAX (__TCA_DEF_MAX - 1)
20
21#endif
diff --git a/include/net/act_generic.h b/include/net/act_generic.h
new file mode 100644
index 000000000000..95b120781c14
--- /dev/null
+++ b/include/net/act_generic.h
@@ -0,0 +1,142 @@
1/*
2 * include/net/act_generic.h
3 *
4*/
5#ifndef ACT_GENERIC_H
6#define ACT_GENERIC_H
7static inline int tcf_defact_release(struct tcf_defact *p, int bind)
8{
9 int ret = 0;
10 if (p) {
11 if (bind) {
12 p->bindcnt--;
13 }
14 p->refcnt--;
15 if (p->bindcnt <= 0 && p->refcnt <= 0) {
16 kfree(p->defdata);
17 tcf_hash_destroy(p);
18 ret = 1;
19 }
20 }
21 return ret;
22}
23
24static inline int
25alloc_defdata(struct tcf_defact *p, u32 datalen, void *defdata)
26{
27 p->defdata = kmalloc(datalen, GFP_KERNEL);
28 if (p->defdata == NULL)
29 return -ENOMEM;
30 p->datalen = datalen;
31 memcpy(p->defdata, defdata, datalen);
32 return 0;
33}
34
35static inline int
36realloc_defdata(struct tcf_defact *p, u32 datalen, void *defdata)
37{
38 /* safer to be just brute force for now */
39 kfree(p->defdata);
40 return alloc_defdata(p, datalen, defdata);
41}
42
43static inline int
44tcf_defact_init(struct rtattr *rta, struct rtattr *est,
45 struct tc_action *a, int ovr, int bind)
46{
47 struct rtattr *tb[TCA_DEF_MAX];
48 struct tc_defact *parm;
49 struct tcf_defact *p;
50 void *defdata;
51 u32 datalen = 0;
52 int ret = 0;
53
54 if (rta == NULL || rtattr_parse_nested(tb, TCA_DEF_MAX, rta) < 0)
55 return -EINVAL;
56
57 if (tb[TCA_DEF_PARMS - 1] == NULL ||
58 RTA_PAYLOAD(tb[TCA_DEF_PARMS - 1]) < sizeof(*parm))
59 return -EINVAL;
60
61 parm = RTA_DATA(tb[TCA_DEF_PARMS - 1]);
62 defdata = RTA_DATA(tb[TCA_DEF_DATA - 1]);
63 if (defdata == NULL)
64 return -EINVAL;
65
66 datalen = RTA_PAYLOAD(tb[TCA_DEF_DATA - 1]);
67 if (datalen <= 0)
68 return -EINVAL;
69
70 p = tcf_hash_check(parm->index, a, ovr, bind);
71 if (p == NULL) {
72 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
73 if (p == NULL)
74 return -ENOMEM;
75
76 ret = alloc_defdata(p, datalen, defdata);
77 if (ret < 0) {
78 kfree(p);
79 return ret;
80 }
81 ret = ACT_P_CREATED;
82 } else {
83 if (!ovr) {
84 tcf_defact_release(p, bind);
85 return -EEXIST;
86 }
87 realloc_defdata(p, datalen, defdata);
88 }
89
90 spin_lock_bh(&p->lock);
91 p->action = parm->action;
92 spin_unlock_bh(&p->lock);
93 if (ret == ACT_P_CREATED)
94 tcf_hash_insert(p);
95 return ret;
96}
97
98static inline int tcf_defact_cleanup(struct tc_action *a, int bind)
99{
100 struct tcf_defact *p = PRIV(a, defact);
101
102 if (p != NULL)
103 return tcf_defact_release(p, bind);
104 return 0;
105}
106
107static inline int
108tcf_defact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
109{
110 unsigned char *b = skb->tail;
111 struct tc_defact opt;
112 struct tcf_defact *p = PRIV(a, defact);
113 struct tcf_t t;
114
115 opt.index = p->index;
116 opt.refcnt = p->refcnt - ref;
117 opt.bindcnt = p->bindcnt - bind;
118 opt.action = p->action;
119 RTA_PUT(skb, TCA_DEF_PARMS, sizeof(opt), &opt);
120 RTA_PUT(skb, TCA_DEF_DATA, p->datalen, p->defdata);
121 t.install = jiffies_to_clock_t(jiffies - p->tm.install);
122 t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
123 t.expires = jiffies_to_clock_t(p->tm.expires);
124 RTA_PUT(skb, TCA_DEF_TM, sizeof(t), &t);
125 return skb->len;
126
127rtattr_failure:
128 skb_trim(skb, b - skb->data);
129 return -1;
130}
131
132#define tca_use_default_ops \
133 .dump = tcf_defact_dump, \
134 .cleanup = tcf_defact_cleanup, \
135 .init = tcf_defact_init, \
136 .walk = tcf_generic_walker, \
137
138#define tca_use_default_defines(name) \
139 static u32 idx_gen; \
140 static struct tcf_defact *tcf_##name_ht[MY_TAB_SIZE]; \
141 static DEFINE_RWLOCK(##name_lock);
142#endif /* _NET_ACT_GENERIC_H */
diff --git a/include/net/tc_act/tc_defact.h b/include/net/tc_act/tc_defact.h
new file mode 100644
index 000000000000..463aa671f95d
--- /dev/null
+++ b/include/net/tc_act/tc_defact.h
@@ -0,0 +1,13 @@
1#ifndef __NET_TC_DEF_H
2#define __NET_TC_DEF_H
3
4#include <net/act_api.h>
5
6struct tcf_defact
7{
8 tca_gen(defact);
9 u32 datalen;
10 void *defdata;
11};
12
13#endif
diff --git a/net/sched/Kconfig b/net/sched/Kconfig
index 3d1d902dd1a1..9c118baed9dc 100644
--- a/net/sched/Kconfig
+++ b/net/sched/Kconfig
@@ -506,3 +506,13 @@ config NET_CLS_POLICE
506 Say Y to support traffic policing (bandwidth limits). Needed for 506 Say Y to support traffic policing (bandwidth limits). Needed for
507 ingress and egress rate limiting. 507 ingress and egress rate limiting.
508 508
509config NET_ACT_SIMP
510 tristate "Simple action"
511 depends on NET_CLS_ACT
512 ---help---
513 You must have new iproute2 to use this feature.
514 This adds a very simple action for demonstration purposes
515 The idea is to give action authors a basic example to look at.
516 All this action will do is print on the console the configured
517 policy string followed by _ then packet count.
518
diff --git a/net/sched/Makefile b/net/sched/Makefile
index 431e55786efd..eb3fe583eba8 100644
--- a/net/sched/Makefile
+++ b/net/sched/Makefile
@@ -6,13 +6,14 @@ obj-y := sch_generic.o
6 6
7obj-$(CONFIG_NET_SCHED) += sch_api.o sch_fifo.o 7obj-$(CONFIG_NET_SCHED) += sch_api.o sch_fifo.o
8obj-$(CONFIG_NET_CLS) += cls_api.o 8obj-$(CONFIG_NET_CLS) += cls_api.o
9obj-$(CONFIG_NET_CLS_ACT) += act_api.o 9obj-$(CONFIG_NET_CLS_ACT) += act_api.o
10obj-$(CONFIG_NET_ACT_POLICE) += police.o 10obj-$(CONFIG_NET_ACT_POLICE) += police.o
11obj-$(CONFIG_NET_CLS_POLICE) += police.o 11obj-$(CONFIG_NET_CLS_POLICE) += police.o
12obj-$(CONFIG_NET_ACT_GACT) += gact.o 12obj-$(CONFIG_NET_ACT_GACT) += gact.o
13obj-$(CONFIG_NET_ACT_MIRRED) += mirred.o 13obj-$(CONFIG_NET_ACT_MIRRED) += mirred.o
14obj-$(CONFIG_NET_ACT_IPT) += ipt.o 14obj-$(CONFIG_NET_ACT_IPT) += ipt.o
15obj-$(CONFIG_NET_ACT_PEDIT) += pedit.o 15obj-$(CONFIG_NET_ACT_PEDIT) += pedit.o
16obj-$(CONFIG_NET_ACT_SIMP) += simple.o
16obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o 17obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o
17obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o 18obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
18obj-$(CONFIG_NET_SCH_HPFQ) += sch_hpfq.o 19obj-$(CONFIG_NET_SCH_HPFQ) += sch_hpfq.o
diff --git a/net/sched/simple.c b/net/sched/simple.c
new file mode 100644
index 000000000000..b0d3d15848ad
--- /dev/null
+++ b/net/sched/simple.c
@@ -0,0 +1,107 @@
1/*
2 * net/sched/simp.c Simple example of an action
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: Jamal Hadi Salim (2005)
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
36#define TCA_ACT_SIMP 22
37
38/* XXX: Hide all these common elements under some macro
39 * probably
40*/
41#include <linux/tc_act/tc_defact.h>
42#include <net/tc_act/tc_defact.h>
43
44/* use generic hash table with 8 buckets */
45#define MY_TAB_SIZE 8
46#define MY_TAB_MASK (MY_TAB_SIZE - 1)
47static u32 idx_gen;
48static struct tcf_defact *tcf_simp_ht[MY_TAB_SIZE];
49static DEFINE_RWLOCK(simp_lock);
50
51/* override the defaults */
52#define tcf_st tcf_defact
53#define tc_st tc_defact
54#define tcf_t_lock simp_lock
55#define tcf_ht tcf_simp_ht
56
57#define CONFIG_NET_ACT_INIT 1
58#include <net/pkt_act.h>
59#include <net/act_generic.h>
60
61static int tcf_simp(struct sk_buff **pskb, struct tc_action *a)
62{
63 struct sk_buff *skb = *pskb;
64 struct tcf_defact *p = PRIV(a, defact);
65
66 spin_lock(&p->lock);
67 p->tm.lastuse = jiffies;
68 p->bstats.bytes += skb->len;
69 p->bstats.packets++;
70
71 /* print policy string followed by _ then packet count
72 * Example if this was the 3rd packet and the string was "hello"
73 * then it would look like "hello_3" (without quotes)
74 **/
75 printk("simple: %s_%d\n", (char *)p->defdata, p->bstats.packets);
76 spin_unlock(&p->lock);
77 return p->action;
78}
79
80static struct tc_action_ops act_simp_ops = {
81 .kind = "simple",
82 .type = TCA_ACT_SIMP,
83 .capab = TCA_CAP_NONE,
84 .owner = THIS_MODULE,
85 .act = tcf_simp,
86 tca_use_default_ops
87};
88
89MODULE_AUTHOR("Jamal Hadi Salim(2005)");
90MODULE_DESCRIPTION("Simple example action");
91MODULE_LICENSE("GPL");
92
93static int __init simp_init_module(void)
94{
95 int ret = tcf_register_action(&act_simp_ops);
96 if (!ret)
97 printk("Simple TC action Loaded\n");
98 return ret;
99}
100
101static void __exit simp_cleanup_module(void)
102{
103 tcf_unregister_action(&act_simp_ops);
104}
105
106module_init(simp_init_module);
107module_exit(simp_cleanup_module);