Linux 5.7.7
[linux/fpc-iii.git] / net / sched / sch_fifo.c
bloba579a4131d22d771c9766f5ad6cdb16ece3034c0
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/sch_fifo.c The simplest FIFO queue.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 */
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);
52 return NET_XMIT_CN;
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)
61 return;
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)
75 return;
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)
99 bool bypass;
100 bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
102 if (opt == NULL) {
103 u32 limit = qdisc_dev(sch)->tx_queue_len;
105 if (is_bfifo)
106 limit *= psched_mtu(qdisc_dev(sch));
108 sch->limit = limit;
109 } else {
110 struct tc_fifo_qopt *ctl = nla_data(opt);
112 if (nla_len(opt) < sizeof(*ctl))
113 return -EINVAL;
115 sch->limit = ctl->limit;
118 if (is_bfifo)
119 bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
120 else
121 bypass = sch->limit >= 1;
123 if (bypass)
124 sch->flags |= TCQ_F_CAN_BYPASS;
125 else
126 sch->flags &= ~TCQ_F_CAN_BYPASS;
128 return 0;
131 static int fifo_init(struct Qdisc *sch, struct nlattr *opt,
132 struct netlink_ext_ack *extack)
134 int err;
136 err = __fifo_init(sch, opt, extack);
137 if (err)
138 return err;
140 fifo_offload_init(sch);
141 return 0;
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;
161 return skb->len;
163 nla_put_failure:
164 return -1;
167 static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
169 int err;
171 err = fifo_offload_dump(sch);
172 if (err)
173 return err;
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 = {
184 .id = "pfifo",
185 .priv_size = 0,
186 .enqueue = pfifo_enqueue,
187 .dequeue = qdisc_dequeue_head,
188 .peek = qdisc_peek_head,
189 .init = fifo_init,
190 .destroy = fifo_destroy,
191 .reset = qdisc_reset_queue,
192 .change = fifo_init,
193 .dump = fifo_dump,
194 .owner = THIS_MODULE,
196 EXPORT_SYMBOL(pfifo_qdisc_ops);
198 struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
199 .id = "bfifo",
200 .priv_size = 0,
201 .enqueue = bfifo_enqueue,
202 .dequeue = qdisc_dequeue_head,
203 .peek = qdisc_peek_head,
204 .init = fifo_init,
205 .destroy = fifo_destroy,
206 .reset = qdisc_reset_queue,
207 .change = fifo_init,
208 .dump = fifo_dump,
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",
215 .priv_size = 0,
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)
229 struct nlattr *nla;
230 int ret = -ENOMEM;
232 /* Hack to avoid sending change message to non-FIFO */
233 if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
234 return 0;
236 nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
237 if (nla) {
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);
243 kfree(nla);
245 return ret;
247 EXPORT_SYMBOL(fifo_set_limit);
249 struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
250 unsigned int limit,
251 struct netlink_ext_ack *extack)
253 struct Qdisc *q;
254 int err = -ENOMEM;
256 q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1),
257 extack);
258 if (q) {
259 err = fifo_set_limit(q, limit);
260 if (err < 0) {
261 qdisc_put(q);
262 q = NULL;
266 return q ? : ERR_PTR(err);
268 EXPORT_SYMBOL(fifo_create_dflt);