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
*cgrp_cls_state(struct cgroup
*cgrp
)
28 return container_of(cgroup_subsys_state(cgrp
, net_cls_subsys_id
),
29 struct cgroup_cls_state
, css
);
32 static inline struct cgroup_cls_state
*task_cls_state(struct task_struct
*p
)
34 return container_of(task_subsys_state(p
, net_cls_subsys_id
),
35 struct cgroup_cls_state
, css
);
38 static struct cgroup_subsys_state
*cgrp_css_alloc(struct cgroup
*cgrp
)
40 struct cgroup_cls_state
*cs
;
42 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
44 return ERR_PTR(-ENOMEM
);
48 static int cgrp_css_online(struct cgroup
*cgrp
)
51 cgrp_cls_state(cgrp
)->classid
=
52 cgrp_cls_state(cgrp
->parent
)->classid
;
56 static void cgrp_css_free(struct cgroup
*cgrp
)
58 kfree(cgrp_cls_state(cgrp
));
61 static int update_classid(const void *v
, struct file
*file
, unsigned n
)
64 struct socket
*sock
= sock_from_file(file
, &err
);
66 sock
->sk
->sk_classid
= (u32
)(unsigned long)v
;
70 static void cgrp_attach(struct cgroup
*cgrp
, struct cgroup_taskset
*tset
)
72 struct task_struct
*p
;
75 cgroup_taskset_for_each(p
, cgrp
, tset
) {
77 v
= (void *)(unsigned long)task_cls_classid(p
);
78 iterate_fd(p
->files
, 0, update_classid
, v
);
83 static u64
read_classid(struct cgroup
*cgrp
, struct cftype
*cft
)
85 return cgrp_cls_state(cgrp
)->classid
;
88 static int write_classid(struct cgroup
*cgrp
, struct cftype
*cft
, u64 value
)
90 cgrp_cls_state(cgrp
)->classid
= (u32
) value
;
94 static struct cftype ss_files
[] = {
97 .read_u64
= read_classid
,
98 .write_u64
= write_classid
,
103 struct cgroup_subsys net_cls_subsys
= {
105 .css_alloc
= cgrp_css_alloc
,
106 .css_online
= cgrp_css_online
,
107 .css_free
= cgrp_css_free
,
108 .attach
= cgrp_attach
,
109 .subsys_id
= net_cls_subsys_id
,
110 .base_cftypes
= ss_files
,
111 .module
= THIS_MODULE
,
114 struct cls_cgroup_head
{
116 struct tcf_exts exts
;
117 struct tcf_ematch_tree ematches
;
120 static int cls_cgroup_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
121 struct tcf_result
*res
)
123 struct cls_cgroup_head
*head
= tp
->root
;
127 classid
= task_cls_state(current
)->classid
;
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.
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.
140 if (in_serving_softirq()) {
141 /* If there is an sk_classid we'll use that. */
144 classid
= skb
->sk
->sk_classid
;
150 if (!tcf_em_tree_match(skb
, &head
->ematches
, NULL
))
153 res
->classid
= classid
;
155 return tcf_exts_exec(skb
, &head
->exts
, res
);
158 static unsigned long cls_cgroup_get(struct tcf_proto
*tp
, u32 handle
)
163 static void cls_cgroup_put(struct tcf_proto
*tp
, unsigned long f
)
167 static int cls_cgroup_init(struct tcf_proto
*tp
)
172 static const struct tcf_ext_map cgroup_ext_map
= {
173 .action
= TCA_CGROUP_ACT
,
174 .police
= TCA_CGROUP_POLICE
,
177 static const struct nla_policy cgroup_policy
[TCA_CGROUP_MAX
+ 1] = {
178 [TCA_CGROUP_EMATCHES
] = { .type
= NLA_NESTED
},
181 static int cls_cgroup_change(struct net
*net
, struct sk_buff
*in_skb
,
182 struct tcf_proto
*tp
, unsigned long base
,
183 u32 handle
, struct nlattr
**tca
,
186 struct nlattr
*tb
[TCA_CGROUP_MAX
+ 1];
187 struct cls_cgroup_head
*head
= tp
->root
;
188 struct tcf_ematch_tree t
;
192 if (!tca
[TCA_OPTIONS
])
199 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
203 head
->handle
= handle
;
210 if (handle
!= head
->handle
)
213 err
= nla_parse_nested(tb
, TCA_CGROUP_MAX
, tca
[TCA_OPTIONS
],
218 err
= tcf_exts_validate(net
, tp
, tb
, tca
[TCA_RATE
], &e
,
223 err
= tcf_em_tree_validate(tp
, tb
[TCA_CGROUP_EMATCHES
], &t
);
227 tcf_exts_change(tp
, &head
->exts
, &e
);
228 tcf_em_tree_change(tp
, &head
->ematches
, &t
);
233 static void cls_cgroup_destroy(struct tcf_proto
*tp
)
235 struct cls_cgroup_head
*head
= tp
->root
;
238 tcf_exts_destroy(tp
, &head
->exts
);
239 tcf_em_tree_destroy(tp
, &head
->ematches
);
244 static int cls_cgroup_delete(struct tcf_proto
*tp
, unsigned long arg
)
249 static void cls_cgroup_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
251 struct cls_cgroup_head
*head
= tp
->root
;
253 if (arg
->count
< arg
->skip
)
256 if (arg
->fn(tp
, (unsigned long) head
, arg
) < 0) {
264 static int cls_cgroup_dump(struct tcf_proto
*tp
, unsigned long fh
,
265 struct sk_buff
*skb
, struct tcmsg
*t
)
267 struct cls_cgroup_head
*head
= tp
->root
;
268 unsigned char *b
= skb_tail_pointer(skb
);
271 t
->tcm_handle
= head
->handle
;
273 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
275 goto nla_put_failure
;
277 if (tcf_exts_dump(skb
, &head
->exts
, &cgroup_ext_map
) < 0 ||
278 tcf_em_tree_dump(skb
, &head
->ematches
, TCA_CGROUP_EMATCHES
) < 0)
279 goto nla_put_failure
;
281 nla_nest_end(skb
, nest
);
283 if (tcf_exts_dump_stats(skb
, &head
->exts
, &cgroup_ext_map
) < 0)
284 goto nla_put_failure
;
293 static struct tcf_proto_ops cls_cgroup_ops __read_mostly
= {
295 .init
= cls_cgroup_init
,
296 .change
= cls_cgroup_change
,
297 .classify
= cls_cgroup_classify
,
298 .destroy
= cls_cgroup_destroy
,
299 .get
= cls_cgroup_get
,
300 .put
= cls_cgroup_put
,
301 .delete = cls_cgroup_delete
,
302 .walk
= cls_cgroup_walk
,
303 .dump
= cls_cgroup_dump
,
304 .owner
= THIS_MODULE
,
307 static int __init
init_cgroup_cls(void)
311 ret
= cgroup_load_subsys(&net_cls_subsys
);
315 ret
= register_tcf_proto_ops(&cls_cgroup_ops
);
317 cgroup_unload_subsys(&net_cls_subsys
);
323 static void __exit
exit_cgroup_cls(void)
325 unregister_tcf_proto_ops(&cls_cgroup_ops
);
327 cgroup_unload_subsys(&net_cls_subsys
);
330 module_init(init_cgroup_cls
);
331 module_exit(exit_cgroup_cls
);
332 MODULE_LICENSE("GPL");