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 ((struct tc_fifo_qopt *)RTA_DATA(rta))->limit = limit;
169 ret = q->ops->change(q, rta);
175 /* Setup underlying FIFO discipline */
176 static int dly_change(struct Qdisc *sch, struct rtattr *opt)
178 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
179 struct tc_dly_qopt *qopt = RTA_DATA(opt);
182 if (q->qdisc == &noop_qdisc) {
184 = qdisc_create_dflt(sch->dev, &bfifo_qdisc_ops);
190 err = change_limit(q->qdisc, qopt->limit);
192 qdisc_destroy(q->qdisc);
193 q->qdisc = &noop_qdisc;
195 q->latency = qopt->latency;
196 q->limit = qopt->limit;
201 static int dly_init(struct Qdisc *sch, struct rtattr *opt)
203 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
208 init_timer(&q->timer);
209 q->timer.function = dly_timer;
210 q->timer.data = (unsigned long) sch;
211 q->qdisc = &noop_qdisc;
213 return dly_change(sch, opt);
216 static void dly_destroy(struct Qdisc *sch)
218 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
220 del_timer(&q->timer);
221 qdisc_destroy(q->qdisc);
222 q->qdisc = &noop_qdisc;
225 static int dly_dump(struct Qdisc *sch, struct sk_buff *skb)
227 struct dly_sched_data *q = (struct dly_sched_data *)sch->data;
228 unsigned char *b = skb->tail;
229 struct tc_dly_qopt qopt;
231 qopt.latency = q->latency;
232 qopt.limit = q->limit;
234 RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
239 skb_trim(skb, b - skb->data);
243 static struct Qdisc_ops dly_qdisc_ops = {
245 .priv_size = sizeof(struct dly_sched_data),
246 .enqueue = dly_enqueue,
247 .dequeue = dly_dequeue,
248 .requeue = dly_requeue,
252 .destroy = dly_destroy,
253 .change = dly_change,
255 .owner = THIS_MODULE,
259 static int __init dly_module_init(void)
261 return register_qdisc(&dly_qdisc_ops);
263 static void __exit dly_module_exit(void)
265 unregister_qdisc(&dly_qdisc_ops);
267 module_init(dly_module_init)
268 module_exit(dly_module_exit)
269 MODULE_LICENSE("GPL");