blob: 84838128b9c5b53db597c3923ca98ea5326081d7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Borkmann1f211a12016-01-07 22:29:47 +01002/* net/sched/sch_ingress.c - Ingress and clsact qdisc
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Jamal Hadi Salim 1999
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/types.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -07009#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/rtnetlink.h>
Daniel Borkmannd2788d32015-05-09 22:51:32 +020012
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070013#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010015#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Jiri Pirko6529eab2017-05-17 11:07:55 +020017struct ingress_sched_data {
18 struct tcf_block *block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +020019 struct tcf_block_ext_info block_info;
Jiri Pirko46209402017-11-03 11:46:25 +010020 struct mini_Qdisc_pair miniqp;
Jiri Pirko6529eab2017-05-17 11:07:55 +020021};
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
24{
25 return NULL;
26}
27
WANG Cong143976c2017-08-24 16:51:29 -070028static unsigned long ingress_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return TC_H_MIN(classid) + 1;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080034 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
WANG Cong143976c2017-08-24 16:51:29 -070036 return ingress_find(sch, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
WANG Cong143976c2017-08-24 16:51:29 -070039static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41}
42
Patrick McHardy58f4df42008-01-21 00:11:01 -080043static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Alexander Aringcbaacc42017-12-20 12:35:16 -050047static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl,
48 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Jiri Pirko6529eab2017-05-17 11:07:55 +020050 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jiri Pirko6529eab2017-05-17 11:07:55 +020052 return q->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010055static void clsact_chain_head_change(struct tcf_proto *tp_head, void *priv)
56{
Jiri Pirko46209402017-11-03 11:46:25 +010057 struct mini_Qdisc_pair *miniqp = priv;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010058
Jiri Pirko46209402017-11-03 11:46:25 +010059 mini_qdisc_pair_swap(miniqp, tp_head);
Jiri Pirko51ab2992018-01-17 11:46:53 +010060};
61
62static void ingress_ingress_block_set(struct Qdisc *sch, u32 block_index)
63{
64 struct ingress_sched_data *q = qdisc_priv(sch);
65
66 q->block_info.block_index = block_index;
67}
68
69static u32 ingress_ingress_block_get(struct Qdisc *sch)
70{
71 struct ingress_sched_data *q = qdisc_priv(sch);
72
73 return q->block_info.block_index;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010074}
75
Alexander Aringe63d7df2017-12-20 12:35:13 -050076static int ingress_init(struct Qdisc *sch, struct nlattr *opt,
77 struct netlink_ext_ack *extack)
Daniel Borkmann45771392015-04-10 23:07:54 +020078{
Jiri Pirko6529eab2017-05-17 11:07:55 +020079 struct ingress_sched_data *q = qdisc_priv(sch);
80 struct net_device *dev = qdisc_dev(sch);
Paul Blakey7d17c542020-02-16 12:01:22 +020081 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +020082
Jiri Pirkob59e6972017-12-15 12:40:13 +010083 net_inc_ingress_queue();
84
Jiri Pirko46209402017-11-03 11:46:25 +010085 mini_qdisc_pair_init(&q->miniqp, sch, &dev->miniq_ingress);
86
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +020087 q->block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010088 q->block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +010089 q->block_info.chain_head_change_priv = &q->miniqp;
Jiri Pirko6e40cf22017-10-19 15:50:30 +020090
Paul Blakey7d17c542020-02-16 12:01:22 +020091 err = tcf_block_get_ext(&q->block, sch, &q->block_info, extack);
92 if (err)
93 return err;
94
95 mini_qdisc_pair_block_init(&q->miniqp, q->block);
96
97 return 0;
Daniel Borkmann45771392015-04-10 23:07:54 +020098}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void ingress_destroy(struct Qdisc *sch)
101{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200102 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100104 tcf_block_put_ext(q->block, sch, &q->block_info);
Daniel Borkmann45771392015-04-10 23:07:54 +0200105 net_dec_ingress_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
109{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800110 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200112 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800113 if (nest == NULL)
114 goto nla_put_failure;
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200115
Yang Yingliangd59b7d82014-03-12 10:20:32 +0800116 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Patrick McHardy1e904742008-01-22 22:11:17 -0800118nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800119 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -1;
121}
122
Eric Dumazet20fea082007-11-14 01:44:41 -0800123static const struct Qdisc_class_ops ingress_class_ops = {
Vlad Buslov7a096d52019-06-12 10:14:35 +0300124 .flags = QDISC_CLASS_OPS_DOIT_UNLOCKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700126 .find = ingress_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200128 .tcf_block = ingress_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .bind_tcf = ingress_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700130 .unbind_tcf = ingress_unbind_filter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Eric Dumazet20fea082007-11-14 01:44:41 -0800133static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Jiri Pirko51ab2992018-01-17 11:46:53 +0100134 .cl_ops = &ingress_class_ops,
135 .id = "ingress",
136 .priv_size = sizeof(struct ingress_sched_data),
137 .static_flags = TCQ_F_CPUSTATS,
138 .init = ingress_init,
139 .destroy = ingress_destroy,
140 .dump = ingress_dump,
141 .ingress_block_set = ingress_ingress_block_set,
142 .ingress_block_get = ingress_ingress_block_get,
143 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Jiri Pirko6529eab2017-05-17 11:07:55 +0200146struct clsact_sched_data {
147 struct tcf_block *ingress_block;
148 struct tcf_block *egress_block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200149 struct tcf_block_ext_info ingress_block_info;
150 struct tcf_block_ext_info egress_block_info;
Jiri Pirko46209402017-11-03 11:46:25 +0100151 struct mini_Qdisc_pair miniqp_ingress;
152 struct mini_Qdisc_pair miniqp_egress;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200153};
154
WANG Cong143976c2017-08-24 16:51:29 -0700155static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100156{
157 switch (TC_H_MIN(classid)) {
158 case TC_H_MIN(TC_H_MIN_INGRESS):
159 case TC_H_MIN(TC_H_MIN_EGRESS):
160 return TC_H_MIN(classid);
161 default:
162 return 0;
163 }
164}
165
166static unsigned long clsact_bind_filter(struct Qdisc *sch,
167 unsigned long parent, u32 classid)
168{
WANG Cong143976c2017-08-24 16:51:29 -0700169 return clsact_find(sch, classid);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100170}
171
Alexander Aringcbaacc42017-12-20 12:35:16 -0500172static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl,
173 struct netlink_ext_ack *extack)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100174{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200175 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100176
177 switch (cl) {
178 case TC_H_MIN(TC_H_MIN_INGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200179 return q->ingress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100180 case TC_H_MIN(TC_H_MIN_EGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200181 return q->egress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100182 default:
183 return NULL;
184 }
185}
186
Jiri Pirko51ab2992018-01-17 11:46:53 +0100187static void clsact_ingress_block_set(struct Qdisc *sch, u32 block_index)
188{
189 struct clsact_sched_data *q = qdisc_priv(sch);
190
191 q->ingress_block_info.block_index = block_index;
192}
193
194static void clsact_egress_block_set(struct Qdisc *sch, u32 block_index)
195{
196 struct clsact_sched_data *q = qdisc_priv(sch);
197
198 q->egress_block_info.block_index = block_index;
199}
200
201static u32 clsact_ingress_block_get(struct Qdisc *sch)
202{
203 struct clsact_sched_data *q = qdisc_priv(sch);
204
205 return q->ingress_block_info.block_index;
206}
207
208static u32 clsact_egress_block_get(struct Qdisc *sch)
209{
210 struct clsact_sched_data *q = qdisc_priv(sch);
211
212 return q->egress_block_info.block_index;
213}
214
Alexander Aringe63d7df2017-12-20 12:35:13 -0500215static int clsact_init(struct Qdisc *sch, struct nlattr *opt,
216 struct netlink_ext_ack *extack)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100217{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200218 struct clsact_sched_data *q = qdisc_priv(sch);
219 struct net_device *dev = qdisc_dev(sch);
220 int err;
221
Jiri Pirkob59e6972017-12-15 12:40:13 +0100222 net_inc_ingress_queue();
223 net_inc_egress_queue();
224
Jiri Pirko46209402017-11-03 11:46:25 +0100225 mini_qdisc_pair_init(&q->miniqp_ingress, sch, &dev->miniq_ingress);
226
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +0200227 q->ingress_block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100228 q->ingress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100229 q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200230
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500231 err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info,
232 extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200233 if (err)
234 return err;
235
Paul Blakey7d17c542020-02-16 12:01:22 +0200236 mini_qdisc_pair_block_init(&q->miniqp_ingress, q->ingress_block);
237
Jiri Pirko46209402017-11-03 11:46:25 +0100238 mini_qdisc_pair_init(&q->miniqp_egress, sch, &dev->miniq_egress);
239
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +0200240 q->egress_block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100241 q->egress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100242 q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200243
David S. Millerc02b3742018-01-17 00:00:25 -0500244 return tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info, extack);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100245}
246
247static void clsact_destroy(struct Qdisc *sch)
248{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200249 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100250
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100251 tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
252 tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100253
254 net_dec_ingress_queue();
255 net_dec_egress_queue();
256}
257
258static const struct Qdisc_class_ops clsact_class_ops = {
Vlad Buslov87f37392019-06-13 19:12:05 +0300259 .flags = QDISC_CLASS_OPS_DOIT_UNLOCKED,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100260 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700261 .find = clsact_find,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100262 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200263 .tcf_block = clsact_tcf_block,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100264 .bind_tcf = clsact_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700265 .unbind_tcf = ingress_unbind_filter,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100266};
267
268static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
Jiri Pirko51ab2992018-01-17 11:46:53 +0100269 .cl_ops = &clsact_class_ops,
270 .id = "clsact",
271 .priv_size = sizeof(struct clsact_sched_data),
272 .static_flags = TCQ_F_CPUSTATS,
273 .init = clsact_init,
274 .destroy = clsact_destroy,
275 .dump = ingress_dump,
276 .ingress_block_set = clsact_ingress_block_set,
277 .egress_block_set = clsact_egress_block_set,
278 .ingress_block_get = clsact_ingress_block_get,
279 .egress_block_get = clsact_egress_block_get,
280 .owner = THIS_MODULE,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100281};
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int __init ingress_module_init(void)
284{
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100285 int ret;
286
287 ret = register_qdisc(&ingress_qdisc_ops);
288 if (!ret) {
289 ret = register_qdisc(&clsact_qdisc_ops);
290 if (ret)
291 unregister_qdisc(&ingress_qdisc_ops);
292 }
293
294 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800296
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900297static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unregister_qdisc(&ingress_qdisc_ops);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100300 unregister_qdisc(&clsact_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800302
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200303module_init(ingress_module_init);
304module_exit(ingress_module_exit);
305
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100306MODULE_ALIAS("sch_clsact");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307MODULE_LICENSE("GPL");