Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'netdev_print'

Veaceslav Falico says:

====================
net: print net_device name/state more often

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.

v2->v3:
Correct the string for _UNINITIALIZED and warn on a bad reg_state,
per Joe Perches's comments.

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.
====================

Signed-off-by: Veaceslav Falico <vfalico@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

+29 -9
+19 -3
include/linux/netdevice.h
··· 3383 3383 3384 3384 static inline const char *netdev_name(const struct net_device *dev) 3385 3385 { 3386 - if (dev->reg_state != NETREG_REGISTERED) 3387 - return "(unregistered net_device)"; 3386 + if (!dev->name[0] || strchr(dev->name, '%')) 3387 + return "(unnamed net_device)"; 3388 3388 return dev->name; 3389 + } 3390 + 3391 + static inline const char *netdev_reg_state(const struct net_device *dev) 3392 + { 3393 + switch (dev->reg_state) { 3394 + case NETREG_UNINITIALIZED: return " (uninitialized)"; 3395 + case NETREG_REGISTERED: return ""; 3396 + case NETREG_UNREGISTERING: return " (unregistering)"; 3397 + case NETREG_UNREGISTERED: return " (unregistered)"; 3398 + case NETREG_RELEASED: return " (released)"; 3399 + case NETREG_DUMMY: return " (dummy)"; 3400 + } 3401 + 3402 + WARN_ONCE(1, "%s: unknown reg_state %d\n", dev->name, dev->reg_state); 3403 + return " (unknown)"; 3389 3404 } 3390 3405 3391 3406 __printf(3, 4) ··· 3459 3444 * file/line information and a backtrace. 3460 3445 */ 3461 3446 #define netdev_WARN(dev, format, args...) \ 3462 - WARN(1, "netdevice: %s\n" format, netdev_name(dev), ##args) 3447 + WARN(1, "netdevice: %s%s\n" format, netdev_name(dev), \ 3448 + netdev_reg_state(dev), ##args) 3463 3449 3464 3450 /* netif printk helpers, similar to netdev_printk */ 3465 3451
+5 -3
lib/dynamic_debug.c
··· 614 614 char buf[PREFIX_SIZE]; 615 615 616 616 res = dev_printk_emit(7, dev->dev.parent, 617 - "%s%s %s %s: %pV", 617 + "%s%s %s %s%s: %pV", 618 618 dynamic_emit_prefix(descriptor, buf), 619 619 dev_driver_string(dev->dev.parent), 620 620 dev_name(dev->dev.parent), 621 - netdev_name(dev), &vaf); 621 + netdev_name(dev), netdev_reg_state(dev), 622 + &vaf); 622 623 } else if (dev) { 623 - res = printk(KERN_DEBUG "%s: %pV", netdev_name(dev), &vaf); 624 + res = printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev), 625 + netdev_reg_state(dev), &vaf); 624 626 } else { 625 627 res = printk(KERN_DEBUG "(NULL net_device): %pV", &vaf); 626 628 }
+5 -3
net/core/dev.c
··· 6950 6950 if (dev && dev->dev.parent) { 6951 6951 r = dev_printk_emit(level[1] - '0', 6952 6952 dev->dev.parent, 6953 - "%s %s %s: %pV", 6953 + "%s %s %s%s: %pV", 6954 6954 dev_driver_string(dev->dev.parent), 6955 6955 dev_name(dev->dev.parent), 6956 - netdev_name(dev), vaf); 6956 + netdev_name(dev), netdev_reg_state(dev), 6957 + vaf); 6957 6958 } else if (dev) { 6958 - r = printk("%s%s: %pV", level, netdev_name(dev), vaf); 6959 + r = printk("%s%s%s: %pV", level, netdev_name(dev), 6960 + netdev_reg_state(dev), vaf); 6959 6961 } else { 6960 6962 r = printk("%s(NULL net_device): %pV", level, vaf); 6961 6963 }