mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yana Bashlykova <yana2bsh@gmail.com>
To: "David S. Miller" <davem@davemloft.net>
Cc: Yana Bashlykova <yana2bsh@gmail.com>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	lvc-project@linuxtesting.org
Subject: [PATCH 6.1 01/15] genetlink: add sysfs test module for Generic Netlink
Date: Fri, 12 Sep 2025 22:53:24 +0300	[thread overview]
Message-ID: <20250912195339.20635-2-yana2bsh@gmail.com> (raw)
In-Reply-To: <20250912195339.20635-1-yana2bsh@gmail.com>

Add a test module that creates sysfs interfaces for Generic Netlink testing:
- /sys/kernel/genl_test with value/message/info attributes
- /sys/kernel/parallel_genl with message attribute
- /sys/kernel/third_genl with message attribute

Implements basic read/write operations with proper error handling and cleanup.
Will be used as foundation for netlink testing infrastructure.

Signed-off-by: Yana Bashlykova <yana2bsh@gmail.com>
---
 drivers/net/Kconfig                           |   2 +
 drivers/net/Makefile                          |   2 +
 drivers/net/genetlink/Kconfig                 |   8 +
 drivers/net/genetlink/Makefile                |   3 +
 .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 288 ++++++++++++++++++
 5 files changed, 303 insertions(+)
 create mode 100644 drivers/net/genetlink/Kconfig
 create mode 100644 drivers/net/genetlink/Makefile
 create mode 100644 drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 9e63b8c43f3e..2f5f74185da5 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -631,4 +631,6 @@ config NETDEV_LEGACY_INIT
 	  Drivers that call netdev_boot_setup_check() should select this
 	  symbol, everything else no longer needs it.
 
+source "drivers/net/genetlink/Kconfig"
+
 endif # NETDEVICES
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index 6ce076462dbf..934dff748416 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -89,3 +89,5 @@ thunderbolt-net-y += thunderbolt.o
 obj-$(CONFIG_USB4_NET) += thunderbolt-net.o
 obj-$(CONFIG_NETDEVSIM) += netdevsim/
 obj-$(CONFIG_NET_FAILOVER) += net_failover.o
