cls_cgroup.c revision 4baf6e33251b37f111e21289f8ee71fe4cce236e
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/types.h>
15f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/string.h>
16f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/errno.h>
17f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/skbuff.h>
18f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <linux/cgroup.h>
19f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <linux/rcupdate.h>
20f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/rtnetlink.h>
21f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf#include <net/pkt_cls.h>
22f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/sock.h>
23f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#include <net/cls_cgroup.h>
24f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
258e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
26f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
278e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
288e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
298e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan}
308e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan
318e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
328e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan{
338e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(task_subsys_state(p, net_cls_subsys_id),
348e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
35f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
36f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
37761b3ef50e1c2649cffbfa67a4dcb2dcdb7982edLi Zefanstatic struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
38f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
39f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cgroup_cls_state *cs;
40f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
41cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
42cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	if (!cs)
43f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return ERR_PTR(-ENOMEM);
44f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
45f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (cgrp->parent)
468e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan		cs->classid = cgrp_cls_state(cgrp->parent)->classid;
47f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
48f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return &cs->css;
49f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
50f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
51761b3ef50e1c2649cffbfa67a4dcb2dcdb7982edLi Zefanstatic void cgrp_destroy(struct cgroup *cgrp)
52f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
538e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	kfree(cgrp_cls_state(cgrp));
54f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
55f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
56f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
57f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
588e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return cgrp_cls_state(cgrp)->classid;
59f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
60f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
61f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
62f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
638e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	cgrp_cls_state(cgrp)->classid = (u32) value;
64f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
65f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
66f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
67f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cftype ss_files[] = {
68f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	{
69f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.name = "classid",
70f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.read_u64 = read_classid,
71f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.write_u64 = write_classid,
72f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	},
734baf6e33251b37f111e21289f8ee71fe4cce236eTejun Heo	{ }	/* terminate */
74f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
75f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
76676f7c8f84d15e94065841529016da5ab92e901bTejun Heostruct cgroup_subsys net_cls_subsys = {
77676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.name		= "net_cls",
78676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.create		= cgrp_create,
79676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.destroy	= cgrp_destroy,
80676f7c8f84d15e94065841529016da5ab92e901bTejun Heo#ifdef CONFIG_NET_CLS_CGROUP
81676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.subsys_id	= net_cls_subsys_id,
82676f7c8f84d15e94065841529016da5ab92e901bTejun Heo#endif
834baf6e33251b37f111e21289f8ee71fe4cce236eTejun Heo	.base_cftypes	= ss_files,
84676f7c8f84d15e94065841529016da5ab92e901bTejun Heo	.module		= THIS_MODULE,
85676f7c8f84d15e94065841529016da5ab92e901bTejun Heo};
86676f7c8f84d15e94065841529016da5ab92e901bTejun Heo
87cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazetstruct cls_cgroup_head {
88f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	u32			handle;
89f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts		exts;
90f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree	ematches;
91f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
92f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
93dc7f9f6e8838556f226c2ebd1da7bb305cb25654Eric Dumazetstatic int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
94f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       struct tcf_result *res)
95f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
96f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
97e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	u32 classid;
98f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
99f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_lock();
100f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	classid = task_cls_state(current)->classid;
101f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_unlock();
102f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
103f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	/*
104f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * Due to the nature of the classifier it is required to ignore all
105f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * packets originating from softirq context as accessing `current'
106f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * would lead to false results.
107f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 *
108f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * This test assumes that all callers of dev_queue_xmit() explicitely
109f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * disable bh. Knowing this, it is possible to detect softirq based
110f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * calls by looking at the number of nested bh disable calls because
111f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * softirqs always disables bh.
112f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 */
11375e1056f5c57050415b64cb761a3acc35d91f013Venkatesh Pallipadi	if (in_serving_softirq()) {
114f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		/* If there is an sk_classid we'll use that. */
115f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		if (!skb->sk)
116f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu			return -1;
117f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		classid = skb->sk->sk_classid;
118f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	}
119f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
120e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!classid)
121e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
122e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
123e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
124e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
125e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
126e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->classid = classid;
127e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->class = 0;
128e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	return tcf_exts_exec(skb, &head->exts, res);
129f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
130f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
131f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
132f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
133f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0UL;
134f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
135f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
136f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
137f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
138f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
139f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
140f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_init(struct tcf_proto *tp)
141f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
142f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
143f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
144f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
145f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct tcf_ext_map cgroup_ext_map = {
146f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.action = TCA_CGROUP_ACT,
147f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.police = TCA_CGROUP_POLICE,
148f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
149f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
150f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
151f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
152f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
153f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
154f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_change(struct tcf_proto *tp, unsigned long base,
155f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     u32 handle, struct nlattr **tca,
156f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     unsigned long *arg)
157f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
158cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	struct nlattr *tb[TCA_CGROUP_MAX + 1];
159f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
160f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree t;
161f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts e;
162f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	int err;
163f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
16452ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui	if (!tca[TCA_OPTIONS])
16552ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui		return -EINVAL;
16652ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui
167f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head == NULL) {
168f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (!handle)
169f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -EINVAL;
170f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
171f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head = kzalloc(sizeof(*head), GFP_KERNEL);
172f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (head == NULL)
173f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -ENOBUFS;
174f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
175f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head->handle = handle;
176f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
177f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_lock(tp);
178f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tp->root = head;
179f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_unlock(tp);
180f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
181f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
182f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (handle != head->handle)
183f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return -ENOENT;
184f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
185f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
186f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       cgroup_policy);
187f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
188f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
189f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
190f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
191f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
192f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
193f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
194f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
195f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
196f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
197f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
198f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_exts_change(tp, &head->exts, &e);
199f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_em_tree_change(tp, &head->ematches, &t);
200f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
201f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
202f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
203f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
204f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_destroy(struct tcf_proto *tp)
205f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
20647a1a1d4be2910b13a8e90f75c17e253c39531ffPatrick McHardy	struct cls_cgroup_head *head = tp->root;
207f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
208f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head) {
209f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_exts_destroy(tp, &head->exts);
210f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_em_tree_destroy(tp, &head->ematches);
211f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		kfree(head);
212f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
213f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
214f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
215f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
216f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
217f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -EOPNOTSUPP;
218f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
219f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
220f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
221f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
222f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
223f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
224f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->count < arg->skip)
225f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto skip;
226f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
227f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
228f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		arg->stop = 1;
229f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return;
230f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
231f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafskip:
232f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	arg->count++;
233f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
234f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
235f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
236f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			   struct sk_buff *skb, struct tcmsg *t)
237f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
238f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
239f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unsigned char *b = skb_tail_pointer(skb);
240f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *nest;
241f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
242f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	t->tcm_handle = head->handle;
243f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
244f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nest = nla_nest_start(skb, TCA_OPTIONS);
245f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (nest == NULL)
246f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
247f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
248f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
249f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
250f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
251f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
252f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nla_nest_end(skb, nest);
253f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
254f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
255f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
256f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
257f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return skb->len;
258f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
259f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafnla_put_failure:
260f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nlmsg_trim(skb, b);
261f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -1;
262f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
263f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
264f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
265f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.kind		=	"cgroup",
266f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.init		=	cls_cgroup_init,
267f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.change		=	cls_cgroup_change,
268f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.classify	=	cls_cgroup_classify,
269f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.destroy	=	cls_cgroup_destroy,
270f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.get		=	cls_cgroup_get,
271f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.put		=	cls_cgroup_put,
272f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.delete		=	cls_cgroup_delete,
273f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.walk		=	cls_cgroup_walk,
274f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.dump		=	cls_cgroup_dump,
275f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.owner		=	THIS_MODULE,
276f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
277f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
278f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int __init init_cgroup_cls(void)
279f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
280f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	int ret;
281f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
2828e039d84b323c4503c4d56863faa47c783660826Ben Blum	ret = cgroup_load_subsys(&net_cls_subsys);
2838e039d84b323c4503c4d56863faa47c783660826Ben Blum	if (ret)
284f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		goto out;
285f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
286f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
287f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	/* We can't use rcu_assign_pointer because this is an int. */
288f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	smp_wmb();
289f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = net_cls_subsys.subsys_id;
290f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
291f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
292f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	ret = register_tcf_proto_ops(&cls_cgroup_ops);
293f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	if (ret)
294f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		cgroup_unload_subsys(&net_cls_subsys);
295f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
296f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xuout:
2978e039d84b323c4503c4d56863faa47c783660826Ben Blum	return ret;
298f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
299f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
300f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void __exit exit_cgroup_cls(void)
301f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
302f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unregister_tcf_proto_ops(&cls_cgroup_ops);
303f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
304f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
305f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = -1;
306f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	synchronize_rcu();
307f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
308f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
3098e039d84b323c4503c4d56863faa47c783660826Ben Blum	cgroup_unload_subsys(&net_cls_subsys);
310f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
311f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
312f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_init(init_cgroup_cls);
313f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_exit(exit_cgroup_cls);
314f400923735ecbb67cbe4a3606c9479f694754f51Thomas GrafMODULE_LICENSE("GPL");
315