blob: 45939bafbdf894dfcb61a0ed993d1d79204aa5a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_netem.c Network emulator
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 * Many of the algorithms and ideas for this came from
10 * NIST Net which is not copyrighted.
11 *
12 * Authors: Stephen Hemminger <shemminger@osdl.org>
13 * Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/bitops.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/netdevice.h>
22#include <linux/skbuff.h>
23#include <linux/rtnetlink.h>
24
25#include <net/pkt_sched.h>
26
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -080027#define VERSION "1.2"
Stephen Hemmingereb229c42005-11-03 13:49:01 -080028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Network Emulation Queuing algorithm.
30 ====================================
31
32 Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
33 Network Emulation Tool
34 [2] Luigi Rizzo, DummyNet for FreeBSD
35
36 ----------------------------------------------------------------
37
38 This started out as a simple way to delay outgoing packets to
39 test TCP but has grown to include most of the functionality
40 of a full blown network emulator like NISTnet. It can delay
41 packets and add random jitter (and correlation). The random
42 distribution can be loaded from a table as well to provide
43 normal, Pareto, or experimental curves. Packet loss,
44 duplication, and reordering can also be emulated.
45
46 This qdisc does not do classification that can be handled in
47 layering other disciplines. It does not need to do bandwidth
48 control either since that can be handled by using token
49 bucket or other rate control.
50
51 The simulator is limited by the Linux timer resolution
52 and will create packet bursts on the HZ boundary (1ms).
53*/
54
55struct netem_sched_data {
56 struct Qdisc *qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct timer_list timer;
58
59 u32 latency;
60 u32 loss;
61 u32 limit;
62 u32 counter;
63 u32 gap;
64 u32 jitter;
65 u32 duplicate;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -070066 u32 reorder;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -080067 u32 corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 struct crndstate {
70 unsigned long last;
71 unsigned long rho;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -080072 } delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 struct disttable {
75 u32 size;
76 s16 table[0];
77 } *delay_dist;
78};
79
80/* Time stamp put into socket buffer control block */
81struct netem_skb_cb {
82 psched_time_t time_to_send;
83};
84
85/* init_crandom - initialize correlated random number generator
86 * Use entropy source for initial seed.
87 */
88static void init_crandom(struct crndstate *state, unsigned long rho)
89{
90 state->rho = rho;
91 state->last = net_random();
92}
93
94/* get_crandom - correlated random number generator
95 * Next number depends on last value.
96 * rho is scaled to avoid floating point.
97 */
98static unsigned long get_crandom(struct crndstate *state)
99{
100 u64 value, rho;
101 unsigned long answer;
102
103 if (state->rho == 0) /* no correllation */
104 return net_random();
105
106 value = net_random();
107 rho = (u64)state->rho + 1;
108 answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
109 state->last = answer;
110 return answer;
111}
112
113/* tabledist - return a pseudo-randomly distributed value with mean mu and
114 * std deviation sigma. Uses table lookup to approximate the desired
115 * distribution, and a uniformly-distributed pseudo-random source.
116 */
117static long tabledist(unsigned long mu, long sigma,
118 struct crndstate *state, const struct disttable *dist)
119{
120 long t, x;
121 unsigned long rnd;
122
123 if (sigma == 0)
124 return mu;
125
126 rnd = get_crandom(state);
127
128 /* default uniform distribution */
129 if (dist == NULL)
130 return (rnd % (2*sigma)) - sigma + mu;
131
132 t = dist->table[rnd % dist->size];
133 x = (sigma % NETEM_DIST_SCALE) * t;
134 if (x >= 0)
135 x += NETEM_DIST_SCALE/2;
136 else
137 x -= NETEM_DIST_SCALE/2;
138
139 return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
140}
141
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700142/*
143 * Insert one skb into qdisc.
144 * Note: parent depends on return value to account for queue length.
145 * NET_XMIT_DROP: queue length didn't change.
146 * NET_XMIT_SUCCESS: one skb was queued.
147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
149{
150 struct netem_sched_data *q = qdisc_priv(sch);
Guillaume Chazarain89e1df72006-07-21 14:45:25 -0700151 /* We don't fill cb now as skb_unshare() may invalidate it */
152 struct netem_skb_cb *cb;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700153 struct sk_buff *skb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int ret;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700155 int count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Stephen Hemminger771018e2005-05-03 16:24:32 -0700157 pr_debug("netem_enqueue skb=%p\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700159 /* Random duplication */
160 if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
161 ++count;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Random packet drop 0 => none, ~0 => all */
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700164 if (q->loss && q->loss >= get_crandom(&q->loss_cor))
165 --count;
166
167 if (count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 sch->qstats.drops++;
169 kfree_skb(skb);
Stephen Hemminger89bbb0a32006-04-28 12:11:36 -0700170 return NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700173 /*
174 * If we need to duplicate packet, then re-insert at top of the
175 * qdisc tree, since parent queuer expects that only one
176 * skb will be queued.
177 */
178 if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
179 struct Qdisc *rootq = sch->dev->qdisc;
180 u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
181 q->duplicate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700183 rootq->enqueue(skb2, rootq);
184 q->duplicate = dupsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800187 /*
188 * Randomized packet corruption.
189 * Make copy if needed since we are modifying
190 * If packet is going to be hardware checksummed, then
191 * do it now in software before we mangle it.
192 */
193 if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
194 if (!(skb = skb_unshare(skb, GFP_ATOMIC))
Patrick McHardy84fa7932006-08-29 16:44:56 -0700195 || (skb->ip_summed == CHECKSUM_PARTIAL
196 && skb_checksum_help(skb))) {
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800197 sch->qstats.drops++;
198 return NET_XMIT_DROP;
199 }
200
201 skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
202 }
203
Guillaume Chazarain89e1df72006-07-21 14:45:25 -0700204 cb = (struct netem_skb_cb *)skb->cb;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700205 if (q->gap == 0 /* not doing reordering */
206 || q->counter < q->gap /* inside last reordering gap */
207 || q->reorder < get_crandom(&q->reorder_cor)) {
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700208 psched_time_t now;
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800209 psched_tdiff_t delay;
210
211 delay = tabledist(q->latency, q->jitter,
212 &q->delay_cor, q->delay_dist);
213
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700214 PSCHED_GET_TIME(now);
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800215 PSCHED_TADD2(now, delay, cb->time_to_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ++q->counter;
217 ret = q->qdisc->enqueue(skb, q->qdisc);
218 } else {
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700219 /*
220 * Do re-ordering by putting one out of N packets at the front
221 * of the queue.
222 */
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700223 PSCHED_GET_TIME(cb->time_to_send);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700224 q->counter = 0;
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700225 ret = q->qdisc->ops->requeue(skb, q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
228 if (likely(ret == NET_XMIT_SUCCESS)) {
229 sch->q.qlen++;
230 sch->bstats.bytes += skb->len;
231 sch->bstats.packets++;
232 } else
233 sch->qstats.drops++;
234
Stephen Hemmingerd5d75cd2005-05-03 16:24:57 -0700235 pr_debug("netem: enqueue ret %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
237}
238
239/* Requeue packets but don't change time stamp */
240static int netem_requeue(struct sk_buff *skb, struct Qdisc *sch)
241{
242 struct netem_sched_data *q = qdisc_priv(sch);
243 int ret;
244
245 if ((ret = q->qdisc->ops->requeue(skb, q->qdisc)) == 0) {
246 sch->q.qlen++;
247 sch->qstats.requeues++;
248 }
249
250 return ret;
251}
252
253static unsigned int netem_drop(struct Qdisc* sch)
254{
255 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy6d037a22006-03-20 19:00:49 -0800256 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Patrick McHardy6d037a22006-03-20 19:00:49 -0800258 if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sch->q.qlen--;
260 sch->qstats.drops++;
261 }
262 return len;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static struct sk_buff *netem_dequeue(struct Qdisc *sch)
266{
267 struct netem_sched_data *q = qdisc_priv(sch);
268 struct sk_buff *skb;
269
270 skb = q->qdisc->dequeue(q->qdisc);
Stephen Hemminger771018e2005-05-03 16:24:32 -0700271 if (skb) {
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700272 const struct netem_skb_cb *cb
273 = (const struct netem_skb_cb *)skb->cb;
274 psched_time_t now;
Stephen Hemminger771018e2005-05-03 16:24:32 -0700275
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700276 /* if more time remaining? */
277 PSCHED_GET_TIME(now);
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800278
279 if (PSCHED_TLESS(cb->time_to_send, now)) {
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700280 pr_debug("netem_dequeue: return skb=%p\n", skb);
281 sch->q.qlen--;
282 sch->flags &= ~TCQ_F_THROTTLED;
283 return skb;
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800284 } else {
285 psched_tdiff_t delay = PSCHED_TDIFF(cb->time_to_send, now);
286
287 if (q->qdisc->ops->requeue(skb, q->qdisc) != NET_XMIT_SUCCESS) {
288 sch->qstats.drops++;
289
290 /* After this qlen is confused */
291 printk(KERN_ERR "netem: queue discpline %s could not requeue\n",
292 q->qdisc->ops->id);
293
294 sch->q.qlen--;
295 }
296
297 mod_timer(&q->timer, jiffies + PSCHED_US2JIFFIE(delay));
298 sch->flags |= TCQ_F_THROTTLED;
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700299 }
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700300 }
301
302 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static void netem_watchdog(unsigned long arg)
306{
307 struct Qdisc *sch = (struct Qdisc *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Stephen Hemminger771018e2005-05-03 16:24:32 -0700309 pr_debug("netem_watchdog qlen=%d\n", sch->q.qlen);
310 sch->flags &= ~TCQ_F_THROTTLED;
311 netif_schedule(sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static void netem_reset(struct Qdisc *sch)
315{
316 struct netem_sched_data *q = qdisc_priv(sch);
317
318 qdisc_reset(q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sch->q.qlen = 0;
Stephen Hemminger771018e2005-05-03 16:24:32 -0700320 sch->flags &= ~TCQ_F_THROTTLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 del_timer_sync(&q->timer);
322}
323
Stephen Hemminger300ce172005-10-30 13:47:34 -0800324/* Pass size change message down to embedded FIFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int set_fifo_limit(struct Qdisc *q, int limit)
326{
327 struct rtattr *rta;
328 int ret = -ENOMEM;
329
Stephen Hemminger300ce172005-10-30 13:47:34 -0800330 /* Hack to avoid sending change message to non-FIFO */
331 if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
332 return 0;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rta = kmalloc(RTA_LENGTH(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
335 if (rta) {
336 rta->rta_type = RTM_NEWQDISC;
337 rta->rta_len = RTA_LENGTH(sizeof(struct tc_fifo_qopt));
338 ((struct tc_fifo_qopt *)RTA_DATA(rta))->limit = limit;
339
340 ret = q->ops->change(q, rta);
341 kfree(rta);
342 }
343 return ret;
344}
345
346/*
347 * Distribution data is a variable size payload containing
348 * signed 16 bit values.
349 */
350static int get_dist_table(struct Qdisc *sch, const struct rtattr *attr)
351{
352 struct netem_sched_data *q = qdisc_priv(sch);
353 unsigned long n = RTA_PAYLOAD(attr)/sizeof(__s16);
354 const __s16 *data = RTA_DATA(attr);
355 struct disttable *d;
356 int i;
357
358 if (n > 65536)
359 return -EINVAL;
360
361 d = kmalloc(sizeof(*d) + n*sizeof(d->table[0]), GFP_KERNEL);
362 if (!d)
363 return -ENOMEM;
364
365 d->size = n;
366 for (i = 0; i < n; i++)
367 d->table[i] = data[i];
368
369 spin_lock_bh(&sch->dev->queue_lock);
370 d = xchg(&q->delay_dist, d);
371 spin_unlock_bh(&sch->dev->queue_lock);
372
373 kfree(d);
374 return 0;
375}
376
377static int get_correlation(struct Qdisc *sch, const struct rtattr *attr)
378{
379 struct netem_sched_data *q = qdisc_priv(sch);
380 const struct tc_netem_corr *c = RTA_DATA(attr);
381
382 if (RTA_PAYLOAD(attr) != sizeof(*c))
383 return -EINVAL;
384
385 init_crandom(&q->delay_cor, c->delay_corr);
386 init_crandom(&q->loss_cor, c->loss_corr);
387 init_crandom(&q->dup_cor, c->dup_corr);
388 return 0;
389}
390
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700391static int get_reorder(struct Qdisc *sch, const struct rtattr *attr)
392{
393 struct netem_sched_data *q = qdisc_priv(sch);
394 const struct tc_netem_reorder *r = RTA_DATA(attr);
395
396 if (RTA_PAYLOAD(attr) != sizeof(*r))
397 return -EINVAL;
398
399 q->reorder = r->probability;
400 init_crandom(&q->reorder_cor, r->correlation);
401 return 0;
402}
403
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800404static int get_corrupt(struct Qdisc *sch, const struct rtattr *attr)
405{
406 struct netem_sched_data *q = qdisc_priv(sch);
407 const struct tc_netem_corrupt *r = RTA_DATA(attr);
408
409 if (RTA_PAYLOAD(attr) != sizeof(*r))
410 return -EINVAL;
411
412 q->corrupt = r->probability;
413 init_crandom(&q->corrupt_cor, r->correlation);
414 return 0;
415}
416
417/* Parse netlink message to set options */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static int netem_change(struct Qdisc *sch, struct rtattr *opt)
419{
420 struct netem_sched_data *q = qdisc_priv(sch);
421 struct tc_netem_qopt *qopt;
422 int ret;
423
424 if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
425 return -EINVAL;
426
427 qopt = RTA_DATA(opt);
428 ret = set_fifo_limit(q->qdisc, qopt->limit);
429 if (ret) {
430 pr_debug("netem: can't set fifo limit\n");
431 return ret;
432 }
433
434 q->latency = qopt->latency;
435 q->jitter = qopt->jitter;
436 q->limit = qopt->limit;
437 q->gap = qopt->gap;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700438 q->counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 q->loss = qopt->loss;
440 q->duplicate = qopt->duplicate;
441
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700442 /* for compatiablity with earlier versions.
443 * if gap is set, need to assume 100% probablity
444 */
445 q->reorder = ~0;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Handle nested options after initial queue options.
448 * Should have put all options in nested format but too late now.
449 */
450 if (RTA_PAYLOAD(opt) > sizeof(*qopt)) {
451 struct rtattr *tb[TCA_NETEM_MAX];
452 if (rtattr_parse(tb, TCA_NETEM_MAX,
453 RTA_DATA(opt) + sizeof(*qopt),
454 RTA_PAYLOAD(opt) - sizeof(*qopt)))
455 return -EINVAL;
456
457 if (tb[TCA_NETEM_CORR-1]) {
458 ret = get_correlation(sch, tb[TCA_NETEM_CORR-1]);
459 if (ret)
460 return ret;
461 }
462
463 if (tb[TCA_NETEM_DELAY_DIST-1]) {
464 ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST-1]);
465 if (ret)
466 return ret;
467 }
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800468
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700469 if (tb[TCA_NETEM_REORDER-1]) {
470 ret = get_reorder(sch, tb[TCA_NETEM_REORDER-1]);
471 if (ret)
472 return ret;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800475 if (tb[TCA_NETEM_CORRUPT-1]) {
476 ret = get_corrupt(sch, tb[TCA_NETEM_CORRUPT-1]);
477 if (ret)
478 return ret;
479 }
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return 0;
483}
484
Stephen Hemminger300ce172005-10-30 13:47:34 -0800485/*
486 * Special case version of FIFO queue for use by netem.
487 * It queues in order based on timestamps in skb's
488 */
489struct fifo_sched_data {
490 u32 limit;
491};
492
493static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
494{
495 struct fifo_sched_data *q = qdisc_priv(sch);
496 struct sk_buff_head *list = &sch->q;
497 const struct netem_skb_cb *ncb
498 = (const struct netem_skb_cb *)nskb->cb;
499 struct sk_buff *skb;
500
501 if (likely(skb_queue_len(list) < q->limit)) {
502 skb_queue_reverse_walk(list, skb) {
503 const struct netem_skb_cb *cb
504 = (const struct netem_skb_cb *)skb->cb;
505
Andrea Bittauaa875162005-11-20 13:41:05 -0800506 if (!PSCHED_TLESS(ncb->time_to_send, cb->time_to_send))
Stephen Hemminger300ce172005-10-30 13:47:34 -0800507 break;
508 }
509
510 __skb_queue_after(list, skb, nskb);
511
512 sch->qstats.backlog += nskb->len;
513 sch->bstats.bytes += nskb->len;
514 sch->bstats.packets++;
515
516 return NET_XMIT_SUCCESS;
517 }
518
519 return qdisc_drop(nskb, sch);
520}
521
522static int tfifo_init(struct Qdisc *sch, struct rtattr *opt)
523{
524 struct fifo_sched_data *q = qdisc_priv(sch);
525
526 if (opt) {
527 struct tc_fifo_qopt *ctl = RTA_DATA(opt);
528 if (RTA_PAYLOAD(opt) < sizeof(*ctl))
529 return -EINVAL;
530
531 q->limit = ctl->limit;
532 } else
533 q->limit = max_t(u32, sch->dev->tx_queue_len, 1);
534
535 return 0;
536}
537
538static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
539{
540 struct fifo_sched_data *q = qdisc_priv(sch);
541 struct tc_fifo_qopt opt = { .limit = q->limit };
542
543 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
544 return skb->len;
545
546rtattr_failure:
547 return -1;
548}
549
550static struct Qdisc_ops tfifo_qdisc_ops = {
551 .id = "tfifo",
552 .priv_size = sizeof(struct fifo_sched_data),
553 .enqueue = tfifo_enqueue,
554 .dequeue = qdisc_dequeue_head,
555 .requeue = qdisc_requeue,
556 .drop = qdisc_queue_drop,
557 .init = tfifo_init,
558 .reset = qdisc_reset_queue,
559 .change = tfifo_init,
560 .dump = tfifo_dump,
561};
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int netem_init(struct Qdisc *sch, struct rtattr *opt)
564{
565 struct netem_sched_data *q = qdisc_priv(sch);
566 int ret;
567
568 if (!opt)
569 return -EINVAL;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 init_timer(&q->timer);
572 q->timer.function = netem_watchdog;
573 q->timer.data = (unsigned long) sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Stephen Hemminger300ce172005-10-30 13:47:34 -0800575 q->qdisc = qdisc_create_dflt(sch->dev, &tfifo_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!q->qdisc) {
577 pr_debug("netem: qdisc create failed\n");
578 return -ENOMEM;
579 }
580
581 ret = netem_change(sch, opt);
582 if (ret) {
583 pr_debug("netem: change failed\n");
584 qdisc_destroy(q->qdisc);
585 }
586 return ret;
587}
588
589static void netem_destroy(struct Qdisc *sch)
590{
591 struct netem_sched_data *q = qdisc_priv(sch);
592
593 del_timer_sync(&q->timer);
594 qdisc_destroy(q->qdisc);
595 kfree(q->delay_dist);
596}
597
598static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
599{
600 const struct netem_sched_data *q = qdisc_priv(sch);
601 unsigned char *b = skb->tail;
602 struct rtattr *rta = (struct rtattr *) b;
603 struct tc_netem_qopt qopt;
604 struct tc_netem_corr cor;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700605 struct tc_netem_reorder reorder;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800606 struct tc_netem_corrupt corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 qopt.latency = q->latency;
609 qopt.jitter = q->jitter;
610 qopt.limit = q->limit;
611 qopt.loss = q->loss;
612 qopt.gap = q->gap;
613 qopt.duplicate = q->duplicate;
614 RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
615
616 cor.delay_corr = q->delay_cor.rho;
617 cor.loss_corr = q->loss_cor.rho;
618 cor.dup_corr = q->dup_cor.rho;
619 RTA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700620
621 reorder.probability = q->reorder;
622 reorder.correlation = q->reorder_cor.rho;
623 RTA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
624
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800625 corrupt.probability = q->corrupt;
626 corrupt.correlation = q->corrupt_cor.rho;
627 RTA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 rta->rta_len = skb->tail - b;
630
631 return skb->len;
632
633rtattr_failure:
634 skb_trim(skb, b - skb->data);
635 return -1;
636}
637
638static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
639 struct sk_buff *skb, struct tcmsg *tcm)
640{
641 struct netem_sched_data *q = qdisc_priv(sch);
642
643 if (cl != 1) /* only one class */
644 return -ENOENT;
645
646 tcm->tcm_handle |= TC_H_MIN(1);
647 tcm->tcm_info = q->qdisc->handle;
648
649 return 0;
650}
651
652static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
653 struct Qdisc **old)
654{
655 struct netem_sched_data *q = qdisc_priv(sch);
656
657 if (new == NULL)
658 new = &noop_qdisc;
659
660 sch_tree_lock(sch);
661 *old = xchg(&q->qdisc, new);
662 qdisc_reset(*old);
663 sch->q.qlen = 0;
664 sch_tree_unlock(sch);
665
666 return 0;
667}
668
669static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
670{
671 struct netem_sched_data *q = qdisc_priv(sch);
672 return q->qdisc;
673}
674
675static unsigned long netem_get(struct Qdisc *sch, u32 classid)
676{
677 return 1;
678}
679
680static void netem_put(struct Qdisc *sch, unsigned long arg)
681{
682}
683
684static int netem_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
685 struct rtattr **tca, unsigned long *arg)
686{
687 return -ENOSYS;
688}
689
690static int netem_delete(struct Qdisc *sch, unsigned long arg)
691{
692 return -ENOSYS;
693}
694
695static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
696{
697 if (!walker->stop) {
698 if (walker->count >= walker->skip)
699 if (walker->fn(sch, 1, walker) < 0) {
700 walker->stop = 1;
701 return;
702 }
703 walker->count++;
704 }
705}
706
707static struct tcf_proto **netem_find_tcf(struct Qdisc *sch, unsigned long cl)
708{
709 return NULL;
710}
711
712static struct Qdisc_class_ops netem_class_ops = {
713 .graft = netem_graft,
714 .leaf = netem_leaf,
715 .get = netem_get,
716 .put = netem_put,
717 .change = netem_change_class,
718 .delete = netem_delete,
719 .walk = netem_walk,
720 .tcf_chain = netem_find_tcf,
721 .dump = netem_dump_class,
722};
723
724static struct Qdisc_ops netem_qdisc_ops = {
725 .id = "netem",
726 .cl_ops = &netem_class_ops,
727 .priv_size = sizeof(struct netem_sched_data),
728 .enqueue = netem_enqueue,
729 .dequeue = netem_dequeue,
730 .requeue = netem_requeue,
731 .drop = netem_drop,
732 .init = netem_init,
733 .reset = netem_reset,
734 .destroy = netem_destroy,
735 .change = netem_change,
736 .dump = netem_dump,
737 .owner = THIS_MODULE,
738};
739
740
741static int __init netem_module_init(void)
742{
Stephen Hemmingereb229c42005-11-03 13:49:01 -0800743 pr_info("netem: version " VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return register_qdisc(&netem_qdisc_ops);
745}
746static void __exit netem_module_exit(void)
747{
748 unregister_qdisc(&netem_qdisc_ops);
749}
750module_init(netem_module_init)
751module_exit(netem_module_exit)
752MODULE_LICENSE("GPL");