1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/sch_fifo.c The simplest FIFO queue.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/skbuff.h>
14 #include <net/pkt_sched.h>
15 #include <net/pkt_cls.h>
17 /* 1 band FIFO pseudo-"scheduler" */
19 static int bfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
20 struct sk_buff
**to_free
)
22 if (likely(sch
->qstats
.backlog
+ qdisc_pkt_len(skb
) <= sch
->limit
))
23 return qdisc_enqueue_tail(skb
, sch
);
25 return qdisc_drop(skb
, sch
, to_free
);
28 static int pfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
29 struct sk_buff
**to_free
)
31 if (likely(sch
->q
.qlen
< sch
->limit
))
32 return qdisc_enqueue_tail(skb
, sch
);
34 return qdisc_drop(skb
, sch
, to_free
);
37 static int pfifo_tail_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
38 struct sk_buff
**to_free
)
40 unsigned int prev_backlog
;
42 if (likely(sch
->q
.qlen
< sch
->limit
))
43 return qdisc_enqueue_tail(skb
, sch
);
45 prev_backlog
= sch
->qstats
.backlog
;
46 /* queue full, remove one skb to fulfill the limit */
47 __qdisc_queue_drop_head(sch
, &sch
->q
, to_free
);
48 qdisc_qstats_drop(sch
);
49 qdisc_enqueue_tail(skb
, sch
);
51 qdisc_tree_reduce_backlog(sch
, 0, prev_backlog
- sch
->qstats
.backlog
);
55 static void fifo_offload_init(struct Qdisc
*sch
)
57 struct net_device
*dev
= qdisc_dev(sch
);
58 struct tc_fifo_qopt_offload qopt
;
60 if (!tc_can_offload(dev
) || !dev
->netdev_ops
->ndo_setup_tc
)
63 qopt
.command
= TC_FIFO_REPLACE
;
64 qopt
.handle
= sch
->handle
;
65 qopt
.parent
= sch
->parent
;
66 dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_QDISC_FIFO
, &qopt
);
69 static void fifo_offload_destroy(struct Qdisc
*sch
)
71 struct net_device
*dev
= qdisc_dev(sch
);
72 struct tc_fifo_qopt_offload qopt
;
74 if (!tc_can_offload(dev
) || !dev
->netdev_ops
->ndo_setup_tc
)
77 qopt
.command
= TC_FIFO_DESTROY
;
78 qopt
.handle
= sch
->handle
;
79 qopt
.parent
= sch
->parent
;
80 dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_QDISC_FIFO
, &qopt
);
83 static int fifo_offload_dump(struct Qdisc
*sch
)
85 struct tc_fifo_qopt_offload qopt
;
87 qopt
.command
= TC_FIFO_STATS
;
88 qopt
.handle
= sch
->handle
;
89 qopt
.parent
= sch
->parent
;
90 qopt
.stats
.bstats
= &sch
->bstats
;
91 qopt
.stats
.qstats
= &sch
->qstats
;
93 return qdisc_offload_dump_helper(sch
, TC_SETUP_QDISC_FIFO
, &qopt
);
96 static int __fifo_init(struct Qdisc
*sch
, struct nlattr
*opt
,
97 struct netlink_ext_ack
*extack
)
100 bool is_bfifo
= sch
->ops
== &bfifo_qdisc_ops
;
103 u32 limit
= qdisc_dev(sch
)->tx_queue_len
;
106 limit
*= psched_mtu(qdisc_dev(sch
));
110 struct tc_fifo_qopt
*ctl
= nla_data(opt
);
112 if (nla_len(opt
) < sizeof(*ctl
))
115 sch
->limit
= ctl
->limit
;
119 bypass
= sch
->limit
>= psched_mtu(qdisc_dev(sch
));
121 bypass
= sch
->limit
>= 1;
124 sch
->flags
|= TCQ_F_CAN_BYPASS
;
126 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
131 static int fifo_init(struct Qdisc
*sch
, struct nlattr
*opt
,
132 struct netlink_ext_ack
*extack
)
136 err
= __fifo_init(sch
, opt
, extack
);
140 fifo_offload_init(sch
);
144 static int fifo_hd_init(struct Qdisc
*sch
, struct nlattr
*opt
,
145 struct netlink_ext_ack
*extack
)
147 return __fifo_init(sch
, opt
, extack
);
150 static void fifo_destroy(struct Qdisc
*sch
)
152 fifo_offload_destroy(sch
);
155 static int __fifo_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
157 struct tc_fifo_qopt opt
= { .limit
= sch
->limit
};
159 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
160 goto nla_put_failure
;
167 static int fifo_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
171 err
= fifo_offload_dump(sch
);
175 return __fifo_dump(sch
, skb
);
178 static int fifo_hd_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
180 return __fifo_dump(sch
, skb
);
183 struct Qdisc_ops pfifo_qdisc_ops __read_mostly
= {
186 .enqueue
= pfifo_enqueue
,
187 .dequeue
= qdisc_dequeue_head
,
188 .peek
= qdisc_peek_head
,
190 .destroy
= fifo_destroy
,
191 .reset
= qdisc_reset_queue
,
194 .owner
= THIS_MODULE
,
196 EXPORT_SYMBOL(pfifo_qdisc_ops
);
198 struct Qdisc_ops bfifo_qdisc_ops __read_mostly
= {
201 .enqueue
= bfifo_enqueue
,
202 .dequeue
= qdisc_dequeue_head
,
203 .peek
= qdisc_peek_head
,
205 .destroy
= fifo_destroy
,
206 .reset
= qdisc_reset_queue
,
209 .owner
= THIS_MODULE
,
211 EXPORT_SYMBOL(bfifo_qdisc_ops
);
213 struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly
= {
214 .id
= "pfifo_head_drop",
216 .enqueue
= pfifo_tail_enqueue
,
217 .dequeue
= qdisc_dequeue_head
,
218 .peek
= qdisc_peek_head
,
219 .init
= fifo_hd_init
,
220 .reset
= qdisc_reset_queue
,
221 .change
= fifo_hd_init
,
222 .dump
= fifo_hd_dump
,
223 .owner
= THIS_MODULE
,
226 /* Pass size change message down to embedded FIFO */
227 int fifo_set_limit(struct Qdisc
*q
, unsigned int limit
)
232 /* Hack to avoid sending change message to non-FIFO */
233 if (strncmp(q
->ops
->id
+ 1, "fifo", 4) != 0)
236 nla
= kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt
)), GFP_KERNEL
);
238 nla
->nla_type
= RTM_NEWQDISC
;
239 nla
->nla_len
= nla_attr_size(sizeof(struct tc_fifo_qopt
));
240 ((struct tc_fifo_qopt
*)nla_data(nla
))->limit
= limit
;
242 ret
= q
->ops
->change(q
, nla
, NULL
);
247 EXPORT_SYMBOL(fifo_set_limit
);
249 struct Qdisc
*fifo_create_dflt(struct Qdisc
*sch
, struct Qdisc_ops
*ops
,
251 struct netlink_ext_ack
*extack
)
256 q
= qdisc_create_dflt(sch
->dev_queue
, ops
, TC_H_MAKE(sch
->handle
, 1),
259 err
= fifo_set_limit(q
, limit
);
266 return q
? : ERR_PTR(err
);
268 EXPORT_SYMBOL(fifo_create_dflt
);