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: <20210122094648.1631078-10-jiri@resnulli.us>
Date:   Fri, 22 Jan 2021 10:46:47 +0100
From:   Jiri Pirko <jiri@...nulli.us>
To:     netdev@...r.kernel.org
Cc:     davem@...emloft.net, kuba@...nel.org, jacob.e.keller@...el.com,
        roopa@...dia.com, andrew@...n.ch, dsahern@...il.com,
        mlxsw@...dia.com
Subject: [patch net-next RFCv2 09/10] netdevsim: implement line card activation

From: Jiri Pirko <jiri@...dia.com>

On real HW, the activation typically happens upon line card insertion.
Emulate such event using write to debugfs file "inserted_type".

Signed-off-by: Jiri Pirko <jiri@...dia.com>
---
RFC->RFCv2:
- converted bool "active" to string "inserted_type" and adjust loginc
  around it wrt provision/unprovision work
---
 drivers/net/netdevsim/dev.c       | 136 ++++++++++++++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h |   2 +
 2 files changed, 138 insertions(+)

diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index 84daef00073b..d37e69fc13d6 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -43,6 +43,16 @@ static const unsigned int nsim_dev_linecard_port_counts[] = {
 	1, 2, 4,
 };
 
+static int nsim_dev_linecard_type_index(const char *type)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(nsim_dev_linecard_supported_types); i++)
+		if (!strcmp(nsim_dev_linecard_supported_types[i], type))
+			return i;
+	return -1;
+}
+
 static unsigned int
 nsim_dev_linecard_port_count(struct nsim_dev_linecard *nsim_dev_linecard)
 {
@@ -64,6 +74,60 @@ nsim_dev_port_index(struct nsim_dev_linecard *nsim_dev_linecard,
 	       port_index;
 }
 
+static int
+nsim_dev_linecard_activate(struct nsim_dev_linecard *nsim_dev_linecard)
+{
+	struct nsim_dev_port *nsim_dev_port;
+
+	if (!nsim_dev_linecard->inserted || !nsim_dev_linecard->provisioned)
+		return 0;
+
+	if (nsim_dev_linecard->type_index !=
+	    nsim_dev_linecard->inserted_type_index)
+		return -EINVAL;
+
+	list_for_each_entry(nsim_dev_port, &nsim_dev_linecard->port_list,
+			    list_lc)
+		netif_carrier_on(nsim_dev_port->ns->netdev);
+
+	devlink_linecard_activate(nsim_dev_linecard->devlink_linecard);
+	return 0;
+}
+
+static int
+nsim_dev_linecard_insert(struct nsim_dev_linecard *nsim_dev_linecard,
+			 u32 inserted_type_index)
+{
+	if (nsim_dev_linecard->type_index != inserted_type_index)
+		return -EINVAL;
+
+	nsim_dev_linecard->inserted_type_index = inserted_type_index;
+	nsim_dev_linecard->inserted = true;
+	return nsim_dev_linecard_activate(nsim_dev_linecard);
+}
+
+static void
+nsim_dev_linecard_deactivate(struct nsim_dev_linecard *nsim_dev_linecard)
+{
+	struct nsim_dev_port *nsim_dev_port;
+
+	if (!nsim_dev_linecard->inserted)
+		return;
+	list_for_each_entry(nsim_dev_port, &nsim_dev_linecard->port_list,
+			    list_lc)
+		netif_carrier_off(nsim_dev_port->ns->netdev);
+}
+
+static void
+nsim_dev_linecard_remove(struct nsim_dev_linecard *nsim_dev_linecard)
+{
+	if (!nsim_dev_linecard->inserted)
+		return;
+	nsim_dev_linecard_deactivate(nsim_dev_linecard);
+	nsim_dev_linecard->inserted = false;
+	devlink_linecard_deactivate(nsim_dev_linecard->devlink_linecard);
+}
+
 static struct dentry *nsim_dev_ddir;
 
 #define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
@@ -299,6 +363,72 @@ static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
 	debugfs_remove_recursive(nsim_dev_port->ddir);
 }
 
