cls_cgroup.c revision dc7f9f6e8838556f226c2ebd1da7bb305cb25654
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
258e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
268e039d84b323c4503c4d56863faa47c783660826Ben Blum					       struct cgroup *cgrp);
278e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp);
288e039d84b323c4503c4d56863faa47c783660826Ben Blumstatic int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
298e039d84b323c4503c4d56863faa47c783660826Ben Blum
308e039d84b323c4503c4d56863faa47c783660826Ben Blumstruct cgroup_subsys net_cls_subsys = {
318e039d84b323c4503c4d56863faa47c783660826Ben Blum	.name		= "net_cls",
328e039d84b323c4503c4d56863faa47c783660826Ben Blum	.create		= cgrp_create,
338e039d84b323c4503c4d56863faa47c783660826Ben Blum	.destroy	= cgrp_destroy,
348e039d84b323c4503c4d56863faa47c783660826Ben Blum	.populate	= cgrp_populate,
358e039d84b323c4503c4d56863faa47c783660826Ben Blum#ifdef CONFIG_NET_CLS_CGROUP
368e039d84b323c4503c4d56863faa47c783660826Ben Blum	.subsys_id	= net_cls_subsys_id,
378e039d84b323c4503c4d56863faa47c783660826Ben Blum#endif
388e039d84b323c4503c4d56863faa47c783660826Ben Blum	.module		= THIS_MODULE,
398e039d84b323c4503c4d56863faa47c783660826Ben Blum};
408e039d84b323c4503c4d56863faa47c783660826Ben Blum
418e039d84b323c4503c4d56863faa47c783660826Ben Blum
428e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
43f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
448e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
458e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
468e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan}
478e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan
488e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefanstatic inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
498e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan{
508e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return container_of(task_subsys_state(p, net_cls_subsys_id),
518e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan			    struct cgroup_cls_state, css);
52f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
53f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
54f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
55f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf						 struct cgroup *cgrp)
56f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
57f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cgroup_cls_state *cs;
58f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
59cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
60cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	if (!cs)
61f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return ERR_PTR(-ENOMEM);
62f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
63f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (cgrp->parent)
648e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan		cs->classid = cgrp_cls_state(cgrp->parent)->classid;
65f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
66f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return &cs->css;
67f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
68f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
69f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
70f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
718e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	kfree(cgrp_cls_state(cgrp));
72f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
73f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
74f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
75f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
768e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	return cgrp_cls_state(cgrp)->classid;
77f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
78f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
79f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
80f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
818e8ba85417366afd2361e315c6ba5949d3eff56fLi Zefan	cgrp_cls_state(cgrp)->classid = (u32) value;
82f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
83f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
84f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
85f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct cftype ss_files[] = {
86f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	{
87f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.name = "classid",
88f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.read_u64 = read_classid,
89f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		.write_u64 = write_classid,
90f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	},
91f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
92f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
93f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
94f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
95f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
96f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
97f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
98cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazetstruct cls_cgroup_head {
99f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	u32			handle;
100f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts		exts;
101f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree	ematches;
102f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
103f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
104dc7f9f6e8838556f226c2ebd1da7bb305cb25654Eric Dumazetstatic int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
105f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       struct tcf_result *res)
106f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
107f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
108e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	u32 classid;
109f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
110f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_lock();
111f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	classid = task_cls_state(current)->classid;
112f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	rcu_read_unlock();
113f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
114f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	/*
115f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * Due to the nature of the classifier it is required to ignore all
116f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * packets originating from softirq context as accessing `current'
117f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * would lead to false results.
118f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 *
119f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * This test assumes that all callers of dev_queue_xmit() explicitely
120f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * disable bh. Knowing this, it is possible to detect softirq based
121f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * calls by looking at the number of nested bh disable calls because
122f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 * softirqs always disables bh.
123f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	 */
12475e1056f5c57050415b64cb761a3acc35d91f013Venkatesh Pallipadi	if (in_serving_softirq()) {
125f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		/* If there is an sk_classid we'll use that. */
126f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		if (!skb->sk)
127f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu			return -1;
128f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		classid = skb->sk->sk_classid;
129f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	}
130f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
131e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!classid)
132e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
133e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
134e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
135e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage		return -1;
136e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage
137e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->classid = classid;
138e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	res->class = 0;
139e65fcfd63a9a62baa5708484ff8edbe56eb3e7ecPaul Menage	return tcf_exts_exec(skb, &head->exts, res);
140f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
141f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
142f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
143f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
144f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0UL;
145f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
146f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
147f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
148f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
149f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
150f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
151f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_init(struct tcf_proto *tp)
152f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
153f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
154f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
155f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
156f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct tcf_ext_map cgroup_ext_map = {
157f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.action = TCA_CGROUP_ACT,
158f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.police = TCA_CGROUP_POLICE,
159f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
160f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
161f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
162f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
163f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
164f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
165f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_change(struct tcf_proto *tp, unsigned long base,
166f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     u32 handle, struct nlattr **tca,
167f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			     unsigned long *arg)
168f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
169cc7ec456f82da7f89a5b376e613b3ac4311b3e9aEric Dumazet	struct nlattr *tb[TCA_CGROUP_MAX + 1];
170f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
171f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_ematch_tree t;
172f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct tcf_exts e;
173f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	int err;
174f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
17552ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui	if (!tca[TCA_OPTIONS])
17652ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui		return -EINVAL;
17752ea3a56a3268bc2a5a7c75e98c81463004e38efMinoru Usui
178f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head == NULL) {
179f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (!handle)
180f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -EINVAL;
181f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
182f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head = kzalloc(sizeof(*head), GFP_KERNEL);
183f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		if (head == NULL)
184f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			return -ENOBUFS;
185f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
186f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		head->handle = handle;
187f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
188f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_lock(tp);
189f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tp->root = head;
190f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_tree_unlock(tp);
191f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
192f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
193f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (handle != head->handle)
194f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return -ENOENT;
195f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
196f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
197f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			       cgroup_policy);
198f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
199f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
200f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
201f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
202f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
203f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
204f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
205f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
206f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (err < 0)
207f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return err;
208f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
209f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_exts_change(tp, &head->exts, &e);
210f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	tcf_em_tree_change(tp, &head->ematches, &t);
211f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
212f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return 0;
213f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
214f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
215f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_destroy(struct tcf_proto *tp)
216f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
21747a1a1d4be2910b13a8e90f75c17e253c39531ffPatrick McHardy	struct cls_cgroup_head *head = tp->root;
218f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
219f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (head) {
220f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_exts_destroy(tp, &head->exts);
221f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		tcf_em_tree_destroy(tp, &head->ematches);
222f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		kfree(head);
223f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
224f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
225f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
226f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
227f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
228f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -EOPNOTSUPP;
229f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
230f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
231f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
232f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
233f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
234f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
235f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->count < arg->skip)
236f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto skip;
237f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
238f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
239f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		arg->stop = 1;
240f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		return;
241f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	}
242f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafskip:
243f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	arg->count++;
244f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
245f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
246f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
247f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf			   struct sk_buff *skb, struct tcmsg *t)
248f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
249f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct cls_cgroup_head *head = tp->root;
250f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unsigned char *b = skb_tail_pointer(skb);
251f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	struct nlattr *nest;
252f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
253f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	t->tcm_handle = head->handle;
254f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
255f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nest = nla_nest_start(skb, TCA_OPTIONS);
256f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (nest == NULL)
257f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
258f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
259f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
260f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
261f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
262f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
263f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nla_nest_end(skb, nest);
264f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
265f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
266f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf		goto nla_put_failure;
267f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
268f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return skb->len;
269f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
270f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafnla_put_failure:
271f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	nlmsg_trim(skb, b);
272f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	return -1;
273f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
274f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
275f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
276f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.kind		=	"cgroup",
277f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.init		=	cls_cgroup_init,
278f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.change		=	cls_cgroup_change,
279f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.classify	=	cls_cgroup_classify,
280f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.destroy	=	cls_cgroup_destroy,
281f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.get		=	cls_cgroup_get,
282f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.put		=	cls_cgroup_put,
283f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.delete		=	cls_cgroup_delete,
284f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.walk		=	cls_cgroup_walk,
285f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.dump		=	cls_cgroup_dump,
286f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	.owner		=	THIS_MODULE,
287f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf};
288f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
289f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic int __init init_cgroup_cls(void)
290f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
291f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	int ret;
292f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
2938e039d84b323c4503c4d56863faa47c783660826Ben Blum	ret = cgroup_load_subsys(&net_cls_subsys);
2948e039d84b323c4503c4d56863faa47c783660826Ben Blum	if (ret)
295f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		goto out;
296f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
297f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
298f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	/* We can't use rcu_assign_pointer because this is an int. */
299f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	smp_wmb();
300f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = net_cls_subsys.subsys_id;
301f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
302f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
303f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	ret = register_tcf_proto_ops(&cls_cgroup_ops);
304f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	if (ret)
305f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu		cgroup_unload_subsys(&net_cls_subsys);
306f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
307f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xuout:
3088e039d84b323c4503c4d56863faa47c783660826Ben Blum	return ret;
309f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
310f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
311f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafstatic void __exit exit_cgroup_cls(void)
312f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf{
313f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf	unregister_tcf_proto_ops(&cls_cgroup_ops);
314f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
315f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#ifndef CONFIG_NET_CLS_CGROUP
316f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	net_cls_subsys_id = -1;
317f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu	synchronize_rcu();
318f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu#endif
319f845172531fb7410c7fb7780b1a6e51ee6df7d52Herbert Xu
3208e039d84b323c4503c4d56863faa47c783660826Ben Blum	cgroup_unload_subsys(&net_cls_subsys);
321f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf}
322f400923735ecbb67cbe4a3606c9479f694754f51Thomas Graf
323f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_init(init_cgroup_cls);
324f400923735ecbb67cbe4a3606c9479f694754f51Thomas Grafmodule_exit(exit_cgroup_cls);
325f400923735ecbb67cbe4a3606c9479f694754f51Thomas GrafMODULE_LICENSE("GPL");
326