Skip to contentSkip to conversation

net: print net_device name/state more often

PATCH Superseded Veaceslav Falico 4 participants 15 messages Jul 17, 2014

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: print net_device name/state more often

Hi,

Currently we use net_device->name only if it's the NETREG_REGISTERED reg_state, otherwise we return "(unregistered device)".

However, we always populate net_device->name on creation, so it's always available to us for use. The only caveat is that we might have a name like "eth%d", in which case we cannot use it as it might change in the future.

Also, the net_device might not be NETREG_UNREGISTERED when the function is called (_UNINITIALIZED, _UNREGISTERING, _RELEASED, _DUMMY), so it's misleading.

So, a better way would be to always return the dev->name in netdev_name(), unless it's in the form of "eth%d" or it's empty, then return "unnamed net_device". This way we'll always return the name in NETREG_REGISTERED reg_state, and also return it in other states, when possible.

Also, to be more verbose on non-NETREG_REGISTERED states, add a function netdev_reg_state(), which returns a string describing the state, and use it in netdev_printk()-related functions. If the dev is in NETREG_REGISTERED state then a void string is regurned and, thus, nothing changes.

After these two patches we'll have the same behaviour in the usual cases, and more verbose in non-standardad/buggy ones.

v1->v2:
As Tom Gundersen suggested, there might be a case when we have an empty string as a name for a device, so account this also and return "unnamed device" for that case too.

CC "David S. Miller" <[email protected]>
CC Jason Baron <[email protected]>
CC Eric Dumazet <[email protected]>
CC Vlad Yasevich <[email protected]>
CC stephen hemminger <[email protected]>
CC Jerry Chu <[email protected]>
CC Ben Hutchings <[email protected]>
CC Tom Gundersen <[email protected]>
---
 include/linux/netdevice.h | 21 ++++++++++++++++++---
 lib/dynamic_debug.c       |  8 +++++---
 net/core/dev.c            |  8 +++++---
 3 files changed, 28 insertions(+), 9 deletions(-)
#1 lore raw

PATCH v2 1/2 net: use dev->name in netdev_pr* when it's available

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

However, dev->name is always populated on creation, so we can easily use it.

There are two cases when there's no real name - when it's an empty string or when the name is in form of "eth%d", then netdev_name() returns "unnamed net_device".

CC "David S. Miller" <[email protected]>
CC Tom Gundersen <[email protected]>

---

Notes:
v1->v2:
Also account for an empty string, as Tom Gundersen suggested.

 include/linux/netdevice.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/netdevice.h +2 -2
index 15ed750..70256aa 100644
@@ -3383,8 +3383,8 @@ extern struct pernet_operations __net_initdata loopback_net_ops;
 
 static inline const char *netdev_name(const struct net_device *dev)
 {
-	if (dev->reg_state != NETREG_REGISTERED)
-		return "(unregistered net_device)";
+	if (!dev->name[0] || strchr(dev->name, '%'))
+		return "(unnamed net_device)";
 	return dev->name;
 }
 
Signature · 1 line
1.8.4
#2 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 27 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

However, dev->name is always populated on creation, so we can easily use it.

There are two cases when there's no real name - when it's an empty string or when the name is in form of "eth%d", then netdev_name() returns "unnamed net_device".

CC "David S. Miller" <[email protected]>
CC Tom Gundersen <[email protected]>

---

Notes:
v1->v2:
Also account for an empty string, as Tom Gundersen suggested.

 include/linux/netdevice.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/netdevice.h +0 -0
index 15ed750..70256aa 100644

include/linux/netdevice.h extern struct pernet_operations __net_initdata loopback_net_ops; · line 3390

Quoted 10 lines
include/linux/netdevice.h +2 -2
@@ -3383,8 +3383,8 @@ extern struct pernet_operations __net_initdata loopback_net_ops;
 
 static inline const char *netdev_name(const struct net_device *dev)
 {
-	if (dev->reg_state != NETREG_REGISTERED)
-		return "(unregistered net_device)";
+	if (!dev->name[0] || strchr(dev->name, '%'))
+		return "(unnamed net_device)";
 	return dev->name;
 }
 

Maybe this should not be inline and become something like:

const char *netdev_name(const struct net_device *dev)

