netem bug fixes
Subsystems (4)
Stephen Hemminger
maintainerv6 cover netem bug fixes
These bugs were found when doing AI assisted review of sch_netem.c during investigation of the packet duplication recursion problem addressed in Jamal's series.
The fixes cover:
- probability gaps in the 4-state Markov loss model
- queue limit not accounting for reordered packets
- PRNG reseeded on every tc change, breaking reproducibility
- slot delay configuration not validated for inverted ranges
- slot delay arithmetic overflow for ranges above ~2.1 seconds
v6 - drop 3 unnecessary patches; focus on real bugs.
Stephen Hemminger (5):
net/sched: netem: fix probability gaps in 4-state loss model net/sched: netem: fix queue limit check to include reordered packets net/sched: netem: only reseed PRNG when seed is explicitly provided net/sched: netem: check for invalid slot range
net/sched: netem: fix slot delay calculation overflow
net/sched/sch_netem.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-)
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v6 1/5 net/sched: netem: fix probability gaps in 4-state loss model
The 4-state Markov chain in loss_4state() has gaps at the boundaries between transition probability ranges. The comparisons use:
if (rnd < a4) else if (a4 < rnd && rnd < a1 + a4)
When rnd equals a boundary value exactly, neither branch matches and no state transition occurs. The redundant lower-bound check (a4 < rnd) is already implied by being in the else branch.
Remove the unnecessary lower-bound comparisons so the ranges are contiguous and every random value produces a transition, matching the GI (General and Intuitive) loss model specification.
This bug goes back to original implementation of this model.
--- net/sched/sch_netem.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/sched/sch_netem.c +4 -4
@@ -227,10 +227,10 @@ static bool loss_4state(struct netem_sched_data *q)if (rnd < clg->a4) { clg->state =LOST_IN_GAP_PERIOD ;return true; - }else if (clg->a4 < rnd && rnd < clg->a1 + clg->a4) { + }else if (rnd < clg->a1 + clg->a4) { clg->state =LOST_IN_BURST_PERIOD ;return true; - }else if (clg->a1 + clg->a4 < rnd) { + }else { clg->state =TX_IN_GAP_PERIOD ; }
@@ -247,9 +247,9 @@ static bool loss_4state(struct netem_sched_data *q)case LOST_IN_BURST_PERIOD :if (rnd < clg->a3) clg->state =TX_IN_BURST_PERIOD ; -else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) { +else if (rnd < clg->a2 + clg->a3) { clg->state =TX_IN_GAP_PERIOD ; - }else if (clg->a2 + clg->a3 < rnd) { + }else { clg->state =LOST_IN_BURST_PERIOD ;return true; }
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v6 2/5 net/sched: netem: fix queue limit check to include reordered packets
In reply to Stephen HemmingerThe queue limit check in netem_enqueue() uses q->t_len which only counts packets in the internal tfifo. Packets placed in sch->q by the reorder path (__qdisc_enqueue_head) are not counted, allowing the total queue occupancy to exceed sch->limit under reordering.
Include sch->q.qlen in the limit check.
--- net/sched/sch_netem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_netem.c +1 -1
@@ -524,7 +524,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,1 <<get_random_u32_below (8 ); } -if (unlikely (q->t_len >= sch->limit)) { +if (unlikely (sch->q.qlen >= sch->limit)) {/* re-link segs, so that qdisc_drop_all() frees them all */ skb->next = segs;qdisc_drop_all (skb, sch, to_free);
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v6 3/5 net/sched: netem: only reseed PRNG when seed is explicitly provided
In reply to Stephen Hemmingernetem_change() unconditionally reseeds the PRNG on every tc change command. If TCA_NETEM_PRNG_SEED is not specified, a new random seed is generated, destroying reproducibility for users who set a deterministic seed on a previous change.
Move the initial random seed generation to netem_init() and only reseed in netem_change() when TCA_NETEM_PRNG_SEED is explicitly provided by the user.
--- net/sched/sch_netem.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
net/sched/sch_netem.c +6 -4
@@ -1112,11 +1112,10 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,/* capping jitter to the range acceptable by tabledist() */ q->jitter =min_t (s64,abs (q->jitter),INT_MAX ); -if (tb[TCA_NETEM_PRNG_SEED ]) +if (tb[TCA_NETEM_PRNG_SEED ]) { q->prng.seed =nla_get_u64 (tb[TCA_NETEM_PRNG_SEED ]); -else - q->prng.seed =get_random_u64 (); -prandom_seed_state (&q->prng.prng_state, q->prng.seed); +prandom_seed_state (&q->prng.prng_state, q->prng.seed); + } unlock:sch_tree_unlock (sch);
@@ -1139,6 +1138,9 @@ static int netem_init(struct Qdisc *sch, struct nlattr *opt,return -EINVAL ; q->loss_model =CLG_RANDOM ; + q->prng.seed =get_random_u64 (); +prandom_seed_state (&q->prng.prng_state, q->prng.seed); + ret =netem_change (sch, opt, extack);if (ret)pr_info ("netem: change failed\n" );
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v6 4/5 net/sched: netem: check for invalid slot range
In reply to Stephen HemmingerReject slot configuration where min_delay exceeds max_delay. The delay range computation in get_slot_next() underflows in this case, producing bogus results.
--- net/sched/sch_netem.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
net/sched/sch_netem.c +18 -0
@@ -827,6 +827,18 @@ static int get_dist_table(struct disttable **tbl, const struct nlattr *attr)return 0 ; } +static int validate_slot (const struct nlattr *attr, +struct netlink_ext_ack *extack) +{ +const struct tc_netem_slot *c =nla_data (attr); + +if (c->min_delay > c->max_delay) { +NL_SET_ERR_MSG (extack,"slot min delay greater than max delay" ); +return -EINVAL ; + } +return 0 ; +} +static void get_slot (struct netem_sched_data *q,const struct nlattr *attr) {const struct tc_netem_slot *c =nla_data (attr);
@@ -1040,6 +1052,12 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt, goto table_free; } +if (tb[TCA_NETEM_SLOT ]) { + ret =validate_slot (tb[TCA_NETEM_SLOT ], extack); +if (ret) + goto table_free; + } +sch_tree_lock (sch);/* backup q->clg and q->loss_model */ old_clg = q->clg;
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v6 5/5 net/sched: netem: fix slot delay calculation overflow
In reply to Stephen Hemmingerget_slot_next() computes a random delay between min_delay and max_delay using:
get_random_u32() * (max_delay - min_delay) >> 32
This overflows signed 64-bit arithmetic when the delay range exceeds approximately 2.1 seconds (2^31 nanoseconds), producing a negative result that effectively disables slot-based pacing. This is a realistic configuration for WAN emulation (e.g., slot 1s 5s).
Use mul_u64_u32_shr() which handles the widening multiply without overflow.
--- net/sched/sch_netem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
net/sched/sch_netem.c +2 -3
@@ -659,9 +659,8 @@ static void get_slot_next(struct netem_sched_data *q, u64 now)if (!q->slot_dist) next_delay = q->slot_config.min_delay + - (get_random_u32 () * - (q->slot_config.max_delay - - q->slot_config.min_delay) >>32 ); +mul_u64_u32_shr (q->slot_config.max_delay - q->slot_config.min_delay, +get_random_u32 (),32 );else next_delay = tabledist(q->slot_config.dist_delay, (s32)(q->slot_config.dist_jitter),
Signature · 1 line
2.53.0