1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* net/sched/sch_ingress.c - Ingress and clsact qdisc
4 * Authors: Jamal Hadi Salim 1999
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/list.h>
10 #include <linux/skbuff.h>
11 #include <linux/rtnetlink.h>
13 #include <net/netlink.h>
14 #include <net/pkt_sched.h>
15 #include <net/pkt_cls.h>
18 struct ingress_sched_data
{
19 struct tcf_block
*block
;
20 struct tcf_block_ext_info block_info
;
21 struct mini_Qdisc_pair miniqp
;
24 static struct Qdisc
*ingress_leaf(struct Qdisc
*sch
, unsigned long arg
)
29 static unsigned long ingress_find(struct Qdisc
*sch
, u32 classid
)
31 return TC_H_MIN(classid
) + 1;
34 static unsigned long ingress_bind_filter(struct Qdisc
*sch
,
35 unsigned long parent
, u32 classid
)
37 return ingress_find(sch
, classid
);
40 static void ingress_unbind_filter(struct Qdisc
*sch
, unsigned long cl
)
44 static void ingress_walk(struct Qdisc
*sch
, struct qdisc_walker
*walker
)
48 static struct tcf_block
*ingress_tcf_block(struct Qdisc
*sch
, unsigned long cl
,
49 struct netlink_ext_ack
*extack
)
51 struct ingress_sched_data
*q
= qdisc_priv(sch
);
56 static void clsact_chain_head_change(struct tcf_proto
*tp_head
, void *priv
)
58 struct mini_Qdisc_pair
*miniqp
= priv
;
60 mini_qdisc_pair_swap(miniqp
, tp_head
);
63 static void ingress_ingress_block_set(struct Qdisc
*sch
, u32 block_index
)
65 struct ingress_sched_data
*q
= qdisc_priv(sch
);
67 q
->block_info
.block_index
= block_index
;
70 static u32
ingress_ingress_block_get(struct Qdisc
*sch
)
72 struct ingress_sched_data
*q
= qdisc_priv(sch
);
74 return q
->block_info
.block_index
;
77 static int ingress_init(struct Qdisc
*sch
, struct nlattr
*opt
,
78 struct netlink_ext_ack
*extack
)
80 struct ingress_sched_data
*q
= qdisc_priv(sch
);
81 struct net_device
*dev
= qdisc_dev(sch
);
82 struct bpf_mprog_entry
*entry
;
86 if (sch
->parent
!= TC_H_INGRESS
)
89 net_inc_ingress_queue();
91 entry
= tcx_entry_fetch_or_create(dev
, true, &created
);
95 mini_qdisc_pair_init(&q
->miniqp
, sch
, &tcx_entry(entry
)->miniq
);
97 tcx_entry_update(dev
, entry
, true);
99 q
->block_info
.binder_type
= FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS
;
100 q
->block_info
.chain_head_change
= clsact_chain_head_change
;
101 q
->block_info
.chain_head_change_priv
= &q
->miniqp
;
103 err
= tcf_block_get_ext(&q
->block
, sch
, &q
->block_info
, extack
);
107 mini_qdisc_pair_block_init(&q
->miniqp
, q
->block
);
112 static void ingress_destroy(struct Qdisc
*sch
)
114 struct ingress_sched_data
*q
= qdisc_priv(sch
);
115 struct net_device
*dev
= qdisc_dev(sch
);
116 struct bpf_mprog_entry
*entry
= rtnl_dereference(dev
->tcx_ingress
);
118 if (sch
->parent
!= TC_H_INGRESS
)
121 tcf_block_put_ext(q
->block
, sch
, &q
->block_info
);
124 tcx_miniq_dec(entry
);
125 if (!tcx_entry_is_active(entry
)) {
126 tcx_entry_update(dev
, NULL
, true);
127 tcx_entry_free(entry
);
131 net_dec_ingress_queue();
134 static int ingress_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
138 nest
= nla_nest_start_noflag(skb
, TCA_OPTIONS
);
140 goto nla_put_failure
;
142 return nla_nest_end(skb
, nest
);
145 nla_nest_cancel(skb
, nest
);
149 static const struct Qdisc_class_ops ingress_class_ops
= {
150 .flags
= QDISC_CLASS_OPS_DOIT_UNLOCKED
,
151 .leaf
= ingress_leaf
,
152 .find
= ingress_find
,
153 .walk
= ingress_walk
,
154 .tcf_block
= ingress_tcf_block
,
155 .bind_tcf
= ingress_bind_filter
,
156 .unbind_tcf
= ingress_unbind_filter
,
159 static struct Qdisc_ops ingress_qdisc_ops __read_mostly
= {
160 .cl_ops
= &ingress_class_ops
,
162 .priv_size
= sizeof(struct ingress_sched_data
),
163 .static_flags
= TCQ_F_INGRESS
| TCQ_F_CPUSTATS
,
164 .init
= ingress_init
,
165 .destroy
= ingress_destroy
,
166 .dump
= ingress_dump
,
167 .ingress_block_set
= ingress_ingress_block_set
,
168 .ingress_block_get
= ingress_ingress_block_get
,
169 .owner
= THIS_MODULE
,
171 MODULE_ALIAS_NET_SCH("ingress");
173 struct clsact_sched_data
{
174 struct tcf_block
*ingress_block
;
175 struct tcf_block
*egress_block
;
176 struct tcf_block_ext_info ingress_block_info
;
177 struct tcf_block_ext_info egress_block_info
;
178 struct mini_Qdisc_pair miniqp_ingress
;
179 struct mini_Qdisc_pair miniqp_egress
;
182 static unsigned long clsact_find(struct Qdisc
*sch
, u32 classid
)
184 switch (TC_H_MIN(classid
)) {
185 case TC_H_MIN(TC_H_MIN_INGRESS
):
186 case TC_H_MIN(TC_H_MIN_EGRESS
):
187 return TC_H_MIN(classid
);
193 static unsigned long clsact_bind_filter(struct Qdisc
*sch
,
194 unsigned long parent
, u32 classid
)
196 return clsact_find(sch
, classid
);
199 static struct tcf_block
*clsact_tcf_block(struct Qdisc
*sch
, unsigned long cl
,
200 struct netlink_ext_ack
*extack
)
202 struct clsact_sched_data
*q
= qdisc_priv(sch
);
205 case TC_H_MIN(TC_H_MIN_INGRESS
):
206 return q
->ingress_block
;
207 case TC_H_MIN(TC_H_MIN_EGRESS
):
208 return q
->egress_block
;
214 static void clsact_ingress_block_set(struct Qdisc
*sch
, u32 block_index
)
216 struct clsact_sched_data
*q
= qdisc_priv(sch
);
218 q
->ingress_block_info
.block_index
= block_index
;
221 static void clsact_egress_block_set(struct Qdisc
*sch
, u32 block_index
)
223 struct clsact_sched_data
*q
= qdisc_priv(sch
);
225 q
->egress_block_info
.block_index
= block_index
;
228 static u32
clsact_ingress_block_get(struct Qdisc
*sch
)
230 struct clsact_sched_data
*q
= qdisc_priv(sch
);
232 return q
->ingress_block_info
.block_index
;
235 static u32
clsact_egress_block_get(struct Qdisc
*sch
)
237 struct clsact_sched_data
*q
= qdisc_priv(sch
);
239 return q
->egress_block_info
.block_index
;
242 static int clsact_init(struct Qdisc
*sch
, struct nlattr
*opt
,
243 struct netlink_ext_ack
*extack
)
245 struct clsact_sched_data
*q
= qdisc_priv(sch
);
246 struct net_device
*dev
= qdisc_dev(sch
);
247 struct bpf_mprog_entry
*entry
;
251 if (sch
->parent
!= TC_H_CLSACT
)
254 net_inc_ingress_queue();
255 net_inc_egress_queue();
257 entry
= tcx_entry_fetch_or_create(dev
, true, &created
);
260 tcx_miniq_inc(entry
);
261 mini_qdisc_pair_init(&q
->miniqp_ingress
, sch
, &tcx_entry(entry
)->miniq
);
263 tcx_entry_update(dev
, entry
, true);
265 q
->ingress_block_info
.binder_type
= FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS
;
266 q
->ingress_block_info
.chain_head_change
= clsact_chain_head_change
;
267 q
->ingress_block_info
.chain_head_change_priv
= &q
->miniqp_ingress
;
269 err
= tcf_block_get_ext(&q
->ingress_block
, sch
, &q
->ingress_block_info
,
274 mini_qdisc_pair_block_init(&q
->miniqp_ingress
, q
->ingress_block
);
276 entry
= tcx_entry_fetch_or_create(dev
, false, &created
);
279 tcx_miniq_inc(entry
);
280 mini_qdisc_pair_init(&q
->miniqp_egress
, sch
, &tcx_entry(entry
)->miniq
);
282 tcx_entry_update(dev
, entry
, false);
284 q
->egress_block_info
.binder_type
= FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS
;
285 q
->egress_block_info
.chain_head_change
= clsact_chain_head_change
;
286 q
->egress_block_info
.chain_head_change_priv
= &q
->miniqp_egress
;
288 return tcf_block_get_ext(&q
->egress_block
, sch
, &q
->egress_block_info
, extack
);
291 static void clsact_destroy(struct Qdisc
*sch
)
293 struct clsact_sched_data
*q
= qdisc_priv(sch
);
294 struct net_device
*dev
= qdisc_dev(sch
);
295 struct bpf_mprog_entry
*ingress_entry
= rtnl_dereference(dev
->tcx_ingress
);
296 struct bpf_mprog_entry
*egress_entry
= rtnl_dereference(dev
->tcx_egress
);
298 if (sch
->parent
!= TC_H_CLSACT
)
301 tcf_block_put_ext(q
->ingress_block
, sch
, &q
->ingress_block_info
);
302 tcf_block_put_ext(q
->egress_block
, sch
, &q
->egress_block_info
);
305 tcx_miniq_dec(ingress_entry
);
306 if (!tcx_entry_is_active(ingress_entry
)) {
307 tcx_entry_update(dev
, NULL
, true);
308 tcx_entry_free(ingress_entry
);
313 tcx_miniq_dec(egress_entry
);
314 if (!tcx_entry_is_active(egress_entry
)) {
315 tcx_entry_update(dev
, NULL
, false);
316 tcx_entry_free(egress_entry
);
320 net_dec_ingress_queue();
321 net_dec_egress_queue();
324 static const struct Qdisc_class_ops clsact_class_ops
= {
325 .flags
= QDISC_CLASS_OPS_DOIT_UNLOCKED
,
326 .leaf
= ingress_leaf
,
328 .walk
= ingress_walk
,
329 .tcf_block
= clsact_tcf_block
,
330 .bind_tcf
= clsact_bind_filter
,
331 .unbind_tcf
= ingress_unbind_filter
,
334 static struct Qdisc_ops clsact_qdisc_ops __read_mostly
= {
335 .cl_ops
= &clsact_class_ops
,
337 .priv_size
= sizeof(struct clsact_sched_data
),
338 .static_flags
= TCQ_F_INGRESS
| TCQ_F_CPUSTATS
,
340 .destroy
= clsact_destroy
,
341 .dump
= ingress_dump
,
342 .ingress_block_set
= clsact_ingress_block_set
,
343 .egress_block_set
= clsact_egress_block_set
,
344 .ingress_block_get
= clsact_ingress_block_get
,
345 .egress_block_get
= clsact_egress_block_get
,
346 .owner
= THIS_MODULE
,
348 MODULE_ALIAS_NET_SCH("clsact");
350 static int __init
ingress_module_init(void)
354 ret
= register_qdisc(&ingress_qdisc_ops
);
356 ret
= register_qdisc(&clsact_qdisc_ops
);
358 unregister_qdisc(&ingress_qdisc_ops
);
364 static void __exit
ingress_module_exit(void)
366 unregister_qdisc(&ingress_qdisc_ops
);
367 unregister_qdisc(&clsact_qdisc_ops
);
370 module_init(ingress_module_init
);
371 module_exit(ingress_module_exit
);
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("Ingress and clsact based ingress and egress qdiscs");