2 * net/sched/sch_fifo.c The simplest FIFO queue.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <net/pkt_sched.h>
20 /* 1 band FIFO pseudo-"scheduler" */
22 static int bfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
23 struct sk_buff
**to_free
)
25 if (likely(sch
->qstats
.backlog
+ qdisc_pkt_len(skb
) <= sch
->limit
))
26 return qdisc_enqueue_tail(skb
, sch
);
28 return qdisc_drop(skb
, sch
, to_free
);
31 static int pfifo_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
32 struct sk_buff
**to_free
)
34 if (likely(sch
->q
.qlen
< sch
->limit
))
35 return qdisc_enqueue_tail(skb
, sch
);
37 return qdisc_drop(skb
, sch
, to_free
);
40 static int pfifo_tail_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
41 struct sk_buff
**to_free
)
43 unsigned int prev_backlog
;
45 if (likely(sch
->q
.qlen
< sch
->limit
))
46 return qdisc_enqueue_tail(skb
, sch
);
48 prev_backlog
= sch
->qstats
.backlog
;
49 /* queue full, remove one skb to fulfill the limit */
50 __qdisc_queue_drop_head(sch
, &sch
->q
, to_free
);
51 qdisc_qstats_drop(sch
);
52 qdisc_enqueue_tail(skb
, sch
);
54 qdisc_tree_reduce_backlog(sch
, 0, prev_backlog
- sch
->qstats
.backlog
);
58 static int fifo_init(struct Qdisc
*sch
, struct nlattr
*opt
,
59 struct netlink_ext_ack
*extack
)
62 bool is_bfifo
= sch
->ops
== &bfifo_qdisc_ops
;
65 u32 limit
= qdisc_dev(sch
)->tx_queue_len
;
68 limit
*= psched_mtu(qdisc_dev(sch
));
72 struct tc_fifo_qopt
*ctl
= nla_data(opt
);
74 if (nla_len(opt
) < sizeof(*ctl
))
77 sch
->limit
= ctl
->limit
;
81 bypass
= sch
->limit
>= psched_mtu(qdisc_dev(sch
));
83 bypass
= sch
->limit
>= 1;
86 sch
->flags
|= TCQ_F_CAN_BYPASS
;
88 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
92 static int fifo_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
94 struct tc_fifo_qopt opt
= { .limit
= sch
->limit
};
96 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
104 struct Qdisc_ops pfifo_qdisc_ops __read_mostly
= {
107 .enqueue
= pfifo_enqueue
,
108 .dequeue
= qdisc_dequeue_head
,
109 .peek
= qdisc_peek_head
,
111 .reset
= qdisc_reset_queue
,
114 .owner
= THIS_MODULE
,
116 EXPORT_SYMBOL(pfifo_qdisc_ops
);
118 struct Qdisc_ops bfifo_qdisc_ops __read_mostly
= {
121 .enqueue
= bfifo_enqueue
,
122 .dequeue
= qdisc_dequeue_head
,
123 .peek
= qdisc_peek_head
,
125 .reset
= qdisc_reset_queue
,
128 .owner
= THIS_MODULE
,
130 EXPORT_SYMBOL(bfifo_qdisc_ops
);
132 struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly
= {
133 .id
= "pfifo_head_drop",
135 .enqueue
= pfifo_tail_enqueue
,
136 .dequeue
= qdisc_dequeue_head
,
137 .peek
= qdisc_peek_head
,
139 .reset
= qdisc_reset_queue
,
142 .owner
= THIS_MODULE
,
145 /* Pass size change message down to embedded FIFO */
146 int fifo_set_limit(struct Qdisc
*q
, unsigned int limit
)
151 /* Hack to avoid sending change message to non-FIFO */
152 if (strncmp(q
->ops
->id
+ 1, "fifo", 4) != 0)
155 nla
= kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt
)), GFP_KERNEL
);
157 nla
->nla_type
= RTM_NEWQDISC
;
158 nla
->nla_len
= nla_attr_size(sizeof(struct tc_fifo_qopt
));
159 ((struct tc_fifo_qopt
*)nla_data(nla
))->limit
= limit
;
161 ret
= q
->ops
->change(q
, nla
, NULL
);
166 EXPORT_SYMBOL(fifo_set_limit
);
168 struct Qdisc
*fifo_create_dflt(struct Qdisc
*sch
, struct Qdisc_ops
*ops
,
170 struct netlink_ext_ack
*extack
)
175 q
= qdisc_create_dflt(sch
->dev_queue
, ops
, TC_H_MAKE(sch
->handle
, 1),
178 err
= fifo_set_limit(q
, limit
);
185 return q
? : ERR_PTR(err
);
187 EXPORT_SYMBOL(fifo_create_dflt
);