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/skbuff.h> 15 #include <linux/rcupdate.h> 16 #include <net/rtnetlink.h> 17 #include <net/pkt_cls.h> 18 #include <net/sock.h> 19 #include <net/cls_cgroup.h> 20 21 struct cls_cgroup_head { 22 u32 handle; 23 struct tcf_exts exts; 24 struct tcf_ematch_tree ematches; 25 struct tcf_proto *tp; 26 union { 27 struct work_struct work; 28 struct rcu_head rcu; 29 }; 30 }; 31 32 static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp, 33 struct tcf_result *res) 34 { 35 struct cls_cgroup_head *head = rcu_dereference_bh(tp->root); 36 u32 classid = task_get_classid(skb); 37 38 if (!classid) 39 return -1; 40 if (!tcf_em_tree_match(skb, &head->ematches, NULL)) 41 return -1; 42 43 res->classid = classid; 44 res->class = 0; 45 46 return tcf_exts_exec(skb, &head->exts, res); 47 } 48 49 static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle) 50 { 51 return NULL; 52 } 53 54 static int cls_cgroup_init(struct tcf_proto *tp) 55 { 56 return 0; 57 } 58 59 static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { 60 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, 61 }; 62 63 static void cls_cgroup_destroy_work(struct work_struct *work) 64 { 65 struct cls_cgroup_head *head = container_of(work, 66 struct cls_cgroup_head, 67 work); 68 rtnl_lock(); 69 tcf_exts_destroy(&head->exts); 70 tcf_em_tree_destroy(&head->ematches); 71 kfree(head); 72 rtnl_unlock(); 73 } 74 75 static void cls_cgroup_destroy_rcu(struct rcu_head *root) 76 { 77 struct cls_cgroup_head *head = container_of(root, 78 struct cls_cgroup_head, 79 rcu); 80 81 INIT_WORK(&head->work, cls_cgroup_destroy_work); 82 tcf_queue_work(&head->work); 83 } 84 85 static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, 86 struct tcf_proto *tp, unsigned long base, 87 u32 handle, struct nlattr **tca, 88 void **arg, bool ovr) 89 { 90 struct nlattr *tb[TCA_CGROUP_MAX + 1]; 91 struct cls_cgroup_head *head = rtnl_dereference(tp->root); 92 struct cls_cgroup_head *new; 93 int err; 94 95 if (!tca[TCA_OPTIONS]) 96 return -EINVAL; 97 98 if (!head && !handle) 99 return -EINVAL; 100 101 if (head && handle != head->handle) 102 return -ENOENT; 103 104 new = kzalloc(sizeof(*head), GFP_KERNEL); 105 if (!new) 106 return -ENOBUFS; 107 108 err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE); 109 if (err < 0) 110 goto errout; 111 new->handle = handle; 112 new->tp = tp; 113 err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS], 114 cgroup_policy, NULL); 115 if (err < 0) 116 goto errout; 117 118 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, ovr); 119 if (err < 0) 120 goto errout; 121 122 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches); 123 if (err < 0) 124 goto errout; 125 126 rcu_assign_pointer(tp->root, new); 127 if (head) 128 call_rcu(&head->rcu, cls_cgroup_destroy_rcu); 129 return 0; 130 errout: 131 tcf_exts_destroy(&new->exts); 132 kfree(new); 133 return err; 134 } 135 136 static void cls_cgroup_destroy(struct tcf_proto *tp) 137 { 138 struct cls_cgroup_head *head = rtnl_dereference(tp->root); 139 140 /* Head can still be NULL due to cls_cgroup_init(). */ 141 if (head) 142 call_rcu(&head->rcu, cls_cgroup_destroy_rcu); 143 } 144 145 static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last) 146 { 147 return -EOPNOTSUPP; 148 } 149 150 static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg) 151 { 152 struct cls_cgroup_head *head = rtnl_dereference(tp->root); 153 154 if (arg->count < arg->skip) 155 goto skip; 156 157 if (arg->fn(tp, head, arg) < 0) { 158 arg->stop = 1; 159 return; 160 } 161 skip: 162 arg->count++; 163 } 164 165 static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh, 166 struct sk_buff *skb, struct tcmsg *t) 167 { 168 struct cls_cgroup_head *head = rtnl_dereference(tp->root); 169 struct nlattr *nest; 170 171 t->tcm_handle = head->handle; 172 173 nest = nla_nest_start(skb, TCA_OPTIONS); 174 if (nest == NULL) 175 goto nla_put_failure; 176 177 if (tcf_exts_dump(skb, &head->exts) < 0 || 178 tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0) 179 goto nla_put_failure; 180 181 nla_nest_end(skb, nest); 182 183 if (tcf_exts_dump_stats(skb, &head->exts) < 0) 184 goto nla_put_failure; 185 186 return skb->len; 187 188 nla_put_failure: 189 nla_nest_cancel(skb, nest); 190 return -1; 191 } 192 193 static struct tcf_proto_ops cls_cgroup_ops __read_mostly = { 194 .kind = "cgroup", 195 .init = cls_cgroup_init, 196 .change = cls_cgroup_change, 197 .classify = cls_cgroup_classify, 198 .destroy = cls_cgroup_destroy, 199 .get = cls_cgroup_get, 200 .delete = cls_cgroup_delete, 201 .walk = cls_cgroup_walk, 202 .dump = cls_cgroup_dump, 203 .owner = THIS_MODULE, 204 }; 205 206 static int __init init_cgroup_cls(void) 207 { 208 return register_tcf_proto_ops(&cls_cgroup_ops); 209 } 210 211 static void __exit exit_cgroup_cls(void) 212 { 213 unregister_tcf_proto_ops(&cls_cgroup_ops); 214 } 215 216 module_init(init_cgroup_cls); 217 module_exit(exit_cgroup_cls); 218 MODULE_LICENSE("GPL"); 219