lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1395243232-32630-5-git-send-email-jiri@resnulli.us>
Date:	Wed, 19 Mar 2014 16:33:52 +0100
From:	Jiri Pirko <jiri@...nulli.us>
To:	netdev@...r.kernel.org
Cc:	davem@...emloft.net, nhorman@...driver.com, andy@...yhouse.net,
	tgraf@...g.ch, dborkman@...hat.com, ogerlitz@...lanox.com,
	jesse@...ira.com, pshelar@...ira.com, azhou@...ira.com,
	ben@...adent.org.uk, stephen@...workplumber.org,
	jeffrey.t.kirsher@...el.com, vyasevic@...hat.com,
	xiyou.wangcong@...il.com, john.r.fastabend@...el.com,
	edumazet@...gle.com
Subject: [patch net-next RFC 4/4] net: introduce dummy switch

Dummy switch implementation using switchdev API

Signed-off-by: Jiri Pirko <jiri@...nulli.us>
---
 drivers/net/Kconfig       |   7 +++
 drivers/net/Makefile      |   1 +
 drivers/net/dummyswitch.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 drivers/net/dummyswitch.c

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 89402c3..a9629a7 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -71,6 +71,13 @@ config DUMMY
 	  To compile this driver as a module, choose M here: the module
 	  will be called dummy.
 
+config NET_DUMMY_SWITCH
+	tristate "Dummy switch net driver support"
+	depends on NET_SWITCHDEV
+	---help---
+	  To compile this driver as a module, choose M here: the module
+	  will be called dummyswitch.
+
 config EQUALIZER
 	tristate "EQL (serial line load balancing) support"
 	---help---
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index 3fef8a8..d5d4ce6 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -7,6 +7,7 @@
 #
 obj-$(CONFIG_BONDING) += bonding/
 obj-$(CONFIG_DUMMY) += dummy.o
+obj-$(CONFIG_NET_DUMMY_SWITCH) += dummyswitch.o
 obj-$(CONFIG_EQUALIZER) += eql.o
 obj-$(CONFIG_IFB) += ifb.o
 obj-$(CONFIG_MACVLAN) += macvlan.o
diff --git a/drivers/net/dummyswitch.c b/drivers/net/dummyswitch.c
new file mode 100644
index 0000000..8c315cb6
--- /dev/null
+++ b/drivers/net/dummyswitch.c
@@ -0,0 +1,142 @@
+/*
+ * drivers/net/dummyswitch.c - Dummy switch device
+ * Copyright (c) 2014 Jiri Pirko <jiri@...nulli.us>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/moduleparam.h>
+#include <linux/switchdev.h>
+
+#include <net/rtnetlink.h>
+
+static int numswitches = 1;
+static int numports = 8;
+
+module_param(numswitches, int, 1);
+MODULE_PARM_DESC(numswitches, "Number of dummy switch pseudo devices");
+module_param(numports, int, 8);
+MODULE_PARM_DESC(numports, "Number of ports per dummy switch pseudo device");
+
+static const struct swdev_ops dummysw_swdev_ops = {
+	.kind = "dummyswitch",
+};
+
+static netdev_tx_t dummyswport_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	dev_kfree_skb(skb);
+	return NETDEV_TX_OK;
+}
+
+static const struct swportdev_ops dummysw_swportdev_ops = {
+	.kind = "dummyswitchport",
+	.skb_xmit = dummyswport_xmit,
+};
+
+static struct net_device **dummyswdevs;
+
+static void dummysw_exit_ports(struct net_device *dev)
+{
+	struct net_device *tmp;
+
+	for_each_netdev(dev_net(dev), tmp) {
+		if (netdev_master_upper_dev_get(tmp) == dev)
+			swportdev_destroy(tmp);
+	}
+}
+
+static int dummysw_init_ports(struct net_device *dev)
+{
+	struct net_device *tmp;
+	int err;
+	int i;
+
+	for (i = 0; i < numports; i++) {
+		tmp = swportdev_create(dev, &dummysw_swportdev_ops);
+		if (IS_ERR(tmp)) {
+			err = PTR_ERR(tmp);
+			goto exit_ports;
+		}
+	}
+	return 0;
+
+exit_ports:
+	dummysw_exit_ports(dev);
+	return err;
+}
+
+static void dummysw_exit_one(struct net_device **pdev)
+{
+	dummysw_exit_ports(*pdev);
+	swdev_destroy(*pdev);
+}
+
+static int dummysw_init_one(struct net_device **pdev)
+{
+	struct net_device *dev;
+	int err;
+
+	dev = swdev_create(&dummysw_swdev_ops);
+	if (IS_ERR(dev))
+		return PTR_ERR(dev);
+	err = dummysw_init_ports(dev);
+	if (err)
+		goto swdev_destroy;
+	*pdev = dev;
+	return 0;
+
+swdev_destroy:
+	swdev_destroy(dev);
+	return err;
+}
+
+static int __init dummysw_module_init(void)
+{
+	int err;
+	int i;
+
+	dummyswdevs = kmalloc(sizeof(struct net_device) * numswitches,
+			      GFP_KERNEL);
+	if (!dummyswdevs)
+		return -ENOMEM;
+
+	rtnl_lock();
+	for (i = 0; i < numswitches; i++) {
+		err = dummysw_init_one(&dummyswdevs[i]);
+		if (err)
+			goto rollback;
+	}
+	rtnl_unlock();
+	return 0;
+
+rollback:
+	for (i--; i >= 0; i--)
+		dummysw_exit_one(&dummyswdevs[i]);
+	rtnl_unlock();
+	kfree(dummyswdevs);
+	return err;
+}
+
+static void __exit dummysw_module_exit(void)
+{
+	int i;
+
+	rtnl_lock();
+	for (i = 0; i < numswitches; i++)
+		dummysw_exit_one(&dummyswdevs[i]);
+	rtnl_unlock();
+	kfree(dummyswdevs);
+}
+
+module_init(dummysw_module_init);
+module_exit(dummysw_module_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Jiri Pirko <jiri@...nulli.us>");
+MODULE_DESCRIPTION("Dummy switch device");
-- 
1.8.5.3

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