{
	if (dev->reg_state == NETREG_REGISTERED)
		return dev->name;

	if (!dev->name[0])
		return "(unnamed net_device)";

	if (!strchr(dev->name, '%'))
		return "(unregistered net_device)";

	return dev->name;
}

EXPORT_SYMBOL(netdev_name);

#3 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 29 lines · Joe Perches
Quoted 28 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

However, dev->name is always populated on creation, so we can easily use it.

There are two cases when there's no real name - when it's an empty string or when the name is in form of "eth%d", then netdev_name() returns "unnamed net_device".

CC "David S. Miller" <[email protected]>
CC Tom Gundersen <[email protected]>

---

Notes:
v1->v2:
Also account for an empty string, as Tom Gundersen suggested.

 include/linux/netdevice.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/netdevice.h +0 -0
index 15ed750..70256aa 100644
@@ -3383,8 +3383,8 @@ extern struct pernet_operations __net_initdata loopback_net_ops;
 
Quoted 11 lines

include/linux/netdevice.h extern struct pernet_operations __net_initdata loopback_net_ops; · line 3390

Quoted 9 lines
include/linux/netdevice.h +2 -2
 static inline const char *netdev_name(const struct net_device *dev)
 {
-	if (dev->reg_state != NETREG_REGISTERED)
-		return "(unregistered net_device)";
+	if (!dev->name[0] || strchr(dev->name, '%'))
+		return "(unnamed net_device)";
 	return dev->name;
 }
 

Maybe this should not be inline and become something like:

It will miss the states then, when it's not NETREG_REGISTERED.

Quoted 17 lines

const char *netdev_name(const struct net_device *dev)

{
	if (dev->reg_state == NETREG_REGISTERED)
		return dev->name;

	if (!dev->name[0])
		return "(unnamed net_device)";

	if (!strchr(dev->name, '%'))
		return "(unregistered net_device)";

	return dev->name;
}

EXPORT_SYMBOL(netdev_name);

#4 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 4 lines · Veaceslav Falico
Quoted 3 lines · Joe Perches
Quoted 2 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

[]

Quoted 3 lines
Quoted 1 line

Maybe this should not be inline and become something like:

It will miss the states then, when it's not NETREG_REGISTERED.

If it's registered, it has a valid name via
dev_get_valid_name() doesn't it?

Quoted 14 lines
Quoted 14 lines

const char *netdev_name(const struct net_device *dev)

{
	if (dev->reg_state == NETREG_REGISTERED)
		return dev->name;

	if (!dev->name[0])
		return "(unnamed net_device)";

	if (!strchr(dev->name, '%'))
		return "(unregistered net_device)";

	return dev->name;
}

EXPORT_SYMBOL(netdev_name);

#5 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 5 lines · Joe Perches
Quoted 4 lines · Veaceslav Falico
Quoted 3 lines · Joe Perches
Quoted 2 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

Quoted 7 lines

[]

Quoted 3 lines
Quoted 1 line

Maybe this should not be inline and become something like:

It will miss the states then, when it's not NETREG_REGISTERED.

If it's registered, it has a valid name via
dev_get_valid_name() doesn't it?

Yes, I'm speaking about the NETREG_* states when it's not registered.

i.e.:

Jul 17 13:35:29 darkmag kernel: [  602.686489] bond2 (unregistering): Released all slaves

that's with my patchset, when the bond device is unregistering (NETREG_UNREGISTERING). With your patch it would be only "bond2: Released all slaves".

As the most time the device is in the NETREG_REGISTERED state, there will be no differencies in the output (with either my or your approach), however in less-common states/codepaths it will also output its state, which might be quite valuable when analyzing the logs.

Quoted 18 lines
Quoted 15 lines
Quoted 15 lines

const char *netdev_name(const struct net_device *dev)

{
	if (dev->reg_state == NETREG_REGISTERED)
		return dev->name;

	if (!dev->name[0])
		return "(unnamed net_device)";

	if (!strchr(dev->name, '%'))
		return "(unregistered net_device)";

	return dev->name;
}

EXPORT_SYMBOL(netdev_name);

#6 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 11 lines · Veaceslav Falico
Quoted 10 lines · Joe Perches
Quoted 4 lines · Veaceslav Falico
Quoted 3 lines · Joe Perches
Quoted 2 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

[]

Quoted 3 lines
Quoted 1 line

