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>
16 /* 1 band FIFO pseudo-"scheduler" */
18 static int bfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
19 struct sk_buff
**to_free
)
21 if (likely(sch
->qstats
.backlog
+ qdisc_pkt_len(skb
) <= sch
->limit
))
22 return qdisc_enqueue_tail(skb
, sch
);
24 return qdisc_drop(skb
, sch
, to_free
);
27 static int pfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
28 struct sk_buff
**to_free
)
30 if (likely(sch
->q
.qlen
< sch
->limit
))
31 return qdisc_enqueue_tail(skb
, sch
);
33 return qdisc_drop(skb
, sch
, to_free
);
36 static int pfifo_tail_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
37 struct sk_buff
**to_free
)
39 unsigned int prev_backlog
;
41 if (likely(sch
->q
.qlen
< sch
->limit
))
42 return qdisc_enqueue_tail(skb
, sch
);
44 prev_backlog
= sch
->qstats
.backlog
;
45 /* queue full, remove one skb to fulfill the limit */
46 __qdisc_queue_drop_head(sch
, &sch
->q
, to_free
);
47 qdisc_qstats_drop(sch
);
48 qdisc_enqueue_tail(skb
, sch
);
50 qdisc_tree_reduce_backlog(sch
, 0, prev_backlog
- sch
->qstats
.backlog
);
54 static int fifo_init(struct Qdisc
*sch
, struct nlattr
*opt
,
55 struct netlink_ext_ack
*extack
)
58 bool is_bfifo
= sch
->ops
== &bfifo_qdisc_ops
;
61 u32 limit
= qdisc_dev(sch
)->tx_queue_len
;
64 limit
*= psched_mtu(qdisc_dev(sch
));
68 struct tc_fifo_qopt
*ctl
= nla_data(opt
);
70 if (nla_len(opt
) < sizeof(*ctl
))
73 sch
->limit
= ctl
->limit
;
77 bypass
= sch
->limit
>= psched_mtu(qdisc_dev(sch
));
79 bypass
= sch
->limit
>= 1;
82 sch
->flags
|= TCQ_F_CAN_BYPASS
;
84 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
88 static int fifo_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
90 struct tc_fifo_qopt opt
= { .limit
= sch
->limit
};
92 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
100 struct Qdisc_ops pfifo_qdisc_ops __read_mostly
= {
103 .enqueue
= pfifo_enqueue
,
104 .dequeue
= qdisc_dequeue_head
,
105 .peek
= qdisc_peek_head
,
107 .reset
= qdisc_reset_queue
,
110 .owner
= THIS_MODULE
,
112 EXPORT_SYMBOL(pfifo_qdisc_ops
);
114 struct Qdisc_ops bfifo_qdisc_ops __read_mostly
= {
117 .enqueue
= bfifo_enqueue
,
118 .dequeue
= qdisc_dequeue_head
,
119 .peek
= qdisc_peek_head
,
121 .reset
= qdisc_reset_queue
,
124 .owner
= THIS_MODULE
,
126 EXPORT_SYMBOL(bfifo_qdisc_ops
);
128 struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly
= {
129 .id
= "pfifo_head_drop",
131 .enqueue
= pfifo_tail_enqueue
,
132 .dequeue
= qdisc_dequeue_head
,
133 .peek
= qdisc_peek_head
,
135 .reset
= qdisc_reset_queue
,
138 .owner
= THIS_MODULE
,
141 /* Pass size change message down to embedded FIFO */
142 int fifo_set_limit(struct Qdisc
*q
, unsigned int limit
)
147 /* Hack to avoid sending change message to non-FIFO */
148 if (strncmp(q
->ops
->id
+ 1, "fifo", 4) != 0)
151 nla
= kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt
)), GFP_KERNEL
);
153 nla
->nla_type
= RTM_NEWQDISC
;
154 nla
->nla_len
= nla_attr_size(sizeof(struct tc_fifo_qopt
));
155 ((struct tc_fifo_qopt
*)nla_data(nla
))->limit
= limit
;
157 ret
= q
->ops
->change(q
, nla
, NULL
);
162 EXPORT_SYMBOL(fifo_set_limit
);
164 struct Qdisc
*fifo_create_dflt(struct Qdisc
*sch
, struct Qdisc_ops
*ops
,
166 struct netlink_ext_ack
*extack
)
171 q
= qdisc_create_dflt(sch
->dev_queue
, ops
, TC_H_MAKE(sch
->handle
, 1),
174 err
= fifo_set_limit(q
, limit
);
181 return q
? : ERR_PTR(err
);
183 EXPORT_SYMBOL(fifo_create_dflt
);