Blame view

net/sched/cls_cgroup.c 4.91 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
26
  	struct tcf_proto	*tp;
  	struct rcu_head		rcu;
f40092373   Thomas Graf   pkt_sched: Contro...
27
  };
dc7f9f6e8   Eric Dumazet   net: sched: const...
28
  static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
f40092373   Thomas Graf   pkt_sched: Contro...
29
30
  			       struct tcf_result *res)
  {
952313bd6   John Fastabend   net: sched: cls_c...
31
  	struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
b87a173e2   Daniel Borkmann   cls_cgroup: facto...
32
  	u32 classid = task_get_classid(skb);
f40092373   Thomas Graf   pkt_sched: Contro...
33

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

e65fcfd63   Paul Menage   cls_cgroup: read ...
42
  	return tcf_exts_exec(skb, &head->exts, res);
f40092373   Thomas Graf   pkt_sched: Contro...
43
44
45
46
47
48
  }
  
  static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  {
  	return 0UL;
  }
f40092373   Thomas Graf   pkt_sched: Contro...
49
50
51
52
  static int cls_cgroup_init(struct tcf_proto *tp)
  {
  	return 0;
  }
f40092373   Thomas Graf   pkt_sched: Contro...
53
54
55
  static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  	[TCA_CGROUP_EMATCHES]	= { .type = NLA_NESTED },
  };
952313bd6   John Fastabend   net: sched: cls_c...
56
57
58
59
60
  static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  {
  	struct cls_cgroup_head *head = container_of(root,
  						    struct cls_cgroup_head,
  						    rcu);
18d0264f6   WANG Cong   net_sched: remove...
61
  	tcf_exts_destroy(&head->exts);
82a470f11   John Fastabend   net: sched: remov...
62
  	tcf_em_tree_destroy(&head->ematches);
952313bd6   John Fastabend   net: sched: cls_c...
63
64
  	kfree(head);
  }
c1b52739e   Benjamin LaHaise   pkt_sched: namesp...
65
  static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
af4c6641f   Eric W. Biederman   net sched: Pass t...
66
  			     struct tcf_proto *tp, unsigned long base,
f40092373   Thomas Graf   pkt_sched: Contro...
67
  			     u32 handle, struct nlattr **tca,
2f7ef2f87   Cong Wang   sched, cls: check...
68
  			     unsigned long *arg, bool ovr)
f40092373   Thomas Graf   pkt_sched: Contro...
69
  {
cc7ec456f   Eric Dumazet   net_sched: cleanups
70
  	struct nlattr *tb[TCA_CGROUP_MAX + 1];
952313bd6   John Fastabend   net: sched: cls_c...
71
72
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  	struct cls_cgroup_head *new;
f40092373   Thomas Graf   pkt_sched: Contro...
73
74
75
  	struct tcf_ematch_tree t;
  	struct tcf_exts e;
  	int err;
52ea3a56a   Minoru Usui   cls_cgroup: Fix o...
76
77
  	if (!tca[TCA_OPTIONS])
  		return -EINVAL;
952313bd6   John Fastabend   net: sched: cls_c...
78
79
  	if (!head && !handle)
  		return -EINVAL;
f40092373   Thomas Graf   pkt_sched: Contro...
80

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

952313bd6   John Fastabend   net: sched: cls_c...
84
85
86
  	new = kzalloc(sizeof(*head), GFP_KERNEL);
  	if (!new)
  		return -ENOBUFS;
f40092373   Thomas Graf   pkt_sched: Contro...
87

b9a24bb76   WANG Cong   net_sched: proper...
88
89
90
  	err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  	if (err < 0)
  		goto errout;
18b5427ae   Jiri Pirko   net_sched: cls_cg...
91
  	new->handle = handle;
952313bd6   John Fastabend   net: sched: cls_c...
92
  	new->tp = tp;
f40092373   Thomas Graf   pkt_sched: Contro...
93
94
95
  	err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  			       cgroup_policy);
  	if (err < 0)
d14cbfc88   John Fastabend   net: sched: cls_c...
96
  		goto errout;
f40092373   Thomas Graf   pkt_sched: Contro...
97

b9a24bb76   WANG Cong   net_sched: proper...
98
  	err = tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