Maybe this should not be inline and become something like:

It will miss the states then, when it's not NETREG_REGISTERED.

Quoted 8 lines
Quoted 2 lines

If it's registered, it has a valid name via
dev_get_valid_name() doesn't it?

Yes, I'm speaking about the NETREG_* states when it's not registered.

i.e.:

Jul 17 13:35:29 darkmag kernel: [  602.686489] bond2 (unregistering): Released all slaves

OK, I hadn't read the patch where netdev_reg_state was emitted.

Still, it's probably smaller overall code to uninline it now.

#7 lore raw

Re: [PATCH v2 net-next 1/2] net: use dev->name in netdev_pr* when it's available

Quoted 17 lines · Joe Perches
Quoted 16 lines · Veaceslav Falico
Quoted 5 lines · Joe Perches
Quoted 4 lines · Veaceslav Falico
Quoted 3 lines · Joe Perches
Quoted 2 lines · Veaceslav Falico

netdev_name() returns dev->name only when the net_device is in NETREG_REGISTERED state.

Quoted 7 lines

[]

Quoted 3 lines
Quoted 1 line

Maybe this should not be inline and become something like:

It will miss the states then, when it's not NETREG_REGISTERED.

If it's registered, it has a valid name via
dev_get_valid_name() doesn't it?

Yes, I'm speaking about the NETREG_* states when it's not registered.

Quoted 7 lines
Quoted 3 lines

i.e.:

Jul 17 13:35:29 darkmag kernel: [  602.686489] bond2 (unregistering): Released all slaves

OK, I hadn't read the patch where netdev_reg_state was emitted.

Still, it's probably smaller overall code to uninline it now.

I don't really care about that, and it's used only in several non-inlined functions anyway, so the difference would be minimal, if any. I can easily re-spin it unlined, however I don't really see a point in doing so :).

#8 lore raw

PATCH v2 2/2 net: print net_device reg_state in netdev_* unless it's registered

In reply to Veaceslav Falico

This way we'll always know in what status the device is, unless it's running normally (i.e. NETDEV_REGISTERED).

CC "David S. Miller" <[email protected]>
CC Jason Baron <[email protected]>
CC Eric Dumazet <[email protected]>
CC Vlad Yasevich <[email protected]>
CC stephen hemminger <[email protected]>
CC Jerry Chu <[email protected]>
CC Ben Hutchings <[email protected]>
---
 include/linux/netdevice.h | 17 ++++++++++++++++-
 lib/dynamic_debug.c       |  8 +++++---
 net/core/dev.c            |  8 +++++---
 3 files changed, 26 insertions(+), 7 deletions(-)
include/linux/netdevice.h +16 -1
index 70256aa..d868858 100644
@@ -3388,6 +3388,20 @@ static inline const char *netdev_name(const struct net_device *dev)
 	return dev->name;
 }
 
+static inline const char *netdev_reg_state(const struct net_device *dev)
+{
+	switch (dev->reg_state) {
+	case NETREG_UNINITIALIZED: return " (unregistered)";
+	case NETREG_REGISTERED: return "";
+	case NETREG_UNREGISTERING: return " (unregistering)";
+	case NETREG_UNREGISTERED: return " (unregistered)";
+	case NETREG_RELEASED: return " (released)";
+	case NETREG_DUMMY: return " (dummy)";
+	}
+
+	return " (unknown)";
+}
+
 __printf(3, 4)
 int netdev_printk(const char *level, const struct net_device *dev,
 		  const char *format, ...);
@@ -3444,7 +3458,8 @@ do {								\
  * file/line information and a backtrace.
  */
 #define netdev_WARN(dev, format, args...)			\
-	WARN(1, "netdevice: %s\n" format, netdev_name(dev), ##args)
+	WARN(1, "netdevice: %s%s\n" format, netdev_name(dev),	\
+	     netdev_reg_state(dev), ##args)
 
 /* netif printk helpers, similar to netdev_printk */
 
lib/dynamic_debug.c +5 -3
index 7288e38..c9afbe2 100644
@@ -614,13 +614,15 @@ int __dynamic_netdev_dbg(struct _ddebug *descriptor,
 		char buf[PREFIX_SIZE];
 
 		res = dev_printk_emit(7, dev->dev.parent,
-				      "%s%s %s %s: %pV",
+				      "%s%s %s %s%s: %pV",
 				      dynamic_emit_prefix(descriptor, buf),
 				      dev_driver_string(dev->dev.parent),
 				      dev_name(dev->dev.parent),
-				      netdev_name(dev), &vaf);
+				      netdev_name(dev), netdev_reg_state(dev),
+				      &vaf);
 	} else if (dev) {
-		res = printk(KERN_DEBUG "%s: %pV", netdev_name(dev), &vaf);
+		res = printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev),
+			     netdev_reg_state(dev), &vaf);
 	} else {
 		res = printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
 	}
