All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@gmail.com>
To: netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Tom Gundersen <teg@jklm.no>,
	Johannes berg <johannes@sipsolutions.net>,
	linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ben Hutchings <ben@decadent.org.uk>,
	David Laight <David.Laight@aculab.com>,
	David Herrmann <dh.herrmann@gmail.com>
Subject: [PATCH v4 1/4] net: add name_assign_type netdev attribute
Date: Mon, 17 Mar 2014 19:01:35 +0100	[thread overview]
Message-ID: <1395079295-1325-1-git-send-email-dh.herrmann@gmail.com> (raw)
In-Reply-To: <1394990490-1076-2-git-send-email-dh.herrmann@gmail.com>

The name_assign_type attribute gives hints where the interface name of a
given net-device comes from. Three different values are currently defined:
  NET_NAME_ENUM:
    This is the default. The ifname is provided by the kernel with an
    enumerated suffix. Names may be reused and unstable.
  NET_NAME_USER:
    The ifname was provided by user-space during net-device setup.
  NET_NAME_RENAMED:
    The net-device has been renamed via RTNL. Once this type is set, it
    cannot change again.

This attribute comes in handy for reliable net-device names. If an ifname
is provided by user-space, we can safely assume that the naming-policy
avoids reuse and is stable. Only if it was set by the kernel, the
interfaces might need to be renamed.

The NET_NAME_RENAMED value allows us to detect whether some-one else
already renamed the device, in which case we shouldn't touch it again. The
NET_NAME_USER value allows us to detect whether some other naming policy
created the device, in which case there's no need to rename it.

The most significant use-case is to detect virtual wifi-P2P devices, which
are named by wpa_supplicant et al. We shouldn't rename them as wpas
already provides a proper naming-policy.

Note that this patch only provides the core infrastructure. The different
net-dev types need to be manually fixed to use NET_NAME_USER instead of
the default (NET_NAME_ENUM). NET_NAME_ENUM is the least restrictive,
though, so it seems safe to use it as fallback for non-converted net-dev
types.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Acked-by: Tom Gundersen <teg@jklm.no>
---
v3:
 - move constants to uapi header (Ben Hutchings)
