blob: b599db26d34bee7a33750a89bf8e3dd8028ea132 [file] [log] [blame]
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001/* net/sched/sch_ingress.c - Ingress and clsact qdisc
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
7 *
8 * Authors: Jamal Hadi Salim 1999
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070013#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/rtnetlink.h>
Daniel Borkmannd2788d32015-05-09 22:51:32 +020016
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070017#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010019#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jiri Pirko6529eab2017-05-17 11:07:55 +020021struct ingress_sched_data {
22 struct tcf_block *block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +020023 struct tcf_block_ext_info block_info;
Jiri Pirko6529eab2017-05-17 11:07:55 +020024};
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
27{
28 return NULL;
29}
30
WANG Cong143976c2017-08-24 16:51:29 -070031static unsigned long ingress_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return TC_H_MIN(classid) + 1;
34}
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080037 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
WANG Cong143976c2017-08-24 16:51:29 -070039 return ingress_find(sch, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
WANG Cong143976c2017-08-24 16:51:29 -070042static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44}
45
Patrick McHardy58f4df42008-01-21 00:11:01 -080046static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Jiri Pirko6529eab2017-05-17 11:07:55 +020050static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Jiri Pirko6529eab2017-05-17 11:07:55 +020052 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jiri Pirko6529eab2017-05-17 11:07:55 +020054 return q->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Daniel Borkmann45771392015-04-10 23:07:54 +020057static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
58{
Jiri Pirko6529eab2017-05-17 11:07:55 +020059 struct ingress_sched_data *q = qdisc_priv(sch);
60 struct net_device *dev = qdisc_dev(sch);
61 int err;
62
Jiri Pirko6e40cf22017-10-19 15:50:30 +020063 q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
64
65 err = tcf_block_get_ext(&q->block, &dev->ingress_cl_list,
66 sch, &q->block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +020067 if (err)
68 return err;
69
Daniel Borkmann45771392015-04-10 23:07:54 +020070 net_inc_ingress_queue();
Alexei Starovoitov087c1a62015-04-30 20:14:07 -070071 sch->flags |= TCQ_F_CPUSTATS;
Daniel Borkmann45771392015-04-10 23:07:54 +020072
73 return 0;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void ingress_destroy(struct Qdisc *sch)
77{
Jiri Pirko6529eab2017-05-17 11:07:55 +020078 struct ingress_sched_data *q = qdisc_priv(sch);
Jiri Pirko6e40cf22017-10-19 15:50:30 +020079 struct net_device *dev = qdisc_dev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jiri Pirko6e40cf22017-10-19 15:50:30 +020081 tcf_block_put_ext(q->block, &dev->ingress_cl_list,
82 sch, &q->block_info);
Daniel Borkmann45771392015-04-10 23:07:54 +020083 net_dec_ingress_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
87{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080088 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080090 nest = nla_nest_start(skb, TCA_OPTIONS);
91 if (nest == NULL)
92 goto nla_put_failure;
Daniel Borkmannd2788d32015-05-09 22:51:32 +020093
Yang Yingliangd59b7d82014-03-12 10:20:32 +080094 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Patrick McHardy1e904742008-01-22 22:11:17 -080096nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080097 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -1;
99}
100
Eric Dumazet20fea082007-11-14 01:44:41 -0800101static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700103 .find = ingress_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200105 .tcf_block = ingress_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .bind_tcf = ingress_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700107 .unbind_tcf = ingress_unbind_filter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Eric Dumazet20fea082007-11-14 01:44:41 -0800110static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .cl_ops = &ingress_class_ops,
112 .id = "ingress",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200113 .priv_size = sizeof(struct ingress_sched_data),
Daniel Borkmann45771392015-04-10 23:07:54 +0200114 .init = ingress_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .dump = ingress_dump,
117 .owner = THIS_MODULE,
118};
119
Jiri Pirko6529eab2017-05-17 11:07:55 +0200120struct clsact_sched_data {
121 struct tcf_block *ingress_block;
122 struct tcf_block *egress_block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200123 struct tcf_block_ext_info ingress_block_info;
124 struct tcf_block_ext_info egress_block_info;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200125};
126
WANG Cong143976c2017-08-24 16:51:29 -0700127static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100128{
129 switch (TC_H_MIN(classid)) {
130 case TC_H_MIN(TC_H_MIN_INGRESS):
131 case TC_H_MIN(TC_H_MIN_EGRESS):
132 return TC_H_MIN(classid);
133 default:
134 return 0;
135 }
136}
137
138static unsigned long clsact_bind_filter(struct Qdisc *sch,
139 unsigned long parent, u32 classid)
140{
WANG Cong143976c2017-08-24 16:51:29 -0700141 return clsact_find(sch, classid);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100142}
143
Jiri Pirko6529eab2017-05-17 11:07:55 +0200144static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100145{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200146 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100147
148 switch (cl) {
149 case TC_H_MIN(TC_H_MIN_INGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200150 return q->ingress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100151 case TC_H_MIN(TC_H_MIN_EGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200152 return q->egress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100153 default:
154 return NULL;
155 }
156}
157
158static int clsact_init(struct Qdisc *sch, struct nlattr *opt)
159{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200160 struct clsact_sched_data *q = qdisc_priv(sch);
161 struct net_device *dev = qdisc_dev(sch);
162 int err;
163
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200164 q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
165
166 err = tcf_block_get_ext(&q->ingress_block, &dev->ingress_cl_list,
167 sch, &q->ingress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200168 if (err)
169 return err;
170
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200171 q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
172
173 err = tcf_block_get_ext(&q->egress_block, &dev->egress_cl_list,
174 sch, &q->egress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200175 if (err)
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200176 goto err_egress_block_get;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200177
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100178 net_inc_ingress_queue();
179 net_inc_egress_queue();
180
181 sch->flags |= TCQ_F_CPUSTATS;
182
183 return 0;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200184
185err_egress_block_get:
186 tcf_block_put_ext(q->ingress_block, &dev->ingress_cl_list,
187 sch, &q->ingress_block_info);
188 return err;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100189}
190
191static void clsact_destroy(struct Qdisc *sch)
192{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200193 struct clsact_sched_data *q = qdisc_priv(sch);
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200194 struct net_device *dev = qdisc_dev(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100195
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200196 tcf_block_put_ext(q->egress_block, &dev->egress_cl_list,
197 sch, &q->egress_block_info);
198 tcf_block_put_ext(q->ingress_block, &dev->ingress_cl_list,
199 sch, &q->ingress_block_info);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100200
201 net_dec_ingress_queue();
202 net_dec_egress_queue();
203}
204
205static const struct Qdisc_class_ops clsact_class_ops = {
206 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700207 .find = clsact_find,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100208 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200209 .tcf_block = clsact_tcf_block,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100210 .bind_tcf = clsact_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700211 .unbind_tcf = ingress_unbind_filter,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100212};
213
214static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
215 .cl_ops = &clsact_class_ops,
216 .id = "clsact",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200217 .priv_size = sizeof(struct clsact_sched_data),
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100218 .init = clsact_init,
219 .destroy = clsact_destroy,
220 .dump = ingress_dump,
221 .owner = THIS_MODULE,
222};
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int __init ingress_module_init(void)
225{
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100226 int ret;
227
228 ret = register_qdisc(&ingress_qdisc_ops);
229 if (!ret) {
230 ret = register_qdisc(&clsact_qdisc_ops);
231 if (ret)
232 unregister_qdisc(&ingress_qdisc_ops);
233 }
234
235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800237
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900238static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 unregister_qdisc(&ingress_qdisc_ops);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100241 unregister_qdisc(&clsact_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800243
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200244module_init(ingress_module_init);
245module_exit(ingress_module_exit);
246
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100247MODULE_ALIAS("sch_clsact");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248MODULE_LICENSE("GPL");