net/core/dev.c +5 -3
index 239722a..81d6101 100644
@@ -6950,12 +6950,14 @@ static int __netdev_printk(const char *level, const struct net_device *dev,
 	if (dev && dev->dev.parent) {
 		r = dev_printk_emit(level[1] - '0',
 				    dev->dev.parent,
-				    "%s %s %s: %pV",
+				    "%s %s %s%s: %pV",
 				    dev_driver_string(dev->dev.parent),
 				    dev_name(dev->dev.parent),
-				    netdev_name(dev), vaf);
+				    netdev_name(dev), netdev_reg_state(dev),
+				    vaf);
 	} else if (dev) {
-		r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
+		r = printk("%s%s%s: %pV", level, netdev_name(dev),
+			   netdev_reg_state(dev), vaf);
 	} else {
 		r = printk("%s(NULL net_device): %pV", level, vaf);
 	}
Signature · 1 line
1.8.4
#9 lore raw

Re: [PATCH v2 net-next 2/2] net: print net_device reg_state in netdev_* unless it's registered

Quoted 2 lines · Veaceslav Falico

This way we'll always know in what status the device is, unless it's running normally (i.e. NETDEV_REGISTERED).

[]

include/linux/netdevice.h

Quoted 1 line
include/linux/netdevice.h +0 -0

[]

include/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 3394

Quoted 8 lines
include/linux/netdevice.h +4 -0
@@ -3388,6 +3388,20 @@ static inline const char *netdev_name(const struct net_device *dev)
 	return dev->name;
 }
 
+static inline const char *netdev_reg_state(const struct net_device *dev)
+{
+	switch (dev->reg_state) {
+	case NETREG_UNINITIALIZED: return " (unregistered)";

Why not " (uninitialized)"?

include/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 3402

Quoted 8 lines
include/linux/netdevice.h +8 -0
+	case NETREG_REGISTERED: return "";
+	case NETREG_UNREGISTERING: return " (unregistering)";
+	case NETREG_UNREGISTERED: return " (unregistered)";
+	case NETREG_RELEASED: return " (released)";
+	case NETREG_DUMMY: return " (dummy)";
+	}
+
+	return " (unknown)";

Shouldn't this " (unknown)" have stronger text
and use a WARN_ON_ONCE?

I'd put this in net/core/dev.c and make it not be static inline.

#10 lore raw

Re: [PATCH v2 net-next 2/2] net: print net_device reg_state in netdev_* unless it's registered

Quoted 10 lines · Joe Perches
Quoted 2 lines · Veaceslav Falico

This way we'll always know in what status the device is, unless it's running normally (i.e. NETDEV_REGISTERED).

[]

include/linux/netdevice.h

Quoted 1 line
include/linux/netdevice.h +0 -0

[]

Quoted 4 lines
include/linux/netdevice.h +0 -0
@@ -3388,6 +3388,20 @@ static inline const char *netdev_name(const struct net_device *dev)
 	return dev->name;
 }
 
Quoted 6 lines

include/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 3394

Quoted 4 lines
include/linux/netdevice.h +4 -0
+static inline const char *netdev_reg_state(const struct net_device *dev)
+{
+	switch (dev->reg_state) {
+	case NETREG_UNINITIALIZED: return " (unregistered)";

Why not " (uninitialized)"?

Good one, thank you, missed it somehow. Will send v2.

Quoted 12 lines

include/linux/netdevice.h static inline const char *netdev_name(const struct net_device *dev) · line 3402

Quoted 9 lines
include/linux/netdevice.h +8 -0
+	case NETREG_REGISTERED: return "";
+	case NETREG_UNREGISTERING: return " (unregistering)";
+	case NETREG_UNREGISTERED: return " (unregistered)";
+	case NETREG_RELEASED: return " (released)";
+	case NETREG_DUMMY: return " (dummy)";
+	}
+
+	return " (unknown)";

Shouldn't this " (unknown)" have stronger text
and use a WARN_ON_ONCE?

Hrm, I don't remember why, but I've specifically dropped the warning here. Now it seems like a good idea, so I'll add it here.

Quoted 2 lines

I'd put this in net/core/dev.c and make it not be static inline.

Again, I don't think it's really needed, as it's used in 4 functions (which aren't inline), so the benefits are minimal, if any.