f40092373   Thomas Graf   pkt_sched: Contro...
99
  	if (err < 0)
d14cbfc88   John Fastabend   net: sched: cls_c...
100
  		goto errout;
b9a24bb76   WANG Cong   net_sched: proper...
101
102
103
104
105
  	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  	if (err < 0) {
  		tcf_exts_destroy(&e);
  		goto errout;
  	}
f40092373   Thomas Graf   pkt_sched: Contro...
106
107
  
  	err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
d14cbfc88   John Fastabend   net: sched: cls_c...
108
  	if (err < 0) {
18d0264f6   WANG Cong   net_sched: remove...
109
  		tcf_exts_destroy(&e);
d14cbfc88   John Fastabend   net: sched: cls_c...
110
111
  		goto errout;
  	}
f40092373   Thomas Graf   pkt_sched: Contro...
112

952313bd6   John Fastabend   net: sched: cls_c...
113
114
  	tcf_exts_change(tp, &new->exts, &e);
  	tcf_em_tree_change(tp, &new->ematches, &t);
f40092373   Thomas Graf   pkt_sched: Contro...
115

952313bd6   John Fastabend   net: sched: cls_c...
116
117
118
  	rcu_assign_pointer(tp->root, new);
  	if (head)
  		call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
f40092373   Thomas Graf   pkt_sched: Contro...
119
  	return 0;
d14cbfc88   John Fastabend   net: sched: cls_c...
120
  errout:
b9a24bb76   WANG Cong   net_sched: proper...
121
  	tcf_exts_destroy(&new->exts);
d14cbfc88   John Fastabend   net: sched: cls_c...
122
123
  	kfree(new);
  	return err;
f40092373   Thomas Graf   pkt_sched: Contro...
124
  }
1e052be69   Cong Wang   net_sched: destro...
125
  static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
f40092373   Thomas Graf   pkt_sched: Contro...
126
  {
952313bd6   John Fastabend   net: sched: cls_c...
127
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
128

1e052be69   Cong Wang   net_sched: destro...
129
130
  	if (!force)
  		return false;
d93637741   Daniel Borkmann   net, sched: respe...
131
132
  	/* Head can still be NULL due to cls_cgroup_init(). */
  	if (head)
13990f815   John Fastabend   net: sched: cls_c...
133
  		call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
d93637741   Daniel Borkmann   net, sched: respe...
134

1e052be69   Cong Wang   net_sched: destro...
135
  	return true;
f40092373   Thomas Graf   pkt_sched: Contro...
136
137
138
139
140
141
142
143
144
  }
  
  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)
  {
952313bd6   John Fastabend   net: sched: cls_c...
145
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
146
147
148
149
150
151
152
153
154
155
156
  
  	if (arg->count < arg->skip)
  		goto skip;
  
  	if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  		arg->stop = 1;
  		return;
  	}
  skip:
  	arg->count++;
  }
832d1d5bf   WANG Cong   net_sched: add st...
157
  static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
f40092373   Thomas Graf   pkt_sched: Contro...
158
159
  			   struct sk_buff *skb, struct tcmsg *t)
  {
952313bd6   John Fastabend   net: sched: cls_c...
160
  	struct cls_cgroup_head *head = rtnl_dereference(tp->root);
f40092373   Thomas Graf   pkt_sched: Contro...
161
162
163
164
165
166
167
  	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...
168
  	if (tcf_exts_dump(skb, &head->exts) < 0 ||
f40092373   Thomas Graf   pkt_sched: Contro...
169
170
171
172
  	    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...
173
  	if (tcf_exts_dump_stats(skb, &head->exts) < 0)
f40092373   Thomas Graf   pkt_sched: Contro...
174
175
176
177
178
  		goto nla_put_failure;
  
  	return skb->len;
  
  nla_put_failure:
6ea3b446b   Jiri Pirko   net: sched: cls: ...
179
  	nla_nest_cancel(skb, nest);
f40092373   Thomas Graf   pkt_sched: Contro...
180
181
182
183
184
185
186
187
188
189
  	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...
190
191
192
193
194
195
196
197
  	.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...
198
  	return register_tcf_proto_ops(&cls_cgroup_ops);
f40092373   Thomas Graf   pkt_sched: Contro...
199
200
201
202
203
204
205
206
207
208
  }
  
  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");