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