Blame view

net/sched/cls_cgroup.c 5.13 KB
f40092373   Thomas Graf   pkt_sched: Contro...
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>
5a0e3ad6a   Tejun Heo   include cleanup: ...
13
  #include <linux/slab.h>
f40092373   Thomas Graf   pkt_sched: Contro...
14
  #include <linux/skbuff.h>
f84517253   Herbert Xu   cls_cgroup: Store...
15
  #include <linux/rcupdate.h>
f40092373   Thomas Graf   pkt_sched: Contro...
16
17
  #include <net/rtnetlink.h>
  #include <net/pkt_cls.h>
f84517253   Herbert Xu   cls_cgroup: Store...
18
19
  #include <net/sock.h>
  #include <net/cls_cgroup.h>
f40092373   Thomas Graf   pkt_sched: Contro...
20

cc7ec456f   Eric Dumazet   net_sched: cleanups
21
  struct cls_cgroup_head {
f40092373   Thomas Graf   pkt_sched: Contro...
22
23
24
  	u32			handle;
  	struct tcf_exts		exts;
  	struct tcf_ematch_tree	ematches;
952313bd6   John Fastabend   net: sched: cls_c...
25
  	struct tcf_proto	*tp;
b1b5b04fd   Cong Wang   net_sched: use tc...
26
27
28
29
  	union {
  		struct work_struct	work;
  		struct rcu_head		rcu;
  	};
f40092373   Thomas Graf   pkt_sched: Contro...
30
  };
dc7f9f6e8   Eric Dumazet   net: sched: const...
31
  static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
f40092373   Thomas Graf   pkt_sched: Contro...
32
33
  			       struct tcf_result *res)
  {
952313bd6   John Fastabend   net: sched: cls_c...
34
  	struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
b87a173e2   Daniel Borkmann   cls_cgroup: facto...
35
  	u32 classid = task_get_classid(skb);
f40092373   Thomas Graf   pkt_sched: Contro...
36

e65fcfd63   Paul Menage   cls_cgroup: read ...
37
38
  	if (!classid)
  		return -1;
e65fcfd63   Paul Menage   cls_cgroup: read ...
39
40
41
42
43
  	if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  		return -1;
  
  	res->classid = classid;
  	res->class = 0;
b87a173e2   Daniel Borkmann   cls_cgroup: facto...
44

e65fcfd63   Paul Menage   cls_cgroup: read ...
45
  	return tcf_exts_exec(skb, &head->exts, res);
f40092373   Thomas Graf   pkt_sched: Contro...
46
  }
8113c0956   WANG Cong   net_sched: use vo...
47
  static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
f40092373   Thomas Graf   pkt_sched: Contro...
48
  {
8113c0956   WANG Cong   net_sched: use vo...
49
  	return NULL;
f40092373   Thomas Graf   pkt_sched: Contro...
50
  }
f40092373   Thomas Graf   pkt_sched: Contro...
51
52
53
54
  static int cls_cgroup_init(struct tcf_proto *tp)
  {
  	return 0;
  }
f40092373   Thomas Graf   pkt_sched: Contro...
55
56
57
  static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
  };
ed1481681   Cong Wang   cls_cgroup: use t...
58
59
60
61
62
63
64
  static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
  {
  	tcf_exts_destroy(&head->exts);
  	tcf_em_tree_destroy(&head->ematches);
  	tcf_exts_put_net(&head->exts);
  	kfree(head);
  }
b1b5b04fd   Cong Wang   net_sched: use tc...
65
66
67
68
69
70
  static void cls_cgroup_destroy_work(struct work_struct *work)
  {
  	struct cls_cgroup_head *head = container_of(work,
  						    struct cls_cgroup_head,
  						    work);
  	rtnl_lock();
ed1481681   Cong Wang   cls_cgroup: use t...
71
  	__cls_cgroup_destroy(head);
b1b5b04fd   Cong Wang   net_sched: use tc...
72
73
  	rtnl_unlock();
  }