+static ssize_t nsim_dev_linecard_inserted_type_read(struct file *file,
+						    char __user *data,
+						    size_t count, loff_t *ppos)
+{
+	struct nsim_dev_linecard *nsim_dev_linecard = file->private_data;
+	const char *type;
+
+	if (!nsim_dev_linecard->inserted)
+		return -EOPNOTSUPP;
+
+	type = nsim_dev_linecard_supported_types[nsim_dev_linecard->inserted_type_index];
+	return simple_read_from_buffer(data, count, ppos, type, strlen(type));
+}
+
+static ssize_t nsim_dev_linecard_inserted_type_write(struct file *file,
+						     const char __user *data,
+						     size_t count, loff_t *ppos)
+{
+	struct nsim_dev_linecard *nsim_dev_linecard = file->private_data;
+	char *buf;
+	int err;
+	int ret;
+
+	if (*ppos != 0)
+		return -EINVAL;
+	buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = simple_write_to_buffer(buf, count, ppos, data, count);
+	if (ret < 0)
+		goto free_buf;
+
+	buf[count - 1] = '\0';
+	if (strlen(buf) > 0) {
+		int inserted_type_index;
+
+		inserted_type_index = nsim_dev_linecard_type_index(buf);
+		if (inserted_type_index == -1)
+			return -EINVAL;
+
+		if (nsim_dev_linecard->inserted)
+			return -EBUSY;
+
+		err = nsim_dev_linecard_insert(nsim_dev_linecard,
+					       inserted_type_index);
+		if (err)
+			return err;
+	} else {
+		nsim_dev_linecard_remove(nsim_dev_linecard);
+	}
+	return count;
+
+free_buf:
+	kfree(buf);
+	return ret;
+}
+
+static const struct file_operations nsim_dev_linecard_inserted_type_fops = {
+	.open = simple_open,
+	.read = nsim_dev_linecard_inserted_type_read,
+	.write = nsim_dev_linecard_inserted_type_write,
+	.llseek = generic_file_llseek,
+	.owner = THIS_MODULE,
+};
+
 static ssize_t nsim_dev_linecard_type_read(struct file *file, char __user *data,
 					   size_t count, loff_t *ppos)
 {
@@ -334,6 +464,8 @@ nsim_dev_linecard_debugfs_init(struct nsim_dev *nsim_dev,
 	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
 		nsim_dev->nsim_bus_dev->dev.id);
 	debugfs_create_symlink("dev", nsim_dev_linecard->ddir, dev_link_name);
+	debugfs_create_file("inserted_type", 0600, nsim_dev_linecard->ddir,
+			    nsim_dev_linecard, &nsim_dev_linecard_inserted_type_fops);
 	debugfs_create_file("type", 0400, nsim_dev_linecard->ddir,
 			    nsim_dev_linecard, &nsim_dev_linecard_type_fops);
 
@@ -1110,6 +1242,8 @@ static void nsim_dev_linecard_provision_work(struct work_struct *work)
 	nsim_dev_linecard->provisioned = true;
 	devlink_linecard_provision_set(nsim_dev_linecard->devlink_linecard,
 				       nsim_dev_linecard->type_index);
+
+	nsim_dev_linecard_activate(nsim_dev_linecard);
 	return;
 
 err_port_del_all:
@@ -1138,6 +1272,8 @@ static void nsim_dev_linecard_unprovision_work(struct work_struct *work)
 	nsim_dev_linecard = container_of(work, struct nsim_dev_linecard,
 					 unprovision_work);
 
+	nsim_dev_linecard_deactivate(nsim_dev_linecard);
+
 	nsim_bus_dev = nsim_dev_linecard->nsim_dev->nsim_bus_dev;
 	nsim_dev_linecard->provisioned = false;
 	devlink_linecard_provision_clear(nsim_dev_linecard->devlink_linecard);
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index 01edff939c3a..8ee036e00a56 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -203,6 +203,8 @@ struct nsim_dev_linecard {
 	struct dentry *ddir;
 	bool provisioned;
 	u32 type_index;
+	bool inserted;
+	u32 inserted_type_index;
 	struct work_struct provision_work;
 	struct work_struct unprovision_work;
 };
-- 
2.26.2

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