cls_cgroup.c 4.8 KB
Newer Older
T
Thomas Graf 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * net/sched/cls_cgroup.c	Control Group Classifier
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Thomas Graf <tgraf@suug.ch>
 */

#include <linux/module.h>
13
#include <linux/slab.h>
T
Thomas Graf 已提交
14
#include <linux/skbuff.h>
15
#include <linux/rcupdate.h>
T
Thomas Graf 已提交
16 17
#include <net/rtnetlink.h>
#include <net/pkt_cls.h>
18 19
#include <net/sock.h>
#include <net/cls_cgroup.h>
T
Thomas Graf 已提交
20

E
Eric Dumazet 已提交
21
struct cls_cgroup_head {
T
Thomas Graf 已提交
22 23 24
	u32			handle;
	struct tcf_exts		exts;
	struct tcf_ematch_tree	ematches;
J
John Fastabend 已提交
25 26
	struct tcf_proto	*tp;
	struct rcu_head		rcu;
T
Thomas Graf 已提交
27 28
};

29
static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
T
Thomas Graf 已提交
30 31
			       struct tcf_result *res)
{
J
John Fastabend 已提交
32
	struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
33
	u32 classid = task_get_classid(skb);
T
Thomas Graf 已提交
34

35 36 37 38 39 40 41
	if (!classid)
		return -1;
	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
		return -1;

	res->classid = classid;
	res->class = 0;
42

43
	return tcf_exts_exec(skb, &head->exts, res);
T
Thomas Graf 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
{
	return 0UL;
}

static int cls_cgroup_init(struct tcf_proto *tp)
{
	return 0;
}

static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
};

J
John Fastabend 已提交
60 61 62 63 64 65
static void cls_cgroup_destroy_rcu(struct rcu_head *root)
{
	struct cls_cgroup_head *head = container_of(root,
						    struct cls_cgroup_head,
						    rcu);

66
	tcf_exts_destroy(&head->exts);
67
	tcf_em_tree_destroy(&head->ematches);
J
John Fastabend 已提交
68 69 70
	kfree(head);
}

71
static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
72
			     struct tcf_proto *tp, unsigned long base,
T
Thomas Graf 已提交
73
			     u32 handle, struct nlattr **tca,
74
			     unsigned long *arg, bool ovr)
T
Thomas Graf 已提交
75
{
E
Eric Dumazet 已提交
76
	struct nlattr *tb[TCA_CGROUP_MAX + 1];
J
John Fastabend 已提交
77 78
	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
	struct cls_cgroup_head *new;
T
Thomas Graf 已提交
79 80 81 82
	struct tcf_ematch_tree t;
	struct tcf_exts e;
	int err;

83 84 85
	if (!tca[TCA_OPTIONS])
		return -EINVAL;

J
John Fastabend 已提交
86 87
	if (!head && !handle)
		return -EINVAL;
T
Thomas Graf 已提交
88

J
John Fastabend 已提交
89 90
	if (head && handle != head->handle)
		return -ENOENT;
T
Thomas Graf 已提交
91

J
John Fastabend 已提交
92 93 94
	new = kzalloc(sizeof(*head), GFP_KERNEL);
	if (!new)
		return -ENOBUFS;
T
Thomas Graf 已提交
95

96
	tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
97
	new->handle = handle;
J
John Fastabend 已提交
98
	new->tp = tp;
T
Thomas Graf 已提交
99 100 101
	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
			       cgroup_policy);
	if (err < 0)
102
		goto errout;
T
Thomas Graf 已提交
103

104
	tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
105
	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
T
Thomas Graf 已提交
106
	if (err < 0)
107
		goto errout;
T
Thomas Graf 已提交
108 109

	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
110
	if (err < 0) {
111
		tcf_exts_destroy(&e);
112 113
		goto errout;
	}
T
Thomas Graf 已提交
114

J
John Fastabend 已提交
115 116
	tcf_exts_change(tp, &new->exts, &e);
	tcf_em_tree_change(tp, &new->ematches, &t);
T
Thomas Graf 已提交
117

J
John Fastabend 已提交
118 119 120
	rcu_assign_pointer(tp->root, new);
	if (head)
		call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
T
Thomas Graf 已提交
121
	return 0;
122 123 124
errout:
	kfree(new);
	return err;
T
Thomas Graf 已提交
125 126
}

127
static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
T
Thomas Graf 已提交
128
{
J
John Fastabend 已提交
129
	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
T
Thomas Graf 已提交
130

131 132 133
	if (!force)
		return false;

T
Thomas Graf 已提交
134
	if (head) {
J
John Fastabend 已提交
135
		RCU_INIT_POINTER(tp->root, NULL);
136
		call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
T
Thomas Graf 已提交
137
	}
138
	return true;
T
Thomas Graf 已提交
139 140 141 142 143 144 145 146 147
}

static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
{
	return -EOPNOTSUPP;
}

static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
{
J
John Fastabend 已提交
148
	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
T
Thomas Graf 已提交
149 150 151 152 153 154 155 156 157 158 159 160

	if (arg->count < arg->skip)
		goto skip;

	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
		arg->stop = 1;
		return;
	}
skip:
	arg->count++;
}

161
static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
T
Thomas Graf 已提交
162 163
			   struct sk_buff *skb, struct tcmsg *t)
{
J
John Fastabend 已提交
164
	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
T
Thomas Graf 已提交
165 166 167 168 169 170 171 172
	struct nlattr *nest;

	t->tcm_handle = head->handle;

	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;

173
	if (tcf_exts_dump(skb, &head->exts) < 0 ||
T
Thomas Graf 已提交
174 175 176 177 178
	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
		goto nla_put_failure;

	nla_nest_end(skb, nest);

179
	if (tcf_exts_dump_stats(skb, &head->exts) < 0)
T
Thomas Graf 已提交
180 181 182 183 184
		goto nla_put_failure;

	return skb->len;

nla_put_failure:
185
	nla_nest_cancel(skb, nest);
T
Thomas Graf 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	return -1;
}

static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
	.kind		=	"cgroup",
	.init		=	cls_cgroup_init,
	.change		=	cls_cgroup_change,
	.classify	=	cls_cgroup_classify,
	.destroy	=	cls_cgroup_destroy,
	.get		=	cls_cgroup_get,
	.delete		=	cls_cgroup_delete,
	.walk		=	cls_cgroup_walk,
	.dump		=	cls_cgroup_dump,
	.owner		=	THIS_MODULE,
};

static int __init init_cgroup_cls(void)
{
204
	return register_tcf_proto_ops(&cls_cgroup_ops);
T
Thomas Graf 已提交
205 206 207 208 209 210 211 212 213 214
}

static void __exit exit_cgroup_cls(void)
{
	unregister_tcf_proto_ops(&cls_cgroup_ops);
}

module_init(init_cgroup_cls);
module_exit(exit_cgroup_cls);
MODULE_LICENSE("GPL");