+
+obj-$(CONFIG_NETLINK_TEST) += genetlink/
diff --git a/drivers/net/genetlink/Kconfig b/drivers/net/genetlink/Kconfig
new file mode 100644
index 000000000000..e1fd8da50488
--- /dev/null
+++ b/drivers/net/genetlink/Kconfig
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config NETLINK_TEST
+    tristate "Test module for netlink communication"
+    depends on NET
+    help
+      This module provides testing interface for netlink communication.
+      Used by selftests in tools/testing/selftests/net/.
diff --git a/drivers/net/genetlink/Makefile b/drivers/net/genetlink/Makefile
new file mode 100644
index 000000000000..0336eac4cc28
--- /dev/null
+++ b/drivers/net/genetlink/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_NETLINK_TEST) += net-pf-16-proto-16-family-PARALLEL_GENL.o
diff --git a/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c b/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
new file mode 100644
index 000000000000..c50c0daae392
--- /dev/null
+++ b/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
@@ -0,0 +1,288 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/kobject.h>
+#include <linux/if_arp.h>
+#include <linux/sysfs.h>
+#include <linux/string.h>
+#include <linux/device.h>
+#include <linux/netlink.h>
+#include <linux/skbuff.h>
+#include <linux/fs.h>
+#include <linux/err.h>
+#include <linux/slab.h>
+#include <net/sock.h>
+#include <linux/kstrtox.h>
+#include <linux/etherdevice.h>
+#include <net/genetlink.h>
+#include <net/rtnetlink.h>
+#include <linux/notifier.h>
+#include <linux/mutex.h>
+
+MODULE_LICENSE("GPL");
+
+static struct kobject *kobj_genl_test;
+static struct device *dev_genl_test;
+static struct kobject *kobj_parallel_genl;
+static struct kobject *kobj_third_genl;
+
+#define MAX_DATA_LEN 256
+
+struct {
+	char genl_test_message[MAX_DATA_LEN];
+	char genl_test_info[MAX_DATA_LEN];
+	u32 genl_test_value;
+	char parallel_genl_message[MAX_DATA_LEN];
+	char third_genl_message[MAX_DATA_LEN];
+}
+
+sysfs_data = {
+	.genl_test_message = "default",
+	.genl_test_info = "default",
+	.genl_test_value = -20,
+	.parallel_genl_message = "default",
+	.third_genl_message = "default",
+};
+
+static ssize_t show_genl_test_info(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n", sysfs_data.genl_test_info);
+}
+
+static ssize_t store_genl_test_info(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t count)
+{
+	snprintf(sysfs_data.genl_test_info, sizeof(sysfs_data.genl_test_info),
+		 "%.*s", (int)min(count, sizeof(sysfs_data.genl_test_info) - 1),
+		 buf);
+	return count;
+}
+
+static ssize_t show_genl_test_message(struct kobject *kobj,
+				      struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s", sysfs_data.genl_test_message);
+}
+
+static ssize_t store_genl_test_message(struct kobject *kobj,
+				       struct kobj_attribute *attr,
+				       const char *buf, size_t count)
+{
+	size_t len = min(count, sizeof(sysfs_data.genl_test_message) - 1);
+
+	strncpy(sysfs_data.genl_test_message, buf, len);
+	sysfs_data.genl_test_message[len] = '\0';
+	return count;
+}
+
+static ssize_t show_genl_test_value(struct kobject *kobj,
+				    struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d", sysfs_data.genl_test_value);
+}
+
+static ssize_t store_genl_test_value(struct kobject *kobj,
+				     struct kobj_attribute *attr,
+				     const char *buf, size_t count)
+{
+	int rt;
+
+	rt = kstrtouint(buf, 0, &sysfs_data.genl_test_value);
+	return count;
+}
+
+static ssize_t show_parallel_genl_message(struct kobject *kobj,
+					  struct kobj_attribute *attr,
+					  char *buf)
+{
+	return sprintf(buf, "%s", sysfs_data.parallel_genl_message);
+}
+
+static ssize_t store_parallel_genl_message(struct kobject *kobj,
+					   struct kobj_attribute *attr,
+					   const char *buf, size_t count)
+{
+	size_t len = min(count, sizeof(sysfs_data.parallel_genl_message) - 1);
+
+	strncpy(sysfs_data.parallel_genl_message, buf, len);
+	sysfs_data.parallel_genl_message[len] = '\0';
+	return count;
+}
+
+static ssize_t show_third_genl_message(struct kobject *kobj,
+				       struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s", sysfs_data.third_genl_message);
+}
+
+static ssize_t store_third_genl_message(struct kobject *kobj,
+					struct kobj_attribute *attr,
+					const char *buf, size_t count)
+{
+	size_t len = min(count, sizeof(sysfs_data.third_genl_message) - 1);
+
+	strncpy(sysfs_data.third_genl_message, buf, len);
+	sysfs_data.third_genl_message[len] = '\0';
+	return count;
+}
+
+static struct device_attribute dev_attr_info_genl_test =
+	__ATTR(some_info, 0664, show_genl_test_info, store_genl_test_info);
+
+static struct kobj_attribute my_attr_str_genl_test =
+	__ATTR(message, 0664, show_genl_test_message, store_genl_test_message);
+
+static struct kobj_attribute my_attr_u32_genl_test =
+	__ATTR(value, 0664, show_genl_test_value, store_genl_test_value);
+
+static struct kobj_attribute my_attr_str_parallel_genl =
+	__ATTR(message, 0664, show_parallel_genl_message, store_parallel_genl_message);
+
+static struct kobj_attribute my_attr_str_third_genl =
+	__ATTR(message, 0664, show_third_genl_message, store_third_genl_message);
+
+static int __init init_sysfs_third_genl(void)
+{
+	int ret;
+
+	kobj_third_genl = kobject_create_and_add("third_genl", kernel_kobj);
+
+	if (!kobj_third_genl) {
+		pr_err("%s: Failed to create kobject\n", __func__);
+		return -ENOMEM;
+	}
+
+	ret = sysfs_create_file(kobj_third_genl, &my_attr_str_third_genl.attr);
+	if (ret) {
+		pr_err("%s: Failed to create sysfs file\n", __func__);
+		goto err_sysfs;
+	}
+
+	return 0;
+
+err_sysfs:
+	kobject_put(kobj_third_genl);
+	return ret;
+}
+
+static int __init init_sysfs_parallel_genl(void)
+{
+	int ret;
+
+	kobj_parallel_genl =
+		kobject_create_and_add("parallel_genl", kernel_kobj);
+
+	if (!kobj_parallel_genl) {
+		pr_err("%s: Failed to create kobject\n", __func__);
+		return -ENOMEM;
+	}
+
+	ret = sysfs_create_file(kobj_parallel_genl,
+				&my_attr_str_parallel_genl.attr);
+	if (ret) {
+		pr_err("%s: Failed to create sysfs file\n", __func__);
+		goto err_sysfs;
+	}
+
+	return 0;
+
+err_sysfs:
+	kobject_put(kobj_parallel_genl);
+	return ret;
+}
+
+static int __init init_sysfs_genl_test(void)
+{
+	int ret;
+
+	kobj_genl_test = kobject_create_and_add("genl_test", kernel_kobj);
+	dev_genl_test = kobj_to_dev(kobj_genl_test);
+
+	if (!kobj_genl_test) {
+		pr_err("%s: Failed to create kobject\n", __func__);
+		return -ENOMEM;
+	}
+
+	ret = sysfs_create_file(kobj_genl_test, &my_attr_u32_genl_test.attr);
+	if (ret) {
+		pr_err("%s: Failed to create sysfs file 1\n", __func__);
+		goto err_sysfs;
+	}
+
+	ret = sysfs_create_file(kobj_genl_test, &my_attr_str_genl_test.attr);
+	if (ret) {
+		pr_err("%s: Failed to create sysfs file 2\n", __func__);
+		goto err_sysfs_2;
+	}
+
+	ret = device_create_file(dev_genl_test, &dev_attr_info_genl_test);
+	if (ret) {
+		pr_err("%s: Failed to create device file\n", __func__);
+		goto err_device;
+	};
+
+	return 0;
+
+err_device:
+	sysfs_remove_file(kobj_genl_test, &my_attr_str_genl_test.attr);
+err_sysfs_2:
+	sysfs_remove_file(kobj_genl_test, &my_attr_u32_genl_test.attr);
+err_sysfs:
+	kobject_put(kobj_genl_test);
+	return ret;
+}
+
+static int __init module_netlink_init(void)
+{
+	int ret;
+
+	ret = init_sysfs_genl_test();
+	if (ret)
+		goto err_sysfs;
+
+	ret = init_sysfs_parallel_genl();
+	if (ret)
+		goto err_sysfs;
+
+	ret = init_sysfs_third_genl();
+	if (ret)
+		goto err_sysfs;
+
+	return 0;
+
+err_sysfs:
+	sysfs_remove_file(kobj_genl_test, &my_attr_u32_genl_test.attr);
+	sysfs_remove_file(kobj_genl_test, &my_attr_str_genl_test.attr);
+	device_remove_file(dev_genl_test, &dev_attr_info_genl_test);
+	kobject_put(kobj_genl_test);
+
+	sysfs_remove_file(kobj_parallel_genl, &my_attr_str_parallel_genl.attr);
+	kobject_put(kobj_parallel_genl);
+
+	sysfs_remove_file(kobj_third_genl, &my_attr_str_third_genl.attr);
+	kobject_put(kobj_third_genl);
+	return ret;
+}
+
+static void __exit module_netlink_exit(void)
+{
+	sysfs_remove_file(kobj_genl_test, &my_attr_u32_genl_test.attr);
+	sysfs_remove_file(kobj_genl_test, &my_attr_str_genl_test.attr);
+	device_remove_file(dev_genl_test, &dev_attr_info_genl_test);
+	kobject_put(kobj_genl_test);
+
+	sysfs_remove_file(kobj_parallel_genl, &my_attr_str_parallel_genl.attr);
+	kobject_put(kobj_parallel_genl);
+
+	sysfs_remove_file(kobj_third_genl, &my_attr_str_third_genl.attr);
+	kobject_put(kobj_third_genl);
+	pr_info("%s: Module is exited\n", __func__);
+}
+
+module_init(module_netlink_init);
+module_exit(module_netlink_exit);
-- 
2.34.1


  reply	other threads:[~2025-09-12 19:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-12 19:53 [PATCH 6.1 00/15] genetlink: Test Netlink subsystem of Linux v6.1 Yana Bashlykova
2025-09-12 19:53 ` Yana Bashlykova [this message]
2025-09-12 19:53 ` [PATCH 6.1 02/15] genetlink: add TEST_GENL family for netlink testing Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 03/15] genetlink: add PARALLEL_GENL test family Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 04/15] genetlink: add test case for duplicate genl family registration Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 05/15] genetlink: add test case for family with invalid ops Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 06/15] genetlink: add netlink notifier support Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 07/15] genetlink: add THIRD_GENL family Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 08/15] genetlink: verify unregister fails for non-registered family Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 09/15] genetlink: add LARGE_GENL stress test family Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 10/15] selftests: net: genetlink: add packet capture test infrastructure Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 11/15] selftests: net: genetlink: add /proc/net/netlink test Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 12/15] selftests: net: genetlink: add Generic Netlink controller tests Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 13/15] selftests: net: genetlink: add large family ID resolution test Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 14/15] selftests: net: genetlink: add Netlink and Generic Netlink test suite Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 15/15] selftests: net: genetlink: fix expectation for large family resolution Yana Bashlykova
2025-09-12 20:17 ` [PATCH 6.1 00/15] genetlink: Test Netlink subsystem of Linux v6.1 Jakub Kicinski
2025-10-15 14:49   ` Яна Башлыкова
2025-10-15 15:04     ` Jakub Kicinski

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=20250912195339.20635-2-yana2bsh@gmail.com \
    --to=yana2bsh@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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

all inboxes | Powered by JetHome®