ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sched / sch_fifo.c
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
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/in.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
32 #include <net/ip.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
35 #include <net/sock.h>
36 #include <net/pkt_sched.h>
37
38 /* 1 band FIFO pseudo-"scheduler" */
39
40 struct fifo_sched_data
41 {
42         unsigned limit;
43 };
44
45 static int
46 bfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
47 {
48         struct fifo_sched_data *q = (struct fifo_sched_data *)sch->data;
49
50         if (sch->stats.backlog + skb->len <= q->limit) {
51                 __skb_queue_tail(&sch->q, skb);
52                 sch->stats.backlog += skb->len;
53                 sch->stats.bytes += skb->len;
54                 sch->stats.packets++;
55                 return 0;
56         }
57         sch->stats.drops++;
58 #ifdef CONFIG_NET_CLS_POLICE
59         if (sch->reshape_fail==NULL || sch->reshape_fail(skb, sch))
60 #endif
61                 kfree_skb(skb);
62         return NET_XMIT_DROP;
63 }
64
65 static int
66 bfifo_requeue(struct sk_buff *skb, struct Qdisc* sch)
67 {
68         __skb_queue_head(&sch->q, skb);
69         sch->stats.backlog += skb->len;
70         return 0;
71 }
72
73 static struct sk_buff *
74 bfifo_dequeue(struct Qdisc* sch)
75 {
76         struct sk_buff *skb;
77
78         skb = __skb_dequeue(&sch->q);
79         if (skb)
80                 sch->stats.backlog -= skb->len;
81         return skb;
82 }
83
84 static unsigned int 
85 fifo_drop(struct Qdisc* sch)
86 {
87         struct sk_buff *skb;
88
89         skb = __skb_dequeue_tail(&sch->q);
90         if (skb) {
91                 unsigned int len = skb->len;
92                 sch->stats.backlog -= len;
93                 kfree_skb(skb);
94                 return len;
95         }
96         return 0;
97 }
98
99 static void
100 fifo_reset(struct Qdisc* sch)
101 {
102         skb_queue_purge(&sch->q);
103         sch->stats.backlog = 0;
104 }
105
106 static int
107 pfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
108 {
109         struct fifo_sched_data *q = (struct fifo_sched_data *)sch->data;
110
111         if (sch->q.qlen < q->limit) {
112                 __skb_queue_tail(&sch->q, skb);
113                 sch->stats.bytes += skb->len;
114                 sch->stats.packets++;
115                 return 0;
116         }
117         sch->stats.drops++;
118 #ifdef CONFIG_NET_CLS_POLICE
119         if (sch->reshape_fail==NULL || sch->reshape_fail(skb, sch))
120 #endif
121                 kfree_skb(skb);
122         return NET_XMIT_DROP;
123 }
124
125 static int
126 pfifo_requeue(struct sk_buff *skb, struct Qdisc* sch)
127 {
128         __skb_queue_head(&sch->q, skb);
129         return 0;
130 }
131
132
133 static struct sk_buff *
134 pfifo_dequeue(struct Qdisc* sch)
135 {
136         return __skb_dequeue(&sch->q);
137 }
138
139 static int fifo_init(struct Qdisc *sch, struct rtattr *opt)
140 {
141         struct fifo_sched_data *q = (void*)sch->data;
142
143         if (opt == NULL) {
144                 unsigned int limit = sch->dev->tx_queue_len ? : 1;
145
146                 if (sch->ops == &bfifo_qdisc_ops)
147                         q->limit = limit*sch->dev->mtu;
148                 else    
149                         q->limit = limit;
150         } else {
151                 struct tc_fifo_qopt *ctl = RTA_DATA(opt);
152                 if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
153                         return -EINVAL;
154                 q->limit = ctl->limit;
155         }
156         return 0;
157 }
158
159 static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
160 {
161         struct fifo_sched_data *q = (void*)sch->data;
162         unsigned char    *b = skb->tail;
163         struct tc_fifo_qopt opt;
164
165         opt.limit = q->limit;
166         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
167
168         return skb->len;
169
170 rtattr_failure:
171         skb_trim(skb, b - skb->data);
172         return -1;
173 }
174
175 struct Qdisc_ops pfifo_qdisc_ops = {
176         .next           =       NULL,
177         .cl_ops         =       NULL,
178         .id             =       "pfifo",
179         .priv_size      =       sizeof(struct fifo_sched_data),
180         .enqueue        =       pfifo_enqueue,
181         .dequeue        =       pfifo_dequeue,
182         .requeue        =       pfifo_requeue,
183         .drop           =       fifo_drop,
184         .init           =       fifo_init,
185         .reset          =       fifo_reset,
186         .destroy        =       NULL,
187         .change         =       fifo_init,
188         .dump           =       fifo_dump,
189         .owner          =       THIS_MODULE,
190 };
191
192 struct Qdisc_ops bfifo_qdisc_ops = {
193         .next           =       NULL,
194         .cl_ops         =       NULL,
195         .id             =       "bfifo",
196         .priv_size      =       sizeof(struct fifo_sched_data),
197         .enqueue        =       bfifo_enqueue,
198         .dequeue        =       bfifo_dequeue,
199         .requeue        =       bfifo_requeue,
200         .drop           =       fifo_drop,
201         .init           =       fifo_init,
202         .reset          =       fifo_reset,
203         .destroy        =       NULL,
204         .change         =       fifo_init,
205         .dump           =       fifo_dump,
206         .owner          =       THIS_MODULE,
207 };
208
209 EXPORT_SYMBOL(bfifo_qdisc_ops);
210 EXPORT_SYMBOL(pfifo_qdisc_ops);