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