From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 670AE24A067 for ; Fri, 8 May 2026 01:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778205604; cv=none; b=CRvU9c8VlLJlzNOzDdiq879JlYRGGaZ85s440mUUxfsQk0H4VcswXzfDkGccydhy8EEsC/aUTN+oE1+7hMKt6kQyj8prM3FFg1LxBm3eEez+e6MDsfDZ7bjCK3U1uFBIeEpkiwZEfw6JE/C+DlFbe8Sfswa3OFwyaWy+pGIEcqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778205604; c=relaxed/simple; bh=zDZZU1Z3DfyjTFMKqFayRyXWNU+msBLAqyMWqy6Tf5Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HM3OlZu7dbR9A+Jueej6LeRI1d+CkRxKAuNkaedl1zwJ1UOqelDgWx7XUdGH8kqMHI6E/9qKwMb7BmsJ7Fbf4zX/Z7fc/SmgVw4CydnBrXCYPimcMwLQIS+HDzhoZ3xmKCFUFcPh/gqmKW/JUrLaWNWxVz9TvPrwl6ov4V72P+M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=networkplumber.org; spf=pass smtp.mailfrom=networkplumber.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20251104.gappssmtp.com header.i=@networkplumber-org.20251104.gappssmtp.com header.b=AQuuTF4W; arc=none smtp.client-ip=74.125.82.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=networkplumber.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=networkplumber.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20251104.gappssmtp.com header.i=@networkplumber-org.20251104.gappssmtp.com header.b="AQuuTF4W" Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-2ef38cf04f0so1172341eec.1 for ; Thu, 07 May 2026 18:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20251104.gappssmtp.com; s=20251104; t=1778205594; x=1778810394; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1ektXhM/nH1iZmXUIf3cTS2OfeEcpGx9AtCtI+vX/hM=; b=AQuuTF4WjkSlzEmFfO2CVYYtYq6kp/D9kdmTygrVbVPAUeBT7FCLu33ki4uCQjuiJh 3uVhP5OqHD4V2MT4Ov7+upxqO0658/Clqemw8GiaotGSlgZVFgILaHxM6t48LjLNFpme G/Jbz5kbxxaPr+FFbjyZ/2fNUnFUtU6VFmRejk0CaksL2S+yuUOQ50DxXPjkwQrYN0kf Zj7AViW0RCs4Gvg0rV+PatY83fu41rJi6WlWE/OL1D1I63y24k7Ss+xuQ4R+fCZUf4lm oW6Jv3DIZt2ei9Rm6uV6IG6CNsHa/lY/PFIXWoieo79ENZnGQ5iq3/CH1oBw/PPPbuf9 DKZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778205594; x=1778810394; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=1ektXhM/nH1iZmXUIf3cTS2OfeEcpGx9AtCtI+vX/hM=; b=j5K2hvv6rdHVGc9cTkWD+XtdeXDW1gxeRnA9IXrsWvosY//XqTK8jT6T7jhQaPdwZl V0vybWA+hM384ertmfBL/vpj+qxluhI+1Jh93B0GBZ1c2uotTfuJN4KFaCnwf5plsaN2 u7T/eZBhw2qRF8mvs4/o6QADi9kG638dhY23G7mQKyt9X+JWaHsCyMDmGu5/jwa6yahi 4e8focRZFl5EC3swunFhNj4YUqMhuoFYIzzhtWijpB9HURm2sJ6WZtllGaJblQoms6Ch wry1gNf5BKD5q+Nl67gtaFLMj6racxNVxcBJgDo1knCuWldcvWddw3iw+vl0k+oqEPtf TZIg== X-Forwarded-Encrypted: i=1; AFNElJ+i824i5fj4InCnZF/nKTguarfdFNNRuIUpSgajJYeEJIqvX/3O5ng2awG7uNJbGoCCOcmGft8c2RVA/xI=@vger.kernel.org X-Gm-Message-State: AOJu0YxAMrOFRc4RvnVulRmEX2YBJQK/V0be+BAvEImKhdnuCxHCCV+l Dx3ZUmbveLPqNukEvY/yKPs1vbgDxDFJdK81Ii8V/IVISrs5JXOBbAw/AWsPH9YJc0g= X-Gm-Gg: Acq92OGbpzRZEM13pFDSOYaMhq0O0HX+Kgk53aUpaDtR3ZW0IoHaKPBGCdc8NodNcpM cC6wOiJ7fABTMMFLBl7yTcag2rEFKOZHTGPPrhWrff3Fw5LtlY47Ofz8ldBqZMQWi8G6kfiZuuH Ijs17hr6rUZH59o9OKAbzwD8r5A+iIkGP1sX5Ul6QtXFL/kWZFtGGECMyAQYMEG6c41BfnnmQJ6 DWN9+/yKBGYD/2lZ7JaZhokzBou1TxIXzx6fmOU/zCeGgk8408P/HOWMd6A8dIEGsf0qCzU8I+f VC/fq5Hs1vF1aXGZ06iRjQkFm/4bxP221gQ10yvhl+vWx7T0OUkKq/OGeQ0QGrnhujI7qiGWAd7 v7evSpqlhhgU15fZeAiQCSX2/kpzrsgvnYWwSN8a2IY2LYhquFmltB35+wLTZsF5iiNZnnOpTpT 0CTR0YSiAsnzV4mcugVOvmAiID6gi1FJVmBMiKqH565iA= X-Received: by 2002:a05:7300:7255:b0:2d4:aa5a:391c with SMTP id 5a478bee46e88-2f54869a41bmr5287816eec.12.1778205594423; Thu, 07 May 2026 18:59:54 -0700 (PDT) Received: from phoenix.lan ([104.202.41.210]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2f88847504dsm324090eec.15.2026.05.07.18.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2026 18:59:54 -0700 (PDT) From: Stephen Hemminger To: netdev@vger.kernel.org Cc: jhs@mojatatu.com, Stephen Hemminger , Jiri Pirko , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v4 5/5] net/sched: netem: add per-impairment extended statistics Date: Thu, 7 May 2026 18:55:21 -0700 Message-ID: <20260508015940.554462-6-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260508015940.554462-1-stephen@networkplumber.org> References: <20260508015940.554462-1-stephen@networkplumber.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Adds new counters that keep track of when netem applied impairments (delay, loss, corruption, duplication, reordering). Add a struct tc_netem_xstats reported via TCA_STATS_APP so that userspace (tc -s qdisc show) can display per-impairment counters. The READ_ONCE/WRITE_ONCE pairing here matches the model used elsewhere for xstats in the qdisc layer. Also fixes a pre-existing accounting bug: ECN CE-marked packets were being counted as drops (that is not what other qdisc's do). Instead, now that there is a proper ecn_marked counter, the spurious qdisc_qstats_drop() call can be removed. Signed-off-by: Stephen Hemminger --- Note to reviewers. The READ_ONCE/WRITE_ONCE pattern is allowed. 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 | 9 +++++++++ net/sched/sch_netem.c | 37 ++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/pkt_sched.h b/include/uapi/linux/pkt_sched.h index 66e8072f44df..1c84c8076e22 100644 --- a/include/uapi/linux/pkt_sched.h +++ b/include/uapi/linux/pkt_sched.h @@ -569,6 +569,15 @@ 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)*/ +}; + /* DRR */ enum { diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c index 37df05bf43ee..6d8f4c4cedf3 100644 --- a/net/sched/sch_netem.c +++ b/net/sched/sch_netem.c @@ -152,6 +152,14 @@ struct netem_sched_data { u8 state; } clg; + /* Impairment counters */ + u64 delayed; + u64 dropped; + u64 corrupted; + u64 duplicated; + u64 ecn_marked; + u64 reordered; + /* Cold tail: slot reschedule config and the watchdog timer. */ struct tc_netem_slot slot_config; struct qdisc_watchdog watchdog; @@ -462,17 +470,21 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch, skb->prev = NULL; /* Random duplication */ - if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor, &q->prng)) + if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor, &q->prng)) { ++count; + WRITE_ONCE(q->duplicated, q->duplicated + 1); + } /* Drop packet? */ if (loss_event(q)) { if (q->ecn && INET_ECN_set_ce(skb)) - qdisc_qstats_drop(sch); /* mark packet */ + WRITE_ONCE(q->ecn_marked, q->ecn_marked + 1); else --count; } + if (count == 0) { + WRITE_ONCE(q->dropped, q->dropped + 1); qdisc_qstats_drop(sch); __qdisc_drop(skb, to_free); return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; @@ -523,6 +535,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch, if (skb->len) { u32 offset = get_random_u32_below(skb->len); skb->data[offset] ^= 1 << get_random_u32_below(8); + WRITE_ONCE(q->corrupted, q->corrupted + 1); } } @@ -604,12 +617,16 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch, cb->time_to_send = now + delay; ++q->counter; + if (delay) + WRITE_ONCE(q->delayed, q->delayed + 1); + tfifo_enqueue(skb, sch); } else { /* * Do re-ordering by putting one out of N packets at the front * of the queue. */ + WRITE_ONCE(q->reordered, q->reordered + 1); cb->time_to_send = ktime_get_ns(); q->counter = 0; @@ -1350,6 +1367,21 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb) return -1; } +static int netem_dump_stats(struct Qdisc *sch, struct gnet_dump *d) +{ + struct netem_sched_data *q = qdisc_priv(sch); + struct tc_netem_xstats st = { + .delayed = READ_ONCE(q->delayed), + .dropped = READ_ONCE(q->dropped), + .corrupted = READ_ONCE(q->corrupted), + .duplicated = READ_ONCE(q->duplicated), + .reordered = READ_ONCE(q->reordered), + .ecn_marked = READ_ONCE(q->ecn_marked), + }; + + return gnet_stats_copy_app(d, &st, sizeof(st)); +} + static int netem_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, struct tcmsg *tcm) { @@ -1412,6 +1444,7 @@ static struct Qdisc_ops netem_qdisc_ops __read_mostly = { .destroy = netem_destroy, .change = netem_change, .dump = netem_dump, + .dump_stats = netem_dump_stats, .owner = THIS_MODULE, }; MODULE_ALIAS_NET_SCH("netem"); -- 2.53.0