blob: a6f175e6401681b83a5d846a0e7674af8f99ec05 [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
Alexander Aringe63d7df2017-12-20 12:35:13 -050065static int ingress_init(struct Qdisc *sch, struct nlattr *opt,
66 struct netlink_ext_ack *extack)
Daniel Borkmann45771392015-04-10 23:07:54 +020067{
Jiri Pirko6529eab2017-05-17 11:07:55 +020068 struct ingress_sched_data *q = qdisc_priv(sch);
69 struct net_device *dev = qdisc_dev(sch);
70 int err;
71
Jiri Pirkob59e6972017-12-15 12:40:13 +010072 net_inc_ingress_queue();
73
Jiri Pirko46209402017-11-03 11:46:25 +010074 mini_qdisc_pair_init(&q->miniqp, sch, &dev->miniq_ingress);
75
Jiri Pirko6e40cf22017-10-19 15:50:30 +020076 q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010077 q->block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +010078 q->block_info.chain_head_change_priv = &q->miniqp;
Jiri Pirko6e40cf22017-10-19 15:50:30 +020079
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010080 err = tcf_block_get_ext(&q->block, sch, &q->block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +020081 if (err)
82 return err;
83
Alexei Starovoitov087c1a62015-04-30 20:14:07 -070084 sch->flags |= TCQ_F_CPUSTATS;
Daniel Borkmann45771392015-04-10 23:07:54 +020085
86 return 0;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void ingress_destroy(struct Qdisc *sch)
90{
Jiri Pirko6529eab2017-05-17 11:07:55 +020091 struct ingress_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +010093 tcf_block_put_ext(q->block, sch, &q->block_info);
Daniel Borkmann45771392015-04-10 23:07:54 +020094 net_dec_ingress_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
98{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080099 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800101 nest = nla_nest_start(skb, TCA_OPTIONS);
102 if (nest == NULL)
103 goto nla_put_failure;
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200104
Yang Yingliangd59b7d82014-03-12 10:20:32 +0800105 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Patrick McHardy1e904742008-01-22 22:11:17 -0800107nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800108 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -1;
110}
111
Eric Dumazet20fea082007-11-14 01:44:41 -0800112static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700114 .find = ingress_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200116 .tcf_block = ingress_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .bind_tcf = ingress_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700118 .unbind_tcf = ingress_unbind_filter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Eric Dumazet20fea082007-11-14 01:44:41 -0800121static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .cl_ops = &ingress_class_ops,
123 .id = "ingress",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200124 .priv_size = sizeof(struct ingress_sched_data),
Daniel Borkmann45771392015-04-10 23:07:54 +0200125 .init = ingress_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .dump = ingress_dump,
128 .owner = THIS_MODULE,
129};
130
Jiri Pirko6529eab2017-05-17 11:07:55 +0200131struct clsact_sched_data {
132 struct tcf_block *ingress_block;
133 struct tcf_block *egress_block;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200134 struct tcf_block_ext_info ingress_block_info;
135 struct tcf_block_ext_info egress_block_info;
Jiri Pirko46209402017-11-03 11:46:25 +0100136 struct mini_Qdisc_pair miniqp_ingress;
137 struct mini_Qdisc_pair miniqp_egress;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200138};
139
WANG Cong143976c2017-08-24 16:51:29 -0700140static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100141{
142 switch (TC_H_MIN(classid)) {
143 case TC_H_MIN(TC_H_MIN_INGRESS):
144 case TC_H_MIN(TC_H_MIN_EGRESS):
145 return TC_H_MIN(classid);
146 default:
147 return 0;
148 }
149}
150
151static unsigned long clsact_bind_filter(struct Qdisc *sch,
152 unsigned long parent, u32 classid)
153{
WANG Cong143976c2017-08-24 16:51:29 -0700154 return clsact_find(sch, classid);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100155}
156
Jiri Pirko6529eab2017-05-17 11:07:55 +0200157static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100158{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200159 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100160
161 switch (cl) {
162 case TC_H_MIN(TC_H_MIN_INGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200163 return q->ingress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100164 case TC_H_MIN(TC_H_MIN_EGRESS):
Jiri Pirko6529eab2017-05-17 11:07:55 +0200165 return q->egress_block;
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100166 default:
167 return NULL;
168 }
169}
170
Alexander Aringe63d7df2017-12-20 12:35:13 -0500171static int clsact_init(struct Qdisc *sch, struct nlattr *opt,
172 struct netlink_ext_ack *extack)
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100173{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200174 struct clsact_sched_data *q = qdisc_priv(sch);
175 struct net_device *dev = qdisc_dev(sch);
176 int err;
177
Jiri Pirkob59e6972017-12-15 12:40:13 +0100178 net_inc_ingress_queue();
179 net_inc_egress_queue();
180
Jiri Pirko46209402017-11-03 11:46:25 +0100181 mini_qdisc_pair_init(&q->miniqp_ingress, sch, &dev->miniq_ingress);
182
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200183 q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100184 q->ingress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100185 q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200186
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100187 err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200188 if (err)
189 return err;
190
Jiri Pirko46209402017-11-03 11:46:25 +0100191 mini_qdisc_pair_init(&q->miniqp_egress, sch, &dev->miniq_egress);
192
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200193 q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100194 q->egress_block_info.chain_head_change = clsact_chain_head_change;
Jiri Pirko46209402017-11-03 11:46:25 +0100195 q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
Jiri Pirko6e40cf22017-10-19 15:50:30 +0200196
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100197 err = tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200198 if (err)
Jiri Pirko343723d2017-12-15 12:40:12 +0100199 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200200
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100201 sch->flags |= TCQ_F_CPUSTATS;
202
203 return 0;
204}
205
206static void clsact_destroy(struct Qdisc *sch)
207{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200208 struct clsact_sched_data *q = qdisc_priv(sch);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100209
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100210 tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
211 tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100212
213 net_dec_ingress_queue();
214 net_dec_egress_queue();
215}
216
217static const struct Qdisc_class_ops clsact_class_ops = {
218 .leaf = ingress_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700219 .find = clsact_find,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100220 .walk = ingress_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200221 .tcf_block = clsact_tcf_block,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100222 .bind_tcf = clsact_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700223 .unbind_tcf = ingress_unbind_filter,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100224};
225
226static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
227 .cl_ops = &clsact_class_ops,
228 .id = "clsact",
Jiri Pirko6529eab2017-05-17 11:07:55 +0200229 .priv_size = sizeof(struct clsact_sched_data),
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100230 .init = clsact_init,
231 .destroy = clsact_destroy,
232 .dump = ingress_dump,
233 .owner = THIS_MODULE,
234};
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static int __init ingress_module_init(void)
237{
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100238 int ret;
239
240 ret = register_qdisc(&ingress_qdisc_ops);
241 if (!ret) {
242 ret = register_qdisc(&clsact_qdisc_ops);
243 if (ret)
244 unregister_qdisc(&ingress_qdisc_ops);
245 }
246
247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800249
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900250static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unregister_qdisc(&ingress_qdisc_ops);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100253 unregister_qdisc(&clsact_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800255
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200256module_init(ingress_module_init);
257module_exit(ingress_module_exit);
258
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100259MODULE_ALIAS("sch_clsact");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260MODULE_LICENSE("GPL");