1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_cgroup.c Control Group Classifier
5 * Authors: Thomas Graf <tgraf@suug.ch>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/skbuff.h>
11 #include <linux/rcupdate.h>
12 #include <net/rtnetlink.h>
13 #include <net/pkt_cls.h>
15 #include <net/cls_cgroup.h>
17 struct cls_cgroup_head {
20 struct tcf_ematch_tree ematches;
22 struct rcu_work rwork;
25 static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
26 struct tcf_result *res)
28 struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
29 u32 classid = task_get_classid(skb);
35 if (!tcf_em_tree_match(skb, &head->ematches, NULL))
38 res->classid = classid;
41 return tcf_exts_exec(skb, &head->exts, res);
44 static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
49 static int cls_cgroup_init(struct tcf_proto *tp)
54 static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
55 [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
58 static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
60 tcf_exts_destroy(&head->exts);
61 tcf_em_tree_destroy(&head->ematches);
62 tcf_exts_put_net(&head->exts);
66 static void cls_cgroup_destroy_work(struct work_struct *work)
68 struct cls_cgroup_head *head = container_of(to_rcu_work(work),
69 struct cls_cgroup_head,
72 __cls_cgroup_destroy(head);
76 static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
77 struct tcf_proto *tp, unsigned long base,
78 u32 handle, struct nlattr **tca,
79 void **arg, u32 flags,
80 struct netlink_ext_ack *extack)
82 struct nlattr *tb[TCA_CGROUP_MAX + 1];
83 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
84 struct cls_cgroup_head *new;
87 if (!tca[TCA_OPTIONS])
93 if (head && handle != head->handle)
96 new = kzalloc(sizeof(*head), GFP_KERNEL);
100 err = tcf_exts_init(&new->exts, net, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
103 new->handle = handle;
105 err = nla_parse_nested_deprecated(tb, TCA_CGROUP_MAX,
106 tca[TCA_OPTIONS], cgroup_policy,
111 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, flags,
116 err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
120 rcu_assign_pointer(tp->root, new);
122 tcf_exts_get_net(&head->exts);
123 tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
127 tcf_exts_destroy(&new->exts);
132 static void cls_cgroup_destroy(struct tcf_proto *tp, bool rtnl_held,
133 struct netlink_ext_ack *extack)
135 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
137 /* Head can still be NULL due to cls_cgroup_init(). */
139 if (tcf_exts_get_net(&head->exts))
140 tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
142 __cls_cgroup_destroy(head);
146 static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last,
147 bool rtnl_held, struct netlink_ext_ack *extack)
152 static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg,
155 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
157 if (arg->count < arg->skip)
162 if (arg->fn(tp, head, arg) < 0) {
170 static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
171 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
173 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
176 t->tcm_handle = head->handle;
178 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
180 goto nla_put_failure;
182 if (tcf_exts_dump(skb, &head->exts) < 0 ||
183 tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
184 goto nla_put_failure;
186 nla_nest_end(skb, nest);
188 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
189 goto nla_put_failure;
194 nla_nest_cancel(skb, nest);
198 static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
200 .init = cls_cgroup_init,
201 .change = cls_cgroup_change,
202 .classify = cls_cgroup_classify,
203 .destroy = cls_cgroup_destroy,
204 .get = cls_cgroup_get,
205 .delete = cls_cgroup_delete,
206 .walk = cls_cgroup_walk,
207 .dump = cls_cgroup_dump,
208 .owner = THIS_MODULE,
211 static int __init init_cgroup_cls(void)
213 return register_tcf_proto_ops(&cls_cgroup_ops);
216 static void __exit exit_cgroup_cls(void)
218 unregister_tcf_proto_ops(&cls_cgroup_ops);
221 module_init(init_cgroup_cls);
222 module_exit(exit_cgroup_cls);
223 MODULE_LICENSE("GPL");