blob: 9e6b75e5efce4261cb46e5fe9160526564f74cd8 [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
Tejun Heoa7c6d552013-08-08 20:11:23 -040026static 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
Li Zefan8e8ba852008-12-29 19:39:03 -080031static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080032{
Tejun Heoa7c6d552013-08-08 20:11:23 -040033 return css_cls_state(cgroup_css(cgrp, net_cls_subsys_id));
Li Zefan8e8ba852008-12-29 19:39:03 -080034}
35
36static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
37{
Tejun Heoa7c6d552013-08-08 20:11:23 -040038 return css_cls_state(task_css(p, net_cls_subsys_id));
Thomas Graff4009232008-11-07 22:56:00 -080039}
40
Tejun Heo92fb9742012-11-19 08:13:38 -080041static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080042{
43 struct cgroup_cls_state *cs;
44
Eric Dumazetcc7ec452011-01-19 19:26:56 +000045 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
46 if (!cs)
Thomas Graff4009232008-11-07 22:56:00 -080047 return ERR_PTR(-ENOMEM);
Thomas Graff4009232008-11-07 22:56:00 -080048 return &cs->css;
49}
50
Tejun Heo0ba18f72012-11-22 07:32:46 -080051static int cgrp_css_online(struct cgroup *cgrp)
52{
Tejun Heo63876982013-08-08 20:11:23 -040053 struct cgroup_cls_state *cs = cgrp_cls_state(cgrp);
54 struct cgroup_cls_state *parent = css_cls_state(css_parent(&cs->css));
55
56 if (parent)
57 cs->classid = parent->classid;
Tejun Heo0ba18f72012-11-22 07:32:46 -080058 return 0;
59}
60
Tejun Heo92fb9742012-11-19 08:13:38 -080061static void cgrp_css_free(struct cgroup *cgrp)
Thomas Graff4009232008-11-07 22:56:00 -080062{
Li Zefan8e8ba852008-12-29 19:39:03 -080063 kfree(cgrp_cls_state(cgrp));
Thomas Graff4009232008-11-07 22:56:00 -080064}
65
Daniel Wagner6a328d82012-10-25 04:16:59 +000066static int update_classid(const void *v, struct file *file, unsigned n)
67{
68 int err;
69 struct socket *sock = sock_from_file(file, &err);
70 if (sock)
71 sock->sk->sk_classid = (u32)(unsigned long)v;
72 return 0;
73}
74
75static void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
76{
77 struct task_struct *p;
78 void *v;
79
80 cgroup_taskset_for_each(p, cgrp, tset) {
81 task_lock(p);
82 v = (void *)(unsigned long)task_cls_classid(p);
83 iterate_fd(p->files, 0, update_classid, v);
84 task_unlock(p);
85 }
86}
87
Thomas Graff4009232008-11-07 22:56:00 -080088static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
89{
Li Zefan8e8ba852008-12-29 19:39:03 -080090 return cgrp_cls_state(cgrp)->classid;
Thomas Graff4009232008-11-07 22:56:00 -080091}
92
93static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
94{
Li Zefan8e8ba852008-12-29 19:39:03 -080095 cgrp_cls_state(cgrp)->classid = (u32) value;
Thomas Graff4009232008-11-07 22:56:00 -080096 return 0;
97}
98
99static struct cftype ss_files[] = {
100 {
101 .name = "classid",
102 .read_u64 = read_classid,
103 .write_u64 = write_classid,
104 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700105 { } /* terminate */
Thomas Graff4009232008-11-07 22:56:00 -0800106};
107
Tejun Heo676f7c82012-04-01 12:09:55 -0700108struct cgroup_subsys net_cls_subsys = {
109 .name = "net_cls",
Tejun Heo92fb9742012-11-19 08:13:38 -0800110 .css_alloc = cgrp_css_alloc,
Tejun Heo0ba18f72012-11-22 07:32:46 -0800111 .css_online = cgrp_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -0800112 .css_free = cgrp_css_free,
Daniel Wagner6a328d82012-10-25 04:16:59 +0000113 .attach = cgrp_attach,
Tejun Heo676f7c82012-04-01 12:09:55 -0700114 .subsys_id = net_cls_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700115 .base_cftypes = ss_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700116 .module = THIS_MODULE,
117};
118
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000119struct cls_cgroup_head {
Thomas Graff4009232008-11-07 22:56:00 -0800120 u32 handle;
121 struct tcf_exts exts;
122 struct tcf_ematch_tree ematches;
123};
124
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000125static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Thomas Graff4009232008-11-07 22:56:00 -0800126 struct tcf_result *res)
127{
128 struct cls_cgroup_head *head = tp->root;
Paul Menagee65fcfd2009-05-26 20:47:02 -0700129 u32 classid;
Thomas Graff4009232008-11-07 22:56:00 -0800130
Herbert Xuf8451722010-05-24 00:12:34 -0700131 rcu_read_lock();
132 classid = task_cls_state(current)->classid;
133 rcu_read_unlock();
134
Thomas Graff4009232008-11-07 22:56:00 -0800135 /*
136 * Due to the nature of the classifier it is required to ignore all
137 * packets originating from softirq context as accessing `current'
138 * would lead to false results.
139 *
140 * This test assumes that all callers of dev_queue_xmit() explicitely
141 * disable bh. Knowing this, it is possible to detect softirq based
142 * calls by looking at the number of nested bh disable calls because
143 * softirqs always disables bh.
144 */
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700145 if (in_serving_softirq()) {
Herbert Xuf8451722010-05-24 00:12:34 -0700146 /* If there is an sk_classid we'll use that. */
147 if (!skb->sk)
148 return -1;
149 classid = skb->sk->sk_classid;
150 }
Thomas Graff4009232008-11-07 22:56:00 -0800151
Paul Menagee65fcfd2009-05-26 20:47:02 -0700152 if (!classid)
153 return -1;
154
155 if (!tcf_em_tree_match(skb, &head->ematches, NULL))
156 return -1;
157
158 res->classid = classid;
159 res->class = 0;
160 return tcf_exts_exec(skb, &head->exts, res);
Thomas Graff4009232008-11-07 22:56:00 -0800161}
162
163static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
164{
165 return 0UL;
166}
167
168static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
169{
170}
171
172static int cls_cgroup_init(struct tcf_proto *tp)
173{
174 return 0;
175}
176
177static const struct tcf_ext_map cgroup_ext_map = {
178 .action = TCA_CGROUP_ACT,
179 .police = TCA_CGROUP_POLICE,
180};
181
182static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
183 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
184};
185
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000186static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600187 struct tcf_proto *tp, unsigned long base,
Thomas Graff4009232008-11-07 22:56:00 -0800188 u32 handle, struct nlattr **tca,
189 unsigned long *arg)
190{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000191 struct nlattr *tb[TCA_CGROUP_MAX + 1];
Thomas Graff4009232008-11-07 22:56:00 -0800192 struct cls_cgroup_head *head = tp->root;
193 struct tcf_ematch_tree t;
194 struct tcf_exts e;
195 int err;
196
Minoru Usui52ea3a52009-06-09 04:03:09 -0700197 if (!tca[TCA_OPTIONS])
198 return -EINVAL;
199
Thomas Graff4009232008-11-07 22:56:00 -0800200 if (head == NULL) {
201 if (!handle)
202 return -EINVAL;
203
204 head = kzalloc(sizeof(*head), GFP_KERNEL);
205 if (head == NULL)
206 return -ENOBUFS;
207
208 head->handle = handle;
209
210 tcf_tree_lock(tp);
211 tp->root = head;
212 tcf_tree_unlock(tp);
213 }
214
215 if (handle != head->handle)
216 return -ENOENT;
217
218 err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
219 cgroup_policy);
220 if (err < 0)
221 return err;
222
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000223 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e,
224 &cgroup_ext_map);
Thomas Graff4009232008-11-07 22:56:00 -0800225 if (err < 0)
226 return err;
227
228 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
229 if (err < 0)
230 return err;
231
232 tcf_exts_change(tp, &head->exts, &e);
233 tcf_em_tree_change(tp, &head->ematches, &t);
234
235 return 0;
236}
237
238static void cls_cgroup_destroy(struct tcf_proto *tp)
239{
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000240 struct cls_cgroup_head *head = tp->root;
Thomas Graff4009232008-11-07 22:56:00 -0800241
242 if (head) {
243 tcf_exts_destroy(tp, &head->exts);
244 tcf_em_tree_destroy(tp, &head->ematches);
245 kfree(head);
246 }
247}
248
249static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
250{
251 return -EOPNOTSUPP;
252}
253
254static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
255{
256 struct cls_cgroup_head *head = tp->root;
257
258 if (arg->count < arg->skip)
259 goto skip;
260
261 if (arg->fn(tp, (unsigned long) head, arg) < 0) {
262 arg->stop = 1;
263 return;
264 }
265skip:
266 arg->count++;
267}
268
269static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
270 struct sk_buff *skb, struct tcmsg *t)
271{
272 struct cls_cgroup_head *head = tp->root;
273 unsigned char *b = skb_tail_pointer(skb);
274 struct nlattr *nest;
275
276 t->tcm_handle = head->handle;
277
278 nest = nla_nest_start(skb, TCA_OPTIONS);
279 if (nest == NULL)
280 goto nla_put_failure;
281
282 if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
283 tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
284 goto nla_put_failure;
285
286 nla_nest_end(skb, nest);
287
288 if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
289 goto nla_put_failure;
290
291 return skb->len;
292
293nla_put_failure:
294 nlmsg_trim(skb, b);
295 return -1;
296}
297
298static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
299 .kind = "cgroup",
300 .init = cls_cgroup_init,
301 .change = cls_cgroup_change,
302 .classify = cls_cgroup_classify,
303 .destroy = cls_cgroup_destroy,
304 .get = cls_cgroup_get,
305 .put = cls_cgroup_put,
306 .delete = cls_cgroup_delete,
307 .walk = cls_cgroup_walk,
308 .dump = cls_cgroup_dump,
309 .owner = THIS_MODULE,
310};
311
312static int __init init_cgroup_cls(void)
313{
Herbert Xuf8451722010-05-24 00:12:34 -0700314 int ret;
315
Ben Blum8e039d82010-03-23 05:24:03 +0000316 ret = cgroup_load_subsys(&net_cls_subsys);
317 if (ret)
Herbert Xuf8451722010-05-24 00:12:34 -0700318 goto out;
319
Herbert Xuf8451722010-05-24 00:12:34 -0700320 ret = register_tcf_proto_ops(&cls_cgroup_ops);
321 if (ret)
322 cgroup_unload_subsys(&net_cls_subsys);
323
324out:
Ben Blum8e039d82010-03-23 05:24:03 +0000325 return ret;
Thomas Graff4009232008-11-07 22:56:00 -0800326}
327
328static void __exit exit_cgroup_cls(void)
329{
330 unregister_tcf_proto_ops(&cls_cgroup_ops);
Herbert Xuf8451722010-05-24 00:12:34 -0700331
Ben Blum8e039d82010-03-23 05:24:03 +0000332 cgroup_unload_subsys(&net_cls_subsys);
Thomas Graff4009232008-11-07 22:56:00 -0800333}
334
335module_init(init_cgroup_cls);
336module_exit(exit_cgroup_cls);
337MODULE_LICENSE("GPL");