2 * net/sched/sch_delay.c Simple constant delay
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.
9 * Authors: Stephen Hemminger <shemminger@osdl.org>
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
17 #include <linux/string.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
22 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/if_ether.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/notifier.h>
30 #include <net/route.h>
31 #include <linux/skbuff.h>
33 #include <net/pkt_sched.h>
35 /* Network delay simulator
36 This scheduler adds a fixed delay to all packets.
37 Similar to NISTnet and BSD Dummynet.
39 It uses byte fifo underneath similar to TBF */
40 struct dly_sched_data {
43 struct timer_list timer;
47 /* Time stamp put into socket buffer control block */
49 psched_time_t queuetime;
52 /* Enqueue packets with underlying discipline (fifo)
53 * but mark them with current time first.
55 static int dly_enqueue(struct sk_buff *skb, struct Qdisc *sch)
57 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
58 struct dly_skb_cb *cb = (struct dly_skb_cb *)skb->cb;
61 PSCHED_GET_TIME(cb->queuetime);
63 /* Queue to underlying scheduler */
64 ret = q->qdisc->enqueue(skb, q->qdisc);
69 sch->stats.bytes += skb->len;
75 /* Requeue packets but don't change time stamp */
76 static int dly_requeue(struct sk_buff *skb, struct Qdisc *sch)
78 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
81 ret = q->qdisc->ops->requeue(skb, q->qdisc);
87 static unsigned int dly_drop(struct Qdisc *sch)
89 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
92 len = q->qdisc->ops->drop(q->qdisc);
101 * If packet needs to be held up, then stop the
102 * queue and set timer to wakeup later.
104 static struct sk_buff *dly_dequeue(struct Qdisc *sch)
106 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
107 struct sk_buff *skb = q->qdisc->dequeue(q->qdisc);
110 struct dly_skb_cb *cb = (struct dly_skb_cb *)skb->cb;
114 PSCHED_GET_TIME(now);
115 diff = q->latency - PSCHED_TDIFF(now, cb->queuetime);
119 sch->flags &= ~TCQ_F_THROTTLED;
123 if (!netif_queue_stopped(sch->dev)) {
124 long delay = PSCHED_US2JIFFIE(diff);
127 mod_timer(&q->timer, jiffies+delay);
130 if (q->qdisc->ops->requeue(skb, q->qdisc) != NET_XMIT_SUCCESS) {
134 sch->flags |= TCQ_F_THROTTLED;
139 static void dly_reset(struct Qdisc *sch)
141 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
143 qdisc_reset(q->qdisc);
145 sch->flags &= ~TCQ_F_THROTTLED;
146 del_timer(&q->timer);
149 static void dly_timer(unsigned long arg)
151 struct Qdisc *sch = (struct Qdisc *)arg;
153 sch->flags &= ~TCQ_F_THROTTLED;
154 netif_schedule(sch->dev);
157 /* Tell Fifo the new limit. */
158 static int change_limit(struct Qdisc *q, u32 limit)
163 rta = kmalloc(RTA_LENGTH(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
167 rta->rta_type = RTM_NEWQDISC;
168 rta->rta_len = RTA_LENGTH(sizeof(struct tc_fifo_qopt));
169 ((struct tc_fifo_qopt *)RTA_DATA(rta))->limit = limit;
170 ret = q->ops->change(q, rta);
176 /* Setup underlying FIFO discipline */
177 static int dly_change(struct Qdisc *sch, struct rtattr *opt)
179 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
180 struct tc_dly_qopt *qopt = RTA_DATA(opt);
183 if (q->qdisc == &noop_qdisc) {
185 = qdisc_create_dflt(sch->dev, &bfifo_qdisc_ops);
191 err = change_limit(q->qdisc, qopt->limit);
193 qdisc_destroy(q->qdisc);
194 q->qdisc = &noop_qdisc;
196 q->latency = qopt->latency;
197 q->limit = qopt->limit;
202 static int dly_init(struct Qdisc *sch, struct rtattr *opt)
204 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
209 init_timer(&q->timer);
210 q->timer.function = dly_timer;
211 q->timer.data = (unsigned long) sch;
212 q->qdisc = &noop_qdisc;
214 return dly_change(sch, opt);
217 static void dly_destroy(struct Qdisc *sch)
219 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
221 del_timer(&q->timer);
222 qdisc_destroy(q->qdisc);
223 q->qdisc = &noop_qdisc;
226 static int dly_dump(struct Qdisc *sch, struct sk_buff *skb)
228 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
229 unsigned char *b = skb->tail;
230 struct tc_dly_qopt qopt;
232 qopt.latency = q->latency;
233 qopt.limit = q->limit;
235 RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
240 skb_trim(skb, b - skb->data);
244 static struct Qdisc_ops dly_qdisc_ops = {
246 .priv_size = sizeof(struct dly_sched_data),
247 .enqueue = dly_enqueue,
248 .dequeue = dly_dequeue,
249 .requeue = dly_requeue,
253 .destroy = dly_destroy,
254 .change = dly_change,
256 .owner = THIS_MODULE,
260 static int __init dly_module_init(void)
262 return register_qdisc(&dly_qdisc_ops);
264 static void __exit dly_module_exit(void)
266 unregister_qdisc(&dly_qdisc_ops);
268 module_init(dly_module_init)
269 module_exit(dly_module_exit)
270 MODULE_LICENSE("GPL");