]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sched/cls_cgroup.c
cgroup: pass around cgroup_subsys_state instead of cgroup in subsystem methods
[mirror_ubuntu-bionic-kernel.git] / net / sched / cls_cgroup.c
1 /*
2 * net/sched/cls_cgroup.c Control Group Classifier
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <linux/cgroup.h>
19 #include <linux/rcupdate.h>
20 #include <linux/fdtable.h>
21 #include <net/rtnetlink.h>
22 #include <net/pkt_cls.h>
23 #include <net/sock.h>
24 #include <net/cls_cgroup.h>
25
26 static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
27 {
28 return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
29 }
30
31 static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
32 {
33 return css_cls_state(cgroup_css(cgrp, net_cls_subsys_id));
34 }
35
36 static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
37 {
38 return css_cls_state(task_css(p, net_cls_subsys_id));
39 }
40
41 static struct cgroup_subsys_state *
42 cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
43 {
44 struct cgroup_cls_state *cs;
45
46 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
47 if (!cs)
48 return ERR_PTR(-ENOMEM);
49 return &cs->css;
50 }
51
52 static int cgrp_css_online(struct cgroup_subsys_state *css)
53 {
54 struct cgroup_cls_state *cs = css_cls_state(css);
55 struct cgroup_cls_state *parent = css_cls_state(css_parent(css));
56
57 if (parent)
58 cs->classid = parent->classid;
59 return 0;
60 }
61
62 static void cgrp_css_free(struct cgroup_subsys_state *css)
63 {
64 kfree(css_cls_state(css));
65 }
66
67 static int update_classid(const void *v, struct file *file, unsigned n)
68 {
69 int err;
70 struct socket *sock = sock_from_file(file, &err);
71 if (sock)
72 sock->sk->sk_classid = (u32)(unsigned long)v;
73 return 0;
74 }
75
76 static void cgrp_attach(struct cgroup_subsys_state *css,
77 struct cgroup_taskset *tset)
78 {
79 struct task_struct *p;
80 void *v;
81
82 cgroup_taskset_for_each(p, css->cgroup, tset) {
83 task_lock(p);
84 v = (void *)(unsigned long)task_cls_classid(p);
85 iterate_fd(p->files, 0, update_classid, v);
86 task_unlock(p);
87 }
88 }
89
90 static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
91 {
92 return cgrp_cls_state(cgrp)->classid;
93 }
94
95 static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
96 {
97 cgrp_cls_state(cgrp)->classid = (u32) value;
98 return 0;
99 }
100
101 static struct cftype ss_files[] = {
102 {
103 .name = "classid",
104 .read_u64 = read_classid,
105 .write_u64 = write_classid,
106 },
107 { } /* terminate */
108 };
109
110 struct cgroup_subsys net_cls_subsys = {
111 .name = "net_cls",
112 .css_alloc = cgrp_css_alloc,
113 .css_online = cgrp_css_online,
114 .css_free = cgrp_css_free,
115 .attach = cgrp_attach,
116 .subsys_id = net_cls_subsys_id,
117 .base_cftypes = ss_files,
118 .module = THIS_MODULE,
119 };
120
121 struct cls_cgroup_head {
122 u32 handle;
123 struct tcf_exts exts;
124 struct tcf_ematch_tree ematches;
125 };
126
127 static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
128 struct tcf_result *res)
129 {
130 struct cls_cgroup_head *head = tp->root;
131 u32 classid;
132
133 rcu_read_lock();
134 classid = task_cls_state(current)->classid;
135 rcu_read_unlock();
136
137 /*
138 * Due to the nature of the classifier it is required to ignore all
139 * packets originating from softirq context as accessing `current'
140 * would lead to false results.
141 *
142 * This test assumes that all callers of dev_queue_xmit() explicitely
143 * disable bh. Knowing this, it is possible to detect softirq based
144 * calls by looking at the number of nested bh disable calls because
145 * softirqs always disables bh.
146 */
147 if (in_serving_softirq()) {
148 /* If there is an sk_classid we'll use that. */
149 if (!skb->sk)
150 return -1;
151 classid = skb->sk->sk_classid;
152 }
153
154 if (!classid)
155 return -1;
156
157 if (!tcf_em_tree_match(skb, &head->ematches, NULL))
158 return -1;
159
160 res->classid = classid;
161 res->class = 0;
162 return tcf_exts_exec(skb, &head->exts, res);
163 }
164
165 static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
166 {
167 return 0UL;
168 }
169
170 static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
171 {
172 }
173
174 static int cls_cgroup_init(struct tcf_proto *tp)
175 {
176 return 0;
177 }
178
179 static const struct tcf_ext_map cgroup_ext_map = {
180 .action = TCA_CGROUP_ACT,
181 .police = TCA_CGROUP_POLICE,
182 };
183
184 static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
185 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
186 };
187
188 static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
189 struct tcf_proto *tp, unsigned long base,
190 u32 handle, struct nlattr **tca,
191 unsigned long *arg)
192 {
193 struct nlattr *tb[TCA_CGROUP_MAX + 1];
194 struct cls_cgroup_head *head = tp->root;
195 struct tcf_ematch_tree t;
196 struct tcf_exts e;
197 int err;
198
199 if (!tca[TCA_OPTIONS])
200 return -EINVAL;
201
202 if (head == NULL) {
203 if (!handle)
204 return -EINVAL;
205
206 head = kzalloc(sizeof(*head), GFP_KERNEL);
207 if (head == NULL)
208 return -ENOBUFS;
209
210 head->handle = handle;
211
212 tcf_tree_lock(tp);
213 tp->root = head;
214 tcf_tree_unlock(tp);
215 }
216
217 if (handle != head->handle)
218 return -ENOENT;
219
220 err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
221 cgroup_policy);
222 if (err < 0)
223 return err;
224
225 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e,
226 &cgroup_ext_map);
227 if (err < 0)
228 return err;
229
230 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
231 if (err < 0)
232 return err;
233
234 tcf_exts_change(tp, &head->exts, &e);
235 tcf_em_tree_change(tp, &head->ematches, &t);
236
237 return 0;
238 }
239
240 static void cls_cgroup_destroy(struct tcf_proto *tp)
241 {
242 struct cls_cgroup_head *head = tp->root;
243
244 if (head) {
245 tcf_exts_destroy(tp, &head->exts);
246 tcf_em_tree_destroy(tp, &head->ematches);
247 kfree(head);
248 }
249 }
250
251 static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
252 {
253 return -EOPNOTSUPP;
254 }
255
256 static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
257 {
258 struct cls_cgroup_head *head = tp->root;
259
260 if (arg->count < arg->skip)
261 goto skip;
262
263 if (arg->fn(tp, (unsigned long) head, arg) < 0) {
264 arg->stop = 1;
265 return;
266 }
267 skip:
268 arg->count++;
269 }
270
271 static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
272 struct sk_buff *skb, struct tcmsg *t)
273 {
274 struct cls_cgroup_head *head = tp->root;
275 unsigned char *b = skb_tail_pointer(skb);
276 struct nlattr *nest;
277
278 t->tcm_handle = head->handle;
279
280 nest = nla_nest_start(skb, TCA_OPTIONS);
281 if (nest == NULL)
282 goto nla_put_failure;
283
284 if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
285 tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
286 goto nla_put_failure;
287
288 nla_nest_end(skb, nest);
289
290 if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
291 goto nla_put_failure;
292
293 return skb->len;
294
295 nla_put_failure:
296 nlmsg_trim(skb, b);
297 return -1;
298 }
299
300 static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
301 .kind = "cgroup",
302 .init = cls_cgroup_init,
303 .change = cls_cgroup_change,
304 .classify = cls_cgroup_classify,
305 .destroy = cls_cgroup_destroy,
306 .get = cls_cgroup_get,
307 .put = cls_cgroup_put,
308 .delete = cls_cgroup_delete,
309 .walk = cls_cgroup_walk,
310 .dump = cls_cgroup_dump,
311 .owner = THIS_MODULE,
312 };
313
314 static int __init init_cgroup_cls(void)
315 {
316 int ret;
317
318 ret = cgroup_load_subsys(&net_cls_subsys);
319 if (ret)
320 goto out;
321
322 ret = register_tcf_proto_ops(&cls_cgroup_ops);
323 if (ret)
324 cgroup_unload_subsys(&net_cls_subsys);
325
326 out:
327 return ret;
328 }
329
330 static void __exit exit_cgroup_cls(void)
331 {
332 unregister_tcf_proto_ops(&cls_cgroup_ops);
333
334 cgroup_unload_subsys(&net_cls_subsys);
335 }
336
337 module_init(init_cgroup_cls);
338 module_exit(exit_cgroup_cls);
339 MODULE_LICENSE("GPL");