Skip to content

Commit 7bc0f28

Browse files
hgnDavid S. Miller
authored andcommitted
netem: rate extension
Currently netem is not in the ability to emulate channel bandwidth. Only static delay (and optional random jitter) can be configured. To emulate the channel rate the token bucket filter (sch_tbf) can be used. But TBF has some major emulation flaws. The buffer (token bucket depth/rate) cannot be 0. Also the idea behind TBF is that the credit (token in buckets) fills if no packet is transmitted. So that there is always a "positive" credit for new packets. In real life this behavior contradicts the law of nature where nothing can travel faster as speed of light. E.g.: on an emulated 1000 byte/s link a small IPv4/TCP SYN packet with ~50 byte require ~0.05 seconds - not 0 seconds. Netem is an excellent place to implement a rate limiting feature: static delay is already implemented, tfifo already has time information and the user can skip TBF configuration completely. This patch implement rate feature which can be configured via tc. e.g: tc qdisc add dev eth0 root netem rate 10kbit To emulate a link of 5000byte/s and add an additional static delay of 10ms: tc qdisc add dev eth0 root netem delay 10ms rate 5KBps Note: similar to TBF the rate extension is bounded to the kernel timing system. Depending on the architecture timer granularity, higher rates (e.g. 10mbit/s and higher) tend to transmission bursts. Also note: further queues living in network adaptors; see ethtool(8). Signed-off-by: Hagen Paul Pfeifer <hagen@jauu.net> Acked-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@drr.davemloft.net>
1 parent 99d2f47 commit 7bc0f28

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎include/linux/pkt_sched.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ enum {
465465
TCA_NETEM_REORDER,
466466
TCA_NETEM_CORRUPT,
467467
TCA_NETEM_LOSS,
468+
TCA_NETEM_RATE,
468469
__TCA_NETEM_MAX,
469470
};
470471

@@ -495,6 +496,10 @@ struct tc_netem_corrupt {
495496
__u32 correlation;
496497
};
497498

499+
struct tc_netem_rate {
500+
__u32 rate; /* byte/s */
501+
};
502+
498503
enum {
499504
NETEM_LOSS_UNSPEC,
500505
NETEM_LOSS_GI, /* General Intuitive - 4 state model */

‎net/sched/sch_netem.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct netem_sched_data {
7979
u32 duplicate;
8080
u32 reorder;
8181
u32 corrupt;
82+
u32 rate;
8283

8384
struct crndstate {
8485
u32 last;
@@ -298,6 +299,11 @@ static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
298299
return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
299300
}
300301

302+
static psched_time_t packet_len_2_sched_time(unsigned int len, u32 rate)
303+
{
304+
return PSCHED_NS2TICKS((u64)len * NSEC_PER_SEC / rate);
305+
}
306+
301307
/*
302308
* Insert one skb into qdisc.
303309
* Note: parent depends on return value to account for queue length.
@@ -371,6 +377,24 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
371377
&q->delay_cor, q->delay_dist);
372378

373379
now = psched_get_time();
380+
381+
if (q->rate) {
382+
struct sk_buff_head *list = &q->qdisc->q;
383+
384+
delay += packet_len_2_sched_time(skb->len, q->rate);
385+
386+
if (!skb_queue_empty(list)) {
387+
/*
388+
* Last packet in queue is reference point (now).
389+
* First packet in queue is already in flight,
390+
* calculate this time bonus and substract
391+
* from delay.
392+
*/
393+
delay -= now - netem_skb_cb(skb_peek(list))->time_to_send;
394+
now = netem_skb_cb(skb_peek_tail(list))->time_to_send;
395+
}
396+
}
397+
374398
cb->time_to_send = now + delay;
375399
++q->counter;
376400
ret = qdisc_enqueue(skb, q->qdisc);
@@ -535,6 +559,14 @@ static void get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
535559
init_crandom(&q->corrupt_cor, r->correlation);
536560
}
537561

562+
static void get_rate(struct Qdisc *sch, const struct nlattr *attr)
563+
{
564+
struct netem_sched_data *q = qdisc_priv(sch);
565+
const struct tc_netem_rate *r = nla_data(attr);
566+
567+
q->rate = r->rate;
568+
}
569+
538570
static int get_loss_clg(struct Qdisc *sch, const struct nlattr *attr)
539571
{
540572
struct netem_sched_data *q = qdisc_priv(sch);
@@ -594,6 +626,7 @@ static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
594626
[TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
595627
[TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
596628
[TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
629+
[TCA_NETEM_RATE] = { .len = sizeof(struct tc_netem_rate) },
597630
[TCA_NETEM_LOSS] = { .type = NLA_NESTED },
598631
};
599632

@@ -666,6 +699,9 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt)
666699
if (tb[TCA_NETEM_CORRUPT])
667700
get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
668701

702+
if (tb[TCA_NETEM_RATE])
703+
get_rate(sch, tb[TCA_NETEM_RATE]);
704+
669705
q->loss_model = CLG_RANDOM;
670706
if (tb[TCA_NETEM_LOSS])
671707
ret = get_loss_clg(sch, tb[TCA_NETEM_LOSS]);
@@ -846,6 +882,7 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
846882
struct tc_netem_corr cor;
847883
struct tc_netem_reorder reorder;
848884
struct tc_netem_corrupt corrupt;
885+
struct tc_netem_rate rate;
849886

850887
qopt.latency = q->latency;
851888
qopt.jitter = q->jitter;
@@ -868,6 +905,9 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
868905
corrupt.correlation = q->corrupt_cor.rho;
869906
NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
870907

908+
rate.rate = q->rate;
909+
NLA_PUT(skb, TCA_NETEM_RATE, sizeof(rate), &rate);
910+
871911
if (dump_loss_model(q, skb) != 0)
872912
goto nla_put_failure;
873913

0 commit comments

Comments
 (0)