diff mbox

[net-next,v3,3/3] net: export physical port id via sysfs

Message ID 1374498317-6442-4-git-send-email-jiri@resnulli.us
State Changes Requested, archived
Delegated to: David Miller
Headers show

Commit Message

Jiri Pirko July 22, 2013, 1:05 p.m. UTC
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
Acked-by: Ben Hutchings <bhutchings@solarflare.com>
Signed-off-by: Narendra K <narendra_k@dell.com>
---
 net/core/net-sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

Comments

David Miller July 24, 2013, 11:01 p.m. UTC | #1
From: Jiri Pirko <jiri@resnulli.us>
Date: Mon, 22 Jul 2013 15:05:17 +0200

> +static size_t _format_port_id(char *buf, int buflen,
> +			      const unsigned char *id, int len)
> +{
> +	int i;
> +	char *cp = buf;
> +
> +	for (i = 0; i < len; i++)
> +		cp += scnprintf(cp, buflen - (cp - buf), "%02x", id[i]);
> +	return cp - buf;
> +}

This is "%phN", please use the printf strings we have support for
in the kernel instead of reimplementing them :-)
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 981fed3..56b4635 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -334,6 +334,47 @@  static ssize_t store_group(struct device *dev, struct device_attribute *attr,
 	return netdev_store(dev, attr, buf, len, change_group);
 }
 
+static size_t _format_port_id(char *buf, int buflen,
+			      const unsigned char *id, int len)
+{
+	int i;
+	char *cp = buf;
+
+	for (i = 0; i < len; i++)
+		cp += scnprintf(cp, buflen - (cp - buf), "%02x", id[i]);
+	return cp - buf;
+}
+
+ssize_t sysfs_format_port_id(char *buf, const unsigned char *id, int len)
+{
+	size_t l;
+
+	l = _format_port_id(buf, PAGE_SIZE, id, len);
+	l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
+	return (ssize_t)l;
+}
+
+static ssize_t show_phys_port_id(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	struct net_device *netdev = to_net_dev(dev);
+	ssize_t ret = -EINVAL;
+
+	if (!rtnl_trylock())
+		return restart_syscall();
+
+	if (dev_isalive(netdev)) {
+		struct netdev_phys_port_id ppid;
+
+		ret = dev_get_phys_port_id(netdev, &ppid);
+		if (!ret)
+			ret = sysfs_format_port_id(buf, ppid.id, ppid.id_len);
+	}
+	rtnl_unlock();
+
+	return ret;
+}
+
 static struct device_attribute net_class_attributes[] = {
 	__ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
 	__ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
@@ -355,6 +396,7 @@  static struct device_attribute net_class_attributes[] = {
 	__ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
 	       store_tx_queue_len),
 	__ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
+	__ATTR(phys_port_id, S_IRUGO, show_phys_port_id, NULL),
 	{}
 };