blob: fc1286f499c1462ab29c5054f734237788974e0e [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 Pirko46209402017-11-03 11:46:25 +010024 struct mini_Qdisc_pair miniqp;
Jiri Pirko6529eab2017-05-17 11:07:55 +020025};
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
28{
29 return NULL;
30}
31
WANG Cong143976c2017-08-24 16:51:29 -070032static unsigned long ingress_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return TC_H_MIN(classid) + 1;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080038 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
WANG Cong143976c2017-08-24 16:51:29 -070040 return ingress_find(sch, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
WANG Cong143976c2017-08-24 16:51:29 -070043static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45}
46
Patrick McHardy58f4df42008-01-21 00:11:01 -080047static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Jiri Pirko6529eab2017-05-17 11:07:55 +020051static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Jiri Pirko6529eab2017-05-17 11:07:55 +020053 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Pirko6529eab2017-05-17 11:07:55 +020055 return q->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010058static void clsact_chain_head_change(struct tcf_proto *tp_head, void *priv)
59{
Jiri Pirko46209402017-11-03 11:46:25 +010060 struct mini_Qdisc_pair *miniqp = priv;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010061
Jiri Pirko46209402017-11-03 11:46:25 +010062 mini_qdisc_pair_swap(miniqp, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010063}
64
Daniel Borkmann45771392015-04-10 23:07:54 +020065static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
66{
Jiri Pirko6529eab2017-05-17 11:07:55 +020067 struct ingress_sched_data *q = qdisc_priv(sch);
68 struct net_device *dev = qdisc_dev(sch);
69 int err;
70
Jiri Pirkob59e6972017-12-15 12:40:13 +010071 net_inc_ingress_queue();
72
Jiri Pirko46209402017-11-03 11:46:25 +010073 mini_qdisc_pair_init(&q->miniqp, sch, &dev->miniq_ingress);
74
Jiri Pirko6e40cf22017-10-19 15:50:30 +020075 q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010076 q->block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +010077 q->block_info.chain_head_change_priv = &q->miniqp;
Jiri Pirko6e40cf22017-10-19 15:50:30 +020078
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010079 err = tcf_block_get_ext(&q->block, sch, &q->block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +020080 if (err)
81 return err;
82
Alexei Starovoitov087c1a62015-04-30 20:14:07 -070083 sch->flags |= TCQ_F_CPUSTATS;
Daniel Borkmann45771392015-04-10 23:07:54 +020084
85 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void ingress_destroy(struct Qdisc *sch)
89{
Jiri Pirko6529eab2017-05-17 11:07:55 +020090 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010092 tcf_block_put_ext(q->block, sch, &q->block_info);
Daniel Borkmann45771392015-04-10 23:07:54 +020093 net_dec_ingress_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
97{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080098 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800100 nest = nla_nest_start(skb, TCA_OPTIONS);
101 if (nest == NULL)
102 goto nla_put_failure;
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200103
Yang Yingliangd59b7d82014-03-12 10:20:32 +0800104 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Patrick McHardy1e904742008-01-22 22:11:17 -0800106nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800107 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -1;
109}
110
Eric Dumazet20fea082007-11-14 01:44:41 -0800111static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700113 .find = ingress_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200115 .tcf_block = ingress_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .bind_tcf = ingress_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700117 .unbind_tcf = ingress_unbind_filter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Eric Dumazet20fea082007-11-14 01:44:41 -0800120static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .cl_ops = &ingress_class_ops,
122 .id = "ingress",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200123 .priv_size = sizeof(struct ingress_sched_data),
Daniel Borkmann45771392015-04-10 23:07:54 +0200124 .init = ingress_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .dump = ingress_dump,
127 .owner = THIS_MODULE,
128};
129
Jiri Pirko6529eab2017-05-17 11:07:55 +0200130struct clsact_sched_data {
131 struct tcf_block *ingress_block;
132 struct tcf_block *egress_block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200133 struct tcf_block_ext_info ingress_block_info;
134 struct tcf_block_ext_info egress_block_info;
Jiri Pirko46209402017-11-03 11:46:25 +0100135 struct mini_Qdisc_pair miniqp_ingress;
136 struct mini_Qdisc_pair miniqp_egress;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200137};
138
WANG Cong143976c2017-08-24 16:51:29 -0700139static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100140{
141 switch (TC_H_MIN(classid)) {
142 case TC_H_MIN(TC_H_MIN_INGRESS):
143 case TC_H_MIN(TC_H_MIN_EGRESS):
144 return TC_H_MIN(classid);
145 default:
146 return 0;
147 }
148}
149
150static unsigned long clsact_bind_filter(struct Qdisc *sch,
151 unsigned long parent, u32 classid)
152{
WANG Cong143976c2017-08-24 16:51:29 -0700153 return clsact_find(sch, classid);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100154}
155
Jiri Pirko6529eab2017-05-17 11:07:55 +0200156static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100157{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200158 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100159
160 switch (cl) {
161 case TC_H_MIN(TC_H_MIN_INGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200162 return q->ingress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100163 case TC_H_MIN(TC_H_MIN_EGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200164 return q->egress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100165 default:
166 return NULL;
167 }
168}
169
170static int clsact_init(struct Qdisc *sch, struct nlattr *opt)
171{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200172 struct clsact_sched_data *q = qdisc_priv(sch);
173 struct net_device *dev = qdisc_dev(sch);
174 int err;
175
Jiri Pirkob59e6972017-12-15 12:40:13 +0100176 net_inc_ingress_queue();
177 net_inc_egress_queue();
178
Jiri Pirko46209402017-11-03 11:46:25 +0100179 mini_qdisc_pair_init(&q->miniqp_ingress, sch, &dev->miniq_ingress);
180
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200181 q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100182 q->ingress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100183 q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200184
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100185 err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200186 if (err)
187 return err;
188
Jiri Pirko46209402017-11-03 11:46:25 +0100189 mini_qdisc_pair_init(&q->miniqp_egress, sch, &dev->miniq_egress);
190
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200191 q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100192 q->egress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100193 q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200194
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100195 err = tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200196 if (err)
Jiri Pirko343723d2017-12-15 12:40:12 +0100197 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200198
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100199 sch->flags |= TCQ_F_CPUSTATS;
200
201 return 0;
202}
203
204static void clsact_destroy(struct Qdisc *sch)
205{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200206 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100207
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100208 tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
209 tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100210
211 net_dec_ingress_queue();
212 net_dec_egress_queue();
213}
214
215static const struct Qdisc_class_ops clsact_class_ops = {
216 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700217 .find = clsact_find,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100218 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200219 .tcf_block = clsact_tcf_block,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100220 .bind_tcf = clsact_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700221 .unbind_tcf = ingress_unbind_filter,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100222};
223
224static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
225 .cl_ops = &clsact_class_ops,
226 .id = "clsact",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200227 .priv_size = sizeof(struct clsact_sched_data),
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100228 .init = clsact_init,
229 .destroy = clsact_destroy,
230 .dump = ingress_dump,
231 .owner = THIS_MODULE,
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int __init ingress_module_init(void)
235{
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100236 int ret;
237
238 ret = register_qdisc(&ingress_qdisc_ops);
239 if (!ret) {
240 ret = register_qdisc(&clsact_qdisc_ops);
241 if (ret)
242 unregister_qdisc(&ingress_qdisc_ops);
243 }
244
245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800247
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900248static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 unregister_qdisc(&ingress_qdisc_ops);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100251 unregister_qdisc(&clsact_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800253
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200254module_init(ingress_module_init);
255module_exit(ingress_module_exit);
256
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100257MODULE_ALIAS("sch_clsact");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258MODULE_LICENSE("GPL");