blob: e1040421b79797fefaa26b8d7d3f44b91896e1de [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/sch_fifo.c The simplest FIFO queue.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/pkt_sched.h>
Petr Machataaaca9402020-03-05 09:16:40 +020015#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/* 1 band FIFO pseudo-"scheduler" */
18
Eric Dumazet520ac302016-06-21 23:16:49 -070019static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
20 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Eric Dumazetd2760552011-03-03 11:10:02 -080022 if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit))
Thomas Grafaaae3012005-06-18 22:57:42 -070023 return qdisc_enqueue_tail(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Eric Dumazet520ac302016-06-21 23:16:49 -070025 return qdisc_drop(skb, sch, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
Eric Dumazet520ac302016-06-21 23:16:49 -070028static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
29 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Florian Westphal97d06782016-09-18 00:57:31 +020031 if (likely(sch->q.qlen < sch->limit))
Thomas Grafaaae3012005-06-18 22:57:42 -070032 return qdisc_enqueue_tail(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Eric Dumazet520ac302016-06-21 23:16:49 -070034 return qdisc_drop(skb, sch, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Eric Dumazet520ac302016-06-21 23:16:49 -070037static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
38 struct sk_buff **to_free)
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +000039{
Eric Dumazet6c0d54f2016-06-12 20:01:25 -070040 unsigned int prev_backlog;
41
Florian Westphal97d06782016-09-18 00:57:31 +020042 if (likely(sch->q.qlen < sch->limit))
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +000043 return qdisc_enqueue_tail(skb, sch);
44
Eric Dumazet6c0d54f2016-06-12 20:01:25 -070045 prev_backlog = sch->qstats.backlog;
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +000046 /* queue full, remove one skb to fulfill the limit */
Eric Dumazet520ac302016-06-21 23:16:49 -070047 __qdisc_queue_drop_head(sch, &sch->q, to_free);
John Fastabend25331d62014-09-28 11:53:29 -070048 qdisc_qstats_drop(sch);
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +000049 qdisc_enqueue_tail(skb, sch);
50
Eric Dumazet6c0d54f2016-06-12 20:01:25 -070051 qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +000052 return NET_XMIT_CN;
53}
54
Petr Machataaaca9402020-03-05 09:16:40 +020055static void fifo_offload_init(struct Qdisc *sch)
56{
57 struct net_device *dev = qdisc_dev(sch);
58 struct tc_fifo_qopt_offload qopt;
59
60 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
61 return;
62
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);
67}
68
69static void fifo_offload_destroy(struct Qdisc *sch)
70{
71 struct net_device *dev = qdisc_dev(sch);
72 struct tc_fifo_qopt_offload qopt;
73
74 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
75 return;
76
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);
81}
82
83static int fifo_offload_dump(struct Qdisc *sch)
84{
85 struct tc_fifo_qopt_offload qopt;
86
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;
92
93 return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_FIFO, &qopt);
94}
95
96static int __fifo_init(struct Qdisc *sch, struct nlattr *opt,
97 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Eric Dumazet23624932011-01-21 16:26:09 -080099 bool bypass;
100 bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (opt == NULL) {
Phil Sutter348e3432015-08-18 10:30:49 +0200103 u32 limit = qdisc_dev(sch)->tx_queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Eric Dumazet23624932011-01-21 16:26:09 -0800105 if (is_bfifo)
Patrick McHardy64739902009-05-06 16:45:07 -0700106 limit *= psched_mtu(qdisc_dev(sch));
Thomas Graf6fc8e842005-06-18 22:58:00 -0700107
Eric Dumazetd2760552011-03-03 11:10:02 -0800108 sch->limit = limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 } else {
Patrick McHardy1e904742008-01-22 22:11:17 -0800110 struct tc_fifo_qopt *ctl = nla_data(opt);
Thomas Graf6fc8e842005-06-18 22:58:00 -0700111
Patrick McHardy1e904742008-01-22 22:11:17 -0800112 if (nla_len(opt) < sizeof(*ctl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -EINVAL;
Thomas Graf6fc8e842005-06-18 22:58:00 -0700114
Eric Dumazetd2760552011-03-03 11:10:02 -0800115 sch->limit = ctl->limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Thomas Graf6fc8e842005-06-18 22:58:00 -0700117
Eric Dumazet23624932011-01-21 16:26:09 -0800118 if (is_bfifo)
Eric Dumazetd2760552011-03-03 11:10:02 -0800119 bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
Eric Dumazet23624932011-01-21 16:26:09 -0800120 else
Eric Dumazetd2760552011-03-03 11:10:02 -0800121 bypass = sch->limit >= 1;
Eric Dumazet23624932011-01-21 16:26:09 -0800122
123 if (bypass)
124 sch->flags |= TCQ_F_CAN_BYPASS;
125 else
126 sch->flags &= ~TCQ_F_CAN_BYPASS;
Petr Machataaaca9402020-03-05 09:16:40 +0200127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129}
130
Petr Machataaaca9402020-03-05 09:16:40 +0200131static int fifo_init(struct Qdisc *sch, struct nlattr *opt,
132 struct netlink_ext_ack *extack)
133{
134 int err;
135
136 err = __fifo_init(sch, opt, extack);
137 if (err)
138 return err;
139
140 fifo_offload_init(sch);
141 return 0;
142}
143
144static int fifo_hd_init(struct Qdisc *sch, struct nlattr *opt,
145 struct netlink_ext_ack *extack)
146{
147 return __fifo_init(sch, opt, extack);
148}
149
150static void fifo_destroy(struct Qdisc *sch)
151{
152 fifo_offload_destroy(sch);
153}
154
155static int __fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Eric Dumazetd2760552011-03-03 11:10:02 -0800157 struct tc_fifo_qopt opt = { .limit = sch->limit };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David S. Miller1b34ec42012-03-29 05:11:39 -0400159 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
160 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return skb->len;
162
Patrick McHardy1e904742008-01-22 22:11:17 -0800163nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -1;
165}
166
Petr Machataaaca9402020-03-05 09:16:40 +0200167static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
168{
169 int err;
170
171 err = fifo_offload_dump(sch);
172 if (err)
173 return err;
174
175 return __fifo_dump(sch, skb);
176}
177
178static int fifo_hd_dump(struct Qdisc *sch, struct sk_buff *skb)
179{
180 return __fifo_dump(sch, skb);
181}
182
Eric Dumazet20fea082007-11-14 01:44:41 -0800183struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .id = "pfifo",
Eric Dumazetd2760552011-03-03 11:10:02 -0800185 .priv_size = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .enqueue = pfifo_enqueue,
Thomas Grafaaae3012005-06-18 22:57:42 -0700187 .dequeue = qdisc_dequeue_head,
Patrick McHardy48a8f512008-10-31 00:44:18 -0700188 .peek = qdisc_peek_head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .init = fifo_init,
Petr Machataaaca9402020-03-05 09:16:40 +0200190 .destroy = fifo_destroy,
Thomas Grafaaae3012005-06-18 22:57:42 -0700191 .reset = qdisc_reset_queue,
Alexander Aring20307212017-12-20 12:35:14 -0500192 .change = fifo_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .dump = fifo_dump,
194 .owner = THIS_MODULE,
195};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800196EXPORT_SYMBOL(pfifo_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Eric Dumazet20fea082007-11-14 01:44:41 -0800198struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .id = "bfifo",
Eric Dumazetd2760552011-03-03 11:10:02 -0800200 .priv_size = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .enqueue = bfifo_enqueue,
Thomas Grafaaae3012005-06-18 22:57:42 -0700202 .dequeue = qdisc_dequeue_head,
Patrick McHardy48a8f512008-10-31 00:44:18 -0700203 .peek = qdisc_peek_head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 .init = fifo_init,
Petr Machataaaca9402020-03-05 09:16:40 +0200205 .destroy = fifo_destroy,
Thomas Grafaaae3012005-06-18 22:57:42 -0700206 .reset = qdisc_reset_queue,
Alexander Aring20307212017-12-20 12:35:14 -0500207 .change = fifo_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .dump = fifo_dump,
209 .owner = THIS_MODULE,
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211EXPORT_SYMBOL(bfifo_qdisc_ops);
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700212
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +0000213struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
214 .id = "pfifo_head_drop",
Eric Dumazetd2760552011-03-03 11:10:02 -0800215 .priv_size = 0,
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +0000216 .enqueue = pfifo_tail_enqueue,
217 .dequeue = qdisc_dequeue_head,
218 .peek = qdisc_peek_head,
Petr Machataaaca9402020-03-05 09:16:40 +0200219 .init = fifo_hd_init,
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +0000220 .reset = qdisc_reset_queue,
Petr Machataaaca9402020-03-05 09:16:40 +0200221 .change = fifo_hd_init,
222 .dump = fifo_hd_dump,
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +0000223 .owner = THIS_MODULE,
224};
225
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700226/* Pass size change message down to embedded FIFO */
227int fifo_set_limit(struct Qdisc *q, unsigned int limit)
228{
229 struct nlattr *nla;
230 int ret = -ENOMEM;
231
232 /* Hack to avoid sending change message to non-FIFO */
233 if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
234 return 0;
235
Eric Dumazet560ee192021-09-30 14:22:39 -0700236 if (!q->ops->change)
237 return 0;
238
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700239 nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
240 if (nla) {
241 nla->nla_type = RTM_NEWQDISC;
242 nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
243 ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
244
Alexander Aring20307212017-12-20 12:35:14 -0500245 ret = q->ops->change(q, nla, NULL);
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700246 kfree(nla);
247 }
248 return ret;
249}
250EXPORT_SYMBOL(fifo_set_limit);
251
252struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
Alexander Aringa38a98822017-12-20 12:35:21 -0500253 unsigned int limit,
254 struct netlink_ext_ack *extack)
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700255{
256 struct Qdisc *q;
257 int err = -ENOMEM;
258
Alexander Aringa38a98822017-12-20 12:35:21 -0500259 q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1),
260 extack);
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700261 if (q) {
262 err = fifo_set_limit(q, limit);
263 if (err < 0) {
Vlad Buslov86bd4462018-09-24 19:22:50 +0300264 qdisc_put(q);
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700265 q = NULL;
266 }
267 }
268
269 return q ? : ERR_PTR(err);
270}
271EXPORT_SYMBOL(fifo_create_dflt);