Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 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> |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 19 | #include <linux/rcupdate.h> |
Daniel Wagner | 6a328d8 | 2012-10-25 04:16:59 +0000 | [diff] [blame^] | 20 | #include <linux/fdtable.h> |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 21 | #include <net/rtnetlink.h> |
| 22 | #include <net/pkt_cls.h> |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 23 | #include <net/sock.h> |
| 24 | #include <net/cls_cgroup.h> |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 25 | |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 26 | static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp) |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 27 | { |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 28 | return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id), |
| 29 | struct cgroup_cls_state, css); |
| 30 | } |
| 31 | |
| 32 | static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p) |
| 33 | { |
| 34 | return container_of(task_subsys_state(p, net_cls_subsys_id), |
| 35 | struct cgroup_cls_state, css); |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 38 | static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp) |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 39 | { |
| 40 | struct cgroup_cls_state *cs; |
| 41 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 42 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
| 43 | if (!cs) |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 44 | return ERR_PTR(-ENOMEM); |
| 45 | |
| 46 | if (cgrp->parent) |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 47 | cs->classid = cgrp_cls_state(cgrp->parent)->classid; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 48 | |
| 49 | return &cs->css; |
| 50 | } |
| 51 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 52 | static void cgrp_destroy(struct cgroup *cgrp) |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 53 | { |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 54 | kfree(cgrp_cls_state(cgrp)); |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Wagner | 6a328d8 | 2012-10-25 04:16:59 +0000 | [diff] [blame^] | 57 | static int update_classid(const void *v, struct file *file, unsigned n) |
| 58 | { |
| 59 | int err; |
| 60 | struct socket *sock = sock_from_file(file, &err); |
| 61 | if (sock) |
| 62 | sock->sk->sk_classid = (u32)(unsigned long)v; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void cgrp_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
| 67 | { |
| 68 | struct task_struct *p; |
| 69 | void *v; |
| 70 | |
| 71 | cgroup_taskset_for_each(p, cgrp, tset) { |
| 72 | task_lock(p); |
| 73 | v = (void *)(unsigned long)task_cls_classid(p); |
| 74 | iterate_fd(p->files, 0, update_classid, v); |
| 75 | task_unlock(p); |
| 76 | } |
| 77 | } |
| 78 | |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 79 | static u64 read_classid(struct cgroup *cgrp, struct cftype *cft) |
| 80 | { |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 81 | return cgrp_cls_state(cgrp)->classid; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value) |
| 85 | { |
Li Zefan | 8e8ba85 | 2008-12-29 19:39:03 -0800 | [diff] [blame] | 86 | cgrp_cls_state(cgrp)->classid = (u32) value; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static struct cftype ss_files[] = { |
| 91 | { |
| 92 | .name = "classid", |
| 93 | .read_u64 = read_classid, |
| 94 | .write_u64 = write_classid, |
| 95 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 96 | { } /* terminate */ |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 99 | struct cgroup_subsys net_cls_subsys = { |
| 100 | .name = "net_cls", |
| 101 | .create = cgrp_create, |
| 102 | .destroy = cgrp_destroy, |
Daniel Wagner | 6a328d8 | 2012-10-25 04:16:59 +0000 | [diff] [blame^] | 103 | .attach = cgrp_attach, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 104 | .subsys_id = net_cls_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 105 | .base_cftypes = ss_files, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 106 | .module = THIS_MODULE, |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * While net_cls cgroup has the rudimentary hierarchy support of |
| 110 | * inheriting the parent's classid on cgroup creation, it doesn't |
| 111 | * properly propagates config changes in ancestors to their |
| 112 | * descendents. A child should follow the parent's configuration |
| 113 | * but be allowed to override it. Fix it and remove the following. |
| 114 | */ |
| 115 | .broken_hierarchy = true, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 118 | struct cls_cgroup_head { |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 119 | u32 handle; |
| 120 | struct tcf_exts exts; |
| 121 | struct tcf_ematch_tree ematches; |
| 122 | }; |
| 123 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 124 | static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 125 | struct tcf_result *res) |
| 126 | { |
| 127 | struct cls_cgroup_head *head = tp->root; |
Paul Menage | e65fcfd | 2009-05-26 20:47:02 -0700 | [diff] [blame] | 128 | u32 classid; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 129 | |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 130 | rcu_read_lock(); |
| 131 | classid = task_cls_state(current)->classid; |
| 132 | rcu_read_unlock(); |
| 133 | |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 134 | /* |
| 135 | * Due to the nature of the classifier it is required to ignore all |
| 136 | * packets originating from softirq context as accessing `current' |
| 137 | * would lead to false results. |
| 138 | * |
| 139 | * This test assumes that all callers of dev_queue_xmit() explicitely |
| 140 | * disable bh. Knowing this, it is possible to detect softirq based |
| 141 | * calls by looking at the number of nested bh disable calls because |
| 142 | * softirqs always disables bh. |
| 143 | */ |
Venkatesh Pallipadi | 75e1056 | 2010-10-04 17:03:16 -0700 | [diff] [blame] | 144 | if (in_serving_softirq()) { |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 145 | /* If there is an sk_classid we'll use that. */ |
| 146 | if (!skb->sk) |
| 147 | return -1; |
| 148 | classid = skb->sk->sk_classid; |
| 149 | } |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 150 | |
Paul Menage | e65fcfd | 2009-05-26 20:47:02 -0700 | [diff] [blame] | 151 | if (!classid) |
| 152 | return -1; |
| 153 | |
| 154 | if (!tcf_em_tree_match(skb, &head->ematches, NULL)) |
| 155 | return -1; |
| 156 | |
| 157 | res->classid = classid; |
| 158 | res->class = 0; |
| 159 | return tcf_exts_exec(skb, &head->exts, res); |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle) |
| 163 | { |
| 164 | return 0UL; |
| 165 | } |
| 166 | |
| 167 | static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | static int cls_cgroup_init(struct tcf_proto *tp) |
| 172 | { |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static const struct tcf_ext_map cgroup_ext_map = { |
| 177 | .action = TCA_CGROUP_ACT, |
| 178 | .police = TCA_CGROUP_POLICE, |
| 179 | }; |
| 180 | |
| 181 | static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { |
| 182 | [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, |
| 183 | }; |
| 184 | |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 185 | static int cls_cgroup_change(struct sk_buff *in_skb, |
| 186 | struct tcf_proto *tp, unsigned long base, |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 187 | u32 handle, struct nlattr **tca, |
| 188 | unsigned long *arg) |
| 189 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 190 | struct nlattr *tb[TCA_CGROUP_MAX + 1]; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 191 | struct cls_cgroup_head *head = tp->root; |
| 192 | struct tcf_ematch_tree t; |
| 193 | struct tcf_exts e; |
| 194 | int err; |
| 195 | |
Minoru Usui | 52ea3a5 | 2009-06-09 04:03:09 -0700 | [diff] [blame] | 196 | if (!tca[TCA_OPTIONS]) |
| 197 | return -EINVAL; |
| 198 | |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 199 | if (head == NULL) { |
| 200 | if (!handle) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 204 | if (head == NULL) |
| 205 | return -ENOBUFS; |
| 206 | |
| 207 | head->handle = handle; |
| 208 | |
| 209 | tcf_tree_lock(tp); |
| 210 | tp->root = head; |
| 211 | tcf_tree_unlock(tp); |
| 212 | } |
| 213 | |
| 214 | if (handle != head->handle) |
| 215 | return -ENOENT; |
| 216 | |
| 217 | err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS], |
| 218 | cgroup_policy); |
| 219 | if (err < 0) |
| 220 | return err; |
| 221 | |
| 222 | err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map); |
| 223 | if (err < 0) |
| 224 | return err; |
| 225 | |
| 226 | err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t); |
| 227 | if (err < 0) |
| 228 | return err; |
| 229 | |
| 230 | tcf_exts_change(tp, &head->exts, &e); |
| 231 | tcf_em_tree_change(tp, &head->ematches, &t); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static void cls_cgroup_destroy(struct tcf_proto *tp) |
| 237 | { |
Patrick McHardy | 47a1a1d | 2008-11-19 08:03:09 +0000 | [diff] [blame] | 238 | struct cls_cgroup_head *head = tp->root; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 239 | |
| 240 | if (head) { |
| 241 | tcf_exts_destroy(tp, &head->exts); |
| 242 | tcf_em_tree_destroy(tp, &head->ematches); |
| 243 | kfree(head); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg) |
| 248 | { |
| 249 | return -EOPNOTSUPP; |
| 250 | } |
| 251 | |
| 252 | static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 253 | { |
| 254 | struct cls_cgroup_head *head = tp->root; |
| 255 | |
| 256 | if (arg->count < arg->skip) |
| 257 | goto skip; |
| 258 | |
| 259 | if (arg->fn(tp, (unsigned long) head, arg) < 0) { |
| 260 | arg->stop = 1; |
| 261 | return; |
| 262 | } |
| 263 | skip: |
| 264 | arg->count++; |
| 265 | } |
| 266 | |
| 267 | static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh, |
| 268 | struct sk_buff *skb, struct tcmsg *t) |
| 269 | { |
| 270 | struct cls_cgroup_head *head = tp->root; |
| 271 | unsigned char *b = skb_tail_pointer(skb); |
| 272 | struct nlattr *nest; |
| 273 | |
| 274 | t->tcm_handle = head->handle; |
| 275 | |
| 276 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 277 | if (nest == NULL) |
| 278 | goto nla_put_failure; |
| 279 | |
| 280 | if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 || |
| 281 | tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0) |
| 282 | goto nla_put_failure; |
| 283 | |
| 284 | nla_nest_end(skb, nest); |
| 285 | |
| 286 | if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0) |
| 287 | goto nla_put_failure; |
| 288 | |
| 289 | return skb->len; |
| 290 | |
| 291 | nla_put_failure: |
| 292 | nlmsg_trim(skb, b); |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | static struct tcf_proto_ops cls_cgroup_ops __read_mostly = { |
| 297 | .kind = "cgroup", |
| 298 | .init = cls_cgroup_init, |
| 299 | .change = cls_cgroup_change, |
| 300 | .classify = cls_cgroup_classify, |
| 301 | .destroy = cls_cgroup_destroy, |
| 302 | .get = cls_cgroup_get, |
| 303 | .put = cls_cgroup_put, |
| 304 | .delete = cls_cgroup_delete, |
| 305 | .walk = cls_cgroup_walk, |
| 306 | .dump = cls_cgroup_dump, |
| 307 | .owner = THIS_MODULE, |
| 308 | }; |
| 309 | |
| 310 | static int __init init_cgroup_cls(void) |
| 311 | { |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 312 | int ret; |
| 313 | |
Ben Blum | 8e039d8 | 2010-03-23 05:24:03 +0000 | [diff] [blame] | 314 | ret = cgroup_load_subsys(&net_cls_subsys); |
| 315 | if (ret) |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 316 | goto out; |
| 317 | |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 318 | ret = register_tcf_proto_ops(&cls_cgroup_ops); |
| 319 | if (ret) |
| 320 | cgroup_unload_subsys(&net_cls_subsys); |
| 321 | |
| 322 | out: |
Ben Blum | 8e039d8 | 2010-03-23 05:24:03 +0000 | [diff] [blame] | 323 | return ret; |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void __exit exit_cgroup_cls(void) |
| 327 | { |
| 328 | unregister_tcf_proto_ops(&cls_cgroup_ops); |
Herbert Xu | f845172 | 2010-05-24 00:12:34 -0700 | [diff] [blame] | 329 | |
Ben Blum | 8e039d8 | 2010-03-23 05:24:03 +0000 | [diff] [blame] | 330 | cgroup_unload_subsys(&net_cls_subsys); |
Thomas Graf | f400923 | 2008-11-07 22:56:00 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | module_init(init_cgroup_cls); |
| 334 | module_exit(exit_cgroup_cls); |
| 335 | MODULE_LICENSE("GPL"); |