12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * net/sched/sch_fifo.c The simplest FIFO queue.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/module.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/skbuff.h>
141da177e4SLinus Torvalds #include <net/pkt_sched.h>
15aaca9408SPetr Machata #include <net/pkt_cls.h>
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds /* 1 band FIFO pseudo-"scheduler" */
181da177e4SLinus Torvalds
bfifo_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)19520ac30fSEric Dumazet static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
20520ac30fSEric Dumazet struct sk_buff **to_free)
211da177e4SLinus Torvalds {
22d276055cSEric Dumazet if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit))
23aaae3013SThomas Graf return qdisc_enqueue_tail(skb, sch);
241da177e4SLinus Torvalds
25520ac30fSEric Dumazet return qdisc_drop(skb, sch, to_free);
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds
pfifo_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)28520ac30fSEric Dumazet static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
29520ac30fSEric Dumazet struct sk_buff **to_free)
301da177e4SLinus Torvalds {
3197d0678fSFlorian Westphal if (likely(sch->q.qlen < sch->limit))
32aaae3013SThomas Graf return qdisc_enqueue_tail(skb, sch);
331da177e4SLinus Torvalds
34520ac30fSEric Dumazet return qdisc_drop(skb, sch, to_free);
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds
pfifo_tail_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)37520ac30fSEric Dumazet static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
38520ac30fSEric Dumazet struct sk_buff **to_free)
3957dbb2d8SHagen Paul Pfeifer {
406c0d54f1SEric Dumazet unsigned int prev_backlog;
416c0d54f1SEric Dumazet
4297d0678fSFlorian Westphal if (likely(sch->q.qlen < sch->limit))
4357dbb2d8SHagen Paul Pfeifer return qdisc_enqueue_tail(skb, sch);
4457dbb2d8SHagen Paul Pfeifer
456c0d54f1SEric Dumazet prev_backlog = sch->qstats.backlog;
4657dbb2d8SHagen Paul Pfeifer /* queue full, remove one skb to fulfill the limit */
47520ac30fSEric Dumazet __qdisc_queue_drop_head(sch, &sch->q, to_free);
4825331d6cSJohn Fastabend qdisc_qstats_drop(sch);
4957dbb2d8SHagen Paul Pfeifer qdisc_enqueue_tail(skb, sch);
5057dbb2d8SHagen Paul Pfeifer
516c0d54f1SEric Dumazet qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
5257dbb2d8SHagen Paul Pfeifer return NET_XMIT_CN;
5357dbb2d8SHagen Paul Pfeifer }
5457dbb2d8SHagen Paul Pfeifer
fifo_offload_init(struct Qdisc * sch)55aaca9408SPetr Machata static void fifo_offload_init(struct Qdisc *sch)
56aaca9408SPetr Machata {
57aaca9408SPetr Machata struct net_device *dev = qdisc_dev(sch);
58aaca9408SPetr Machata struct tc_fifo_qopt_offload qopt;
59aaca9408SPetr Machata
60aaca9408SPetr Machata if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
61aaca9408SPetr Machata return;
62aaca9408SPetr Machata
63aaca9408SPetr Machata qopt.command = TC_FIFO_REPLACE;
64aaca9408SPetr Machata qopt.handle = sch->handle;
65aaca9408SPetr Machata qopt.parent = sch->parent;
66aaca9408SPetr Machata dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_FIFO, &qopt);
67aaca9408SPetr Machata }
68aaca9408SPetr Machata
fifo_offload_destroy(struct Qdisc * sch)69aaca9408SPetr Machata static void fifo_offload_destroy(struct Qdisc *sch)
70aaca9408SPetr Machata {
71aaca9408SPetr Machata struct net_device *dev = qdisc_dev(sch);
72aaca9408SPetr Machata struct tc_fifo_qopt_offload qopt;
73aaca9408SPetr Machata
74aaca9408SPetr Machata if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
75aaca9408SPetr Machata return;
76aaca9408SPetr Machata
77aaca9408SPetr Machata qopt.command = TC_FIFO_DESTROY;
78aaca9408SPetr Machata qopt.handle = sch->handle;
79aaca9408SPetr Machata qopt.parent = sch->parent;
80aaca9408SPetr Machata dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_FIFO, &qopt);
81aaca9408SPetr Machata }
82aaca9408SPetr Machata
fifo_offload_dump(struct Qdisc * sch)83aaca9408SPetr Machata static int fifo_offload_dump(struct Qdisc *sch)
84aaca9408SPetr Machata {
85aaca9408SPetr Machata struct tc_fifo_qopt_offload qopt;
86aaca9408SPetr Machata
87aaca9408SPetr Machata qopt.command = TC_FIFO_STATS;
88aaca9408SPetr Machata qopt.handle = sch->handle;
89aaca9408SPetr Machata qopt.parent = sch->parent;
90aaca9408SPetr Machata qopt.stats.bstats = &sch->bstats;
91aaca9408SPetr Machata qopt.stats.qstats = &sch->qstats;
92aaca9408SPetr Machata
93aaca9408SPetr Machata return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_FIFO, &qopt);
94aaca9408SPetr Machata }
95aaca9408SPetr Machata
__fifo_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)96aaca9408SPetr Machata static int __fifo_init(struct Qdisc *sch, struct nlattr *opt,
97e63d7dfdSAlexander Aring struct netlink_ext_ack *extack)
981da177e4SLinus Torvalds {
9923624935SEric Dumazet bool bypass;
10023624935SEric Dumazet bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds if (opt == NULL) {
103348e3435SPhil Sutter u32 limit = qdisc_dev(sch)->tx_queue_len;
1041da177e4SLinus Torvalds
10523624935SEric Dumazet if (is_bfifo)
1066473990cSPatrick McHardy limit *= psched_mtu(qdisc_dev(sch));
1076fc8e84fSThomas Graf
108d276055cSEric Dumazet sch->limit = limit;
1091da177e4SLinus Torvalds } else {
1101e90474cSPatrick McHardy struct tc_fifo_qopt *ctl = nla_data(opt);
1116fc8e84fSThomas Graf
1121e90474cSPatrick McHardy if (nla_len(opt) < sizeof(*ctl))
1131da177e4SLinus Torvalds return -EINVAL;
1146fc8e84fSThomas Graf
115d276055cSEric Dumazet sch->limit = ctl->limit;
1161da177e4SLinus Torvalds }
1176fc8e84fSThomas Graf
11823624935SEric Dumazet if (is_bfifo)
119d276055cSEric Dumazet bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
12023624935SEric Dumazet else
121d276055cSEric Dumazet bypass = sch->limit >= 1;
12223624935SEric Dumazet
12323624935SEric Dumazet if (bypass)
12423624935SEric Dumazet sch->flags |= TCQ_F_CAN_BYPASS;
12523624935SEric Dumazet else
12623624935SEric Dumazet sch->flags &= ~TCQ_F_CAN_BYPASS;
127aaca9408SPetr Machata
1281da177e4SLinus Torvalds return 0;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds
fifo_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)131aaca9408SPetr Machata static int fifo_init(struct Qdisc *sch, struct nlattr *opt,
132aaca9408SPetr Machata struct netlink_ext_ack *extack)
133aaca9408SPetr Machata {
134aaca9408SPetr Machata int err;
135aaca9408SPetr Machata
136aaca9408SPetr Machata err = __fifo_init(sch, opt, extack);
137aaca9408SPetr Machata if (err)
138aaca9408SPetr Machata return err;
139aaca9408SPetr Machata
140aaca9408SPetr Machata fifo_offload_init(sch);
141aaca9408SPetr Machata return 0;
142aaca9408SPetr Machata }
143aaca9408SPetr Machata
fifo_hd_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)144aaca9408SPetr Machata static int fifo_hd_init(struct Qdisc *sch, struct nlattr *opt,
145aaca9408SPetr Machata struct netlink_ext_ack *extack)
146aaca9408SPetr Machata {
147aaca9408SPetr Machata return __fifo_init(sch, opt, extack);
148aaca9408SPetr Machata }
149aaca9408SPetr Machata
fifo_destroy(struct Qdisc * sch)150aaca9408SPetr Machata static void fifo_destroy(struct Qdisc *sch)
151aaca9408SPetr Machata {
152aaca9408SPetr Machata fifo_offload_destroy(sch);
153aaca9408SPetr Machata }
154aaca9408SPetr Machata
__fifo_dump(struct Qdisc * sch,struct sk_buff * skb)155aaca9408SPetr Machata static int __fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
1561da177e4SLinus Torvalds {
157d276055cSEric Dumazet struct tc_fifo_qopt opt = { .limit = sch->limit };
1581da177e4SLinus Torvalds
1591b34ec43SDavid S. Miller if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
1601b34ec43SDavid S. Miller goto nla_put_failure;
1611da177e4SLinus Torvalds return skb->len;
1621da177e4SLinus Torvalds
1631e90474cSPatrick McHardy nla_put_failure:
1641da177e4SLinus Torvalds return -1;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds
fifo_dump(struct Qdisc * sch,struct sk_buff * skb)167aaca9408SPetr Machata static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
168aaca9408SPetr Machata {
169aaca9408SPetr Machata int err;
170aaca9408SPetr Machata
171aaca9408SPetr Machata err = fifo_offload_dump(sch);
172aaca9408SPetr Machata if (err)
173aaca9408SPetr Machata return err;
174aaca9408SPetr Machata
175aaca9408SPetr Machata return __fifo_dump(sch, skb);
176aaca9408SPetr Machata }
177aaca9408SPetr Machata
fifo_hd_dump(struct Qdisc * sch,struct sk_buff * skb)178aaca9408SPetr Machata static int fifo_hd_dump(struct Qdisc *sch, struct sk_buff *skb)
179aaca9408SPetr Machata {
180aaca9408SPetr Machata return __fifo_dump(sch, skb);
181aaca9408SPetr Machata }
182aaca9408SPetr Machata
18320fea08bSEric Dumazet struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
1841da177e4SLinus Torvalds .id = "pfifo",
185d276055cSEric Dumazet .priv_size = 0,
1861da177e4SLinus Torvalds .enqueue = pfifo_enqueue,
187aaae3013SThomas Graf .dequeue = qdisc_dequeue_head,
18848a8f519SPatrick McHardy .peek = qdisc_peek_head,
1891da177e4SLinus Torvalds .init = fifo_init,
190aaca9408SPetr Machata .destroy = fifo_destroy,
191aaae3013SThomas Graf .reset = qdisc_reset_queue,
1922030721cSAlexander Aring .change = fifo_init,
1931da177e4SLinus Torvalds .dump = fifo_dump,
1941da177e4SLinus Torvalds .owner = THIS_MODULE,
1951da177e4SLinus Torvalds };
19662e3ba1bSPatrick McHardy EXPORT_SYMBOL(pfifo_qdisc_ops);
1971da177e4SLinus Torvalds
19820fea08bSEric Dumazet struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
1991da177e4SLinus Torvalds .id = "bfifo",
200d276055cSEric Dumazet .priv_size = 0,
2011da177e4SLinus Torvalds .enqueue = bfifo_enqueue,
202aaae3013SThomas Graf .dequeue = qdisc_dequeue_head,
20348a8f519SPatrick McHardy .peek = qdisc_peek_head,
2041da177e4SLinus Torvalds .init = fifo_init,
205aaca9408SPetr Machata .destroy = fifo_destroy,
206aaae3013SThomas Graf .reset = qdisc_reset_queue,
2072030721cSAlexander Aring .change = fifo_init,
2081da177e4SLinus Torvalds .dump = fifo_dump,
2091da177e4SLinus Torvalds .owner = THIS_MODULE,
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds EXPORT_SYMBOL(bfifo_qdisc_ops);
212fb0305ceSPatrick McHardy
21357dbb2d8SHagen Paul Pfeifer struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
21457dbb2d8SHagen Paul Pfeifer .id = "pfifo_head_drop",
215d276055cSEric Dumazet .priv_size = 0,
21657dbb2d8SHagen Paul Pfeifer .enqueue = pfifo_tail_enqueue,
21757dbb2d8SHagen Paul Pfeifer .dequeue = qdisc_dequeue_head,
21857dbb2d8SHagen Paul Pfeifer .peek = qdisc_peek_head,
219aaca9408SPetr Machata .init = fifo_hd_init,
22057dbb2d8SHagen Paul Pfeifer .reset = qdisc_reset_queue,
221aaca9408SPetr Machata .change = fifo_hd_init,
222aaca9408SPetr Machata .dump = fifo_hd_dump,
22357dbb2d8SHagen Paul Pfeifer .owner = THIS_MODULE,
22457dbb2d8SHagen Paul Pfeifer };
22557dbb2d8SHagen Paul Pfeifer
226fb0305ceSPatrick McHardy /* Pass size change message down to embedded FIFO */
fifo_set_limit(struct Qdisc * q,unsigned int limit)227fb0305ceSPatrick McHardy int fifo_set_limit(struct Qdisc *q, unsigned int limit)
228fb0305ceSPatrick McHardy {
229fb0305ceSPatrick McHardy struct nlattr *nla;
230fb0305ceSPatrick McHardy int ret = -ENOMEM;
231fb0305ceSPatrick McHardy
232fb0305ceSPatrick McHardy /* Hack to avoid sending change message to non-FIFO */
233fb0305ceSPatrick McHardy if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
234fb0305ceSPatrick McHardy return 0;
235fb0305ceSPatrick McHardy
236*560ee196SEric Dumazet if (!q->ops->change)
237*560ee196SEric Dumazet return 0;
238*560ee196SEric Dumazet
239fb0305ceSPatrick McHardy nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
240fb0305ceSPatrick McHardy if (nla) {
241fb0305ceSPatrick McHardy nla->nla_type = RTM_NEWQDISC;
242fb0305ceSPatrick McHardy nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
243fb0305ceSPatrick McHardy ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
244fb0305ceSPatrick McHardy
2452030721cSAlexander Aring ret = q->ops->change(q, nla, NULL);
246fb0305ceSPatrick McHardy kfree(nla);
247fb0305ceSPatrick McHardy }
248fb0305ceSPatrick McHardy return ret;
249fb0305ceSPatrick McHardy }
250fb0305ceSPatrick McHardy EXPORT_SYMBOL(fifo_set_limit);
251fb0305ceSPatrick McHardy
fifo_create_dflt(struct Qdisc * sch,struct Qdisc_ops * ops,unsigned int limit,struct netlink_ext_ack * extack)252fb0305ceSPatrick McHardy struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
253a38a9882SAlexander Aring unsigned int limit,
254a38a9882SAlexander Aring struct netlink_ext_ack *extack)
255fb0305ceSPatrick McHardy {
256fb0305ceSPatrick McHardy struct Qdisc *q;
257fb0305ceSPatrick McHardy int err = -ENOMEM;
258fb0305ceSPatrick McHardy
259a38a9882SAlexander Aring q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1),
260a38a9882SAlexander Aring extack);
261fb0305ceSPatrick McHardy if (q) {
262fb0305ceSPatrick McHardy err = fifo_set_limit(q, limit);
263fb0305ceSPatrick McHardy if (err < 0) {
26486bd446bSVlad Buslov qdisc_put(q);
265fb0305ceSPatrick McHardy q = NULL;
266fb0305ceSPatrick McHardy }
267fb0305ceSPatrick McHardy }
268fb0305ceSPatrick McHardy
269fb0305ceSPatrick McHardy return q ? : ERR_PTR(err);
270fb0305ceSPatrick McHardy }
271fb0305ceSPatrick McHardy EXPORT_SYMBOL(fifo_create_dflt);
272