952313bd6   John Fastabend   net: sched: cls_c...
74
75
76
77
78
  static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  {
  	struct cls_cgroup_head *head = container_of(root,
  						    struct cls_cgroup_head,
  						    rcu);
b1b5b04fd   Cong Wang   net_sched: use tc...
79
80
  	INIT_WORK(&head->work, cls_cgroup_destroy_work);
  	tcf_queue_work(&head->work);
952313bd6   John Fastabend   net: sched: cls_c...
81
  }
c1b52739e   Benjamin LaHaise   pkt_sched: namesp...
82
  static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
af4c6641f   Eric W. Biederman   net sched: Pass t...
83
  			     struct tcf_proto *tp, unsigned long base,
f40092373   Thomas Graf   pkt_sched: Contro...
84
  			     u32 handle, struct nlattr **tca,
8113c0956   WANG Cong   net_sched: use vo...
85
  			     void **arg, bool ovr)
f40092373   Thomas Graf   pkt_sched: Contro...
86
  {
cc7ec456f   Eric Dumazet   net_sched: cleanups
87
  	struct nlattr *tb[TCA_CGROUP_MAX + 1];
952313bd6   John Fastabend   net: sched: cls_c...
88
89
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  	struct cls_cgroup_head *new;
f40092373   Thomas Graf   pkt_sched: Contro...
90
  	int err;
52ea3a56a   Minoru Usui   cls_cgroup: Fix o...
91
92
  	if (!tca[TCA_OPTIONS])
  		return -EINVAL;
952313bd6   John Fastabend   net: sched: cls_c...
93
94
  	if (!head && !handle)
  		return -EINVAL;
f40092373   Thomas Graf   pkt_sched: Contro...
95

952313bd6   John Fastabend   net: sched: cls_c...
96
97
  	if (head && handle != head->handle)
  		return -ENOENT;
f40092373   Thomas Graf   pkt_sched: Contro...
98

952313bd6   John Fastabend   net: sched: cls_c...
99
100
101
  	new = kzalloc(sizeof(*head), GFP_KERNEL);
  	if (!new)
  		return -ENOBUFS;
f40092373   Thomas Graf   pkt_sched: Contro...
102

b9a24bb76   WANG Cong   net_sched: proper...
103
104
105
  	err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  	if (err < 0)
  		goto errout;
18b5427ae   Jiri Pirko   net_sched: cls_cg...
106
  	new->handle = handle;
952313bd6   John Fastabend   net: sched: cls_c...
107
  	new->tp = tp;
f40092373   Thomas Graf   pkt_sched: Contro...
108
  	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
fceb6435e   Johannes Berg   netlink: pass ext...
109
  			       cgroup_policy, NULL);
f40092373   Thomas Graf   pkt_sched: Contro...
110
  	if (err < 0)
d14cbfc88   John Fastabend   net: sched: cls_c...
111
  		goto errout;
f40092373   Thomas Graf   pkt_sched: Contro...
112

8cc625138   Jiri Pirko   net: sched: cls_c...
113
  	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, ovr);
f40092373   Thomas Graf   pkt_sched: Contro...
114
  	if (err < 0)
d14cbfc88   John Fastabend   net: sched: cls_c...
115
  		goto errout;
f40092373   Thomas Graf   pkt_sched: Contro...
116

4ebc1e3cf   Jiri Pirko   net: sched: remov...
117
  	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
8cc625138   Jiri Pirko   net: sched: cls_c...
118
  	if (err < 0)
d14cbfc88   John Fastabend   net: sched: cls_c...
119
  		goto errout;
f40092373   Thomas Graf   pkt_sched: Contro...
120

952313bd6   John Fastabend   net: sched: cls_c...
121
  	rcu_assign_pointer(tp->root, new);
