2 * net/sched/sch_netem.c Network emulator
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
9 * Many of the algorithms and ideas for this came from
10 * NIST Net which is not copyrighted.
12 * Authors: Stephen Hemminger <shemminger@osdl.org>
13 * Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/skbuff.h>
21 #include <linux/rtnetlink.h>
23 #include <net/netlink.h>
24 #include <net/pkt_sched.h>
28 /* Network Emulation Queuing algorithm.
29 ====================================
31 Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
32 Network Emulation Tool
33 [2] Luigi Rizzo, DummyNet for FreeBSD
35 ----------------------------------------------------------------
37 This started out as a simple way to delay outgoing packets to
38 test TCP but has grown to include most of the functionality
39 of a full blown network emulator like NISTnet. It can delay
40 packets and add random jitter (and correlation). The random
41 distribution can be loaded from a table as well to provide
42 normal, Pareto, or experimental curves. Packet loss,
43 duplication, and reordering can also be emulated.
45 This qdisc does not do classification that can be handled in
46 layering other disciplines. It does not need to do bandwidth
47 control either since that can be handled by using token
48 bucket or other rate control.
50 The simulator is limited by the Linux timer resolution
51 and will create packet bursts on the HZ boundary (1ms).
54 struct netem_sched_data {
56 struct qdisc_watchdog watchdog;
58 psched_tdiff_t latency;
59 psched_tdiff_t jitter;
72 } delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
80 /* Time stamp put into socket buffer control block */
82 psched_time_t time_to_send;
85 static inline struct netem_skb_cb *netem_skb_cb(struct sk_buff *skb)
87 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct netem_skb_cb));
88 return (struct netem_skb_cb *)skb->cb;
91 /* init_crandom - initialize correlated random number generator
92 * Use entropy source for initial seed.
94 static void init_crandom(struct crndstate *state, unsigned long rho)
97 state->last = net_random();
100 /* get_crandom - correlated random number generator
101 * Next number depends on last value.
102 * rho is scaled to avoid floating point.
104 static u32 get_crandom(struct crndstate *state)
107 unsigned long answer;
109 if (state->rho == 0) /* no correlation */
112 value = net_random();
113 rho = (u64)state->rho + 1;
114 answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
115 state->last = answer;
119 /* tabledist - return a pseudo-randomly distributed value with mean mu and
120 * std deviation sigma. Uses table lookup to approximate the desired
121 * distribution, and a uniformly-distributed pseudo-random source.
123 static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
124 struct crndstate *state,
125 const struct disttable *dist)
134 rnd = get_crandom(state);
136 /* default uniform distribution */
138 return (rnd % (2*sigma)) - sigma + mu;
140 t = dist->table[rnd % dist->size];
141 x = (sigma % NETEM_DIST_SCALE) * t;
143 x += NETEM_DIST_SCALE/2;
145 x -= NETEM_DIST_SCALE/2;
147 return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
151 * Insert one skb into qdisc.
152 * Note: parent depends on return value to account for queue length.
153 * NET_XMIT_DROP: queue length didn't change.
154 * NET_XMIT_SUCCESS: one skb was queued.
156 static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
158 struct netem_sched_data *q = qdisc_priv(sch);
159 /* We don't fill cb now as skb_unshare() may invalidate it */
160 struct netem_skb_cb *cb;
161 struct sk_buff *skb2;
165 pr_debug("netem_enqueue skb=%p\n", skb);
167 /* Random duplication */
168 if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
171 /* Random packet drop 0 => none, ~0 => all */
172 if (q->loss && q->loss >= get_crandom(&q->loss_cor))
178 return NET_XMIT_BYPASS;
184 * If we need to duplicate packet, then re-insert at top of the
185 * qdisc tree, since parent queuer expects that only one
186 * skb will be queued.
188 if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
189 struct Qdisc *rootq = qdisc_root(sch);
190 u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
193 qdisc_enqueue_root(skb2, rootq);
194 q->duplicate = dupsave;
198 * Randomized packet corruption.
199 * Make copy if needed since we are modifying
200 * If packet is going to be hardware checksummed, then
201 * do it now in software before we mangle it.
203 if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
204 if (!(skb = skb_unshare(skb, GFP_ATOMIC))
205 || (skb->ip_summed == CHECKSUM_PARTIAL
206 && skb_checksum_help(skb))) {
208 return NET_XMIT_DROP;
211 skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
214 cb = netem_skb_cb(skb);
215 if (q->gap == 0 /* not doing reordering */
216 || q->counter < q->gap /* inside last reordering gap */
217 || q->reorder < get_crandom(&q->reorder_cor)) {
219 psched_tdiff_t delay;
221 delay = tabledist(q->latency, q->jitter,
222 &q->delay_cor, q->delay_dist);
224 now = psched_get_time();
225 cb->time_to_send = now + delay;
227 ret = qdisc_enqueue(skb, q->qdisc);
230 * Do re-ordering by putting one out of N packets at the front
233 cb->time_to_send = psched_get_time();
235 ret = q->qdisc->ops->requeue(skb, q->qdisc);
238 if (likely(ret == NET_XMIT_SUCCESS)) {
240 sch->bstats.bytes += qdisc_pkt_len(skb);
241 sch->bstats.packets++;
245 pr_debug("netem: enqueue ret %d\n", ret);
249 /* Requeue packets but don't change time stamp */
250 static int netem_requeue(struct sk_buff *skb, struct Qdisc *sch)
252 struct netem_sched_data *q = qdisc_priv(sch);
255 if ((ret = q->qdisc->ops->requeue(skb, q->qdisc)) == 0) {
257 sch->qstats.requeues++;
263 static unsigned int netem_drop(struct Qdisc* sch)
265 struct netem_sched_data *q = qdisc_priv(sch);
266 unsigned int len = 0;
268 if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
275 static struct sk_buff *netem_dequeue(struct Qdisc *sch)
277 struct netem_sched_data *q = qdisc_priv(sch);
281 if (sch->flags & TCQ_F_THROTTLED)
284 skb = q->qdisc->dequeue(q->qdisc);
286 const struct netem_skb_cb *cb = netem_skb_cb(skb);
287 psched_time_t now = psched_get_time();
289 /* if more time remaining? */
290 if (cb->time_to_send <= now) {
291 pr_debug("netem_dequeue: return skb=%p\n", skb);
296 if (unlikely(q->qdisc->ops->requeue(skb, q->qdisc) != NET_XMIT_SUCCESS)) {
297 qdisc_tree_decrease_qlen(q->qdisc, 1);
299 printk(KERN_ERR "netem: %s could not requeue\n",
303 qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
309 static void netem_reset(struct Qdisc *sch)
311 struct netem_sched_data *q = qdisc_priv(sch);
313 qdisc_reset(q->qdisc);
315 qdisc_watchdog_cancel(&q->watchdog);
319 * Distribution data is a variable size payload containing
320 * signed 16 bit values.
322 static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
324 struct netem_sched_data *q = qdisc_priv(sch);
325 unsigned long n = nla_len(attr)/sizeof(__s16);
326 const __s16 *data = nla_data(attr);
327 spinlock_t *root_lock;
334 d = kmalloc(sizeof(*d) + n*sizeof(d->table[0]), GFP_KERNEL);
339 for (i = 0; i < n; i++)
340 d->table[i] = data[i];
342 root_lock = qdisc_root_lock(sch);
344 spin_lock_bh(root_lock);
345 d = xchg(&q->delay_dist, d);
346 spin_unlock_bh(root_lock);
352 static int get_correlation(struct Qdisc *sch, const struct nlattr *attr)
354 struct netem_sched_data *q = qdisc_priv(sch);
355 const struct tc_netem_corr *c = nla_data(attr);
357 init_crandom(&q->delay_cor, c->delay_corr);
358 init_crandom(&q->loss_cor, c->loss_corr);
359 init_crandom(&q->dup_cor, c->dup_corr);
363 static int get_reorder(struct Qdisc *sch, const struct nlattr *attr)
365 struct netem_sched_data *q = qdisc_priv(sch);
366 const struct tc_netem_reorder *r = nla_data(attr);
368 q->reorder = r->probability;
369 init_crandom(&q->reorder_cor, r->correlation);
373 static int get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
375 struct netem_sched_data *q = qdisc_priv(sch);
376 const struct tc_netem_corrupt *r = nla_data(attr);
378 q->corrupt = r->probability;
379 init_crandom(&q->corrupt_cor, r->correlation);
383 static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
384 [TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
385 [TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
386 [TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
389 /* Parse netlink message to set options */
390 static int netem_change(struct Qdisc *sch, struct nlattr *opt)
392 struct netem_sched_data *q = qdisc_priv(sch);
393 struct nlattr *tb[TCA_NETEM_MAX + 1];
394 struct tc_netem_qopt *qopt;
400 ret = nla_parse_nested_compat(tb, TCA_NETEM_MAX, opt, netem_policy,
401 qopt, sizeof(*qopt));
405 ret = fifo_set_limit(q->qdisc, qopt->limit);
407 pr_debug("netem: can't set fifo limit\n");
411 q->latency = qopt->latency;
412 q->jitter = qopt->jitter;
413 q->limit = qopt->limit;
416 q->loss = qopt->loss;
417 q->duplicate = qopt->duplicate;
419 /* for compatibility with earlier versions.
420 * if gap is set, need to assume 100% probability
425 if (tb[TCA_NETEM_CORR]) {
426 ret = get_correlation(sch, tb[TCA_NETEM_CORR]);
431 if (tb[TCA_NETEM_DELAY_DIST]) {
432 ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
437 if (tb[TCA_NETEM_REORDER]) {
438 ret = get_reorder(sch, tb[TCA_NETEM_REORDER]);
443 if (tb[TCA_NETEM_CORRUPT]) {
444 ret = get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
453 * Special case version of FIFO queue for use by netem.
454 * It queues in order based on timestamps in skb's
456 struct fifo_sched_data {
458 psched_time_t oldest;
461 static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
463 struct fifo_sched_data *q = qdisc_priv(sch);
464 struct sk_buff_head *list = &sch->q;
465 psched_time_t tnext = netem_skb_cb(nskb)->time_to_send;
468 if (likely(skb_queue_len(list) < q->limit)) {
469 /* Optimize for add at tail */
470 if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
472 return qdisc_enqueue_tail(nskb, sch);
475 skb_queue_reverse_walk(list, skb) {
476 const struct netem_skb_cb *cb = netem_skb_cb(skb);
478 if (tnext >= cb->time_to_send)
482 __skb_queue_after(list, skb, nskb);
484 sch->qstats.backlog += qdisc_pkt_len(nskb);
485 sch->bstats.bytes += qdisc_pkt_len(nskb);
486 sch->bstats.packets++;
488 return NET_XMIT_SUCCESS;
491 return qdisc_reshape_fail(nskb, sch);
494 static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
496 struct fifo_sched_data *q = qdisc_priv(sch);
499 struct tc_fifo_qopt *ctl = nla_data(opt);
500 if (nla_len(opt) < sizeof(*ctl))
503 q->limit = ctl->limit;
505 q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
507 q->oldest = PSCHED_PASTPERFECT;
511 static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
513 struct fifo_sched_data *q = qdisc_priv(sch);
514 struct tc_fifo_qopt opt = { .limit = q->limit };
516 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
523 static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
525 .priv_size = sizeof(struct fifo_sched_data),
526 .enqueue = tfifo_enqueue,
527 .dequeue = qdisc_dequeue_head,
528 .requeue = qdisc_requeue,
529 .drop = qdisc_queue_drop,
531 .reset = qdisc_reset_queue,
532 .change = tfifo_init,
536 static int netem_init(struct Qdisc *sch, struct nlattr *opt)
538 struct netem_sched_data *q = qdisc_priv(sch);
544 qdisc_watchdog_init(&q->watchdog, sch);
546 q->qdisc = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
548 TC_H_MAKE(sch->handle, 1));
550 pr_debug("netem: qdisc create failed\n");
554 ret = netem_change(sch, opt);
556 pr_debug("netem: change failed\n");
557 qdisc_destroy(q->qdisc);
562 static void netem_destroy(struct Qdisc *sch)
564 struct netem_sched_data *q = qdisc_priv(sch);
566 qdisc_watchdog_cancel(&q->watchdog);
567 qdisc_destroy(q->qdisc);
568 kfree(q->delay_dist);
571 static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
573 const struct netem_sched_data *q = qdisc_priv(sch);
574 unsigned char *b = skb_tail_pointer(skb);
575 struct nlattr *nla = (struct nlattr *) b;
576 struct tc_netem_qopt qopt;
577 struct tc_netem_corr cor;
578 struct tc_netem_reorder reorder;
579 struct tc_netem_corrupt corrupt;
581 qopt.latency = q->latency;
582 qopt.jitter = q->jitter;
583 qopt.limit = q->limit;
586 qopt.duplicate = q->duplicate;
587 NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
589 cor.delay_corr = q->delay_cor.rho;
590 cor.loss_corr = q->loss_cor.rho;
591 cor.dup_corr = q->dup_cor.rho;
592 NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
594 reorder.probability = q->reorder;
595 reorder.correlation = q->reorder_cor.rho;
596 NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
598 corrupt.probability = q->corrupt;
599 corrupt.correlation = q->corrupt_cor.rho;
600 NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
602 nla->nla_len = skb_tail_pointer(skb) - b;
611 static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
612 struct sk_buff *skb, struct tcmsg *tcm)
614 struct netem_sched_data *q = qdisc_priv(sch);
616 if (cl != 1) /* only one class */
619 tcm->tcm_handle |= TC_H_MIN(1);
620 tcm->tcm_info = q->qdisc->handle;
625 static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
628 struct netem_sched_data *q = qdisc_priv(sch);
634 *old = xchg(&q->qdisc, new);
635 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
637 sch_tree_unlock(sch);
642 static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
644 struct netem_sched_data *q = qdisc_priv(sch);
648 static unsigned long netem_get(struct Qdisc *sch, u32 classid)
653 static void netem_put(struct Qdisc *sch, unsigned long arg)
657 static int netem_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
658 struct nlattr **tca, unsigned long *arg)
663 static int netem_delete(struct Qdisc *sch, unsigned long arg)
668 static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
671 if (walker->count >= walker->skip)
672 if (walker->fn(sch, 1, walker) < 0) {
680 static struct tcf_proto **netem_find_tcf(struct Qdisc *sch, unsigned long cl)
685 static const struct Qdisc_class_ops netem_class_ops = {
686 .graft = netem_graft,
690 .change = netem_change_class,
691 .delete = netem_delete,
693 .tcf_chain = netem_find_tcf,
694 .dump = netem_dump_class,
697 static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
699 .cl_ops = &netem_class_ops,
700 .priv_size = sizeof(struct netem_sched_data),
701 .enqueue = netem_enqueue,
702 .dequeue = netem_dequeue,
703 .requeue = netem_requeue,
706 .reset = netem_reset,
707 .destroy = netem_destroy,
708 .change = netem_change,
710 .owner = THIS_MODULE,
714 static int __init netem_module_init(void)
716 pr_info("netem: version " VERSION "\n");
717 return register_qdisc(&netem_qdisc_ops);
719 static void __exit netem_module_exit(void)
721 unregister_qdisc(&netem_qdisc_ops);
723 module_init(netem_module_init)
724 module_exit(netem_module_exit)
725 MODULE_LICENSE("GPL");