Commit | Line | Data |
---|---|---|
f4009237 TG |
1 | /* |
2 | * net/sched/cls_cgroup.c Control Group Classifier | |
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: Thomas Graf <tgraf@suug.ch> | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
f4009237 | 14 | #include <linux/skbuff.h> |
f8451725 | 15 | #include <linux/rcupdate.h> |
f4009237 TG |
16 | #include <net/rtnetlink.h> |
17 | #include <net/pkt_cls.h> | |
f8451725 HX |
18 | #include <net/sock.h> |
19 | #include <net/cls_cgroup.h> | |
f4009237 | 20 | |
cc7ec456 | 21 | struct cls_cgroup_head { |
f4009237 TG |
22 | u32 handle; |
23 | struct tcf_exts exts; | |
24 | struct tcf_ematch_tree ematches; | |
952313bd | 25 | struct tcf_proto *tp; |
b1b5b04f CW |
26 | union { |
27 | struct work_struct work; | |
28 | struct rcu_head rcu; | |
29 | }; | |
f4009237 TG |
30 | }; |
31 | ||
dc7f9f6e | 32 | static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
f4009237 TG |
33 | struct tcf_result *res) |
34 | { | |
952313bd | 35 | struct cls_cgroup_head *head = rcu_dereference_bh(tp->root); |
b87a173e | 36 | u32 classid = task_get_classid(skb); |
f4009237 | 37 | |
e65fcfd6 PM |
38 | if (!classid) |
39 | return -1; | |
e65fcfd6 PM |
40 | if (!tcf_em_tree_match(skb, &head->ematches, NULL)) |
41 | return -1; | |
42 | ||
43 | res->classid = classid; | |
44 | res->class = 0; | |
b87a173e | 45 | |
e65fcfd6 | 46 | return tcf_exts_exec(skb, &head->exts, res); |
f4009237 TG |
47 | } |
48 | ||
8113c095 | 49 | static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle) |
f4009237 | 50 | { |
8113c095 | 51 | return NULL; |
f4009237 TG |
52 | } |
53 | ||
f4009237 TG |
54 | static int cls_cgroup_init(struct tcf_proto *tp) |
55 | { | |
56 | return 0; | |
57 | } | |
58 | ||
f4009237 TG |
59 | static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { |
60 | [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, | |
61 | }; | |
62 | ||
b1b5b04f CW |
63 | static void cls_cgroup_destroy_work(struct work_struct *work) |
64 | { | |
65 | struct cls_cgroup_head *head = container_of(work, | |
66 | struct cls_cgroup_head, | |
67 | work); | |
68 | rtnl_lock(); | |
69 | tcf_exts_destroy(&head->exts); | |
70 | tcf_em_tree_destroy(&head->ematches); | |
71 | kfree(head); | |
72 | rtnl_unlock(); | |
73 | } | |
74 | ||
952313bd JF |
75 | static void cls_cgroup_destroy_rcu(struct rcu_head *root) |
76 | { | |
77 | struct cls_cgroup_head *head = container_of(root, | |
78 | struct cls_cgroup_head, | |
79 | rcu); | |
80 | ||
b1b5b04f CW |
81 | INIT_WORK(&head->work, cls_cgroup_destroy_work); |
82 | tcf_queue_work(&head->work); | |
952313bd JF |
83 | } |
84 | ||
c1b52739 | 85 | static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, |
af4c6641 | 86 | struct tcf_proto *tp, unsigned long base, |
f4009237 | 87 | u32 handle, struct nlattr **tca, |
8113c095 | 88 | void **arg, bool ovr) |
f4009237 | 89 | { |
cc7ec456 | 90 | struct nlattr *tb[TCA_CGROUP_MAX + 1]; |
952313bd JF |
91 | struct cls_cgroup_head *head = rtnl_dereference(tp->root); |
92 | struct cls_cgroup_head *new; | |
f4009237 TG |
93 | int err; |
94 | ||
52ea3a56 MU |
95 | if (!tca[TCA_OPTIONS]) |
96 | return -EINVAL; | |
97 | ||
952313bd JF |
98 | if (!head && !handle) |
99 | return -EINVAL; | |
f4009237 | 100 | |
952313bd JF |
101 | if (head && handle != head->handle) |
102 | return -ENOENT; | |
f4009237 | 103 | |
952313bd JF |
104 | new = kzalloc(sizeof(*head), GFP_KERNEL); |
105 | if (!new) | |
106 | return -ENOBUFS; | |
f4009237 | 107 | |
b9a24bb7 WC |
108 | err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE); |
109 | if (err < 0) | |
110 | goto errout; | |
18b5427a | 111 | new->handle = handle; |
952313bd | 112 | new->tp = tp; |
f4009237 | 113 | err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS], |
fceb6435 | 114 | cgroup_policy, NULL); |
f4009237 | 115 | if (err < 0) |
d14cbfc8 | 116 | goto errout; |
f4009237 | 117 | |
8cc62513 | 118 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, ovr); |
f4009237 | 119 | if (err < 0) |
d14cbfc8 | 120 | goto errout; |
f4009237 | 121 | |
4ebc1e3c | 122 | err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches); |
8cc62513 | 123 | if (err < 0) |
d14cbfc8 | 124 | goto errout; |
f4009237 | 125 | |
952313bd JF |
126 | rcu_assign_pointer(tp->root, new); |
127 | if (head) | |
128 | call_rcu(&head->rcu, cls_cgroup_destroy_rcu); | |
f4009237 | 129 | return 0; |
d14cbfc8 | 130 | errout: |
b9a24bb7 | 131 | tcf_exts_destroy(&new->exts); |
d14cbfc8 JF |
132 | kfree(new); |
133 | return err; | |
f4009237 TG |
134 | } |
135 | ||
763dbf63 | 136 | static void cls_cgroup_destroy(struct tcf_proto *tp) |
f4009237 | 137 | { |
952313bd | 138 | struct cls_cgroup_head *head = rtnl_dereference(tp->root); |
f4009237 | 139 | |
d9363774 DB |
140 | /* Head can still be NULL due to cls_cgroup_init(). */ |
141 | if (head) | |
13990f81 | 142 | call_rcu(&head->rcu, cls_cgroup_destroy_rcu); |
f4009237 TG |
143 | } |
144 | ||
8113c095 | 145 | static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last) |
f4009237 TG |
146 | { |
147 | return -EOPNOTSUPP; | |
148 | } | |
149 | ||
150 | static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
151 | { | |
952313bd | 152 | struct cls_cgroup_head *head = rtnl_dereference(tp->root); |
f4009237 TG |
153 | |
154 | if (arg->count < arg->skip) | |
155 | goto skip; | |
156 | ||
8113c095 | 157 | if (arg->fn(tp, head, arg) < 0) { |
f4009237 TG |
158 | arg->stop = 1; |
159 | return; | |
160 | } | |
161 | skip: | |
162 | arg->count++; | |
163 | } | |
164 | ||
8113c095 | 165 | static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh, |
f4009237 TG |
166 | struct sk_buff *skb, struct tcmsg *t) |
167 | { | |
952313bd | 168 | struct cls_cgroup_head *head = rtnl_dereference(tp->root); |
f4009237 TG |
169 | struct nlattr *nest; |
170 | ||
171 | t->tcm_handle = head->handle; | |
172 | ||
173 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
174 | if (nest == NULL) | |
175 | goto nla_put_failure; | |
176 | ||
5da57f42 | 177 | if (tcf_exts_dump(skb, &head->exts) < 0 || |
f4009237 TG |
178 | tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0) |
179 | goto nla_put_failure; | |
180 | ||
181 | nla_nest_end(skb, nest); | |
182 | ||
5da57f42 | 183 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
f4009237 TG |
184 | goto nla_put_failure; |
185 | ||
186 | return skb->len; | |
187 | ||
188 | nla_put_failure: | |
6ea3b446 | 189 | nla_nest_cancel(skb, nest); |
f4009237 TG |
190 | return -1; |
191 | } | |
192 | ||
193 | static struct tcf_proto_ops cls_cgroup_ops __read_mostly = { | |
194 | .kind = "cgroup", | |
195 | .init = cls_cgroup_init, | |
196 | .change = cls_cgroup_change, | |
197 | .classify = cls_cgroup_classify, | |
198 | .destroy = cls_cgroup_destroy, | |
199 | .get = cls_cgroup_get, | |
f4009237 TG |
200 | .delete = cls_cgroup_delete, |
201 | .walk = cls_cgroup_walk, | |
202 | .dump = cls_cgroup_dump, | |
203 | .owner = THIS_MODULE, | |
204 | }; | |
205 | ||
206 | static int __init init_cgroup_cls(void) | |
207 | { | |
fe1217c4 | 208 | return register_tcf_proto_ops(&cls_cgroup_ops); |
f4009237 TG |
209 | } |
210 | ||
211 | static void __exit exit_cgroup_cls(void) | |
212 | { | |
213 | unregister_tcf_proto_ops(&cls_cgroup_ops); | |
214 | } | |
215 | ||
216 | module_init(init_cgroup_cls); | |
217 | module_exit(exit_cgroup_cls); | |
218 | MODULE_LICENSE("GPL"); |