I'll rather keep them inline, I guess...

Quoted 2 lines
#11 lore raw
In reply to Veaceslav Falico

From: Veaceslav Falico

Quoted 6 lines

Currently we use net_device->name only if it's the NETREG_REGISTERED reg_state, otherwise we return "(unregistered device)".

However, we always populate net_device->name on creation, so it's always available to us for use. The only caveat is that we might have a name like "eth%d", in which case we cannot use it as it might change in the future.

While you've got this code out on the operating table what about:
1) Tracing the renames into the kernel message buffer.
2) Including the original name in some of the kernel traces.

I've been trying to diagnose some issues on a system that renames its interfaces, and it can get tricky working out which interface messages refer to.

David

#12 lore raw
Quoted 10 lines · David Laight

From: Veaceslav Falico

Quoted 6 lines

Currently we use net_device->name only if it's the NETREG_REGISTERED reg_state, otherwise we return "(unregistered device)".

However, we always populate net_device->name on creation, so it's always available to us for use. The only caveat is that we might have a name like "eth%d", in which case we cannot use it as it might change in the future.

While you've got this code out on the operating table what about:
1) Tracing the renames into the kernel message buffer.

udev will log to dmesg what renames it does. I agree it would make more sense to do this from the kernel though, as there are other ways to rename interfaces than through udev.

Cheers,

Tom

Quoted 5 lines

2) Including the original name in some of the kernel traces.

I've been trying to diagnose some issues on a system that renames its interfaces, and it can get tricky working out which interface messages refer to.

#13 lore raw

From: Tom Gundersen

Quoted 1 line · David Laight

...

Quoted 4 lines
Quoted 2 lines

While you've got this code out on the operating table what about:
1) Tracing the renames into the kernel message buffer.

udev will log to dmesg what renames it does.

Not on the systems I'm looking at (ubuntu 13.10 server). Possibly the trace level is such that they get dropped.

David

#14 lore raw
In reply to David Laight
Quoted 9 lines · David Laight

From: Veaceslav Falico

Quoted 6 lines

Currently we use net_device->name only if it's the NETREG_REGISTERED reg_state, otherwise we return "(unregistered device)".

However, we always populate net_device->name on creation, so it's always available to us for use. The only caveat is that we might have a name like "eth%d", in which case we cannot use it as it might change in the future.

While you've got this code out on the operating table what about:

I don't think these ideas fit into the current patchset, as it's somehow unrelated to the "lets give the real name/state when possible".

However, I like them, see below.

Quoted 1 line

1) Tracing the renames into the kernel message buffer.

I think you can enable it already. dev_change_name() (net's function to change net_device name) calls device_rename(), which handles the device renaming, and it has:

1805 dev_dbg(dev, "renaming to %s\n", new_name);

It can be easily enabled if you have dynamic debug compiled in.

However, indeed, it might be a good info to watch for, and a pretty simple patch, which might be useful.

net/core/dev.c +0 -0
index 239722a..d14ebf0 100644
@@ -1113,6 +1113,9 @@ int dev_change_name(struct net_device *dev, const char *newname)
		return err;
	}

+ if (oldname[0] && !strchr(oldname, '%'))
+ netdev_info(dev, "renamed from %s\n", oldname);
+

	old_assign_type = dev->name_assign_type;
	dev->name_assign_type = NET_NAME_RENAMED;
Quoted 1 line

2) Including the original name in some of the kernel traces.

The only possible use-case of that, from what I can see (with the patch from 1) ) - is in the renaming functions themselves, which have already quite a few output sources and, tbh, it's a really rare situation to implenment tracking of renames in-kernel.

Quoted 9 lines

I've been trying to diagnose some issues on a system that renames its interfaces, and it can get tricky working out which interface messages refer to.

David