v4:
 - avoid padding after "name_assign_type" by moving it around (David Laight)

 include/linux/netdevice.h      | 2 ++
 include/uapi/linux/netdevice.h | 4 ++++
 net/core/dev.c                 | 7 +++++++
 net/core/net-sysfs.c           | 2 ++
 net/core/rtnetlink.c           | 2 ++
 5 files changed, 17 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index b8d8c80..1d48cee 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -1376,6 +1376,8 @@ struct net_device {
 	struct kset		*queues_kset;
 #endif
 
+	unsigned char		name_assign_type; /* name assignment type */
+
 	bool			uc_promisc;
 	unsigned int		promiscuity;
 	unsigned int		allmulti;
diff --git a/include/uapi/linux/netdevice.h b/include/uapi/linux/netdevice.h
index 6b9500b..ea963e4 100644
--- a/include/uapi/linux/netdevice.h
+++ b/include/uapi/linux/netdevice.h
@@ -36,6 +36,10 @@
 /* Initial net device group. All devices belong to group 0 by default. */
 #define INIT_NETDEV_GROUP	0
 
+/* interface name assignment types (sysfs name_assign_type attribute) */
+#define NET_NAME_ENUM		0	/* enumerated by kernel (default) */
+#define NET_NAME_USER		1	/* provided by user-space */
+#define NET_NAME_RENAMED	2	/* renamed by user-space */
 
 
 /* Media selection options. */
diff --git a/net/core/dev.c b/net/core/dev.c
index 587f9fb..4a2b360 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1083,6 +1083,7 @@ static int dev_get_valid_name(struct net *net,
 int dev_change_name(struct net_device *dev, const char *newname)
 {
 	char oldname[IFNAMSIZ];
+	unsigned char old_assign_type;
 	int err = 0;
 	int ret;
 	struct net *net;
@@ -1109,10 +1110,14 @@ int dev_change_name(struct net_device *dev, const char *newname)
 		return err;
 	}
 
+	old_assign_type = dev->name_assign_type;
+	dev->name_assign_type = NET_NAME_RENAMED;
+
 rollback:
 	ret = device_rename(&dev->dev, dev->name);
 	if (ret) {
 		memcpy(dev->name, oldname, IFNAMSIZ);
+		dev->name_assign_type = old_assign_type;
 		write_seqcount_end(&devnet_rename_seq);
 		return ret;
 	}
@@ -1141,6 +1146,8 @@ rollback:
 			write_seqcount_begin(&devnet_rename_seq);
 			memcpy(dev->name, oldname, IFNAMSIZ);
 			memcpy(oldname, newname, IFNAMSIZ);
+			dev->name_assign_type = old_assign_type;
+			old_assign_type = NET_NAME_RENAMED;
 			goto rollback;
 		} else {
 			pr_err("%s: name change rollback failed: %d\n",
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index daed9a6..886403e 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -105,6 +105,7 @@ static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
 
 NETDEVICE_SHOW_RO(dev_id, fmt_hex);
 NETDEVICE_SHOW_RO(dev_port, fmt_dec);
+NETDEVICE_SHOW_RO(name_assign_type, fmt_dec);
 NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
 NETDEVICE_SHOW_RO(addr_len, fmt_dec);
 NETDEVICE_SHOW_RO(iflink, fmt_dec);
@@ -377,6 +378,7 @@ static struct attribute *net_class_attrs[] = {
 	&dev_attr_dev_port.attr,
 	&dev_attr_iflink.attr,
 	&dev_attr_ifindex.attr,
+	&dev_attr_name_assign_type.attr,
 	&dev_attr_addr_assign_type.attr,
 	&dev_attr_addr_len.attr,
 	&dev_attr_link_mode.attr,
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index fc122fd..f1bd0fe 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -1967,6 +1967,8 @@ replay:
 		}
 
 		dev->ifindex = ifm->ifi_index;
+		if (tb[IFLA_IFNAME])
+			dev->name_assign_type = NET_NAME_USER;
 
 		if (ops->newlink) {
 			err = ops->newlink(net, dev, tb, data);
-- 
1.9.0


  parent reply	other threads:[~2014-03-17 18:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-16 17:21 [PATCH v2 0/4] Provide netdev naming-policy via sysfs David Herrmann
2014-03-16 17:21 ` [PATCH v2 1/4] net: add name_assign_type netdev attribute David Herrmann
2014-03-16 19:17   ` Ben Hutchings
2014-03-16 21:53     ` David Herrmann
2014-03-17 17:03   ` [PATCH v3 " David Herrmann
2014-03-17 17:12     ` David Laight
2014-03-17 18:01   ` David Herrmann [this message]
2014-03-17 19:28     ` [PATCH v4 " David Miller
2014-03-17 19:28       ` David Miller
2014-03-16 17:21 ` [PATCH v2 2/4] mac80211: set NET_NAME_USER for user-space created ifs David Herrmann
2014-03-16 17:21   ` David Herrmann
2014-03-16 17:21 ` [PATCH v2 3/4] ath6kl: set NET_NAME_USER for P2P ifs David Herrmann
2014-03-16 17:21 ` [PATCH v2 4/4] brcmfmac: " David Herrmann
  -- strict thread matches above, loose matches on Subject: below --
2014-03-17 19:42 [PATCH v4 0/4] Provide netdev naming-policy via sysfs David Herrmann
2014-03-17 19:42 ` [PATCH v4 1/4] net: add name_assign_type netdev attribute David Herrmann

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=1395079295-1325-1-git-send-email-dh.herrmann@gmail.com \
    --to=dh.herrmann@gmail.com \
    --cc=David.Laight@aculab.com \
    --cc=ben@decadent.org.uk \
    --cc=davem@davemloft.net \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=teg@jklm.no \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.