net/sched: netem: enhancements
Subsystems (4)
Stephen Hemminger
maintainerv5 cover net/sched: netem: enhancements
This is a collection of improvements to netem found while investigating the fixes now in net tree.
v5 - minor cleanups
- add allocation_errors counter
- align counters with where impairment occurs
Stephen Hemminger (5):
net/sched: netem: reorder struct netem_sched_data net/sched: netem: remove useless VERSION
net/sched: netem: replace pr_info with netlink extack error messages net/sched: netem: handle multi-segment skb in corruption net/sched: netem: add per-impairment extended statistics
include/uapi/linux/pkt_sched.h | 10 ++ net/sched/sch_netem.c | 223 ++++++++++++++++++++------------- 2 files changed, 144 insertions(+), 89 deletions(-)
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v5 1/5 net/sched: netem: reorder struct netem_sched_data
The current layout of struct netem_sched_data can be improved by optimizing cache locality, compacting data types (use u8 for enum) and eliminating unused elements.
Reorganize the struct as follows:
- Cacheline 0 holds the tfifo state (t_root/t_head/t_tail/t_len), counter, and the unconditional enqueue scalars latency/jitter/rate/gap/loss.
- Cacheline 1 holds the remaining zero-check scalars (duplicate/reorder/corrupt/ecn), all five crndstate correlation structures, and loss_model.
- Cacheline 2 holds prng, delay_dist, the slot dequeue state, slot_dist, and the inner classful qdisc pointer.
- Rate-shaping fields, q->limit (config-only; the fast path reads
sch->limit), and the CLG Markov state move to the warm tail.
- tc_netem_slot slot_config and qdisc_watchdog (only consulted on slot reschedule and watchdog wake) move to the cold tail.
Also reorder struct clgstate to place the u8 state member after the u32 transition probabilities. This removes the 3-byte interior hole without changing the struct's size.
Should have no functional change.
--- net/sched/sch_netem.c | 123 +++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 60 deletions(-)
net/sched/sch_netem.c +63 -60
@@ -71,89 +71,92 @@ struct disttable { s16 table[] __counted_by(size); }; -struct netem_sched_data { -/* internal t(ime)fifo qdisc uses t_root and sch->limit */ -struct rb_root t_root; - -/* a linear queue; reduces rbtree rebalancing when jitter is low */ -struct sk_buff *t_head; -struct sk_buff *t_tail; - -u32 t_len; - -/* optional qdisc for classful handling (NULL at netem init) */ -struct Qdisc *qdisc; - -struct qdisc_watchdog watchdog; +/* Loss models */ +enum { +CLG_RANDOM , +CLG_4_STATES , +CLG_GILB_ELL , +}; -s64 latency; -s64 jitter; +/* States in GE model */ +enum { +GOOD_STATE =1 , +BAD_STATE , +}; -u32 loss; -u32 ecn; -u32 limit; -u32 counter; -u32 gap; -u32 duplicate; -u32 reorder; -u32 corrupt; -u64 rate; -s32 packet_overhead; -u32 cell_size; -struct reciprocal_value cell_size_reciprocal; -s32 cell_overhead; +/* States in 4 state model */ +enum { +TX_IN_GAP_PERIOD =1 , +TX_IN_BURST_PERIOD , +LOST_IN_GAP_PERIOD , +LOST_IN_BURST_PERIOD , +}; +struct netem_sched_data { +/* Cacheline 0: tfifo state and per-packet enqueue/dequeue scalars. */ +struct rb_root t_root; +struct sk_buff *t_head; +struct sk_buff *t_tail; +u32 t_len; +u32 counter; +s64 latency; +s64 jitter; +u64 rate; +u32 gap; +u32 loss; + +/* Cacheline 1: zero-check scalars and correlation states. */ +u32 duplicate; +u32 reorder; +u32 corrupt; +u32 ecn;struct crndstate {u32 last;u32 rho; } delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor; +u8 loss_model; -struct prng { +/* Cacheline 2: PRNG, distribution tables, slot dequeue state etc. */ +struct prng {u64 seed;struct rnd_state prng_state; } prng; +struct disttable *delay_dist; +struct slotstate { +u64 slot_next; +s32 packets_left; +s32 bytes_left; + } slot; +struct disttable *slot_dist; +struct Qdisc *qdisc; -struct disttable *delay_dist; - -enum { -CLG_RANDOM , -CLG_4_STATES , -CLG_GILB_ELL , - } loss_model; - -enum { -TX_IN_GAP_PERIOD =1 , -TX_IN_BURST_PERIOD , -LOST_IN_GAP_PERIOD , -LOST_IN_BURST_PERIOD , - } _4_state_model; - -enum { -GOOD_STATE =1 , -BAD_STATE , - } GE_state_model; +/* +* Warm: rate-shaping parameters (only read when rate != 0) and +* configuration-only fields. The fast path reads sch->limit, not +* q->limit. +*/ +s32 packet_overhead; +u32 cell_size; +struct reciprocal_value cell_size_reciprocal; +s32 cell_overhead; +u32 limit;/* 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; -struct tc_netem_slot slot_config; -struct slotstate { -u64 slot_next; -s32 packets_left; -s32 bytes_left; - } slot; +/* state of the Markov chain */ +u8 state; + } clg; -struct disttable *slot_dist; +/* Cold tail: slot reschedule config and the watchdog timer. */ +struct tc_netem_slot slot_config; +struct qdisc_watchdog watchdog; };/* Time stamp put into socket buffer control block
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v5 2/5 net/sched: netem: remove useless VERSION
In reply to Stephen HemmingerThe version printed was never updated and kernel version is better indication of what is fixed or not.
--- net/sched/sch_netem.c | 4 ---- 1 file changed, 4 deletions(-)
net/sched/sch_netem.c +0 -4
@@ -27,8 +27,6 @@#include <net/pkt_sched.h> #include <net/inet_ecn.h> -#define VERSION "1.3" -/* Network Emulation Queuing algorithm. ====================================
@@ -1413,10 +1411,8 @@ static struct Qdisc_ops netem_qdisc_ops __read_mostly = { };MODULE_ALIAS_NET_SCH ("netem" ); -static int __init netem_module_init (void ) { -pr_info ("netem: version " VERSION "\n" );return register_qdisc (&netem_qdisc_ops); } static void __exit netem_module_exit(void)
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v5 3/5 net/sched: netem: replace pr_info with netlink extack error messages
In reply to Stephen HemmingerUse netlink extack to report errors instead of sending them to the kernel log with pr_info(). The error message can them be seen with tc commands; and avoids log spam.
--- net/sched/sch_netem.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
net/sched/sch_netem.c +16 -14
@@ -921,7 +921,8 @@ static void get_rate(struct netem_sched_data *q, const struct nlattr *attr) q->cell_size_reciprocal = (struct reciprocal_value ) {0 }; } -static int get_loss_clg (struct netem_sched_data *q,const struct nlattr *attr) +static int get_loss_clg (struct netem_sched_data *q,const struct nlattr *attr, +struct netlink_ext_ack *extack) {const struct nlattr *la;int rem;
@@ -934,7 +935,8 @@ static int get_loss_clg(struct netem_sched_data *q, const struct nlattr *attr)const struct tc_netem_gimodel *gi =nla_data (la);if (nla_len (la) <sizeof (struct tc_netem_gimodel )) { -pr_info ("netem: incorrect gi model size\n" ); +NL_SET_ERR_MSG_ATTR (extack, la, +"netem: incorrect gi model size" );return -EINVAL ; }
@@ -953,7 +955,8 @@ static int get_loss_clg(struct netem_sched_data *q, const struct nlattr *attr)const struct tc_netem_gemodel *ge =nla_data (la);if (nla_len (la) <sizeof (struct tc_netem_gemodel )) { -pr_info ("netem: incorrect ge model size\n" ); +NL_SET_ERR_MSG_ATTR (extack, la, +"netem: incorrect ge model size" );return -EINVAL ; }
@@ -967,7 +970,8 @@ static int get_loss_clg(struct netem_sched_data *q, const struct nlattr *attr) }default : -pr_info ("netem: unknown loss type %u\n" , type); +NL_SET_ERR_MSG_ATTR_FMT (extack, la, +"netem: unknown loss type %u" , type);return -EINVAL ; } }
@@ -990,19 +994,21 @@ static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = { };static int parse_attr (struct nlattr *tb[],int maxtype,struct nlattr *nla, -const struct nla_policy *policy,int len) +const struct nla_policy *policy,int len, +struct netlink_ext_ack *extack) {int nested_len =nla_len (nla) -NLA_ALIGN (len);if (nested_len <0 ) { -pr_info ("netem: invalid attributes len %d\n" , nested_len); +NL_SET_ERR_MSG_FMT (extack,"netem: invalid attributes len %d < %d" , +nla_len (nla),NLA_ALIGN (len));return -EINVAL ; }if (nested_len >=nla_attr_size (0 ))return nla_parse_deprecated (tb, maxtype,nla_data (nla) +NLA_ALIGN (len), - nested_len, policy,NULL ); + nested_len, policy, extack);memset (tb,0 ,sizeof (struct nlattr *) * (maxtype +1 ));return 0 ;
@@ -1057,7 +1063,7 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,int ret; qopt =nla_data (opt); - ret =parse_attr (tb,TCA_NETEM_MAX , opt, netem_policy,sizeof (*qopt)); + ret =parse_attr (tb,TCA_NETEM_MAX , opt, netem_policy,sizeof (*qopt), extack);if (ret <0 )return ret;
@@ -1097,7 +1103,7 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt, old_loss_model = q->loss_model;if (tb[TCA_NETEM_LOSS ]) { - ret =get_loss_clg (q, tb[TCA_NETEM_LOSS ]); + ret =get_loss_clg (q, tb[TCA_NETEM_LOSS ], extack);if (ret) { q->loss_model = old_loss_model; q->clg = old_clg;
@@ -1181,7 +1187,6 @@ static int netem_init(struct Qdisc *sch, struct nlattr *opt, struct netlink_ext_ack *extack) {struct netem_sched_data *q =qdisc_priv (sch); -int ret;qdisc_watchdog_init (&q->watchdog, sch);
@@ -1192,10 +1197,7 @@ static int netem_init(struct Qdisc *sch, struct nlattr *opt, 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" ); -return ret; +return netem_change (sch, opt, extack); }static void netem_destroy (struct Qdisc *sch)
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v5 4/5 net/sched: netem: handle multi-segment skb in corruption
In reply to Stephen HemmingerThe packet corruption code only flipped bits in the linear header portion of the skb, skipping corruption when skb_headlen() was zero.
Linearize the whole skb if necessary before corruption. Extends d64cb81dcbd5 ("net/sched: sch_netem: fix out-of-bounds access in packet corruption") with a more general solution.
--- net/sched/sch_netem.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/sched/sch_netem.c +6 -5
@@ -513,16 +513,17 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,qdisc_qstats_drop (sch); goto finish_segs; } -if (skb->ip_summed ==CHECKSUM_PARTIAL && -skb_checksum_help (skb)) { +if (skb_linearize (skb) || + (skb->ip_summed ==CHECKSUM_PARTIAL &&skb_checksum_help (skb))) {qdisc_drop (skb, sch, to_free); skb =NULL ; goto finish_segs; } -if (skb_headlen (skb)) - skb->data[get_random_u32_below (skb_headlen (skb))] ^= -1 <<get_random_u32_below (8 ); +if (skb->len) { +u32 offset =get_random_u32_below (skb->len); + skb->data[offset] ^=1 <<get_random_u32_below (8 ); + } }if (unlikely (sch->q.qlen >= sch->limit)) {
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerPATCH v5 5/5 net/sched: netem: add per-impairment extended statistics
In reply to Stephen HemmingerAdd 64-bit counters for each impairment netem applies (delay, loss, ECN marking, corruption, duplication, reordering) and for skb allocation failures during enqueue. Exposed through TCA_STATS_APP as struct tc_netem_xstats.
Counters increment when an impairment is occurs, independent of later events that may mask its on-wire effect. Added allocation_errors (similar to sch_fq) to account for when impairment could not be applied due to memory pressure, etc.
---
Note to reviewers. The READ_ONCE/WRITE_ONCE pattern is to align with upcoming changes removing qdisc_lock.
For some reason current AI prompts are obsessed with complaining about 64 bit torn read/write on these, since the counters are informational only, any such complaints are false positive.
Addition to iproute2 will be sent seperately.
include/uapi/linux/pkt_sched.h | 10 +++++++ net/sched/sch_netem.c | 55 ++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 6 deletions(-)
include/uapi/linux/pkt_sched.h +10 -0
@@ -569,6 +569,16 @@ struct tc_netem_gemodel {#define NETEM_DIST_SCALE 8192#define NETEM_DIST_MAX 16384 +struct tc_netem_xstats { +__u64 delayed;/* packets delayed */ +__u64 dropped;/* packets dropped by loss model */ +__u64 corrupted;/* packets with bit errors injected */ +__u64 duplicated;/* duplicate packets generated */ +__u64 reordered;/* packets sent out of order */ +__u64 ecn_marked;/* packets ECN CE-marked (not dropped)*/ +__u64 allocation_errors; +}; +/* DRR */ enum {
net/sched/sch_netem.c +49 -6
128 lines not on this page · raw message
Signature · 1 line
2.53.0
Stephen Hemminger
maintainerRe: [PATCH net-next v5 5/5] net/sched: netem: add per-impairment extended statistics
Quoted 10 lines · On Sat, 9 May 2026 10:03:26 -0700 Stephen Hemminger…
unters for each impairment netem applies (delay, loss, ECN marking, corruption, duplication, reordering) and for skb allocation failures during enqueue. Exposed through
TCA_STATS_APPas structtc_netem_xstats.Counters increment when an impairment is occurs, independent of later events that may mask its on-wire effect. Added
allocation_errors(similar tosch_fq) to account for when impairment could not be applied due to memory pressure, etc.
Quoted 8 lines
Signed-off-by Stephen Hemminger <[email protected]>---
Note to reviewers. The READ_ONCE/WRITE_ONCE pattern is to align with upcoming changes removing
qdisc_lock.
For some reason current AI prompts are obsessed with complaining about 64 bit torn read/write on these, since the counters are informational only, any such complaints are false positive.
The automated review comments on this patch are false positives. Unfortunately, the review automation doesn't look at precedents in other qdisc like RED, FQ, ...
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 8 lines · On Sat, 9 May 2026 10:03:21 -0700 you wrote:
This is a collection of improvements to netem found while investigating the fixes now in net tree.
v5 - minor cleanups
- addallocation_errorscounter
- align counters with where impairment occurs[...]
Here is the summary with links:
- [net-next,v5,1/5] net/sched: netem: reorder struct netem_sched_data
https://git.kernel.org/netdev/net-next/c/9371b229a1ce - [net-next,v5,2/5] net/sched: netem: remove useless VERSION https://git.kernel.org/netdev/net-next/c/4398429a5ff4 - [net-next,v5,3/5] net/sched: netem: replace pr_info with netlink extack error messages https://git.kernel.org/netdev/net-next/c/675f6f1968a6 - [net-next,v5,4/5] net/sched: netem: handle multi-segment skb in corruption https://git.kernel.org/netdev/net-next/c/e8be7cdedc41 - [net-next,v5,5/5] net/sched: netem: add per-impairment extended statistics https://git.kernel.org/netdev/net-next/c/a2f6ed7b4873
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html