2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* net/sched/sch_netem.c Network emulator
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
2006-10-23 11:16:57 +08:00
|
|
|
* 2 of the License.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Many of the algorithms and ideas for this came from
|
2007-02-09 22:25:16 +08:00
|
|
|
* NIST Net which is not copyrighted.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Authors: Stephen Hemminger <shemminger@osdl.org>
|
|
|
|
* Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
|
|
|
|
*/
|
|
|
|
|
2011-06-16 19:01:34 +08:00
|
|
|
#include <linux/mm.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/skbuff.h>
|
2011-02-25 14:48:13 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/rtnetlink.h>
|
2011-12-12 22:30:00 +08:00
|
|
|
#include <linux/reciprocal_div.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-26 14:06:12 +08:00
|
|
|
#include <net/netlink.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/pkt_sched.h>
|
|
|
|
|
2011-02-23 21:04:22 +08:00
|
|
|
#define VERSION "1.3"
|
2005-11-04 05:49:01 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Network Emulation Queuing algorithm.
|
|
|
|
====================================
|
|
|
|
|
|
|
|
Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
|
|
|
|
Network Emulation Tool
|
|
|
|
[2] Luigi Rizzo, DummyNet for FreeBSD
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
This started out as a simple way to delay outgoing packets to
|
|
|
|
test TCP but has grown to include most of the functionality
|
|
|
|
of a full blown network emulator like NISTnet. It can delay
|
|
|
|
packets and add random jitter (and correlation). The random
|
|
|
|
distribution can be loaded from a table as well to provide
|
|
|
|
normal, Pareto, or experimental curves. Packet loss,
|
|
|
|
duplication, and reordering can also be emulated.
|
|
|
|
|
|
|
|
This qdisc does not do classification that can be handled in
|
|
|
|
layering other disciplines. It does not need to do bandwidth
|
|
|
|
control either since that can be handled by using token
|
|
|
|
bucket or other rate control.
|
2011-02-23 21:04:21 +08:00
|
|
|
|
|
|
|
Correlated Loss Generator models
|
|
|
|
|
|
|
|
Added generation of correlated loss according to the
|
|
|
|
"Gilbert-Elliot" model, a 4-state markov model.
|
|
|
|
|
|
|
|
References:
|
|
|
|
[1] NetemCLG Home http://netgroup.uniroma2.it/NetemCLG
|
|
|
|
[2] S. Salsano, F. Ludovici, A. Ordine, "Definition of a general
|
|
|
|
and intuitive loss model for packet networks and its implementation
|
|
|
|
in the Netem module in the Linux kernel", available in [1]
|
|
|
|
|
|
|
|
Authors: Stefano Salsano <stefano.salsano at uniroma2.it
|
|
|
|
Fabio Ludovici <fabio.ludovici at yahoo.it>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct netem_sched_data {
|
|
|
|
struct Qdisc *qdisc;
|
2007-03-16 16:20:31 +08:00
|
|
|
struct qdisc_watchdog watchdog;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-23 03:16:21 +08:00
|
|
|
psched_tdiff_t latency;
|
|
|
|
psched_tdiff_t jitter;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
u32 loss;
|
|
|
|
u32 limit;
|
|
|
|
u32 counter;
|
|
|
|
u32 gap;
|
|
|
|
u32 duplicate;
|
2005-05-27 03:55:48 +08:00
|
|
|
u32 reorder;
|
2005-12-22 11:03:44 +08:00
|
|
|
u32 corrupt;
|
2011-11-30 20:20:26 +08:00
|
|
|
u32 rate;
|
2011-12-12 22:30:00 +08:00
|
|
|
s32 packet_overhead;
|
|
|
|
u32 cell_size;
|
|
|
|
u32 cell_size_reciprocal;
|
|
|
|
s32 cell_overhead;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct crndstate {
|
2007-03-23 03:16:21 +08:00
|
|
|
u32 last;
|
|
|
|
u32 rho;
|
2005-12-22 11:03:44 +08:00
|
|
|
} delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct disttable {
|
|
|
|
u32 size;
|
|
|
|
s16 table[0];
|
|
|
|
} *delay_dist;
|
2011-02-23 21:04:21 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
CLG_RANDOM,
|
|
|
|
CLG_4_STATES,
|
|
|
|
CLG_GILB_ELL,
|
|
|
|
} loss_model;
|
|
|
|
|
|
|
|
/* Correlated Loss Generation models */
|
|
|
|
struct clgstate {
|
|
|
|
/* state of the Markov chain */
|
|
|
|
u8 state;
|
|
|
|
|
|
|
|
/* 4-states and Gilbert-Elliot models */
|
|
|
|
u32 a1; /* p13 for 4-states or p for GE */
|
|
|
|
u32 a2; /* p31 for 4-states or r for GE */
|
|
|
|
u32 a3; /* p32 for 4-states or h for GE */
|
|
|
|
u32 a4; /* p14 for 4-states or 1-k for GE */
|
|
|
|
u32 a5; /* p23 used only in 4-states */
|
|
|
|
} clg;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Time stamp put into socket buffer control block */
|
|
|
|
struct netem_skb_cb {
|
|
|
|
psched_time_t time_to_send;
|
|
|
|
};
|
|
|
|
|
2008-07-20 15:08:04 +08:00
|
|
|
static inline struct netem_skb_cb *netem_skb_cb(struct sk_buff *skb)
|
|
|
|
{
|
2008-07-20 15:08:47 +08:00
|
|
|
BUILD_BUG_ON(sizeof(skb->cb) <
|
|
|
|
sizeof(struct qdisc_skb_cb) + sizeof(struct netem_skb_cb));
|
|
|
|
return (struct netem_skb_cb *)qdisc_skb_cb(skb)->data;
|
2008-07-20 15:08:04 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* init_crandom - initialize correlated random number generator
|
|
|
|
* Use entropy source for initial seed.
|
|
|
|
*/
|
|
|
|
static void init_crandom(struct crndstate *state, unsigned long rho)
|
|
|
|
{
|
|
|
|
state->rho = rho;
|
|
|
|
state->last = net_random();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get_crandom - correlated random number generator
|
|
|
|
* Next number depends on last value.
|
|
|
|
* rho is scaled to avoid floating point.
|
|
|
|
*/
|
2007-03-23 03:16:21 +08:00
|
|
|
static u32 get_crandom(struct crndstate *state)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
u64 value, rho;
|
|
|
|
unsigned long answer;
|
|
|
|
|
2007-03-23 15:12:09 +08:00
|
|
|
if (state->rho == 0) /* no correlation */
|
2005-04-17 06:20:36 +08:00
|
|
|
return net_random();
|
|
|
|
|
|
|
|
value = net_random();
|
|
|
|
rho = (u64)state->rho + 1;
|
|
|
|
answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
|
|
|
|
state->last = answer;
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
/* loss_4state - 4-state model loss generator
|
|
|
|
* Generates losses according to the 4-state Markov chain adopted in
|
|
|
|
* the GI (General and Intuitive) loss model.
|
|
|
|
*/
|
|
|
|
static bool loss_4state(struct netem_sched_data *q)
|
|
|
|
{
|
|
|
|
struct clgstate *clg = &q->clg;
|
|
|
|
u32 rnd = net_random();
|
|
|
|
|
|
|
|
/*
|
2011-03-31 09:57:33 +08:00
|
|
|
* Makes a comparison between rnd and the transition
|
2011-02-23 21:04:21 +08:00
|
|
|
* probabilities outgoing from the current state, then decides the
|
|
|
|
* next state and if the next packet has to be transmitted or lost.
|
|
|
|
* The four states correspond to:
|
|
|
|
* 1 => successfully transmitted packets within a gap period
|
|
|
|
* 4 => isolated losses within a gap period
|
|
|
|
* 3 => lost packets within a burst period
|
|
|
|
* 2 => successfully transmitted packets within a burst period
|
|
|
|
*/
|
|
|
|
switch (clg->state) {
|
|
|
|
case 1:
|
|
|
|
if (rnd < clg->a4) {
|
|
|
|
clg->state = 4;
|
|
|
|
return true;
|
|
|
|
} else if (clg->a4 < rnd && rnd < clg->a1) {
|
|
|
|
clg->state = 3;
|
|
|
|
return true;
|
|
|
|
} else if (clg->a1 < rnd)
|
|
|
|
clg->state = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (rnd < clg->a5) {
|
|
|
|
clg->state = 3;
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
clg->state = 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (rnd < clg->a3)
|
|
|
|
clg->state = 2;
|
|
|
|
else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) {
|
|
|
|
clg->state = 1;
|
|
|
|
return true;
|
|
|
|
} else if (clg->a2 + clg->a3 < rnd) {
|
|
|
|
clg->state = 3;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
clg->state = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loss_gilb_ell - Gilbert-Elliot model loss generator
|
|
|
|
* Generates losses according to the Gilbert-Elliot loss model or
|
|
|
|
* its special cases (Gilbert or Simple Gilbert)
|
|
|
|
*
|
2011-03-31 09:57:33 +08:00
|
|
|
* Makes a comparison between random number and the transition
|
2011-02-23 21:04:21 +08:00
|
|
|
* probabilities outgoing from the current state, then decides the
|
2011-03-31 09:57:33 +08:00
|
|
|
* next state. A second random number is extracted and the comparison
|
2011-02-23 21:04:21 +08:00
|
|
|
* with the loss probability of the current state decides if the next
|
|
|
|
* packet will be transmitted or lost.
|
|
|
|
*/
|
|
|
|
static bool loss_gilb_ell(struct netem_sched_data *q)
|
|
|
|
{
|
|
|
|
struct clgstate *clg = &q->clg;
|
|
|
|
|
|
|
|
switch (clg->state) {
|
|
|
|
case 1:
|
|
|
|
if (net_random() < clg->a1)
|
|
|
|
clg->state = 2;
|
|
|
|
if (net_random() < clg->a4)
|
|
|
|
return true;
|
|
|
|
case 2:
|
|
|
|
if (net_random() < clg->a2)
|
|
|
|
clg->state = 1;
|
|
|
|
if (clg->a3 > net_random())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool loss_event(struct netem_sched_data *q)
|
|
|
|
{
|
|
|
|
switch (q->loss_model) {
|
|
|
|
case CLG_RANDOM:
|
|
|
|
/* Random packet drop 0 => none, ~0 => all */
|
|
|
|
return q->loss && q->loss >= get_crandom(&q->loss_cor);
|
|
|
|
|
|
|
|
case CLG_4_STATES:
|
|
|
|
/* 4state loss model algorithm (used also for GI model)
|
|
|
|
* Extracts a value from the markov 4 state loss generator,
|
|
|
|
* if it is 1 drops a packet and if needed writes the event in
|
|
|
|
* the kernel logs
|
|
|
|
*/
|
|
|
|
return loss_4state(q);
|
|
|
|
|
|
|
|
case CLG_GILB_ELL:
|
|
|
|
/* Gilbert-Elliot loss model algorithm
|
|
|
|
* Extracts a value from the Gilbert-Elliot loss generator,
|
|
|
|
* if it is 1 drops a packet and if needed writes the event in
|
|
|
|
* the kernel logs
|
|
|
|
*/
|
|
|
|
return loss_gilb_ell(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; /* not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* tabledist - return a pseudo-randomly distributed value with mean mu and
|
|
|
|
* std deviation sigma. Uses table lookup to approximate the desired
|
|
|
|
* distribution, and a uniformly-distributed pseudo-random source.
|
|
|
|
*/
|
2007-03-23 03:16:21 +08:00
|
|
|
static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
|
|
|
|
struct crndstate *state,
|
|
|
|
const struct disttable *dist)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-03-23 03:16:21 +08:00
|
|
|
psched_tdiff_t x;
|
|
|
|
long t;
|
|
|
|
u32 rnd;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (sigma == 0)
|
|
|
|
return mu;
|
|
|
|
|
|
|
|
rnd = get_crandom(state);
|
|
|
|
|
|
|
|
/* default uniform distribution */
|
2007-02-09 22:25:16 +08:00
|
|
|
if (dist == NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
return (rnd % (2*sigma)) - sigma + mu;
|
|
|
|
|
|
|
|
t = dist->table[rnd % dist->size];
|
|
|
|
x = (sigma % NETEM_DIST_SCALE) * t;
|
|
|
|
if (x >= 0)
|
|
|
|
x += NETEM_DIST_SCALE/2;
|
|
|
|
else
|
|
|
|
x -= NETEM_DIST_SCALE/2;
|
|
|
|
|
|
|
|
return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
|
|
|
|
}
|
|
|
|
|
2011-12-12 22:30:00 +08:00
|
|
|
static psched_time_t packet_len_2_sched_time(unsigned int len, struct netem_sched_data *q)
|
2011-11-30 20:20:26 +08:00
|
|
|
{
|
2011-12-12 22:30:00 +08:00
|
|
|
u64 ticks;
|
2011-12-01 07:32:14 +08:00
|
|
|
|
2011-12-12 22:30:00 +08:00
|
|
|
len += q->packet_overhead;
|
|
|
|
|
|
|
|
if (q->cell_size) {
|
|
|
|
u32 cells = reciprocal_divide(len, q->cell_size_reciprocal);
|
|
|
|
|
|
|
|
if (len > cells * q->cell_size) /* extra cell needed for remainder */
|
|
|
|
cells++;
|
|
|
|
len = cells * (q->cell_size + q->cell_overhead);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticks = (u64)len * NSEC_PER_SEC;
|
|
|
|
|
|
|
|
do_div(ticks, q->rate);
|
2011-12-01 07:32:14 +08:00
|
|
|
return PSCHED_NS2TICKS(ticks);
|
2011-11-30 20:20:26 +08:00
|
|
|
}
|
|
|
|
|
2005-05-27 03:53:49 +08:00
|
|
|
/*
|
|
|
|
* Insert one skb into qdisc.
|
|
|
|
* Note: parent depends on return value to account for queue length.
|
|
|
|
* NET_XMIT_DROP: queue length didn't change.
|
|
|
|
* NET_XMIT_SUCCESS: one skb was queued.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2006-07-22 05:45:25 +08:00
|
|
|
/* We don't fill cb now as skb_unshare() may invalidate it */
|
|
|
|
struct netem_skb_cb *cb;
|
2005-05-27 03:53:49 +08:00
|
|
|
struct sk_buff *skb2;
|
2005-04-17 06:20:36 +08:00
|
|
|
int ret;
|
2005-05-27 03:53:49 +08:00
|
|
|
int count = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-27 03:53:49 +08:00
|
|
|
/* Random duplication */
|
|
|
|
if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
|
|
|
|
++count;
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
/* Drop packet? */
|
|
|
|
if (loss_event(q))
|
2005-05-27 03:53:49 +08:00
|
|
|
--count;
|
|
|
|
|
|
|
|
if (count == 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
sch->qstats.drops++;
|
|
|
|
kfree_skb(skb);
|
2008-08-05 13:39:11 +08:00
|
|
|
return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-10-23 12:00:33 +08:00
|
|
|
skb_orphan(skb);
|
|
|
|
|
2005-05-27 03:53:49 +08:00
|
|
|
/*
|
|
|
|
* If we need to duplicate packet, then re-insert at top of the
|
|
|
|
* qdisc tree, since parent queuer expects that only one
|
|
|
|
* skb will be queued.
|
|
|
|
*/
|
|
|
|
if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
|
2008-07-16 16:42:40 +08:00
|
|
|
struct Qdisc *rootq = qdisc_root(sch);
|
2005-05-27 03:53:49 +08:00
|
|
|
u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
|
|
|
|
q->duplicate = 0;
|
|
|
|
|
2008-07-20 15:08:04 +08:00
|
|
|
qdisc_enqueue_root(skb2, rootq);
|
2005-05-27 03:53:49 +08:00
|
|
|
q->duplicate = dupsave;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-12-22 11:03:44 +08:00
|
|
|
/*
|
|
|
|
* Randomized packet corruption.
|
|
|
|
* Make copy if needed since we are modifying
|
|
|
|
* If packet is going to be hardware checksummed, then
|
|
|
|
* do it now in software before we mangle it.
|
|
|
|
*/
|
|
|
|
if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
|
2009-11-30 08:55:45 +08:00
|
|
|
if (!(skb = skb_unshare(skb, GFP_ATOMIC)) ||
|
|
|
|
(skb->ip_summed == CHECKSUM_PARTIAL &&
|
|
|
|
skb_checksum_help(skb))) {
|
2005-12-22 11:03:44 +08:00
|
|
|
sch->qstats.drops++;
|
|
|
|
return NET_XMIT_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
|
|
|
|
}
|
|
|
|
|
2008-07-20 15:08:04 +08:00
|
|
|
cb = netem_skb_cb(skb);
|
2011-01-20 03:26:56 +08:00
|
|
|
if (q->gap == 0 || /* not doing reordering */
|
|
|
|
q->counter < q->gap || /* inside last reordering gap */
|
2009-11-30 08:55:45 +08:00
|
|
|
q->reorder < get_crandom(&q->reorder_cor)) {
|
2005-05-27 03:55:01 +08:00
|
|
|
psched_time_t now;
|
2005-11-04 05:43:07 +08:00
|
|
|
psched_tdiff_t delay;
|
|
|
|
|
|
|
|
delay = tabledist(q->latency, q->jitter,
|
|
|
|
&q->delay_cor, q->delay_dist);
|
|
|
|
|
2007-03-24 02:29:25 +08:00
|
|
|
now = psched_get_time();
|
2011-11-30 20:20:26 +08:00
|
|
|
|
|
|
|
if (q->rate) {
|
|
|
|
struct sk_buff_head *list = &q->qdisc->q;
|
|
|
|
|
2011-12-12 22:30:00 +08:00
|
|
|
delay += packet_len_2_sched_time(skb->len, q);
|
2011-11-30 20:20:26 +08:00
|
|
|
|
|
|
|
if (!skb_queue_empty(list)) {
|
|
|
|
/*
|
|
|
|
* Last packet in queue is reference point (now).
|
|
|
|
* First packet in queue is already in flight,
|
|
|
|
* calculate this time bonus and substract
|
|
|
|
* from delay.
|
|
|
|
*/
|
|
|
|
delay -= now - netem_skb_cb(skb_peek(list))->time_to_send;
|
|
|
|
now = netem_skb_cb(skb_peek_tail(list))->time_to_send;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-24 02:27:45 +08:00
|
|
|
cb->time_to_send = now + delay;
|
2005-04-17 06:20:36 +08:00
|
|
|
++q->counter;
|
2008-07-20 15:08:04 +08:00
|
|
|
ret = qdisc_enqueue(skb, q->qdisc);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2007-02-09 22:25:16 +08:00
|
|
|
/*
|
2005-05-27 03:55:48 +08:00
|
|
|
* Do re-ordering by putting one out of N packets at the front
|
|
|
|
* of the queue.
|
|
|
|
*/
|
2007-03-24 02:29:25 +08:00
|
|
|
cb->time_to_send = psched_get_time();
|
2005-05-27 03:55:48 +08:00
|
|
|
q->counter = 0;
|
2008-11-02 15:36:03 +08:00
|
|
|
|
|
|
|
__skb_queue_head(&q->qdisc->q, skb);
|
|
|
|
q->qdisc->qstats.backlog += qdisc_pkt_len(skb);
|
|
|
|
q->qdisc->qstats.requeues++;
|
|
|
|
ret = NET_XMIT_SUCCESS;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:20 +08:00
|
|
|
if (ret != NET_XMIT_SUCCESS) {
|
|
|
|
if (net_xmit_drop_count(ret)) {
|
|
|
|
sch->qstats.drops++;
|
|
|
|
return ret;
|
|
|
|
}
|
2008-08-05 13:31:03 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 21:04:20 +08:00
|
|
|
sch->q.qlen++;
|
|
|
|
return NET_XMIT_SUCCESS;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
static unsigned int netem_drop(struct Qdisc *sch)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2006-03-21 11:00:49 +08:00
|
|
|
unsigned int len = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-21 11:00:49 +08:00
|
|
|
if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
sch->q.qlen--;
|
|
|
|
sch->qstats.drops++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *netem_dequeue(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2011-01-20 13:27:16 +08:00
|
|
|
if (qdisc_is_throttled(sch))
|
2007-03-23 03:17:42 +08:00
|
|
|
return NULL;
|
|
|
|
|
2008-10-31 15:46:19 +08:00
|
|
|
skb = q->qdisc->ops->peek(q->qdisc);
|
2005-05-04 07:24:32 +08:00
|
|
|
if (skb) {
|
2008-07-20 15:08:04 +08:00
|
|
|
const struct netem_skb_cb *cb = netem_skb_cb(skb);
|
2007-03-24 02:29:25 +08:00
|
|
|
psched_time_t now = psched_get_time();
|
2005-05-27 03:55:01 +08:00
|
|
|
|
|
|
|
/* if more time remaining? */
|
2007-03-24 02:28:07 +08:00
|
|
|
if (cb->time_to_send <= now) {
|
2008-10-31 15:47:01 +08:00
|
|
|
skb = qdisc_dequeue_peeked(q->qdisc);
|
|
|
|
if (unlikely(!skb))
|
2008-10-31 15:46:19 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-04-17 18:08:49 +08:00
|
|
|
#ifdef CONFIG_NET_CLS_ACT
|
|
|
|
/*
|
|
|
|
* If it's at ingress let's pretend the delay is
|
|
|
|
* from the network (tstamp will be updated).
|
|
|
|
*/
|
|
|
|
if (G_TC_FROM(skb->tc_verd) & AT_INGRESS)
|
|
|
|
skb->tstamp.tv64 = 0;
|
|
|
|
#endif
|
2011-02-23 21:04:20 +08:00
|
|
|
|
2005-05-27 03:55:01 +08:00
|
|
|
sch->q.qlen--;
|
2011-02-23 21:04:20 +08:00
|
|
|
qdisc_unthrottled(sch);
|
|
|
|
qdisc_bstats_update(sch, skb);
|
2005-05-27 03:55:01 +08:00
|
|
|
return skb;
|
2005-11-04 05:43:07 +08:00
|
|
|
}
|
2007-03-23 03:17:42 +08:00
|
|
|
|
|
|
|
qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
|
2005-05-27 03:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void netem_reset(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
qdisc_reset(q->qdisc);
|
|
|
|
sch->q.qlen = 0;
|
2007-03-16 16:20:31 +08:00
|
|
|
qdisc_watchdog_cancel(&q->watchdog);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:18 +08:00
|
|
|
static void dist_free(struct disttable *d)
|
|
|
|
{
|
|
|
|
if (d) {
|
|
|
|
if (is_vmalloc_addr(d))
|
|
|
|
vfree(d);
|
|
|
|
else
|
|
|
|
kfree(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Distribution data is a variable size payload containing
|
|
|
|
* signed 16 bit values.
|
|
|
|
*/
|
2008-01-23 14:11:17 +08:00
|
|
|
static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2011-02-23 21:04:18 +08:00
|
|
|
size_t n = nla_len(attr)/sizeof(__s16);
|
2008-01-23 14:11:17 +08:00
|
|
|
const __s16 *data = nla_data(attr);
|
2008-07-16 16:42:40 +08:00
|
|
|
spinlock_t *root_lock;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct disttable *d;
|
|
|
|
int i;
|
2011-02-23 21:04:18 +08:00
|
|
|
size_t s;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 21:04:19 +08:00
|
|
|
if (n > NETEM_DIST_MAX)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2011-02-23 21:04:18 +08:00
|
|
|
s = sizeof(struct disttable) + n * sizeof(s16);
|
|
|
|
d = kmalloc(s, GFP_KERNEL);
|
|
|
|
if (!d)
|
|
|
|
d = vmalloc(s);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
d->size = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
d->table[i] = data[i];
|
2007-02-09 22:25:16 +08:00
|
|
|
|
2008-08-30 05:21:52 +08:00
|
|
|
root_lock = qdisc_root_sleeping_lock(sch);
|
2008-07-16 16:42:40 +08:00
|
|
|
|
|
|
|
spin_lock_bh(root_lock);
|
2011-02-23 21:04:18 +08:00
|
|
|
dist_free(q->delay_dist);
|
2008-11-20 20:11:36 +08:00
|
|
|
q->delay_dist = d;
|
2008-07-16 16:42:40 +08:00
|
|
|
spin_unlock_bh(root_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
static void get_correlation(struct Qdisc *sch, const struct nlattr *attr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2008-01-23 14:11:17 +08:00
|
|
|
const struct tc_netem_corr *c = nla_data(attr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
init_crandom(&q->delay_cor, c->delay_corr);
|
|
|
|
init_crandom(&q->loss_cor, c->loss_corr);
|
|
|
|
init_crandom(&q->dup_cor, c->dup_corr);
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
static void get_reorder(struct Qdisc *sch, const struct nlattr *attr)
|
2005-05-27 03:55:48 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2008-01-23 14:11:17 +08:00
|
|
|
const struct tc_netem_reorder *r = nla_data(attr);
|
2005-05-27 03:55:48 +08:00
|
|
|
|
|
|
|
q->reorder = r->probability;
|
|
|
|
init_crandom(&q->reorder_cor, r->correlation);
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
static void get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
|
2005-12-22 11:03:44 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2008-01-23 14:11:17 +08:00
|
|
|
const struct tc_netem_corrupt *r = nla_data(attr);
|
2005-12-22 11:03:44 +08:00
|
|
|
|
|
|
|
q->corrupt = r->probability;
|
|
|
|
init_crandom(&q->corrupt_cor, r->correlation);
|
|
|
|
}
|
|
|
|
|
2011-11-30 20:20:26 +08:00
|
|
|
static void get_rate(struct Qdisc *sch, const struct nlattr *attr)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
const struct tc_netem_rate *r = nla_data(attr);
|
|
|
|
|
|
|
|
q->rate = r->rate;
|
2011-12-12 22:30:00 +08:00
|
|
|
q->packet_overhead = r->packet_overhead;
|
|
|
|
q->cell_size = r->cell_size;
|
|
|
|
if (q->cell_size)
|
|
|
|
q->cell_size_reciprocal = reciprocal_value(q->cell_size);
|
|
|
|
q->cell_overhead = r->cell_overhead;
|
2011-11-30 20:20:26 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
static int get_loss_clg(struct Qdisc *sch, const struct nlattr *attr)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
const struct nlattr *la;
|
|
|
|
int rem;
|
|
|
|
|
|
|
|
nla_for_each_nested(la, attr, rem) {
|
|
|
|
u16 type = nla_type(la);
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case NETEM_LOSS_GI: {
|
|
|
|
const struct tc_netem_gimodel *gi = nla_data(la);
|
|
|
|
|
2011-12-23 17:16:30 +08:00
|
|
|
if (nla_len(la) < sizeof(struct tc_netem_gimodel)) {
|
2011-02-23 21:04:21 +08:00
|
|
|
pr_info("netem: incorrect gi model size\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
q->loss_model = CLG_4_STATES;
|
|
|
|
|
|
|
|
q->clg.state = 1;
|
|
|
|
q->clg.a1 = gi->p13;
|
|
|
|
q->clg.a2 = gi->p31;
|
|
|
|
q->clg.a3 = gi->p32;
|
|
|
|
q->clg.a4 = gi->p14;
|
|
|
|
q->clg.a5 = gi->p23;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NETEM_LOSS_GE: {
|
|
|
|
const struct tc_netem_gemodel *ge = nla_data(la);
|
|
|
|
|
2011-12-23 17:16:30 +08:00
|
|
|
if (nla_len(la) < sizeof(struct tc_netem_gemodel)) {
|
|
|
|
pr_info("netem: incorrect ge model size\n");
|
2011-02-23 21:04:21 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
q->loss_model = CLG_GILB_ELL;
|
|
|
|
q->clg.state = 1;
|
|
|
|
q->clg.a1 = ge->p;
|
|
|
|
q->clg.a2 = ge->r;
|
|
|
|
q->clg.a3 = ge->h;
|
|
|
|
q->clg.a4 = ge->k1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
pr_info("netem: unknown loss type %u\n", type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-24 12:35:39 +08:00
|
|
|
static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
|
|
|
|
[TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
|
|
|
|
[TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
|
|
|
|
[TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
|
2011-11-30 20:20:26 +08:00
|
|
|
[TCA_NETEM_RATE] = { .len = sizeof(struct tc_netem_rate) },
|
2011-02-23 21:04:21 +08:00
|
|
|
[TCA_NETEM_LOSS] = { .type = NLA_NESTED },
|
2008-01-24 12:35:39 +08:00
|
|
|
};
|
|
|
|
|
2008-09-03 08:30:27 +08:00
|
|
|
static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
|
|
|
|
const struct nla_policy *policy, int len)
|
|
|
|
{
|
|
|
|
int nested_len = nla_len(nla) - NLA_ALIGN(len);
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
if (nested_len < 0) {
|
|
|
|
pr_info("netem: invalid attributes len %d\n", nested_len);
|
2008-09-03 08:30:27 +08:00
|
|
|
return -EINVAL;
|
2011-02-23 21:04:21 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 08:30:27 +08:00
|
|
|
if (nested_len >= nla_attr_size(0))
|
|
|
|
return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
|
|
|
|
nested_len, policy);
|
2011-02-23 21:04:21 +08:00
|
|
|
|
2008-09-03 08:30:27 +08:00
|
|
|
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-22 11:03:44 +08:00
|
|
|
/* Parse netlink message to set options */
|
2008-01-23 14:11:17 +08:00
|
|
|
static int netem_change(struct Qdisc *sch, struct nlattr *opt)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
2008-01-24 12:32:21 +08:00
|
|
|
struct nlattr *tb[TCA_NETEM_MAX + 1];
|
2005-04-17 06:20:36 +08:00
|
|
|
struct tc_netem_qopt *qopt;
|
|
|
|
int ret;
|
2007-02-09 22:25:16 +08:00
|
|
|
|
2008-01-24 12:32:21 +08:00
|
|
|
if (opt == NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2008-09-03 08:30:27 +08:00
|
|
|
qopt = nla_data(opt);
|
|
|
|
ret = parse_attr(tb, TCA_NETEM_MAX, opt, netem_policy, sizeof(*qopt));
|
2008-01-24 12:32:21 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2008-07-06 14:40:21 +08:00
|
|
|
ret = fifo_set_limit(q->qdisc, qopt->limit);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ret) {
|
2011-02-23 21:04:22 +08:00
|
|
|
pr_info("netem: can't set fifo limit\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2007-02-09 22:25:16 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
q->latency = qopt->latency;
|
|
|
|
q->jitter = qopt->jitter;
|
|
|
|
q->limit = qopt->limit;
|
|
|
|
q->gap = qopt->gap;
|
2005-05-27 03:55:48 +08:00
|
|
|
q->counter = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
q->loss = qopt->loss;
|
|
|
|
q->duplicate = qopt->duplicate;
|
|
|
|
|
2007-03-23 15:12:09 +08:00
|
|
|
/* for compatibility with earlier versions.
|
|
|
|
* if gap is set, need to assume 100% probability
|
2005-05-27 03:55:48 +08:00
|
|
|
*/
|
2007-03-23 03:15:45 +08:00
|
|
|
if (q->gap)
|
|
|
|
q->reorder = ~0;
|
2005-05-27 03:55:48 +08:00
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
if (tb[TCA_NETEM_CORR])
|
|
|
|
get_correlation(sch, tb[TCA_NETEM_CORR]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-24 12:32:21 +08:00
|
|
|
if (tb[TCA_NETEM_DELAY_DIST]) {
|
|
|
|
ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2005-12-22 11:03:44 +08:00
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
if (tb[TCA_NETEM_REORDER])
|
|
|
|
get_reorder(sch, tb[TCA_NETEM_REORDER]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-04 13:13:26 +08:00
|
|
|
if (tb[TCA_NETEM_CORRUPT])
|
|
|
|
get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-11-30 20:20:26 +08:00
|
|
|
if (tb[TCA_NETEM_RATE])
|
|
|
|
get_rate(sch, tb[TCA_NETEM_RATE]);
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
q->loss_model = CLG_RANDOM;
|
|
|
|
if (tb[TCA_NETEM_LOSS])
|
|
|
|
ret = get_loss_clg(sch, tb[TCA_NETEM_LOSS]);
|
|
|
|
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 05:47:34 +08:00
|
|
|
/*
|
|
|
|
* Special case version of FIFO queue for use by netem.
|
|
|
|
* It queues in order based on timestamps in skb's
|
|
|
|
*/
|
|
|
|
struct fifo_sched_data {
|
|
|
|
u32 limit;
|
2007-03-23 03:17:05 +08:00
|
|
|
psched_time_t oldest;
|
2005-10-31 05:47:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct fifo_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct sk_buff_head *list = &sch->q;
|
2008-07-20 15:08:04 +08:00
|
|
|
psched_time_t tnext = netem_skb_cb(nskb)->time_to_send;
|
2005-10-31 05:47:34 +08:00
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
if (likely(skb_queue_len(list) < q->limit)) {
|
2007-03-23 03:17:05 +08:00
|
|
|
/* Optimize for add at tail */
|
2007-03-24 02:28:07 +08:00
|
|
|
if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
|
2007-03-23 03:17:05 +08:00
|
|
|
q->oldest = tnext;
|
|
|
|
return qdisc_enqueue_tail(nskb, sch);
|
|
|
|
}
|
|
|
|
|
2005-10-31 05:47:34 +08:00
|
|
|
skb_queue_reverse_walk(list, skb) {
|
2008-07-20 15:08:04 +08:00
|
|
|
const struct netem_skb_cb *cb = netem_skb_cb(skb);
|
2005-10-31 05:47:34 +08:00
|
|
|
|
2007-03-24 02:28:07 +08:00
|
|
|
if (tnext >= cb->time_to_send)
|
2005-10-31 05:47:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__skb_queue_after(list, skb, nskb);
|
|
|
|
|
2008-07-20 15:08:27 +08:00
|
|
|
sch->qstats.backlog += qdisc_pkt_len(nskb);
|
2005-10-31 05:47:34 +08:00
|
|
|
|
|
|
|
return NET_XMIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-23 03:17:05 +08:00
|
|
|
return qdisc_reshape_fail(nskb, sch);
|
2005-10-31 05:47:34 +08:00
|
|
|
}
|
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
|
2005-10-31 05:47:34 +08:00
|
|
|
{
|
|
|
|
struct fifo_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
if (opt) {
|
2008-01-23 14:11:17 +08:00
|
|
|
struct tc_fifo_qopt *ctl = nla_data(opt);
|
|
|
|
if (nla_len(opt) < sizeof(*ctl))
|
2005-10-31 05:47:34 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
q->limit = ctl->limit;
|
|
|
|
} else
|
2008-07-09 08:06:30 +08:00
|
|
|
q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
|
2005-10-31 05:47:34 +08:00
|
|
|
|
2007-03-24 02:28:30 +08:00
|
|
|
q->oldest = PSCHED_PASTPERFECT;
|
2005-10-31 05:47:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct fifo_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct tc_fifo_qopt opt = { .limit = q->limit };
|
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
|
2005-10-31 05:47:34 +08:00
|
|
|
return skb->len;
|
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
nla_put_failure:
|
2005-10-31 05:47:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-14 17:44:41 +08:00
|
|
|
static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
|
2005-10-31 05:47:34 +08:00
|
|
|
.id = "tfifo",
|
|
|
|
.priv_size = sizeof(struct fifo_sched_data),
|
|
|
|
.enqueue = tfifo_enqueue,
|
|
|
|
.dequeue = qdisc_dequeue_head,
|
2008-10-31 15:45:55 +08:00
|
|
|
.peek = qdisc_peek_head,
|
2005-10-31 05:47:34 +08:00
|
|
|
.drop = qdisc_queue_drop,
|
|
|
|
.init = tfifo_init,
|
|
|
|
.reset = qdisc_reset_queue,
|
|
|
|
.change = tfifo_init,
|
|
|
|
.dump = tfifo_dump,
|
|
|
|
};
|
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
static int netem_init(struct Qdisc *sch, struct nlattr *opt)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!opt)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-03-16 16:20:31 +08:00
|
|
|
qdisc_watchdog_init(&q->watchdog, sch);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
q->loss_model = CLG_RANDOM;
|
2010-10-16 21:04:08 +08:00
|
|
|
q->qdisc = qdisc_create_dflt(sch->dev_queue, &tfifo_qdisc_ops,
|
2006-11-30 09:35:18 +08:00
|
|
|
TC_H_MAKE(sch->handle, 1));
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!q->qdisc) {
|
2011-02-23 21:04:22 +08:00
|
|
|
pr_notice("netem: qdisc create tfifo qdisc failed\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = netem_change(sch, opt);
|
|
|
|
if (ret) {
|
2011-02-23 21:04:22 +08:00
|
|
|
pr_info("netem: change failed\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
qdisc_destroy(q->qdisc);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netem_destroy(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
2007-03-16 16:20:31 +08:00
|
|
|
qdisc_watchdog_cancel(&q->watchdog);
|
2005-04-17 06:20:36 +08:00
|
|
|
qdisc_destroy(q->qdisc);
|
2011-02-23 21:04:18 +08:00
|
|
|
dist_free(q->delay_dist);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
static int dump_loss_model(const struct netem_sched_data *q,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nlattr *nest;
|
|
|
|
|
|
|
|
nest = nla_nest_start(skb, TCA_NETEM_LOSS);
|
|
|
|
if (nest == NULL)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
switch (q->loss_model) {
|
|
|
|
case CLG_RANDOM:
|
|
|
|
/* legacy loss model */
|
|
|
|
nla_nest_cancel(skb, nest);
|
|
|
|
return 0; /* no data */
|
|
|
|
|
|
|
|
case CLG_4_STATES: {
|
|
|
|
struct tc_netem_gimodel gi = {
|
|
|
|
.p13 = q->clg.a1,
|
|
|
|
.p31 = q->clg.a2,
|
|
|
|
.p32 = q->clg.a3,
|
|
|
|
.p14 = q->clg.a4,
|
|
|
|
.p23 = q->clg.a5,
|
|
|
|
};
|
|
|
|
|
|
|
|
NLA_PUT(skb, NETEM_LOSS_GI, sizeof(gi), &gi);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLG_GILB_ELL: {
|
|
|
|
struct tc_netem_gemodel ge = {
|
|
|
|
.p = q->clg.a1,
|
|
|
|
.r = q->clg.a2,
|
|
|
|
.h = q->clg.a3,
|
|
|
|
.k1 = q->clg.a4,
|
|
|
|
};
|
|
|
|
|
|
|
|
NLA_PUT(skb, NETEM_LOSS_GE, sizeof(ge), &ge);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nla_nest_end(skb, nest);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
nla_nest_cancel(skb, nest);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
const struct netem_sched_data *q = qdisc_priv(sch);
|
2011-02-23 21:04:17 +08:00
|
|
|
struct nlattr *nla = (struct nlattr *) skb_tail_pointer(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct tc_netem_qopt qopt;
|
|
|
|
struct tc_netem_corr cor;
|
2005-05-27 03:55:48 +08:00
|
|
|
struct tc_netem_reorder reorder;
|
2005-12-22 11:03:44 +08:00
|
|
|
struct tc_netem_corrupt corrupt;
|
2011-11-30 20:20:26 +08:00
|
|
|
struct tc_netem_rate rate;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
qopt.latency = q->latency;
|
|
|
|
qopt.jitter = q->jitter;
|
|
|
|
qopt.limit = q->limit;
|
|
|
|
qopt.loss = q->loss;
|
|
|
|
qopt.gap = q->gap;
|
|
|
|
qopt.duplicate = q->duplicate;
|
2008-01-23 14:11:17 +08:00
|
|
|
NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
cor.delay_corr = q->delay_cor.rho;
|
|
|
|
cor.loss_corr = q->loss_cor.rho;
|
|
|
|
cor.dup_corr = q->dup_cor.rho;
|
2008-01-23 14:11:17 +08:00
|
|
|
NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
|
2005-05-27 03:55:48 +08:00
|
|
|
|
|
|
|
reorder.probability = q->reorder;
|
|
|
|
reorder.correlation = q->reorder_cor.rho;
|
2008-01-23 14:11:17 +08:00
|
|
|
NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
|
2005-05-27 03:55:48 +08:00
|
|
|
|
2005-12-22 11:03:44 +08:00
|
|
|
corrupt.probability = q->corrupt;
|
|
|
|
corrupt.correlation = q->corrupt_cor.rho;
|
2008-01-23 14:11:17 +08:00
|
|
|
NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
|
2005-12-22 11:03:44 +08:00
|
|
|
|
2011-11-30 20:20:26 +08:00
|
|
|
rate.rate = q->rate;
|
2011-12-12 22:30:00 +08:00
|
|
|
rate.packet_overhead = q->packet_overhead;
|
|
|
|
rate.cell_size = q->cell_size;
|
|
|
|
rate.cell_overhead = q->cell_overhead;
|
2011-11-30 20:20:26 +08:00
|
|
|
NLA_PUT(skb, TCA_NETEM_RATE, sizeof(rate), &rate);
|
|
|
|
|
2011-02-23 21:04:21 +08:00
|
|
|
if (dump_loss_model(q, skb) != 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2011-02-23 21:04:17 +08:00
|
|
|
return nla_nest_end(skb, nla);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
nla_put_failure:
|
2011-02-23 21:04:17 +08:00
|
|
|
nlmsg_trim(skb, nla);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-23 21:04:20 +08:00
|
|
|
static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
|
|
|
|
struct sk_buff *skb, struct tcmsg *tcm)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
if (cl != 1) /* only one class */
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
tcm->tcm_handle |= TC_H_MIN(1);
|
|
|
|
tcm->tcm_info = q->qdisc->handle;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
|
|
|
|
struct Qdisc **old)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
if (new == NULL)
|
|
|
|
new = &noop_qdisc;
|
|
|
|
|
|
|
|
sch_tree_lock(sch);
|
|
|
|
*old = q->qdisc;
|
|
|
|
q->qdisc = new;
|
|
|
|
qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
|
|
|
|
qdisc_reset(*old);
|
|
|
|
sch_tree_unlock(sch);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct netem_sched_data *q = qdisc_priv(sch);
|
|
|
|
return q->qdisc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long netem_get(struct Qdisc *sch, u32 classid)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netem_put(struct Qdisc *sch, unsigned long arg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
|
|
|
|
{
|
|
|
|
if (!walker->stop) {
|
|
|
|
if (walker->count >= walker->skip)
|
|
|
|
if (walker->fn(sch, 1, walker) < 0) {
|
|
|
|
walker->stop = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
walker->count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct Qdisc_class_ops netem_class_ops = {
|
|
|
|
.graft = netem_graft,
|
|
|
|
.leaf = netem_leaf,
|
|
|
|
.get = netem_get,
|
|
|
|
.put = netem_put,
|
|
|
|
.walk = netem_walk,
|
|
|
|
.dump = netem_dump_class,
|
|
|
|
};
|
|
|
|
|
2007-11-14 17:44:41 +08:00
|
|
|
static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.id = "netem",
|
2011-02-23 21:04:20 +08:00
|
|
|
.cl_ops = &netem_class_ops,
|
2005-04-17 06:20:36 +08:00
|
|
|
.priv_size = sizeof(struct netem_sched_data),
|
|
|
|
.enqueue = netem_enqueue,
|
|
|
|
.dequeue = netem_dequeue,
|
2008-10-31 15:47:01 +08:00
|
|
|
.peek = qdisc_peek_dequeued,
|
2005-04-17 06:20:36 +08:00
|
|
|
.drop = netem_drop,
|
|
|
|
.init = netem_init,
|
|
|
|
.reset = netem_reset,
|
|
|
|
.destroy = netem_destroy,
|
|
|
|
.change = netem_change,
|
|
|
|
.dump = netem_dump,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int __init netem_module_init(void)
|
|
|
|
{
|
2005-11-04 05:49:01 +08:00
|
|
|
pr_info("netem: version " VERSION "\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return register_qdisc(&netem_qdisc_ops);
|
|
|
|
}
|
|
|
|
static void __exit netem_module_exit(void)
|
|
|
|
{
|
|
|
|
unregister_qdisc(&netem_qdisc_ops);
|
|
|
|
}
|
|
|
|
module_init(netem_module_init)
|
|
|
|
module_exit(netem_module_exit)
|
|
|
|
MODULE_LICENSE("GPL");
|