All of lore.kernel.org
 help / color / mirror / Atom feed
From: T S Rameshkumar <rameshsv06@gmail.com>
To: Marc Kleine-Budde <mkl@pengutronix.de>,
	Vincent Mailhol <mailhol@kernel.org>
Cc: linux-can@vger.kernel.org,
	T S Rameshkumar <rameshkumar.t@phytecembedded.in>
Subject: [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test
Date: Thu,  3 Dec 2026 17:35:59 +0530	[thread overview]
Message-ID: <20261203120559.421646-2-rameshsv06@gmail.com> (raw)
In-Reply-To: <20261203120559.421646-1-rameshsv06@gmail.com>

From: T S Rameshkumar <rameshkumar.t@phytecembedded.in>

Add a kselftest (test_vcan_stats) that runs in an isolated network
namespace, spawns concurrent PF_CAN transmitters on vcan0 while reading
/proc/net/dev in parallel behind a startup barrier, checks that
concurrent reads succeed monotonically, and verifies exact rx/tx packet
and byte counter deltas. Skip cleanly if CAN_RAW or vcan is
unavailable.

Signed-off-by: T S Rameshkumar <rameshkumar.t@phytecembedded.in>
---
 tools/testing/selftests/net/can/.gitignore    |   1 +
 tools/testing/selftests/net/can/Makefile      |   5 +-
 .../selftests/net/can/test_vcan_stats.c       | 231 ++++++++++++++++++
 .../selftests/net/can/test_vcan_stats.sh      |  50 ++++
 4 files changed, 285 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/can/test_vcan_stats.c
 create mode 100755 tools/testing/selftests/net/can/test_vcan_stats.sh

diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore
index 764a53fc8..3dee21f85 100644
--- a/tools/testing/selftests/net/can/.gitignore
+++ b/tools/testing/selftests/net/can/.gitignore
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 test_raw_filter
+test_vcan_stats
diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile
index 5b82e60a0..c2e4903b9 100644
--- a/tools/testing/selftests/net/can/Makefile
+++ b/tools/testing/selftests/net/can/Makefile
@@ -3,9 +3,10 @@
 top_srcdir = ../../../../..
 
 CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INCLUDES)
+LDLIBS += -lpthread
 
-TEST_PROGS := test_raw_filter.sh
+TEST_PROGS := test_raw_filter.sh test_vcan_stats.sh
 