ed1481681   Cong Wang   cls_cgroup: use t...
122
123
  	if (head) {
  		tcf_exts_get_net(&head->exts);
952313bd6   John Fastabend   net: sched: cls_c...
124
  		call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
ed1481681   Cong Wang   cls_cgroup: use t...
125
  	}
f40092373   Thomas Graf   pkt_sched: Contro...
126
  	return 0;
d14cbfc88   John Fastabend   net: sched: cls_c...
127
  errout:
b9a24bb76   WANG Cong   net_sched: proper...
128
  	tcf_exts_destroy(&new->exts);
d14cbfc88   John Fastabend   net: sched: cls_c...
129
130
  	kfree(new);
  	return err;
f40092373   Thomas Graf   pkt_sched: Contro...
131
  }
763dbf632   WANG Cong   net_sched: move t...
132
  static void cls_cgroup_destroy(struct tcf_proto *tp)
f40092373   Thomas Graf   pkt_sched: Contro...
133
  {
952313bd6   John Fastabend   net: sched: cls_c...
134
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
135

d93637741   Daniel Borkmann   net, sched: respe...
136
  	/* Head can still be NULL due to cls_cgroup_init(). */
ed1481681   Cong Wang   cls_cgroup: use t...
137
138
139
140
141
142
  	if (head) {
  		if (tcf_exts_get_net(&head->exts))
  			call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  		else
  			__cls_cgroup_destroy(head);
  	}
f40092373   Thomas Graf   pkt_sched: Contro...
143
  }
8113c0956   WANG Cong   net_sched: use vo...
144
  static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last)
f40092373   Thomas Graf   pkt_sched: Contro...
145
146
147
148
149
150
  {
  	return -EOPNOTSUPP;
  }
  
  static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  {
952313bd6   John Fastabend   net: sched: cls_c...
151
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
152
153
154
  
  	if (arg->count < arg->skip)
  		goto skip;
8113c0956   WANG Cong   net_sched: use vo...
155
  	if (arg->fn(tp, head, arg) < 0) {
f40092373   Thomas Graf   pkt_sched: Contro...
156
157
158
159
160
161
  		arg->stop = 1;
  		return;
  	}
  skip:
  	arg->count++;
  }
8113c0956   WANG Cong   net_sched: use vo...
162
  static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
f40092373   Thomas Graf   pkt_sched: Contro...
163
164
  			   struct sk_buff *skb, struct tcmsg *t)
  {
952313bd6   John Fastabend   net: sched: cls_c...
165
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
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;
5da57f422   WANG Cong   net_sched: cls: r...
173
  	if (tcf_exts_dump(skb, &head->exts) < 0 ||
f40092373   Thomas Graf   pkt_sched: Contro...
174
175
176
177
  	    tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  		goto nla_put_failure;
  
  	nla_nest_end(skb, nest);
5da57f422   WANG Cong   net_sched: cls: r...
178
  	if (tcf_exts_dump_stats(skb, &head->exts) < 0)
f40092373   Thomas Graf   pkt_sched: Contro...
179
180
181
182
183
  		goto nla_put_failure;
  
  	return skb->len;
  
  nla_put_failure:
6ea3b446b   Jiri Pirko   net: sched: cls: ...
184
  	nla_nest_cancel(skb, nest);
f40092373   Thomas Graf   pkt_sched: Contro...
185
186
187
188
189
190
191
192
193
194
  	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,
f40092373   Thomas Graf   pkt_sched: Contro...
195
196
197
198
199
200
201
202
  	.delete		=	cls_cgroup_delete,
  	.walk		=	cls_cgroup_walk,
  	.dump		=	cls_cgroup_dump,
  	.owner		=	THIS_MODULE,
  };
  
  static int __init init_cgroup_cls(void)
  {
fe1217c4f   Daniel Borkmann   net: net_cls: mov...
203
  	return register_tcf_proto_ops(&cls_cgroup_ops);
f40092373   Thomas Graf   pkt_sched: Contro...
204
205
206
207
208
209
210
211
212
213
  }
  
  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");