2 * net/sched/cls_basic.c Basic Packet Classifier.
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.
9 * Authors: Thomas Graf <tgraf@suug.ch>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/skbuff.h>
20 #include <linux/idr.h>
21 #include <linux/percpu.h>
22 #include <net/netlink.h>
23 #include <net/act_api.h>
24 #include <net/pkt_cls.h>
27 struct list_head flist;
28 struct idr handle_idr;
35 struct tcf_ematch_tree ematches;
36 struct tcf_result res;
38 struct list_head link;
39 struct tc_basic_pcnt __percpu *pf;
40 struct rcu_work rwork;
43 static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
44 struct tcf_result *res)
47 struct basic_head *head = rcu_dereference_bh(tp->root);
48 struct basic_filter *f;
50 list_for_each_entry_rcu(f, &head->flist, link) {
51 __this_cpu_inc(f->pf->rcnt);
52 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
54 __this_cpu_inc(f->pf->rhit);
56 r = tcf_exts_exec(skb, &f->exts, res);
64 static void *basic_get(struct tcf_proto *tp, u32 handle)
66 struct basic_head *head = rtnl_dereference(tp->root);
67 struct basic_filter *f;
69 list_for_each_entry(f, &head->flist, link) {
70 if (f->handle == handle) {
78 static int basic_init(struct tcf_proto *tp)
80 struct basic_head *head;
82 head = kzalloc(sizeof(*head), GFP_KERNEL);
85 INIT_LIST_HEAD(&head->flist);
86 idr_init(&head->handle_idr);
87 rcu_assign_pointer(tp->root, head);
91 static void __basic_delete_filter(struct basic_filter *f)
93 tcf_exts_destroy(&f->exts);
94 tcf_em_tree_destroy(&f->ematches);
95 tcf_exts_put_net(&f->exts);
100 static void basic_delete_filter_work(struct work_struct *work)
102 struct basic_filter *f = container_of(to_rcu_work(work),
106 __basic_delete_filter(f);
110 static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
111 struct netlink_ext_ack *extack)
113 struct basic_head *head = rtnl_dereference(tp->root);
114 struct basic_filter *f, *n;
116 list_for_each_entry_safe(f, n, &head->flist, link) {
117 list_del_rcu(&f->link);
118 tcf_unbind_filter(tp, &f->res);
119 idr_remove(&head->handle_idr, f->handle);
120 if (tcf_exts_get_net(&f->exts))
121 tcf_queue_work(&f->rwork, basic_delete_filter_work);
123 __basic_delete_filter(f);
125 idr_destroy(&head->handle_idr);
126 kfree_rcu(head, rcu);
129 static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
130 bool rtnl_held, struct netlink_ext_ack *extack)
132 struct basic_head *head = rtnl_dereference(tp->root);
133 struct basic_filter *f = arg;
135 list_del_rcu(&f->link);
136 tcf_unbind_filter(tp, &f->res);
137 idr_remove(&head->handle_idr, f->handle);
138 tcf_exts_get_net(&f->exts);
139 tcf_queue_work(&f->rwork, basic_delete_filter_work);
140 *last = list_empty(&head->flist);
144 static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
145 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
146 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
149 static int basic_set_parms(struct net *net, struct tcf_proto *tp,
150 struct basic_filter *f, unsigned long base,
152 struct nlattr *est, bool ovr,
153 struct netlink_ext_ack *extack)
157 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
161 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
165 if (tb[TCA_BASIC_CLASSID]) {
166 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
167 tcf_bind_filter(tp, &f->res, base);
174 static int basic_change(struct net *net, struct sk_buff *in_skb,
175 struct tcf_proto *tp, unsigned long base, u32 handle,
176 struct nlattr **tca, void **arg, bool ovr,
177 bool rtnl_held, struct netlink_ext_ack *extack)
180 struct basic_head *head = rtnl_dereference(tp->root);
181 struct nlattr *tb[TCA_BASIC_MAX + 1];
182 struct basic_filter *fold = (struct basic_filter *) *arg;
183 struct basic_filter *fnew;
185 if (tca[TCA_OPTIONS] == NULL)
188 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
194 if (handle && fold->handle != handle)
198 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
202 err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
208 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
209 INT_MAX, GFP_KERNEL);
211 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
216 fnew->handle = handle;
217 fnew->pf = alloc_percpu(struct tc_basic_pcnt);
223 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
227 idr_remove(&head->handle_idr, fnew->handle);
234 idr_replace(&head->handle_idr, fnew, fnew->handle);
235 list_replace_rcu(&fold->link, &fnew->link);
236 tcf_unbind_filter(tp, &fold->res);
237 tcf_exts_get_net(&fold->exts);
238 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
240 list_add_rcu(&fnew->link, &head->flist);
245 free_percpu(fnew->pf);
246 tcf_exts_destroy(&fnew->exts);
251 static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
254 struct basic_head *head = rtnl_dereference(tp->root);
255 struct basic_filter *f;
257 list_for_each_entry(f, &head->flist, link) {
258 if (arg->count < arg->skip)
261 if (arg->fn(tp, f, arg) < 0) {
270 static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
272 struct basic_filter *f = fh;
274 if (f && f->res.classid == classid)
278 static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
279 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
281 struct tc_basic_pcnt gpf = {};
282 struct basic_filter *f = fh;
289 t->tcm_handle = f->handle;
291 nest = nla_nest_start(skb, TCA_OPTIONS);
293 goto nla_put_failure;
295 if (f->res.classid &&
296 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
297 goto nla_put_failure;
299 for_each_possible_cpu(cpu) {
300 struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
302 gpf.rcnt += pf->rcnt;
303 gpf.rhit += pf->rhit;
306 if (nla_put_64bit(skb, TCA_BASIC_PCNT,
307 sizeof(struct tc_basic_pcnt),
308 &gpf, TCA_BASIC_PAD))
309 goto nla_put_failure;
311 if (tcf_exts_dump(skb, &f->exts) < 0 ||
312 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
313 goto nla_put_failure;
315 nla_nest_end(skb, nest);
317 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
318 goto nla_put_failure;
323 nla_nest_cancel(skb, nest);
327 static struct tcf_proto_ops cls_basic_ops __read_mostly = {
329 .classify = basic_classify,
331 .destroy = basic_destroy,
333 .change = basic_change,
334 .delete = basic_delete,
337 .bind_class = basic_bind_class,
338 .owner = THIS_MODULE,
341 static int __init init_basic(void)
343 return register_tcf_proto_ops(&cls_basic_ops);
346 static void __exit exit_basic(void)
348 unregister_tcf_proto_ops(&cls_basic_ops);
351 module_init(init_basic)
352 module_exit(exit_basic)
353 MODULE_LICENSE("GPL");