]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/sched/cls_basic.c
Merge tag 'wireless-drivers-next-for-davem-2019-02-22' of git://git.kernel.org/pub...
[mirror_ubuntu-jammy-kernel.git] / net / sched / cls_basic.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/cls_basic.c Basic Packet 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
1da177e4 12#include <linux/module.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4
LT
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/string.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/rtnetlink.h>
19#include <linux/skbuff.h>
1d8134fe 20#include <linux/idr.h>
5954894b 21#include <linux/percpu.h>
dc5fc579 22#include <net/netlink.h>
1da177e4
LT
23#include <net/act_api.h>
24#include <net/pkt_cls.h>
25
cc7ec456 26struct basic_head {
1da177e4 27 struct list_head flist;
1d8134fe 28 struct idr handle_idr;
9888faef 29 struct rcu_head rcu;
1da177e4
LT
30};
31
cc7ec456 32struct basic_filter {
1da177e4
LT
33 u32 handle;
34 struct tcf_exts exts;
35 struct tcf_ematch_tree ematches;
36 struct tcf_result res;
9888faef 37 struct tcf_proto *tp;
1da177e4 38 struct list_head link;
5954894b 39 struct tc_basic_pcnt __percpu *pf;
aaa908ff 40 struct rcu_work rwork;
1da177e4
LT
41};
42
dc7f9f6e 43static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1da177e4
LT
44 struct tcf_result *res)
45{
46 int r;
9888faef 47 struct basic_head *head = rcu_dereference_bh(tp->root);
1da177e4
LT
48 struct basic_filter *f;
49
9888faef 50 list_for_each_entry_rcu(f, &head->flist, link) {
5954894b 51 __this_cpu_inc(f->pf->rcnt);
1da177e4
LT
52 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
53 continue;
5954894b 54 __this_cpu_inc(f->pf->rhit);
1da177e4
LT
55 *res = f->res;
56 r = tcf_exts_exec(skb, &f->exts, res);
57 if (r < 0)
58 continue;
59 return r;
60 }
61 return -1;
62}
63
8113c095 64static void *basic_get(struct tcf_proto *tp, u32 handle)
1da177e4 65{
9888faef 66 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
67 struct basic_filter *f;
68
1c1bc6bd
DB
69 list_for_each_entry(f, &head->flist, link) {
70 if (f->handle == handle) {
8113c095 71 return f;
1c1bc6bd
DB
72 }
73 }
1da177e4 74
8113c095 75 return NULL;
1da177e4
LT
76}
77
1da177e4
LT
78static int basic_init(struct tcf_proto *tp)
79{
d3fa76ee
PM
80 struct basic_head *head;
81
82 head = kzalloc(sizeof(*head), GFP_KERNEL);
83 if (head == NULL)
84 return -ENOBUFS;
85 INIT_LIST_HEAD(&head->flist);
1d8134fe 86 idr_init(&head->handle_idr);
9888faef 87 rcu_assign_pointer(tp->root, head);
1da177e4
LT
88 return 0;
89}
90
0b2a5989
CW
91static void __basic_delete_filter(struct basic_filter *f)
92{
93 tcf_exts_destroy(&f->exts);
94 tcf_em_tree_destroy(&f->ematches);
95 tcf_exts_put_net(&f->exts);
5954894b 96 free_percpu(f->pf);
0b2a5989
CW
97 kfree(f);
98}
99
c96a4838 100static void basic_delete_filter_work(struct work_struct *work)
1da177e4 101{
aaa908ff
CW
102 struct basic_filter *f = container_of(to_rcu_work(work),
103 struct basic_filter,
104 rwork);
c96a4838 105 rtnl_lock();
0b2a5989 106 __basic_delete_filter(f);
c96a4838 107 rtnl_unlock();
1da177e4
LT
108}
109
12db03b6
VB
110static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
111 struct netlink_ext_ack *extack)
1da177e4 112{
9888faef 113 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4 114 struct basic_filter *f, *n;
10297b99 115
1da177e4 116 list_for_each_entry_safe(f, n, &head->flist, link) {
9888faef 117 list_del_rcu(&f->link);
18cdb37e 118 tcf_unbind_filter(tp, &f->res);
9c160941 119 idr_remove(&head->handle_idr, f->handle);
0b2a5989 120 if (tcf_exts_get_net(&f->exts))
aaa908ff 121 tcf_queue_work(&f->rwork, basic_delete_filter_work);
0b2a5989
CW
122 else
123 __basic_delete_filter(f);
1da177e4 124 }
1d8134fe 125 idr_destroy(&head->handle_idr);
9888faef 126 kfree_rcu(head, rcu);
1da177e4
LT
127}
128
571acf21 129static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
12db03b6 130 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 131{
763dbf63 132 struct basic_head *head = rtnl_dereference(tp->root);
8113c095 133 struct basic_filter *f = arg;
1da177e4 134
e4386456
JP
135 list_del_rcu(&f->link);
136 tcf_unbind_filter(tp, &f->res);
9c160941 137 idr_remove(&head->handle_idr, f->handle);
0b2a5989 138 tcf_exts_get_net(&f->exts);
aaa908ff 139 tcf_queue_work(&f->rwork, basic_delete_filter_work);
763dbf63 140 *last = list_empty(&head->flist);
e4386456 141 return 0;
1da177e4
LT
142}
143
6fa8c014
PM
144static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
145 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
146 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
147};
148
c1b52739
BL
149static int basic_set_parms(struct net *net, struct tcf_proto *tp,
150 struct basic_filter *f, unsigned long base,
151 struct nlattr **tb,
50a56190
AA
152 struct nlattr *est, bool ovr,
153 struct netlink_ext_ack *extack)
1da177e4 154{
6459082a 155 int err;
1da177e4 156
ec6743a1 157 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
1da177e4
LT
158 if (err < 0)
159 return err;
160
4ebc1e3c 161 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
1da177e4 162 if (err < 0)
ff1f8ca0 163 return err;
1da177e4 164
add93b61 165 if (tb[TCA_BASIC_CLASSID]) {
1587bac4 166 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
1da177e4
LT
167 tcf_bind_filter(tp, &f->res, base);
168 }
169
9888faef 170 f->tp = tp;
1da177e4 171 return 0;
1da177e4
LT
172}
173
c1b52739 174static int basic_change(struct net *net, struct sk_buff *in_skb,
af4c6641 175 struct tcf_proto *tp, unsigned long base, u32 handle,
7306db38 176 struct nlattr **tca, void **arg, bool ovr,
12db03b6 177 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 178{
cee63723 179 int err;
9888faef 180 struct basic_head *head = rtnl_dereference(tp->root);
add93b61 181 struct nlattr *tb[TCA_BASIC_MAX + 1];
9888faef
JF
182 struct basic_filter *fold = (struct basic_filter *) *arg;
183 struct basic_filter *fnew;
1da177e4 184
add93b61 185 if (tca[TCA_OPTIONS] == NULL)
1da177e4
LT
186 return -EINVAL;
187
6fa8c014 188 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
fceb6435 189 basic_policy, NULL);
cee63723
PM
190 if (err < 0)
191 return err;
1da177e4 192
9888faef
JF
193 if (fold != NULL) {
194 if (handle && fold->handle != handle)
1da177e4 195 return -EINVAL;
1da177e4
LT
196 }
197
9888faef 198 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
bd42b788
JP
199 if (!fnew)
200 return -ENOBUFS;
1da177e4 201
b9a24bb7
WC
202 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
203 if (err < 0)
204 goto errout;
205
05af0ebb
MW
206 if (!handle) {
207 handle = 1;
208 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
209 INT_MAX, GFP_KERNEL);
210 } else if (!fold) {
211 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
212 handle, GFP_KERNEL);
1da177e4 213 }
05af0ebb
MW
214 if (err)
215 goto errout;
216 fnew->handle = handle;
5954894b
CW
217 fnew->pf = alloc_percpu(struct tc_basic_pcnt);
218 if (!fnew->pf) {
219 err = -ENOMEM;
220 goto errout;
221 }
1da177e4 222
50a56190
AA
223 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
224 extack);
1d8134fe
CW
225 if (err < 0) {
226 if (!fold)
9c160941 227 idr_remove(&head->handle_idr, fnew->handle);
1da177e4 228 goto errout;
1d8134fe 229 }
1da177e4 230
8113c095 231 *arg = fnew;
9888faef
JF
232
233 if (fold) {
234a4624 234 idr_replace(&head->handle_idr, fnew, fnew->handle);
9888faef 235 list_replace_rcu(&fold->link, &fnew->link);
18cdb37e 236 tcf_unbind_filter(tp, &fold->res);
0b2a5989 237 tcf_exts_get_net(&fold->exts);
aaa908ff 238 tcf_queue_work(&fold->rwork, basic_delete_filter_work);
9888faef
JF
239 } else {
240 list_add_rcu(&fnew->link, &head->flist);
241 }
1da177e4
LT
242
243 return 0;
244errout:
5954894b 245 free_percpu(fnew->pf);
b9a24bb7 246 tcf_exts_destroy(&fnew->exts);
9888faef 247 kfree(fnew);
1da177e4
LT
248 return err;
249}
250
12db03b6
VB
251static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
252 bool rtnl_held)
1da177e4 253{
9888faef 254 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
255 struct basic_filter *f;
256
257 list_for_each_entry(f, &head->flist, link) {
258 if (arg->count < arg->skip)
259 goto skip;
260
8113c095 261 if (arg->fn(tp, f, arg) < 0) {
1da177e4
LT
262 arg->stop = 1;
263 break;
264 }
265skip:
266 arg->count++;
267 }
268}
269
07d79fc7
CW
270static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
271{
272 struct basic_filter *f = fh;
273
274 if (f && f->res.classid == classid)
275 f->res.class = cl;
276}
277
8113c095 278static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
12db03b6 279 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1da177e4 280{
5954894b 281 struct tc_basic_pcnt gpf = {};
8113c095 282 struct basic_filter *f = fh;
4b3550ef 283 struct nlattr *nest;
5954894b 284 int cpu;
1da177e4
LT
285
286 if (f == NULL)
287 return skb->len;
288
289 t->tcm_handle = f->handle;
290
4b3550ef
PM
291 nest = nla_nest_start(skb, TCA_OPTIONS);
292 if (nest == NULL)
293 goto nla_put_failure;
1da177e4 294
1b34ec43
DM
295 if (f->res.classid &&
296 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
297 goto nla_put_failure;
e1e284a4 298
5954894b
CW
299 for_each_possible_cpu(cpu) {
300 struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
301
302 gpf.rcnt += pf->rcnt;
303 gpf.rhit += pf->rhit;
304 }
305
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;
310
5da57f42 311 if (tcf_exts_dump(skb, &f->exts) < 0 ||
1da177e4 312 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
add93b61 313 goto nla_put_failure;
1da177e4 314
4b3550ef 315 nla_nest_end(skb, nest);
4c46ee52 316
5da57f42 317 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
4c46ee52 318 goto nla_put_failure;
319
1da177e4
LT
320 return skb->len;
321
add93b61 322nla_put_failure:
4b3550ef 323 nla_nest_cancel(skb, nest);
1da177e4
LT
324 return -1;
325}
326
2eb9d75c 327static struct tcf_proto_ops cls_basic_ops __read_mostly = {
1da177e4
LT
328 .kind = "basic",
329 .classify = basic_classify,
330 .init = basic_init,
331 .destroy = basic_destroy,
332 .get = basic_get,
1da177e4
LT
333 .change = basic_change,
334 .delete = basic_delete,
335 .walk = basic_walk,
336 .dump = basic_dump,
07d79fc7 337 .bind_class = basic_bind_class,
1da177e4
LT
338 .owner = THIS_MODULE,
339};
340
341static int __init init_basic(void)
342{
343 return register_tcf_proto_ops(&cls_basic_ops);
344}
345
10297b99 346static void __exit exit_basic(void)
1da177e4
LT
347{
348 unregister_tcf_proto_ops(&cls_basic_ops);
349}
350
351module_init(init_basic)
352module_exit(exit_basic)
353MODULE_LICENSE("GPL");