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 TG |
14 | #include <linux/types.h> |
15 | #include <linux/string.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/skbuff.h> | |
18 | #include <linux/cgroup.h> | |
f8451725 | 19 | #include <linux/rcupdate.h> |
6a328d8c | 20 | #include <linux/fdtable.h> |
f4009237 TG |
21 | #include <net/rtnetlink.h> |
22 | #include <net/pkt_cls.h> | |
f8451725 HX |
23 | #include <net/sock.h> |
24 | #include <net/cls_cgroup.h> | |
f4009237 | 25 | |
8e8ba854 | 26 | static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp) |
f4009237 | 27 | { |
8e8ba854 LZ |
28 | return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id), |
29 | struct cgroup_cls_state, css); | |
30 | } | |
31 | ||
32 | static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p) | |
33 | { | |
34 | return container_of(task_subsys_state(p, net_cls_subsys_id), | |
35 | struct cgroup_cls_state, css); | |
f4009237 TG |
36 | } |
37 | ||
92fb9748 | 38 | static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp) |
f4009237 TG |
39 | { |
40 | struct cgroup_cls_state *cs; | |
41 | ||
cc7ec456 ED |
42 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
43 | if (!cs) | |
f4009237 | 44 | return ERR_PTR(-ENOMEM); |
0ba18f7a TH |
45 | return &cs->css; |
46 | } | |
f4009237 | 47 | |
0ba18f7a TH |
48 | static int cgrp_css_online(struct cgroup *cgrp) |
49 | { | |
f4009237 | 50 | if (cgrp->parent) |
0ba18f7a TH |
51 | cgrp_cls_state(cgrp)->classid = |
52 | cgrp_cls_state(cgrp->parent)->classid; | |
53 | return 0; | |
f4009237 TG |
54 | } |
55 | ||
92fb9748 | 56 | static void cgrp_css_free(struct cgroup *cgrp) |
f4009237 | 57 | { |
8e8ba854 | 58 | kfree(cgrp_cls_state(cgrp)); |
f4009237 TG |
59 | } |
60 | ||
6a328d8c DW |
61 | static int update_classid(const void *v, struct file *file, unsigned n) |
62 | { | |
63 | int err; | |
64 | struct socket *sock = sock_from_file(file, &err); | |
65 | if (sock) | |
66 | sock->sk->sk_classid = (u32)(unsigned long)v; | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) | |
71 | { | |
72 | struct task_struct *p; | |
73 | void *v; | |
74 | ||
75 | cgroup_taskset_for_each(p, cgrp, tset) { | |
76 | task_lock(p); | |
77 | v = (void *)(unsigned long)task_cls_classid(p); | |
78 | iterate_fd(p->files, 0, update_classid, v); | |
79 | task_unlock(p); | |
80 | } | |
81 | } | |
82 | ||
f4009237 TG |
83 | static u64 read_classid(struct cgroup *cgrp, struct cftype *cft) |
84 | { | |
8e8ba854 | 85 | return cgrp_cls_state(cgrp)->classid; |
f4009237 TG |
86 | } |
87 | ||
88 | static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value) | |
89 | { | |
8e8ba854 | 90 | cgrp_cls_state(cgrp)->classid = (u32) value; |
f4009237 TG |
91 | return 0; |
92 | } | |
93 | ||
94 | static struct cftype ss_files[] = { | |
95 | { | |
96 | .name = "classid", | |
97 | .read_u64 = read_classid, | |
98 | .write_u64 = write_classid, | |
99 | }, | |
4baf6e33 | 100 | { } /* terminate */ |
f4009237 TG |
101 | }; |
102 | ||
676f7c8f TH |
103 | struct cgroup_subsys net_cls_subsys = { |
104 | .name = "net_cls", | |
92fb9748 | 105 | .css_alloc = cgrp_css_alloc, |
0ba18f7a | 106 | .css_online = cgrp_css_online, |
92fb9748 | 107 | .css_free = cgrp_css_free, |
6a328d8c | 108 | .attach = cgrp_attach, |
676f7c8f | 109 | .subsys_id = net_cls_subsys_id, |
4baf6e33 | 110 | .base_cftypes = ss_files, |
676f7c8f TH |
111 | .module = THIS_MODULE, |
112 | }; | |
113 | ||
cc7ec456 | 114 | struct cls_cgroup_head { |
f4009237 TG |
115 | u32 handle; |
116 | struct tcf_exts exts; | |
117 | struct tcf_ematch_tree ematches; | |
118 | }; | |
119 | ||
dc7f9f6e | 120 | static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
f4009237 TG |
121 | struct tcf_result *res) |
122 | { | |
123 | struct cls_cgroup_head *head = tp->root; | |
e65fcfd6 | 124 | u32 classid; |
f4009237 | 125 | |
f8451725 HX |
126 | rcu_read_lock(); |
127 | classid = task_cls_state(current)->classid; | |
128 | rcu_read_unlock(); | |
129 | ||
f4009237 TG |
130 | /* |
131 | * Due to the nature of the classifier it is required to ignore all | |
132 | * packets originating from softirq context as accessing `current' | |
133 | * would lead to false results. | |
134 | * | |
135 | * This test assumes that all callers of dev_queue_xmit() explicitely | |
136 | * disable bh. Knowing this, it is possible to detect softirq based | |
137 | * calls by looking at the number of nested bh disable calls because | |
138 | * softirqs always disables bh. | |
139 | */ | |
75e1056f | 140 | if (in_serving_softirq()) { |
f8451725 HX |
141 | /* If there is an sk_classid we'll use that. */ |
142 | if (!skb->sk) | |
143 | return -1; | |
144 | classid = skb->sk->sk_classid; | |
145 | } | |
f4009237 | 146 | |
e65fcfd6 PM |
147 | if (!classid) |
148 | return -1; | |
149 | ||
150 | if (!tcf_em_tree_match(skb, &head->ematches, NULL)) | |
151 | return -1; | |
152 | ||
153 | res->classid = classid; | |
154 | res->class = 0; | |
155 | return tcf_exts_exec(skb, &head->exts, res); | |
f4009237 TG |
156 | } |
157 | ||
158 | static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle) | |
159 | { | |
160 | return 0UL; | |
161 | } | |
162 | ||
163 | static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f) | |
164 | { | |
165 | } | |
166 | ||
167 | static int cls_cgroup_init(struct tcf_proto *tp) | |
168 | { | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static const struct tcf_ext_map cgroup_ext_map = { | |
173 | .action = TCA_CGROUP_ACT, | |
174 | .police = TCA_CGROUP_POLICE, | |
175 | }; | |
176 | ||
177 | static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { | |
178 | [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, | |
179 | }; | |
180 | ||
af4c6641 EB |
181 | static int cls_cgroup_change(struct sk_buff *in_skb, |
182 | struct tcf_proto *tp, unsigned long base, | |
f4009237 TG |
183 | u32 handle, struct nlattr **tca, |
184 | unsigned long *arg) | |
185 | { | |
cc7ec456 | 186 | struct nlattr *tb[TCA_CGROUP_MAX + 1]; |
f4009237 TG |
187 | struct cls_cgroup_head *head = tp->root; |
188 | struct tcf_ematch_tree t; | |
189 | struct tcf_exts e; | |
190 | int err; | |
191 | ||
52ea3a56 MU |
192 | if (!tca[TCA_OPTIONS]) |
193 | return -EINVAL; | |
194 | ||
f4009237 TG |
195 | if (head == NULL) { |
196 | if (!handle) | |
197 | return -EINVAL; | |
198 | ||
199 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
200 | if (head == NULL) | |
201 | return -ENOBUFS; | |
202 | ||
203 | head->handle = handle; | |
204 | ||
205 | tcf_tree_lock(tp); | |
206 | tp->root = head; | |
207 | tcf_tree_unlock(tp); | |
208 | } | |
209 | ||
210 | if (handle != head->handle) | |
211 | return -ENOENT; | |
212 | ||
213 | err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS], | |
214 | cgroup_policy); | |
215 | if (err < 0) | |
216 | return err; | |
217 | ||
218 | err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map); | |
219 | if (err < 0) | |
220 | return err; | |
221 | ||
222 | err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t); | |
223 | if (err < 0) | |
224 | return err; | |
225 | ||
226 | tcf_exts_change(tp, &head->exts, &e); | |
227 | tcf_em_tree_change(tp, &head->ematches, &t); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | static void cls_cgroup_destroy(struct tcf_proto *tp) | |
233 | { | |
47a1a1d4 | 234 | struct cls_cgroup_head *head = tp->root; |
f4009237 TG |
235 | |
236 | if (head) { | |
237 | tcf_exts_destroy(tp, &head->exts); | |
238 | tcf_em_tree_destroy(tp, &head->ematches); | |
239 | kfree(head); | |
240 | } | |
241 | } | |
242 | ||
243 | static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg) | |
244 | { | |
245 | return -EOPNOTSUPP; | |
246 | } | |
247 | ||
248 | static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
249 | { | |
250 | struct cls_cgroup_head *head = tp->root; | |
251 | ||
252 | if (arg->count < arg->skip) | |
253 | goto skip; | |
254 | ||
255 | if (arg->fn(tp, (unsigned long) head, arg) < 0) { | |
256 | arg->stop = 1; | |
257 | return; | |
258 | } | |
259 | skip: | |
260 | arg->count++; | |
261 | } | |
262 | ||
263 | static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh, | |
264 | struct sk_buff *skb, struct tcmsg *t) | |
265 | { | |
266 | struct cls_cgroup_head *head = tp->root; | |
267 | unsigned char *b = skb_tail_pointer(skb); | |
268 | struct nlattr *nest; | |
269 | ||
270 | t->tcm_handle = head->handle; | |
271 | ||
272 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
273 | if (nest == NULL) | |
274 | goto nla_put_failure; | |
275 | ||
276 | if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 || | |
277 | tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0) | |
278 | goto nla_put_failure; | |
279 | ||
280 | nla_nest_end(skb, nest); | |
281 | ||
282 | if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0) | |
283 | goto nla_put_failure; | |
284 | ||
285 | return skb->len; | |
286 | ||
287 | nla_put_failure: | |
288 | nlmsg_trim(skb, b); | |
289 | return -1; | |
290 | } | |
291 | ||
292 | static struct tcf_proto_ops cls_cgroup_ops __read_mostly = { | |
293 | .kind = "cgroup", | |
294 | .init = cls_cgroup_init, | |
295 | .change = cls_cgroup_change, | |
296 | .classify = cls_cgroup_classify, | |
297 | .destroy = cls_cgroup_destroy, | |
298 | .get = cls_cgroup_get, | |
299 | .put = cls_cgroup_put, | |
300 | .delete = cls_cgroup_delete, | |
301 | .walk = cls_cgroup_walk, | |
302 | .dump = cls_cgroup_dump, | |
303 | .owner = THIS_MODULE, | |
304 | }; | |
305 | ||
306 | static int __init init_cgroup_cls(void) | |
307 | { | |
f8451725 HX |
308 | int ret; |
309 | ||
8e039d84 BB |
310 | ret = cgroup_load_subsys(&net_cls_subsys); |
311 | if (ret) | |
f8451725 HX |
312 | goto out; |
313 | ||
f8451725 HX |
314 | ret = register_tcf_proto_ops(&cls_cgroup_ops); |
315 | if (ret) | |
316 | cgroup_unload_subsys(&net_cls_subsys); | |
317 | ||
318 | out: | |
8e039d84 | 319 | return ret; |
f4009237 TG |
320 | } |
321 | ||
322 | static void __exit exit_cgroup_cls(void) | |
323 | { | |
324 | unregister_tcf_proto_ops(&cls_cgroup_ops); | |
f8451725 | 325 | |
8e039d84 | 326 | cgroup_unload_subsys(&net_cls_subsys); |
f4009237 TG |
327 | } |
328 | ||
329 | module_init(init_cgroup_cls); | |
330 | module_exit(exit_cgroup_cls); | |
331 | MODULE_LICENSE("GPL"); |