Merge tag 'nds32-for-linus-4.21' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / net / sched / sch_fifo.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/sch_fifo.c The simplest FIFO queue.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
1da177e4 12#include <linux/module.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4
LT
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/errno.h>
1da177e4 17#include <linux/skbuff.h>
1da177e4
LT
18#include <net/pkt_sched.h>
19
20/* 1 band FIFO pseudo-"scheduler" */
21
520ac30f
ED
22static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
23 struct sk_buff **to_free)
1da177e4 24{
d276055c 25 if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit))
aaae3013 26 return qdisc_enqueue_tail(skb, sch);
1da177e4 27
520ac30f 28 return qdisc_drop(skb, sch, to_free);
1da177e4
LT
29}
30
520ac30f
ED
31static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
32 struct sk_buff **to_free)
1da177e4 33{
97d0678f 34 if (likely(sch->q.qlen < sch->limit))
aaae3013 35 return qdisc_enqueue_tail(skb, sch);
1da177e4 36
520ac30f 37 return qdisc_drop(skb, sch, to_free);
1da177e4
LT
38}
39
520ac30f
ED
40static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
41 struct sk_buff **to_free)
57dbb2d8 42{
6c0d54f1
ED
43 unsigned int prev_backlog;
44
97d0678f 45 if (likely(sch->q.qlen < sch->limit))
57dbb2d8
HPP
46 return qdisc_enqueue_tail(skb, sch);
47
6c0d54f1 48 prev_backlog = sch->qstats.backlog;
57dbb2d8 49 /* queue full, remove one skb to fulfill the limit */
520ac30f 50 __qdisc_queue_drop_head(sch, &sch->q, to_free);
25331d6c 51 qdisc_qstats_drop(sch);
57dbb2d8
HPP
52 qdisc_enqueue_tail(skb, sch);
53
6c0d54f1 54 qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
57dbb2d8
HPP
55 return NET_XMIT_CN;
56}
57
e63d7dfd
AA
58static int fifo_init(struct Qdisc *sch, struct nlattr *opt,
59 struct netlink_ext_ack *extack)
1da177e4 60{
23624935
ED
61 bool bypass;
62 bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
1da177e4
LT
63
64 if (opt == NULL) {
348e3435 65 u32 limit = qdisc_dev(sch)->tx_queue_len;
1da177e4 66
23624935 67 if (is_bfifo)
6473990c 68 limit *= psched_mtu(qdisc_dev(sch));
6fc8e84f 69
d276055c 70 sch->limit = limit;
1da177e4 71 } else {
1e90474c 72 struct tc_fifo_qopt *ctl = nla_data(opt);
6fc8e84f 73
1e90474c 74 if (nla_len(opt) < sizeof(*ctl))
1da177e4 75 return -EINVAL;
6fc8e84f 76
d276055c 77 sch->limit = ctl->limit;
1da177e4 78 }
6fc8e84f 79
23624935 80 if (is_bfifo)
d276055c 81 bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
23624935 82 else
d276055c 83 bypass = sch->limit >= 1;
23624935
ED
84
85 if (bypass)
86 sch->flags |= TCQ_F_CAN_BYPASS;
87 else
88 sch->flags &= ~TCQ_F_CAN_BYPASS;
1da177e4
LT
89 return 0;
90}
91
92static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
93{
d276055c 94 struct tc_fifo_qopt opt = { .limit = sch->limit };
1da177e4 95
1b34ec43
DM
96 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
97 goto nla_put_failure;
1da177e4
LT
98 return skb->len;
99
1e90474c 100nla_put_failure:
1da177e4
LT
101 return -1;
102}
103
20fea08b 104struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
1da177e4 105 .id = "pfifo",
d276055c 106 .priv_size = 0,
1da177e4 107 .enqueue = pfifo_enqueue,
aaae3013 108 .dequeue = qdisc_dequeue_head,
48a8f519 109 .peek = qdisc_peek_head,
1da177e4 110 .init = fifo_init,
aaae3013 111 .reset = qdisc_reset_queue,
2030721c 112 .change = fifo_init,
1da177e4
LT
113 .dump = fifo_dump,
114 .owner = THIS_MODULE,
115};
62e3ba1b 116EXPORT_SYMBOL(pfifo_qdisc_ops);
1da177e4 117
20fea08b 118struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
1da177e4 119 .id = "bfifo",
d276055c 120 .priv_size = 0,
1da177e4 121 .enqueue = bfifo_enqueue,
aaae3013 122 .dequeue = qdisc_dequeue_head,
48a8f519 123 .peek = qdisc_peek_head,
1da177e4 124 .init = fifo_init,
aaae3013 125 .reset = qdisc_reset_queue,
2030721c 126 .change = fifo_init,
1da177e4
LT
127 .dump = fifo_dump,
128 .owner = THIS_MODULE,
129};
1da177e4 130EXPORT_SYMBOL(bfifo_qdisc_ops);
fb0305ce 131
57dbb2d8
HPP
132struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
133 .id = "pfifo_head_drop",
d276055c 134 .priv_size = 0,
57dbb2d8
HPP
135 .enqueue = pfifo_tail_enqueue,
136 .dequeue = qdisc_dequeue_head,
137 .peek = qdisc_peek_head,
57dbb2d8
HPP
138 .init = fifo_init,
139 .reset = qdisc_reset_queue,
2030721c 140 .change = fifo_init,
57dbb2d8
HPP
141 .dump = fifo_dump,
142 .owner = THIS_MODULE,
143};
144
fb0305ce
PM
145/* Pass size change message down to embedded FIFO */
146int fifo_set_limit(struct Qdisc *q, unsigned int limit)
147{
148 struct nlattr *nla;
149 int ret = -ENOMEM;
150
151 /* Hack to avoid sending change message to non-FIFO */
152 if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
153 return 0;
154
155 nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
156 if (nla) {
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;
160
2030721c 161 ret = q->ops->change(q, nla, NULL);
fb0305ce
PM
162 kfree(nla);
163 }
164 return ret;
165}
166EXPORT_SYMBOL(fifo_set_limit);
167
168struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
a38a9882
AA
169 unsigned int limit,
170 struct netlink_ext_ack *extack)
fb0305ce
PM
171{
172 struct Qdisc *q;
173 int err = -ENOMEM;
174
a38a9882
AA
175 q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1),
176 extack);
fb0305ce
PM
177 if (q) {
178 err = fifo_set_limit(q, limit);
179 if (err < 0) {
86bd446b 180 qdisc_put(q);
fb0305ce
PM
181 q = NULL;
182 }
183 }
184
185 return q ? : ERR_PTR(err);
186}
187EXPORT_SYMBOL(fifo_create_dflt);