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>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/skbuff.h>
23 #include <linux/vmalloc.h>
24 #include <linux/rtnetlink.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
31 /* Network Emulation Queuing algorithm.
32 ====================================
34 Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
35 Network Emulation Tool
36 [2] Luigi Rizzo, DummyNet for FreeBSD
38 ----------------------------------------------------------------
40 This started out as a simple way to delay outgoing packets to
41 test TCP but has grown to include most of the functionality
42 of a full blown network emulator like NISTnet. It can delay
43 packets and add random jitter (and correlation). The random
44 distribution can be loaded from a table as well to provide
45 normal, Pareto, or experimental curves. Packet loss,
46 duplication, and reordering can also be emulated.
48 This qdisc does not do classification that can be handled in
49 layering other disciplines. It does not need to do bandwidth
50 control either since that can be handled by using token
51 bucket or other rate control.
53 Correlated Loss Generator models
55 Added generation of correlated loss according to the
56 "Gilbert-Elliot" model, a 4-state markov model.
59 [1] NetemCLG Home http://netgroup.uniroma2.it/NetemCLG
60 [2] S. Salsano, F. Ludovici, A. Ordine, "Definition of a general
61 and intuitive loss model for packet networks and its implementation
62 in the Netem module in the Linux kernel", available in [1]
64 Authors: Stefano Salsano <stefano.salsano at uniroma2.it
65 Fabio Ludovici <fabio.ludovici at yahoo.it>
68 struct netem_sched_data
{
70 struct qdisc_watchdog watchdog
;
72 psched_tdiff_t latency
;
73 psched_tdiff_t jitter
;
87 } delay_cor
, loss_cor
, dup_cor
, reorder_cor
, corrupt_cor
;
100 /* Correlated Loss Generation models */
102 /* state of the Markov chain */
105 /* 4-states and Gilbert-Elliot models */
106 u32 a1
; /* p13 for 4-states or p for GE */
107 u32 a2
; /* p31 for 4-states or r for GE */
108 u32 a3
; /* p32 for 4-states or h for GE */
109 u32 a4
; /* p14 for 4-states or 1-k for GE */
110 u32 a5
; /* p23 used only in 4-states */
115 /* Time stamp put into socket buffer control block */
116 struct netem_skb_cb
{
117 psched_time_t time_to_send
;
120 static inline struct netem_skb_cb
*netem_skb_cb(struct sk_buff
*skb
)
122 BUILD_BUG_ON(sizeof(skb
->cb
) <
123 sizeof(struct qdisc_skb_cb
) + sizeof(struct netem_skb_cb
));
124 return (struct netem_skb_cb
*)qdisc_skb_cb(skb
)->data
;
127 /* init_crandom - initialize correlated random number generator
128 * Use entropy source for initial seed.
130 static void init_crandom(struct crndstate
*state
, unsigned long rho
)
133 state
->last
= net_random();
136 /* get_crandom - correlated random number generator
137 * Next number depends on last value.
138 * rho is scaled to avoid floating point.
140 static u32
get_crandom(struct crndstate
*state
)
143 unsigned long answer
;
145 if (state
->rho
== 0) /* no correlation */
148 value
= net_random();
149 rho
= (u64
)state
->rho
+ 1;
150 answer
= (value
* ((1ull<<32) - rho
) + state
->last
* rho
) >> 32;
151 state
->last
= answer
;
155 /* loss_4state - 4-state model loss generator
156 * Generates losses according to the 4-state Markov chain adopted in
157 * the GI (General and Intuitive) loss model.
159 static bool loss_4state(struct netem_sched_data
*q
)
161 struct clgstate
*clg
= &q
->clg
;
162 u32 rnd
= net_random();
165 * Makes a comparison between rnd and the transition
166 * probabilities outgoing from the current state, then decides the
167 * next state and if the next packet has to be transmitted or lost.
168 * The four states correspond to:
169 * 1 => successfully transmitted packets within a gap period
170 * 4 => isolated losses within a gap period
171 * 3 => lost packets within a burst period
172 * 2 => successfully transmitted packets within a burst period
174 switch (clg
->state
) {
179 } else if (clg
->a4
< rnd
&& rnd
< clg
->a1
) {
182 } else if (clg
->a1
< rnd
)
197 else if (clg
->a3
< rnd
&& rnd
< clg
->a2
+ clg
->a3
) {
200 } else if (clg
->a2
+ clg
->a3
< rnd
) {
213 /* loss_gilb_ell - Gilbert-Elliot model loss generator
214 * Generates losses according to the Gilbert-Elliot loss model or
215 * its special cases (Gilbert or Simple Gilbert)
217 * Makes a comparison between random number and the transition
218 * probabilities outgoing from the current state, then decides the
219 * next state. A second random number is extracted and the comparison
220 * with the loss probability of the current state decides if the next
221 * packet will be transmitted or lost.
223 static bool loss_gilb_ell(struct netem_sched_data
*q
)
225 struct clgstate
*clg
= &q
->clg
;
227 switch (clg
->state
) {
229 if (net_random() < clg
->a1
)
231 if (net_random() < clg
->a4
)
234 if (net_random() < clg
->a2
)
236 if (clg
->a3
> net_random())
243 static bool loss_event(struct netem_sched_data
*q
)
245 switch (q
->loss_model
) {
247 /* Random packet drop 0 => none, ~0 => all */
248 return q
->loss
&& q
->loss
>= get_crandom(&q
->loss_cor
);
251 /* 4state loss model algorithm (used also for GI model)
252 * Extracts a value from the markov 4 state loss generator,
253 * if it is 1 drops a packet and if needed writes the event in
256 return loss_4state(q
);
259 /* Gilbert-Elliot loss model algorithm
260 * Extracts a value from the Gilbert-Elliot loss generator,
261 * if it is 1 drops a packet and if needed writes the event in
264 return loss_gilb_ell(q
);
267 return false; /* not reached */
271 /* tabledist - return a pseudo-randomly distributed value with mean mu and
272 * std deviation sigma. Uses table lookup to approximate the desired
273 * distribution, and a uniformly-distributed pseudo-random source.
275 static psched_tdiff_t
tabledist(psched_tdiff_t mu
, psched_tdiff_t sigma
,
276 struct crndstate
*state
,
277 const struct disttable
*dist
)
286 rnd
= get_crandom(state
);
288 /* default uniform distribution */
290 return (rnd
% (2*sigma
)) - sigma
+ mu
;
292 t
= dist
->table
[rnd
% dist
->size
];
293 x
= (sigma
% NETEM_DIST_SCALE
) * t
;
295 x
+= NETEM_DIST_SCALE
/2;
297 x
-= NETEM_DIST_SCALE
/2;
299 return x
/ NETEM_DIST_SCALE
+ (sigma
/ NETEM_DIST_SCALE
) * t
+ mu
;
302 static psched_time_t
packet_len_2_sched_time(unsigned int len
, u32 rate
)
304 u64 ticks
= (u64
)len
* NSEC_PER_SEC
;
307 return PSCHED_NS2TICKS(ticks
);
311 * Insert one skb into qdisc.
312 * Note: parent depends on return value to account for queue length.
313 * NET_XMIT_DROP: queue length didn't change.
314 * NET_XMIT_SUCCESS: one skb was queued.
316 static int netem_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
318 struct netem_sched_data
*q
= qdisc_priv(sch
);
319 /* We don't fill cb now as skb_unshare() may invalidate it */
320 struct netem_skb_cb
*cb
;
321 struct sk_buff
*skb2
;
325 /* Random duplication */
326 if (q
->duplicate
&& q
->duplicate
>= get_crandom(&q
->dup_cor
))
336 return NET_XMIT_SUCCESS
| __NET_XMIT_BYPASS
;
342 * If we need to duplicate packet, then re-insert at top of the
343 * qdisc tree, since parent queuer expects that only one
344 * skb will be queued.
346 if (count
> 1 && (skb2
= skb_clone(skb
, GFP_ATOMIC
)) != NULL
) {
347 struct Qdisc
*rootq
= qdisc_root(sch
);
348 u32 dupsave
= q
->duplicate
; /* prevent duplicating a dup... */
351 qdisc_enqueue_root(skb2
, rootq
);
352 q
->duplicate
= dupsave
;
356 * Randomized packet corruption.
357 * Make copy if needed since we are modifying
358 * If packet is going to be hardware checksummed, then
359 * do it now in software before we mangle it.
361 if (q
->corrupt
&& q
->corrupt
>= get_crandom(&q
->corrupt_cor
)) {
362 if (!(skb
= skb_unshare(skb
, GFP_ATOMIC
)) ||
363 (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
364 skb_checksum_help(skb
))) {
366 return NET_XMIT_DROP
;
369 skb
->data
[net_random() % skb_headlen(skb
)] ^= 1<<(net_random() % 8);
372 cb
= netem_skb_cb(skb
);
373 if (q
->gap
== 0 || /* not doing reordering */
374 q
->counter
< q
->gap
|| /* inside last reordering gap */
375 q
->reorder
< get_crandom(&q
->reorder_cor
)) {
377 psched_tdiff_t delay
;
379 delay
= tabledist(q
->latency
, q
->jitter
,
380 &q
->delay_cor
, q
->delay_dist
);
382 now
= psched_get_time();
385 struct sk_buff_head
*list
= &q
->qdisc
->q
;
387 delay
+= packet_len_2_sched_time(skb
->len
, q
->rate
);
389 if (!skb_queue_empty(list
)) {
391 * Last packet in queue is reference point (now).
392 * First packet in queue is already in flight,
393 * calculate this time bonus and substract
396 delay
-= now
- netem_skb_cb(skb_peek(list
))->time_to_send
;
397 now
= netem_skb_cb(skb_peek_tail(list
))->time_to_send
;
401 cb
->time_to_send
= now
+ delay
;
403 ret
= qdisc_enqueue(skb
, q
->qdisc
);
406 * Do re-ordering by putting one out of N packets at the front
409 cb
->time_to_send
= psched_get_time();
412 __skb_queue_head(&q
->qdisc
->q
, skb
);
413 q
->qdisc
->qstats
.backlog
+= qdisc_pkt_len(skb
);
414 q
->qdisc
->qstats
.requeues
++;
415 ret
= NET_XMIT_SUCCESS
;
418 if (ret
!= NET_XMIT_SUCCESS
) {
419 if (net_xmit_drop_count(ret
)) {
426 return NET_XMIT_SUCCESS
;
429 static unsigned int netem_drop(struct Qdisc
*sch
)
431 struct netem_sched_data
*q
= qdisc_priv(sch
);
432 unsigned int len
= 0;
434 if (q
->qdisc
->ops
->drop
&& (len
= q
->qdisc
->ops
->drop(q
->qdisc
)) != 0) {
441 static struct sk_buff
*netem_dequeue(struct Qdisc
*sch
)
443 struct netem_sched_data
*q
= qdisc_priv(sch
);
446 if (qdisc_is_throttled(sch
))
449 skb
= q
->qdisc
->ops
->peek(q
->qdisc
);
451 const struct netem_skb_cb
*cb
= netem_skb_cb(skb
);
452 psched_time_t now
= psched_get_time();
454 /* if more time remaining? */
455 if (cb
->time_to_send
<= now
) {
456 skb
= qdisc_dequeue_peeked(q
->qdisc
);
460 #ifdef CONFIG_NET_CLS_ACT
462 * If it's at ingress let's pretend the delay is
463 * from the network (tstamp will be updated).
465 if (G_TC_FROM(skb
->tc_verd
) & AT_INGRESS
)
466 skb
->tstamp
.tv64
= 0;
470 qdisc_unthrottled(sch
);
471 qdisc_bstats_update(sch
, skb
);
475 qdisc_watchdog_schedule(&q
->watchdog
, cb
->time_to_send
);
481 static void netem_reset(struct Qdisc
*sch
)
483 struct netem_sched_data
*q
= qdisc_priv(sch
);
485 qdisc_reset(q
->qdisc
);
487 qdisc_watchdog_cancel(&q
->watchdog
);
490 static void dist_free(struct disttable
*d
)
493 if (is_vmalloc_addr(d
))
501 * Distribution data is a variable size payload containing
502 * signed 16 bit values.
504 static int get_dist_table(struct Qdisc
*sch
, const struct nlattr
*attr
)
506 struct netem_sched_data
*q
= qdisc_priv(sch
);
507 size_t n
= nla_len(attr
)/sizeof(__s16
);
508 const __s16
*data
= nla_data(attr
);
509 spinlock_t
*root_lock
;
514 if (n
> NETEM_DIST_MAX
)
517 s
= sizeof(struct disttable
) + n
* sizeof(s16
);
518 d
= kmalloc(s
, GFP_KERNEL
);
525 for (i
= 0; i
< n
; i
++)
526 d
->table
[i
] = data
[i
];
528 root_lock
= qdisc_root_sleeping_lock(sch
);
530 spin_lock_bh(root_lock
);
531 dist_free(q
->delay_dist
);
533 spin_unlock_bh(root_lock
);
537 static void get_correlation(struct Qdisc
*sch
, const struct nlattr
*attr
)
539 struct netem_sched_data
*q
= qdisc_priv(sch
);
540 const struct tc_netem_corr
*c
= nla_data(attr
);
542 init_crandom(&q
->delay_cor
, c
->delay_corr
);
543 init_crandom(&q
->loss_cor
, c
->loss_corr
);
544 init_crandom(&q
->dup_cor
, c
->dup_corr
);
547 static void get_reorder(struct Qdisc
*sch
, const struct nlattr
*attr
)
549 struct netem_sched_data
*q
= qdisc_priv(sch
);
550 const struct tc_netem_reorder
*r
= nla_data(attr
);
552 q
->reorder
= r
->probability
;
553 init_crandom(&q
->reorder_cor
, r
->correlation
);
556 static void get_corrupt(struct Qdisc
*sch
, const struct nlattr
*attr
)
558 struct netem_sched_data
*q
= qdisc_priv(sch
);
559 const struct tc_netem_corrupt
*r
= nla_data(attr
);
561 q
->corrupt
= r
->probability
;
562 init_crandom(&q
->corrupt_cor
, r
->correlation
);
565 static void get_rate(struct Qdisc
*sch
, const struct nlattr
*attr
)
567 struct netem_sched_data
*q
= qdisc_priv(sch
);
568 const struct tc_netem_rate
*r
= nla_data(attr
);
573 static int get_loss_clg(struct Qdisc
*sch
, const struct nlattr
*attr
)
575 struct netem_sched_data
*q
= qdisc_priv(sch
);
576 const struct nlattr
*la
;
579 nla_for_each_nested(la
, attr
, rem
) {
580 u16 type
= nla_type(la
);
583 case NETEM_LOSS_GI
: {
584 const struct tc_netem_gimodel
*gi
= nla_data(la
);
586 if (nla_len(la
) != sizeof(struct tc_netem_gimodel
)) {
587 pr_info("netem: incorrect gi model size\n");
591 q
->loss_model
= CLG_4_STATES
;
602 case NETEM_LOSS_GE
: {
603 const struct tc_netem_gemodel
*ge
= nla_data(la
);
605 if (nla_len(la
) != sizeof(struct tc_netem_gemodel
)) {
606 pr_info("netem: incorrect gi model size\n");
610 q
->loss_model
= CLG_GILB_ELL
;
620 pr_info("netem: unknown loss type %u\n", type
);
628 static const struct nla_policy netem_policy
[TCA_NETEM_MAX
+ 1] = {
629 [TCA_NETEM_CORR
] = { .len
= sizeof(struct tc_netem_corr
) },
630 [TCA_NETEM_REORDER
] = { .len
= sizeof(struct tc_netem_reorder
) },
631 [TCA_NETEM_CORRUPT
] = { .len
= sizeof(struct tc_netem_corrupt
) },
632 [TCA_NETEM_RATE
] = { .len
= sizeof(struct tc_netem_rate
) },
633 [TCA_NETEM_LOSS
] = { .type
= NLA_NESTED
},
636 static int parse_attr(struct nlattr
*tb
[], int maxtype
, struct nlattr
*nla
,
637 const struct nla_policy
*policy
, int len
)
639 int nested_len
= nla_len(nla
) - NLA_ALIGN(len
);
641 if (nested_len
< 0) {
642 pr_info("netem: invalid attributes len %d\n", nested_len
);
646 if (nested_len
>= nla_attr_size(0))
647 return nla_parse(tb
, maxtype
, nla_data(nla
) + NLA_ALIGN(len
),
650 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
654 /* Parse netlink message to set options */
655 static int netem_change(struct Qdisc
*sch
, struct nlattr
*opt
)
657 struct netem_sched_data
*q
= qdisc_priv(sch
);
658 struct nlattr
*tb
[TCA_NETEM_MAX
+ 1];
659 struct tc_netem_qopt
*qopt
;
665 qopt
= nla_data(opt
);
666 ret
= parse_attr(tb
, TCA_NETEM_MAX
, opt
, netem_policy
, sizeof(*qopt
));
670 ret
= fifo_set_limit(q
->qdisc
, qopt
->limit
);
672 pr_info("netem: can't set fifo limit\n");
676 q
->latency
= qopt
->latency
;
677 q
->jitter
= qopt
->jitter
;
678 q
->limit
= qopt
->limit
;
681 q
->loss
= qopt
->loss
;
682 q
->duplicate
= qopt
->duplicate
;
684 /* for compatibility with earlier versions.
685 * if gap is set, need to assume 100% probability
690 if (tb
[TCA_NETEM_CORR
])
691 get_correlation(sch
, tb
[TCA_NETEM_CORR
]);
693 if (tb
[TCA_NETEM_DELAY_DIST
]) {
694 ret
= get_dist_table(sch
, tb
[TCA_NETEM_DELAY_DIST
]);
699 if (tb
[TCA_NETEM_REORDER
])
700 get_reorder(sch
, tb
[TCA_NETEM_REORDER
]);
702 if (tb
[TCA_NETEM_CORRUPT
])
703 get_corrupt(sch
, tb
[TCA_NETEM_CORRUPT
]);
705 if (tb
[TCA_NETEM_RATE
])
706 get_rate(sch
, tb
[TCA_NETEM_RATE
]);
708 q
->loss_model
= CLG_RANDOM
;
709 if (tb
[TCA_NETEM_LOSS
])
710 ret
= get_loss_clg(sch
, tb
[TCA_NETEM_LOSS
]);
716 * Special case version of FIFO queue for use by netem.
717 * It queues in order based on timestamps in skb's
719 struct fifo_sched_data
{
721 psched_time_t oldest
;
724 static int tfifo_enqueue(struct sk_buff
*nskb
, struct Qdisc
*sch
)
726 struct fifo_sched_data
*q
= qdisc_priv(sch
);
727 struct sk_buff_head
*list
= &sch
->q
;
728 psched_time_t tnext
= netem_skb_cb(nskb
)->time_to_send
;
731 if (likely(skb_queue_len(list
) < q
->limit
)) {
732 /* Optimize for add at tail */
733 if (likely(skb_queue_empty(list
) || tnext
>= q
->oldest
)) {
735 return qdisc_enqueue_tail(nskb
, sch
);
738 skb_queue_reverse_walk(list
, skb
) {
739 const struct netem_skb_cb
*cb
= netem_skb_cb(skb
);
741 if (tnext
>= cb
->time_to_send
)
745 __skb_queue_after(list
, skb
, nskb
);
747 sch
->qstats
.backlog
+= qdisc_pkt_len(nskb
);
749 return NET_XMIT_SUCCESS
;
752 return qdisc_reshape_fail(nskb
, sch
);
755 static int tfifo_init(struct Qdisc
*sch
, struct nlattr
*opt
)
757 struct fifo_sched_data
*q
= qdisc_priv(sch
);
760 struct tc_fifo_qopt
*ctl
= nla_data(opt
);
761 if (nla_len(opt
) < sizeof(*ctl
))
764 q
->limit
= ctl
->limit
;
766 q
->limit
= max_t(u32
, qdisc_dev(sch
)->tx_queue_len
, 1);
768 q
->oldest
= PSCHED_PASTPERFECT
;
772 static int tfifo_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
774 struct fifo_sched_data
*q
= qdisc_priv(sch
);
775 struct tc_fifo_qopt opt
= { .limit
= q
->limit
};
777 NLA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
784 static struct Qdisc_ops tfifo_qdisc_ops __read_mostly
= {
786 .priv_size
= sizeof(struct fifo_sched_data
),
787 .enqueue
= tfifo_enqueue
,
788 .dequeue
= qdisc_dequeue_head
,
789 .peek
= qdisc_peek_head
,
790 .drop
= qdisc_queue_drop
,
792 .reset
= qdisc_reset_queue
,
793 .change
= tfifo_init
,
797 static int netem_init(struct Qdisc
*sch
, struct nlattr
*opt
)
799 struct netem_sched_data
*q
= qdisc_priv(sch
);
805 qdisc_watchdog_init(&q
->watchdog
, sch
);
807 q
->loss_model
= CLG_RANDOM
;
808 q
->qdisc
= qdisc_create_dflt(sch
->dev_queue
, &tfifo_qdisc_ops
,
809 TC_H_MAKE(sch
->handle
, 1));
811 pr_notice("netem: qdisc create tfifo qdisc failed\n");
815 ret
= netem_change(sch
, opt
);
817 pr_info("netem: change failed\n");
818 qdisc_destroy(q
->qdisc
);
823 static void netem_destroy(struct Qdisc
*sch
)
825 struct netem_sched_data
*q
= qdisc_priv(sch
);
827 qdisc_watchdog_cancel(&q
->watchdog
);
828 qdisc_destroy(q
->qdisc
);
829 dist_free(q
->delay_dist
);
832 static int dump_loss_model(const struct netem_sched_data
*q
,
837 nest
= nla_nest_start(skb
, TCA_NETEM_LOSS
);
839 goto nla_put_failure
;
841 switch (q
->loss_model
) {
843 /* legacy loss model */
844 nla_nest_cancel(skb
, nest
);
845 return 0; /* no data */
848 struct tc_netem_gimodel gi
= {
856 NLA_PUT(skb
, NETEM_LOSS_GI
, sizeof(gi
), &gi
);
860 struct tc_netem_gemodel ge
= {
867 NLA_PUT(skb
, NETEM_LOSS_GE
, sizeof(ge
), &ge
);
872 nla_nest_end(skb
, nest
);
876 nla_nest_cancel(skb
, nest
);
880 static int netem_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
882 const struct netem_sched_data
*q
= qdisc_priv(sch
);
883 struct nlattr
*nla
= (struct nlattr
*) skb_tail_pointer(skb
);
884 struct tc_netem_qopt qopt
;
885 struct tc_netem_corr cor
;
886 struct tc_netem_reorder reorder
;
887 struct tc_netem_corrupt corrupt
;
888 struct tc_netem_rate rate
;
890 qopt
.latency
= q
->latency
;
891 qopt
.jitter
= q
->jitter
;
892 qopt
.limit
= q
->limit
;
895 qopt
.duplicate
= q
->duplicate
;
896 NLA_PUT(skb
, TCA_OPTIONS
, sizeof(qopt
), &qopt
);
898 cor
.delay_corr
= q
->delay_cor
.rho
;
899 cor
.loss_corr
= q
->loss_cor
.rho
;
900 cor
.dup_corr
= q
->dup_cor
.rho
;
901 NLA_PUT(skb
, TCA_NETEM_CORR
, sizeof(cor
), &cor
);
903 reorder
.probability
= q
->reorder
;
904 reorder
.correlation
= q
->reorder_cor
.rho
;
905 NLA_PUT(skb
, TCA_NETEM_REORDER
, sizeof(reorder
), &reorder
);
907 corrupt
.probability
= q
->corrupt
;
908 corrupt
.correlation
= q
->corrupt_cor
.rho
;
909 NLA_PUT(skb
, TCA_NETEM_CORRUPT
, sizeof(corrupt
), &corrupt
);
912 NLA_PUT(skb
, TCA_NETEM_RATE
, sizeof(rate
), &rate
);
914 if (dump_loss_model(q
, skb
) != 0)
915 goto nla_put_failure
;
917 return nla_nest_end(skb
, nla
);
920 nlmsg_trim(skb
, nla
);
924 static int netem_dump_class(struct Qdisc
*sch
, unsigned long cl
,
925 struct sk_buff
*skb
, struct tcmsg
*tcm
)
927 struct netem_sched_data
*q
= qdisc_priv(sch
);
929 if (cl
!= 1) /* only one class */
932 tcm
->tcm_handle
|= TC_H_MIN(1);
933 tcm
->tcm_info
= q
->qdisc
->handle
;
938 static int netem_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
941 struct netem_sched_data
*q
= qdisc_priv(sch
);
949 qdisc_tree_decrease_qlen(*old
, (*old
)->q
.qlen
);
951 sch_tree_unlock(sch
);
956 static struct Qdisc
*netem_leaf(struct Qdisc
*sch
, unsigned long arg
)
958 struct netem_sched_data
*q
= qdisc_priv(sch
);
962 static unsigned long netem_get(struct Qdisc
*sch
, u32 classid
)
967 static void netem_put(struct Qdisc
*sch
, unsigned long arg
)
971 static void netem_walk(struct Qdisc
*sch
, struct qdisc_walker
*walker
)
974 if (walker
->count
>= walker
->skip
)
975 if (walker
->fn(sch
, 1, walker
) < 0) {
983 static const struct Qdisc_class_ops netem_class_ops
= {
984 .graft
= netem_graft
,
989 .dump
= netem_dump_class
,
992 static struct Qdisc_ops netem_qdisc_ops __read_mostly
= {
994 .cl_ops
= &netem_class_ops
,
995 .priv_size
= sizeof(struct netem_sched_data
),
996 .enqueue
= netem_enqueue
,
997 .dequeue
= netem_dequeue
,
998 .peek
= qdisc_peek_dequeued
,
1001 .reset
= netem_reset
,
1002 .destroy
= netem_destroy
,
1003 .change
= netem_change
,
1005 .owner
= THIS_MODULE
,
1009 static int __init
netem_module_init(void)
1011 pr_info("netem: version " VERSION
"\n");
1012 return register_qdisc(&netem_qdisc_ops
);
1014 static void __exit
netem_module_exit(void)
1016 unregister_qdisc(&netem_qdisc_ops
);
1018 module_init(netem_module_init
)
1019 module_exit(netem_module_exit
)
1020 MODULE_LICENSE("GPL");