2 * net/sched/cls_cgroup.c Control Group Classifier
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 * Authors: Thomas Graf <tgraf@suug.ch>
12 #include <linux/module.h>
13 #include <linux/slab.h>
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>
19 #include <linux/rcupdate.h>
20 #include <linux/fdtable.h>
21 #include <net/rtnetlink.h>
22 #include <net/pkt_cls.h>
24 #include <net/cls_cgroup.h>
26 static inline struct cgroup_cls_state
*css_cls_state(struct cgroup_subsys_state
*css
)
28 return css
? container_of(css
, struct cgroup_cls_state
, css
) : NULL
;
31 static inline struct cgroup_cls_state
*cgrp_cls_state(struct cgroup
*cgrp
)
33 return css_cls_state(cgroup_css(cgrp
, net_cls_subsys_id
));
36 static inline struct cgroup_cls_state
*task_cls_state(struct task_struct
*p
)
38 return css_cls_state(task_css(p
, net_cls_subsys_id
));
41 static struct cgroup_subsys_state
*
42 cgrp_css_alloc(struct cgroup_subsys_state
*parent_css
)
44 struct cgroup_cls_state
*cs
;
46 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
48 return ERR_PTR(-ENOMEM
);
52 static int cgrp_css_online(struct cgroup_subsys_state
*css
)
54 struct cgroup_cls_state
*cs
= css_cls_state(css
);
55 struct cgroup_cls_state
*parent
= css_cls_state(css_parent(css
));
58 cs
->classid
= parent
->classid
;
62 static void cgrp_css_free(struct cgroup_subsys_state
*css
)
64 kfree(css_cls_state(css
));
67 static int update_classid(const void *v
, struct file
*file
, unsigned n
)
70 struct socket
*sock
= sock_from_file(file
, &err
);
72 sock
->sk
->sk_classid
= (u32
)(unsigned long)v
;
76 static void cgrp_attach(struct cgroup_subsys_state
*css
,
77 struct cgroup_taskset
*tset
)
79 struct task_struct
*p
;
82 cgroup_taskset_for_each(p
, css
->cgroup
, tset
) {
84 v
= (void *)(unsigned long)task_cls_classid(p
);
85 iterate_fd(p
->files
, 0, update_classid
, v
);
90 static u64
read_classid(struct cgroup
*cgrp
, struct cftype
*cft
)
92 return cgrp_cls_state(cgrp
)->classid
;
95 static int write_classid(struct cgroup
*cgrp
, struct cftype
*cft
, u64 value
)
97 cgrp_cls_state(cgrp
)->classid
= (u32
) value
;
101 static struct cftype ss_files
[] = {
104 .read_u64
= read_classid
,
105 .write_u64
= write_classid
,
110 struct cgroup_subsys net_cls_subsys
= {
112 .css_alloc
= cgrp_css_alloc
,
113 .css_online
= cgrp_css_online
,
114 .css_free
= cgrp_css_free
,
115 .attach
= cgrp_attach
,
116 .subsys_id
= net_cls_subsys_id
,
117 .base_cftypes
= ss_files
,
118 .module
= THIS_MODULE
,
121 struct cls_cgroup_head
{
123 struct tcf_exts exts
;
124 struct tcf_ematch_tree ematches
;
127 static int cls_cgroup_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
128 struct tcf_result
*res
)
130 struct cls_cgroup_head
*head
= tp
->root
;
134 classid
= task_cls_state(current
)->classid
;
138 * Due to the nature of the classifier it is required to ignore all
139 * packets originating from softirq context as accessing `current'
140 * would lead to false results.
142 * This test assumes that all callers of dev_queue_xmit() explicitely
143 * disable bh. Knowing this, it is possible to detect softirq based
144 * calls by looking at the number of nested bh disable calls because
145 * softirqs always disables bh.
147 if (in_serving_softirq()) {
148 /* If there is an sk_classid we'll use that. */
151 classid
= skb
->sk
->sk_classid
;
157 if (!tcf_em_tree_match(skb
, &head
->ematches
, NULL
))
160 res
->classid
= classid
;
162 return tcf_exts_exec(skb
, &head
->exts
, res
);
165 static unsigned long cls_cgroup_get(struct tcf_proto
*tp
, u32 handle
)
170 static void cls_cgroup_put(struct tcf_proto
*tp
, unsigned long f
)
174 static int cls_cgroup_init(struct tcf_proto
*tp
)
179 static const struct tcf_ext_map cgroup_ext_map
= {
180 .action
= TCA_CGROUP_ACT
,
181 .police
= TCA_CGROUP_POLICE
,
184 static const struct nla_policy cgroup_policy
[TCA_CGROUP_MAX
+ 1] = {
185 [TCA_CGROUP_EMATCHES
] = { .type
= NLA_NESTED
},
188 static int cls_cgroup_change(struct net
*net
, struct sk_buff
*in_skb
,
189 struct tcf_proto
*tp
, unsigned long base
,
190 u32 handle
, struct nlattr
**tca
,
193 struct nlattr
*tb
[TCA_CGROUP_MAX
+ 1];
194 struct cls_cgroup_head
*head
= tp
->root
;
195 struct tcf_ematch_tree t
;
199 if (!tca
[TCA_OPTIONS
])
206 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
210 head
->handle
= handle
;
217 if (handle
!= head
->handle
)
220 err
= nla_parse_nested(tb
, TCA_CGROUP_MAX
, tca
[TCA_OPTIONS
],
225 err
= tcf_exts_validate(net
, tp
, tb
, tca
[TCA_RATE
], &e
,
230 err
= tcf_em_tree_validate(tp
, tb
[TCA_CGROUP_EMATCHES
], &t
);
234 tcf_exts_change(tp
, &head
->exts
, &e
);
235 tcf_em_tree_change(tp
, &head
->ematches
, &t
);
240 static void cls_cgroup_destroy(struct tcf_proto
*tp
)
242 struct cls_cgroup_head
*head
= tp
->root
;
245 tcf_exts_destroy(tp
, &head
->exts
);
246 tcf_em_tree_destroy(tp
, &head
->ematches
);
251 static int cls_cgroup_delete(struct tcf_proto
*tp
, unsigned long arg
)
256 static void cls_cgroup_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
258 struct cls_cgroup_head
*head
= tp
->root
;
260 if (arg
->count
< arg
->skip
)
263 if (arg
->fn(tp
, (unsigned long) head
, arg
) < 0) {
271 static int cls_cgroup_dump(struct tcf_proto
*tp
, unsigned long fh
,
272 struct sk_buff
*skb
, struct tcmsg
*t
)
274 struct cls_cgroup_head
*head
= tp
->root
;
275 unsigned char *b
= skb_tail_pointer(skb
);
278 t
->tcm_handle
= head
->handle
;
280 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
282 goto nla_put_failure
;
284 if (tcf_exts_dump(skb
, &head
->exts
, &cgroup_ext_map
) < 0 ||
285 tcf_em_tree_dump(skb
, &head
->ematches
, TCA_CGROUP_EMATCHES
) < 0)
286 goto nla_put_failure
;
288 nla_nest_end(skb
, nest
);
290 if (tcf_exts_dump_stats(skb
, &head
->exts
, &cgroup_ext_map
) < 0)
291 goto nla_put_failure
;
300 static struct tcf_proto_ops cls_cgroup_ops __read_mostly
= {
302 .init
= cls_cgroup_init
,
303 .change
= cls_cgroup_change
,
304 .classify
= cls_cgroup_classify
,
305 .destroy
= cls_cgroup_destroy
,
306 .get
= cls_cgroup_get
,
307 .put
= cls_cgroup_put
,
308 .delete = cls_cgroup_delete
,
309 .walk
= cls_cgroup_walk
,
310 .dump
= cls_cgroup_dump
,
311 .owner
= THIS_MODULE
,
314 static int __init
init_cgroup_cls(void)
318 ret
= cgroup_load_subsys(&net_cls_subsys
);
322 ret
= register_tcf_proto_ops(&cls_cgroup_ops
);
324 cgroup_unload_subsys(&net_cls_subsys
);
330 static void __exit
exit_cgroup_cls(void)
332 unregister_tcf_proto_ops(&cls_cgroup_ops
);
334 cgroup_unload_subsys(&net_cls_subsys
);
337 module_init(init_cgroup_cls
);
338 module_exit(exit_cgroup_cls
);
339 MODULE_LICENSE("GPL");