Linux CAN drivers development
 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 2/2] selftests: net: can: add vcan concurrent tx/stats test
Date: Thu,  3 Dec 2026 16:41:03 +0530	[thread overview]
Message-ID: <20261203111103.379929-2-rameshsv06@gmail.com> (raw)
In-Reply-To: <20261203111103.379929-1-rameshsv06@gmail.com>

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

Add a kselftest (test_vcan_stats) that spawns concurrent PF_CAN
transmitters on vcan0 while reading /proc/net/dev in parallel and
verifies exact rx/tx packet and byte counter deltas.

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       | 181 ++++++++++++++++++
 .../selftests/net/can/test_vcan_stats.sh      |  37 ++++
 4 files changed, 222 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..1a4f715ad
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.c
@@ -0,0 +1,181 @@
+// 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 atomic_int stop_reader;
+
+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)
+		return (void *)(long)-errno;
+
+	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", canif);
+	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+		int err = -errno;
+
+		close(s);
+		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);
+		return (void *)(long)err;
+	}
+
+	frame.can_id = 0x100 + id;
+	frame.can_dlc = CAN_FRAME_DLC;
+	memset(frame.data, 0xAA, CAN_FRAME_DLC);
+
+	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 st;
+
+	while (!atomic_load(&stop_reader))
+		read_if_stats(canif, &st);
+
+	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 ret;
+
+	ret = read_if_stats(canif, &before);
+	ASSERT_EQ(ret, 0)
+		TH_LOG("failed to read initial stats for %s", canif);
+
+	atomic_store(&stop_reader, 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);
+
+	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..1153699cb
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.sh
@@ -0,0 +1,37 @@
+#!/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()
+{
+	ip link add name $CANIF type vcan || exit $ksft_skip
+	ip link set dev $CANIF up
+}
+
+cleanup()
+{
+	ip link set dev $CANIF down
+	ip link delete $CANIF
+}
+
+test_vcan_stats()
+{
+	./test_vcan_stats
+	check_err $?
+	log_test "test_vcan_stats"
+}
+
+trap cleanup EXIT
+setup
+
+tests_run
+
+exit $EXIT_STATUS
-- 
2.34.1


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

Thread overview: 4+ 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 ` T S Rameshkumar [this message]
2026-12-03 11:23   ` [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test sashiko-bot
2026-12-03 11:23 ` [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx sashiko-bot

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=20261203111103.379929-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox