Merge branch 'for-linus' of git://git.infradead.org/ubi-2.6
[deliverable/linux.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
22struct fifo_sched_data
23{
6fc8e84f 24 u32 limit;
1da177e4
LT
25};
26
6fc8e84f 27static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
1da177e4
LT
28{
29 struct fifo_sched_data *q = qdisc_priv(sch);
30
0abf77e5 31 if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= q->limit))
aaae3013 32 return qdisc_enqueue_tail(skb, sch);
1da177e4 33
aaae3013 34 return qdisc_reshape_fail(skb, sch);
1da177e4
LT
35}
36
6fc8e84f 37static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
1da177e4
LT
38{
39 struct fifo_sched_data *q = qdisc_priv(sch);
40
aaae3013
TG
41 if (likely(skb_queue_len(&sch->q) < q->limit))
42 return qdisc_enqueue_tail(skb, sch);
1da177e4 43
aaae3013 44 return qdisc_reshape_fail(skb, sch);
1da177e4
LT
45}
46
57dbb2d8
HPP
47static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc* sch)
48{
57dbb2d8
HPP
49 struct fifo_sched_data *q = qdisc_priv(sch);
50
51 if (likely(skb_queue_len(&sch->q) < q->limit))
52 return qdisc_enqueue_tail(skb, sch);
53
54 /* queue full, remove one skb to fulfill the limit */
9190b3b3 55 __qdisc_queue_drop_head(sch, &sch->q);
57dbb2d8 56 sch->qstats.drops++;
57dbb2d8
HPP
57 qdisc_enqueue_tail(skb, sch);
58
59 return NET_XMIT_CN;
60}
61
1e90474c 62static int fifo_init(struct Qdisc *sch, struct nlattr *opt)
1da177e4
LT
63{
64 struct fifo_sched_data *q = qdisc_priv(sch);
65
66 if (opt == NULL) {
5ce2d488 67 u32 limit = qdisc_dev(sch)->tx_queue_len ? : 1;
1da177e4
LT
68
69 if (sch->ops == &bfifo_qdisc_ops)
6473990c 70 limit *= psched_mtu(qdisc_dev(sch));
6fc8e84f
TG
71
72 q->limit = limit;
1da177e4 73 } else {
1e90474c 74 struct tc_fifo_qopt *ctl = nla_data(opt);
6fc8e84f 75
1e90474c 76 if (nla_len(opt) < sizeof(*ctl))
1da177e4 77 return -EINVAL;
6fc8e84f 78
1da177e4
LT
79 q->limit = ctl->limit;
80 }
6fc8e84f 81
1da177e4
LT
82 return 0;
83}
84
85static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
86{
87 struct fifo_sched_data *q = qdisc_priv(sch);
6fc8e84f 88 struct tc_fifo_qopt opt = { .limit = q->limit };
1da177e4 89
1e90474c 90 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
1da177e4
LT
91 return skb->len;
92
1e90474c 93nla_put_failure:
1da177e4
LT
94 return -1;
95}
96
20fea08b 97struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
1da177e4
LT
98 .id = "pfifo",
99 .priv_size = sizeof(struct fifo_sched_data),
100 .enqueue = pfifo_enqueue,
aaae3013 101 .dequeue = qdisc_dequeue_head,
48a8f519 102 .peek = qdisc_peek_head,
aaae3013 103 .drop = qdisc_queue_drop,
1da177e4 104 .init = fifo_init,
aaae3013 105 .reset = qdisc_reset_queue,
1da177e4
LT
106 .change = fifo_init,
107 .dump = fifo_dump,
108 .owner = THIS_MODULE,
109};
62e3ba1b 110EXPORT_SYMBOL(pfifo_qdisc_ops);
1da177e4 111
20fea08b 112struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
1da177e4
LT
113 .id = "bfifo",
114 .priv_size = sizeof(struct fifo_sched_data),
115 .enqueue = bfifo_enqueue,
aaae3013 116 .dequeue = qdisc_dequeue_head,
48a8f519 117 .peek = qdisc_peek_head,
aaae3013 118 .drop = qdisc_queue_drop,
1da177e4 119 .init = fifo_init,
aaae3013 120 .reset = qdisc_reset_queue,
1da177e4
LT
121 .change = fifo_init,
122 .dump = fifo_dump,
123 .owner = THIS_MODULE,
124};
1da177e4 125EXPORT_SYMBOL(bfifo_qdisc_ops);
fb0305ce 126
57dbb2d8
HPP
127struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
128 .id = "pfifo_head_drop",
129 .priv_size = sizeof(struct fifo_sched_data),
130 .enqueue = pfifo_tail_enqueue,
131 .dequeue = qdisc_dequeue_head,
132 .peek = qdisc_peek_head,
133 .drop = qdisc_queue_drop_head,
134 .init = fifo_init,
135 .reset = qdisc_reset_queue,
136 .change = fifo_init,
137 .dump = fifo_dump,
138 .owner = THIS_MODULE,
139};
140
fb0305ce
PM
141/* Pass size change message down to embedded FIFO */
142int fifo_set_limit(struct Qdisc *q, unsigned int limit)
143{
144 struct nlattr *nla;
145 int ret = -ENOMEM;
146
147 /* Hack to avoid sending change message to non-FIFO */
148 if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
149 return 0;
150
151 nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
152 if (nla) {
153 nla->nla_type = RTM_NEWQDISC;
154 nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
155 ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
156
157 ret = q->ops->change(q, nla);
158 kfree(nla);
159 }
160 return ret;
161}
162EXPORT_SYMBOL(fifo_set_limit);
163
164struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
165 unsigned int limit)
166{
167 struct Qdisc *q;
168 int err = -ENOMEM;
169
3511c913 170 q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1));
fb0305ce
PM
171 if (q) {
172 err = fifo_set_limit(q, limit);
173 if (err < 0) {
174 qdisc_destroy(q);
175 q = NULL;
176 }
177 }
178
179 return q ? : ERR_PTR(err);
180}
181EXPORT_SYMBOL(fifo_create_dflt);
This page took 0.505734 seconds and 5 git commands to generate.