blob: 3a294eb98d6178733edc49c98da4069ccf643a39 [file] [log] [blame]
Thomas Graff4009232008-11-07 22:56:00 -08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Thomas Graff4009232008-11-07 22:56:00 -080014#include <linux/types.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/skbuff.h>
18#include <linux/cgroup.h>
Herbert Xuf8451722010-05-24 00:12:34 -070019#include <linux/rcupdate.h>
Daniel Wagner6a328d82012-10-25 04:16:59 +000020#include <linux/fdtable.h>
Thomas Graff4009232008-11-07 22:56:00 -080021#include <net/rtnetlink.h>
22#include <net/pkt_cls.h>
Herbert Xuf8451722010-05-24 00:12:34 -070023#include <net/sock.h>
24#include <net/cls_cgroup.h>
Thomas Graff4009232008-11-07 22:56:00 -080025
Li Zefan8e8ba852008-12-29 19:39:03 -080026static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080027{
Li Zefan8e8ba852008-12-29 19:39:03 -080028 return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
29 struct cgroup_cls_state, css);
30}
31
32static 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);
Thomas Graff4009232008-11-07 22:56:00 -080036}
37
Tejun Heo92fb9742012-11-19 08:13:38 -080038static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080039{
40 struct cgroup_cls_state *cs;
41
Eric Dumazetcc7ec452011-01-19 19:26:56 +000042 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
43 if (!cs)
Thomas Graff4009232008-11-07 22:56:00 -080044 return ERR_PTR(-ENOMEM);
Thomas Graff4009232008-11-07 22:56:00 -080045 return &cs->css;
46}
47
Tejun Heo0ba18f72012-11-22 07:32:46 -080048static int cgrp_css_online(struct cgroup *cgrp)
49{
50 if (cgrp->parent)
51 cgrp_cls_state(cgrp)->classid =
52 cgrp_cls_state(cgrp->parent)->classid;
53 return 0;
54}
55
Tejun Heo92fb9742012-11-19 08:13:38 -080056static void cgrp_css_free(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080057{
Li Zefan8e8ba852008-12-29 19:39:03 -080058 kfree(cgrp_cls_state(cgrp));
Thomas Graff4009232008-11-07 22:56:00 -080059}
60
Daniel Wagner6a328d82012-10-25 04:16:59 +000061static 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
70static 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
Thomas Graff4009232008-11-07 22:56:00 -080083static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
84{
Li Zefan8e8ba852008-12-29 19:39:03 -080085 return cgrp_cls_state(cgrp)->classid;
Thomas Graff4009232008-11-07 22:56:00 -080086}
87
88static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
89{
Li Zefan8e8ba852008-12-29 19:39:03 -080090 cgrp_cls_state(cgrp)->classid = (u32) value;
Thomas Graff4009232008-11-07 22:56:00 -080091 return 0;
92}
93
94static struct cftype ss_files[] = {
95 {
96 .name = "classid",
97 .read_u64 = read_classid,
98 .write_u64 = write_classid,
99 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700100 { } /* terminate */
Thomas Graff4009232008-11-07 22:56:00 -0800101};
102
Tejun Heo676f7c82012-04-01 12:09:55 -0700103struct cgroup_subsys net_cls_subsys = {
104 .name = "net_cls",
Tejun Heo92fb9742012-11-19 08:13:38 -0800105 .css_alloc = cgrp_css_alloc,
Tejun Heo0ba18f72012-11-22 07:32:46 -0800106 .css_online = cgrp_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -0800107 .css_free = cgrp_css_free,
Daniel Wagner6a328d82012-10-25 04:16:59 +0000108 .attach = cgrp_attach,
Tejun Heo676f7c82012-04-01 12:09:55 -0700109 .subsys_id = net_cls_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700110 .base_cftypes = ss_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700111 .module = THIS_MODULE,
112};
113
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000114struct cls_cgroup_head {
Thomas Graff4009232008-11-07 22:56:00 -0800115 u32 handle;
116 struct tcf_exts exts;
117 struct tcf_ematch_tree ematches;
118};
119
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000120static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Thomas Graff4009232008-11-07 22:56:00 -0800121 struct tcf_result *res)
122{
123 struct cls_cgroup_head *head = tp->root;
Paul Menagee65fcfd2009-05-26 20:47:02 -0700124 u32 classid;
Thomas Graff4009232008-11-07 22:56:00 -0800125
Herbert Xuf8451722010-05-24 00:12:34 -0700126 rcu_read_lock();
127 classid = task_cls_state(current)->classid;
128 rcu_read_unlock();
129
Thomas Graff4009232008-11-07 22:56:00 -0800130 /*
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 */
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700140 if (in_serving_softirq()) {
Herbert Xuf8451722010-05-24 00:12:34 -0700141 /* If there is an sk_classid we'll use that. */
142 if (!skb->sk)
143 return -1;
144 classid = skb->sk->sk_classid;
145 }
Thomas Graff4009232008-11-07 22:56:00 -0800146
Paul Menagee65fcfd2009-05-26 20:47:02 -0700147 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);
Thomas Graff4009232008-11-07 22:56:00 -0800156}
157
158static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
159{
160 return 0UL;
161}
162
163static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
164{
165}
166
167static int cls_cgroup_init(struct tcf_proto *tp)
168{
169 return 0;
170}
171
172static const struct tcf_ext_map cgroup_ext_map = {
173 .action = TCA_CGROUP_ACT,
174 .police = TCA_CGROUP_POLICE,
175};
176
177static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
178 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
179};
180
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000181static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600182 struct tcf_proto *tp, unsigned long base,
Thomas Graff4009232008-11-07 22:56:00 -0800183 u32 handle, struct nlattr **tca,
184 unsigned long *arg)
185{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000186 struct nlattr *tb[TCA_CGROUP_MAX + 1];
Thomas Graff4009232008-11-07 22:56:00 -0800187 struct cls_cgroup_head *head = tp->root;
188 struct tcf_ematch_tree t;
189 struct tcf_exts e;
190 int err;
191
Minoru Usui52ea3a52009-06-09 04:03:09 -0700192 if (!tca[TCA_OPTIONS])
193 return -EINVAL;
194
Thomas Graff4009232008-11-07 22:56:00 -0800195 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
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000218 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e,
219 &cgroup_ext_map);
Thomas Graff4009232008-11-07 22:56:00 -0800220 if (err < 0)
221 return err;
222
223 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
224 if (err < 0)
225 return err;
226
227 tcf_exts_change(tp, &head->exts, &e);
228 tcf_em_tree_change(tp, &head->ematches, &t);
229
230 return 0;
231}
232
233static void cls_cgroup_destroy(struct tcf_proto *tp)
234{
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000235 struct cls_cgroup_head *head = tp->root;
Thomas Graff4009232008-11-07 22:56:00 -0800236
237 if (head) {
238 tcf_exts_destroy(tp, &head->exts);
239 tcf_em_tree_destroy(tp, &head->ematches);
240 kfree(head);
241 }
242}
243
244static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
245{
246 return -EOPNOTSUPP;
247}
248
249static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
250{
251 struct cls_cgroup_head *head = tp->root;
252
253 if (arg->count < arg->skip)
254 goto skip;
255
256 if (arg->fn(tp, (unsigned long) head, arg) < 0) {
257 arg->stop = 1;
258 return;
259 }
260skip:
261 arg->count++;
262}
263
264static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
265 struct sk_buff *skb, struct tcmsg *t)
266{
267 struct cls_cgroup_head *head = tp->root;
268 unsigned char *b = skb_tail_pointer(skb);
269 struct nlattr *nest;
270
271 t->tcm_handle = head->handle;
272
273 nest = nla_nest_start(skb, TCA_OPTIONS);
274 if (nest == NULL)
275 goto nla_put_failure;
276
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;
280
281 nla_nest_end(skb, nest);
282
283 if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
284 goto nla_put_failure;
285
286 return skb->len;
287
288nla_put_failure:
289 nlmsg_trim(skb, b);
290 return -1;
291}
292
293static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
294 .kind = "cgroup",
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,
305};
306
307static int __init init_cgroup_cls(void)
308{
Herbert Xuf8451722010-05-24 00:12:34 -0700309 int ret;
310
Ben Blum8e039d82010-03-23 05:24:03 +0000311 ret = cgroup_load_subsys(&net_cls_subsys);
312 if (ret)
Herbert Xuf8451722010-05-24 00:12:34 -0700313 goto out;
314
Herbert Xuf8451722010-05-24 00:12:34 -0700315 ret = register_tcf_proto_ops(&cls_cgroup_ops);
316 if (ret)
317 cgroup_unload_subsys(&net_cls_subsys);
318
319out:
Ben Blum8e039d82010-03-23 05:24:03 +0000320 return ret;
Thomas Graff4009232008-11-07 22:56:00 -0800321}
322
323static void __exit exit_cgroup_cls(void)
324{
325 unregister_tcf_proto_ops(&cls_cgroup_ops);
Herbert Xuf8451722010-05-24 00:12:34 -0700326
Ben Blum8e039d82010-03-23 05:24:03 +0000327 cgroup_unload_subsys(&net_cls_subsys);
Thomas Graff4009232008-11-07 22:56:00 -0800328}
329
330module_init(init_cgroup_cls);
331module_exit(exit_cgroup_cls);
332MODULE_LICENSE("GPL");