-TEST_GEN_FILES := test_raw_filter
+TEST_GEN_FILES := test_raw_filter test_vcan_stats
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.c b/tools/testing/selftests/net/can/test_vcan_stats.c
new file mode 100644
index 000000000..aca72cc09
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.c
@@ -0,0 +1,231 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <errno.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <net/if.h>
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define NUM_THREADS 4
+#define FRAMES_PER_THREAD 1000
+#define CAN_FRAME_DLC 8
+
+static char canif[IFNAMSIZ];
+static pthread_barrier_t start_barrier;
+static atomic_int stop_reader;
+static atomic_int reader_err;
+static atomic_ulong reader_reads;
+
+struct net_dev_stats {
+	unsigned long long rx_bytes;
+	unsigned long long rx_packets;
+	unsigned long long tx_bytes;
+	unsigned long long tx_packets;
+};
+
+static int read_if_stats(const char *ifname, struct net_dev_stats *st)
+{
+	char line[512];
+	FILE *f;
+	int found = 0;
+
+	f = fopen("/proc/net/dev", "r");
+	if (!f)
+		return -errno;
+
+	while (fgets(line, sizeof(line), f)) {
+		char *colon = strchr(line, ':');
+		char *name;
+
+		if (!colon)
+			continue;
+		*colon = '\0';
+		name = line;
+		while (*name == ' ' || *name == '\t')
+			name++;
+		if (strcmp(name, ifname) != 0)
+			continue;
+
+		unsigned long long dummy;
+
+		if (sscanf(colon + 1,
+			   "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
+			   &st->rx_bytes, &st->rx_packets,
+			   &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
+			   &st->tx_bytes, &st->tx_packets) == 10)
+			found = 1;
+		break;
+	}
+
+	fclose(f);
+	return found ? 0 : -ENOENT;
+}
+
+static void *tx_worker(void *arg)
+{
+	int id = (int)(long)arg;
+	struct sockaddr_can addr = {};
+	struct can_frame frame = {};
+	struct ifreq ifr = {};
+	int s;
+
+	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+	if (s < 0) {
+		int err = -errno;
+
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", canif);
+	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+		int err = -errno;
+
+		close(s);
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	addr.can_family = AF_CAN;
+	addr.can_ifindex = ifr.ifr_ifindex;
+	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+		int err = -errno;
+
+		close(s);
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	frame.can_id = 0x100 + id;
+	frame.can_dlc = CAN_FRAME_DLC;
+	memset(frame.data, 0xAA, CAN_FRAME_DLC);
+
+	pthread_barrier_wait(&start_barrier);
+
+	for (int i = 0; i < FRAMES_PER_THREAD; i++) {
+		ssize_t ret = write(s, &frame, sizeof(frame));
+
+		if (ret < 0) {
+			if (errno == ENOBUFS) {
+				usleep(100);
+				i--;
+				continue;
+			}
+			int err = -errno;
+
+			close(s);
+			return (void *)(long)err;
+		}
+	}
+
+	close(s);
+	return NULL;
+}
+
+static void *stats_reader(void *arg)
+{
+	struct net_dev_stats prev = {}, st;
+	unsigned long reads = 0;
+
+	pthread_barrier_wait(&start_barrier);
+
+	while (!atomic_load(&stop_reader)) {
+		int ret = read_if_stats(canif, &st);
+
+		if (ret) {
+			atomic_store(&reader_err, ret);
+			break;
+		}
+		if (reads > 0 &&
+		    (st.rx_packets < prev.rx_packets ||
+		     st.rx_bytes < prev.rx_bytes ||
+		     st.tx_packets < prev.tx_packets ||
+		     st.tx_bytes < prev.tx_bytes)) {
+			atomic_store(&reader_err, -ERANGE);
+			break;
+		}
+		prev = st;
+		reads++;
+		atomic_fetch_add(&reader_reads, 1);
+	}
+
+	return NULL;
+}
+
+TEST(vcan_concurrent_tx_stats)
+{
+	struct net_dev_stats before, after;
+	pthread_t tx[NUM_THREADS], reader;
+	unsigned long long exp_pkts, exp_bytes;
+	int probe_sock, ret;
+
+	probe_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+	if (probe_sock < 0) {
+		if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT)
+			SKIP(return, "CAN_RAW protocol not supported");
+		ASSERT_GE(probe_sock, 0)
+			TH_LOG("failed to create CAN_RAW socket: %d", errno);
+	}
+	close(probe_sock);
+
+	ret = read_if_stats(canif, &before);
+	ASSERT_EQ(ret, 0)
+		TH_LOG("failed to read initial stats for %s", canif);
+
+	ret = pthread_barrier_init(&start_barrier, NULL, NUM_THREADS + 1);
+	ASSERT_EQ(ret, 0);
+
+	atomic_store(&stop_reader, 0);
+	atomic_store(&reader_err, 0);
+	atomic_store(&reader_reads, 0);
+
+	ret = pthread_create(&reader, NULL, stats_reader, NULL);
+	ASSERT_EQ(ret, 0);
+
+	for (long i = 0; i < NUM_THREADS; i++) {
+		ret = pthread_create(&tx[i], NULL, tx_worker, (void *)i);
+		ASSERT_EQ(ret, 0);
+	}
+
+	for (int i = 0; i < NUM_THREADS; i++) {
+		void *res = NULL;
+
+		pthread_join(tx[i], &res);
+		ASSERT_EQ((long)res, 0);
+	}
+
+	atomic_store(&stop_reader, 1);
+	pthread_join(reader, NULL);
+	pthread_barrier_destroy(&start_barrier);
+
+	ASSERT_EQ(atomic_load(&reader_err), 0);
+	EXPECT_GT(atomic_load(&reader_reads), 0UL);
+
+	ret = read_if_stats(canif, &after);
+	ASSERT_EQ(ret, 0);
+
+	exp_pkts = NUM_THREADS * FRAMES_PER_THREAD;
+	exp_bytes = exp_pkts * CAN_FRAME_DLC;
+
+	EXPECT_EQ(after.tx_packets - before.tx_packets, exp_pkts);
+	EXPECT_EQ(after.tx_bytes - before.tx_bytes, exp_bytes);
+	EXPECT_EQ(after.rx_packets - before.rx_packets, exp_pkts);
+	EXPECT_EQ(after.rx_bytes - before.rx_bytes, exp_bytes);
+}
+
+int main(int argc, char **argv)
+{
+	char *env_canif = getenv("CANIF");
+
+	snprintf(canif, sizeof(canif), "%s", env_canif ? env_canif : "vcan0");
+	return test_harness_run(argc, argv);
+}
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.sh b/tools/testing/selftests/net/can/test_vcan_stats.sh
new file mode 100755
index 000000000..2d182dd47
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ALL_TESTS="
+	test_vcan_stats
+"
+
+net_dir=$(dirname $0)/..
+source $net_dir/lib.sh
+
+export CANIF=${CANIF:-"vcan0"}
+
+setup()
+{
+	setup_ns ns || exit $ksft_skip
+	if ! ip -n "$ns" link add name "$CANIF" type vcan; then
+		echo "SKIP: vcan device creation not supported" >&2
+		exit $ksft_skip
+	fi
+	if ! ip -n "$ns" link set dev "$CANIF" up; then
+		echo "FAIL: failed to bring up $CANIF" >&2
+		exit $ksft_fail
+	fi
+}
+
+cleanup()
+{
+	cleanup_all_ns
+}
+
+test_vcan_stats()
+{
+	local rc
+
+	ip netns exec "$ns" ./test_vcan_stats
+	rc=$?
+	if ((rc == ksft_skip)); then
+		log_test_skip "test_vcan_stats"
+	else
+		RET=$rc
+		log_test "test_vcan_stats"
+	fi
+}
+
+trap cleanup EXIT
+setup
+
+tests_run
+
+exit $EXIT_STATUS
-- 
2.34.1


  reply	other threads:[~2026-12-03 12:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:11 [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx T S Rameshkumar
2026-12-03 11:11 ` [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test T S Rameshkumar
2026-12-03 11:23   ` sashiko-bot
2026-12-05  0:14   ` netdev-bot+sashiko
2026-12-03 11:23 ` [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx sashiko-bot
2026-12-03 12:05 ` [PATCH v2 " T S Rameshkumar
2026-12-03 12:05   ` T S Rameshkumar [this message]
2026-12-03 12:37     ` [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test sashiko-bot
2026-12-03 12:50     ` Oliver Hartkopp
2026-12-05  0:41     ` netdev-bot+sashiko
2026-12-05  0:41   ` [PATCH v2 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx netdev-bot+sashiko
2026-12-05  0:14 ` [PATCH " netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203120559.421646-2-rameshsv06@gmail.com \
    --to=rameshsv06@gmail.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=rameshkumar.t@phytecembedded.in \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.