cls_cgroup.c revision fe1217c4f3f7d7cbf8efdd8dd5fdc7204a1d65a8
1f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf/*
2f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf * net/sched/cls_cgroup.c	Control Group Classifier
3f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *
4f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		This program is free software; you can redistribute it and/or
5f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		modify it under the terms of the GNU General Public License
6f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		as published by the Free Software Foundation; either version
7f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *		2 of the License, or (at your option) any later version.
8f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf *
9f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf * Authors:	Thomas Graf <tgraf@suug.ch>
10f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf */
11f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
12f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/module.h>
135a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
14f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/skbuff.h>
15f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <linux/rcupdate.h>
16f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/rtnetlink.h>
17f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/pkt_cls.h>
18f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/sock.h>
19f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/cls_cgroup.h>
20f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
21cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazetstruct cls_cgroup_head {
22f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	u32			handle;
23f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts		exts;
24f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree	ematches;
25f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
26f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
27dc7f9f6e8838556f226c2ebd1da7bb305cb25654Eric Dumazetstatic int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       struct tcf_result *res)
29f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
30f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
31e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	u32 classid;
32f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
33f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_lock();
34f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	classid = task_cls_state(current)->classid;
35f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_unlock();
36f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
37f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	/*
38f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * Due to the nature of the classifier it is required to ignore all
39f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * packets originating from softirq context as accessing `current'
40f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * would lead to false results.
41f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 *
42f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * This test assumes that all callers of dev_queue_xmit() explicitely
43f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * disable bh. Knowing this, it is possible to detect softirq based
44f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * calls by looking at the number of nested bh disable calls because
45f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * softirqs always disables bh.
46f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 */
4775e1056f5c57050415b64cb761a3acc35d91f013Venkatesh Pallipadi	if (in_serving_softirq()) {
48f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		/* If there is an sk_classid we'll use that. */
49f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		if (!skb->sk)
50f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu			return -1;
51f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		classid = skb->sk->sk_classid;
52f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	}
53f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
54e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!classid)
55e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
56e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
57e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
58e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
59e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
60e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->classid = classid;
61e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->class = 0;
62e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	return tcf_exts_exec(skb, &head->exts, res);
63f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
64f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
65f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
66f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
67f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0UL;
68f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
69f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
70f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
71f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
72f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
73f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
74f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_init(struct tcf_proto *tp)
75f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
76f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
77f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
78f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
79f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct tcf_ext_map cgroup_ext_map = {
80f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.action = TCA_CGROUP_ACT,
81f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.police = TCA_CGROUP_POLICE,
82f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
83f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
84f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
85f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
86f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
87f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
88c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaisestatic int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
89af4c6641f5ad445fe6d0832da42406dbd9a37ce4Eric W. Biederman			     struct tcf_proto *tp, unsigned long base,
90f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     u32 handle, struct nlattr **tca,
91f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     unsigned long *arg)
92f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
93cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	struct nlattr *tb[TCA_CGROUP_MAX + 1];
94f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
95f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree t;
96f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts e;
97f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	int err;
98f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
9952ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui	if (!tca[TCA_OPTIONS])
10052ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui		return -EINVAL;
10152ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui
102f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head == NULL) {
103f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (!handle)
104f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -EINVAL;
105f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
106f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head = kzalloc(sizeof(*head), GFP_KERNEL);
107f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (head == NULL)
108f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -ENOBUFS;
109f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
110f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head->handle = handle;
111f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
112f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_lock(tp);
113f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tp->root = head;
114f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_unlock(tp);
115f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
116f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
117f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (handle != head->handle)
118f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return -ENOENT;
119f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
120f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
121f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       cgroup_policy);
122f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
123f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
124f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
125c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaise	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e,
126c1b52739e45f5969b208ebc377f52468280af11eBenjamin LaHaise				&cgroup_ext_map);
127f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
128f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
129f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
130f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
131f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
132f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
133f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
134f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_exts_change(tp, &head->exts, &e);
135f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_em_tree_change(tp, &head->ematches, &t);
136f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
137f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
138f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
139f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
140f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_destroy(struct tcf_proto *tp)
141f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
14247a1a1d4be2910b13a8e90f75c17e253c39531ffPatrick McHardy	struct cls_cgroup_head *head = tp->root;
143f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
144f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head) {
145f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_exts_destroy(tp, &head->exts);
146f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_em_tree_destroy(tp, &head->ematches);
147f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		kfree(head);
148f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
149f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
150f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
151f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
152f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
153f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -EOPNOTSUPP;
154f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
155f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
156f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
157f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
158f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
159f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
160f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->count < arg->skip)
161f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto skip;
162f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
163f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
164f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		arg->stop = 1;
165f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return;
166f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
167f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafskip:
168f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	arg->count++;
169f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
170f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
171f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
172f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			   struct sk_buff *skb, struct tcmsg *t)
173f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
174f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
175f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unsigned char *b = skb_tail_pointer(skb);
176f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *nest;
177f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
178f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	t->tcm_handle = head->handle;
179f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
180f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nest = nla_nest_start(skb, TCA_OPTIONS);
181f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (nest == NULL)
182f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
183f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
184f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
185f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
186f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
187f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
188f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nla_nest_end(skb, nest);
189f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
190f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
191f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
192f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
193f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return skb->len;
194f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
195f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafnla_put_failure:
196f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nlmsg_trim(skb, b);
197f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -1;
198f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
199f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
200f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
201f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.kind		=	"cgroup",
202f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.init		=	cls_cgroup_init,
203f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.change		=	cls_cgroup_change,
204f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.classify	=	cls_cgroup_classify,
205f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.destroy	=	cls_cgroup_destroy,
206f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.get		=	cls_cgroup_get,
207f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.put		=	cls_cgroup_put,
208f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.delete		=	cls_cgroup_delete,
209f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.walk		=	cls_cgroup_walk,
210f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.dump		=	cls_cgroup_dump,
211f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.owner		=	THIS_MODULE,
212f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
213f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
214f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int __init init_cgroup_cls(void)
215f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
216fe1217c4f3f7d7cbf8efdd8dd5fdc7204a1d65a8Daniel Borkmann	return register_tcf_proto_ops(&cls_cgroup_ops);
217f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
218f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
219f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void __exit exit_cgroup_cls(void)
220f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
221f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unregister_tcf_proto_ops(&cls_cgroup_ops);
222f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
223f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
224f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_init(init_cgroup_cls);
225f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_exit(exit_cgroup_cls);
226f400923735ecbb67cbe4a3606c9479f694754f51Thomas GrafMODULE_LICENSE("GPL");
227