* [PATCH] Inline net_device_stats
@ 2007-03-28 9:16 Rusty Russell
2007-03-28 15:52 ` Stephen Hemminger
0 siblings, 1 reply; 4+ messages in thread
From: Rusty Russell @ 2007-03-28 9:16 UTC (permalink / raw)
To: netdev
Hi all,
Does something like this make sense for future drivers?
Cheers,
Rusty.
===
Network drivers which keep stats allocate their own stats structure
then write a get_stats() function to return them. It would be nice if
this were done by default.
1) Add a new "stats" field to "struct net_device".
2) Add a new feature field to say "this driver uses the internal one"
3) Have a default "get_stats" which returns NULL if that feature not set.
4) Change callers to check result of get_stats call for NULL, not if
->get_stats is set.
This should not break backwards compatibility with older drivers, yet
allow modern drivers to shed some boilerplate code.
Lightly tested: works for a modified lguest network driver.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
0 files changed
diff -r 1ccab0a087b7 arch/s390/appldata/appldata_net_sum.c
--- a/arch/s390/appldata/appldata_net_sum.c Tue Mar 27 13:46:10 2007 +1000
+++ b/arch/s390/appldata/appldata_net_sum.c Tue Mar 27 14:28:47 2007 +1000
@@ -108,10 +108,10 @@ static void appldata_get_net_sum_data(vo
collisions = 0;
read_lock(&dev_base_lock);
for (dev = dev_base; dev != NULL; dev = dev->next) {
- if (dev->get_stats == NULL) {
+ stats = dev->get_stats(dev);
+ if (stats == NULL) {
continue;
}
- stats = dev->get_stats(dev);
rx_packets += stats->rx_packets;
tx_packets += stats->tx_packets;
rx_bytes += stats->rx_bytes;
diff -r 1ccab0a087b7 drivers/net/bonding/bond_main.c
--- a/drivers/net/bonding/bond_main.c Tue Mar 27 13:46:10 2007 +1000
+++ b/drivers/net/bonding/bond_main.c Tue Mar 27 14:29:08 2007 +1000
@@ -3621,9 +3621,8 @@ static struct net_device_stats *bond_get
read_lock_bh(&bond->lock);
bond_for_each_slave(bond, slave, i) {
- if (slave->dev->get_stats) {
- sstats = slave->dev->get_stats(slave->dev);
-
+ sstats = slave->dev->get_stats(slave->dev);
+ if (sstats) {
stats->rx_packets += sstats->rx_packets;
stats->rx_bytes += sstats->rx_bytes;
stats->rx_errors += sstats->rx_errors;
diff -r 1ccab0a087b7 drivers/parisc/led.c
--- a/drivers/parisc/led.c Tue Mar 27 13:46:10 2007 +1000
+++ b/drivers/parisc/led.c Tue Mar 27 14:29:17 2007 +1000
@@ -372,9 +372,9 @@ static __inline__ int led_get_net_activi
continue;
if (LOOPBACK(in_dev->ifa_list->ifa_local))
continue;
- if (!dev->get_stats)
+ stats = dev->get_stats(dev);
+ if (!stats)
continue;
- stats = dev->get_stats(dev);
rx_total += stats->rx_packets;
tx_total += stats->tx_packets;
}
diff -r 1ccab0a087b7 include/linux/netdevice.h
--- a/include/linux/netdevice.h Tue Mar 27 13:46:10 2007 +1000
+++ b/include/linux/netdevice.h Tue Mar 27 14:21:09 2007 +1000
@@ -325,6 +325,7 @@ struct net_device
#define NETIF_F_VLAN_CHALLENGED 1024 /* Device cannot handle VLAN packets */
#define NETIF_F_GSO 2048 /* Enable software GSO. */
#define NETIF_F_LLTX 4096 /* LockLess TX */
+#define NETIF_F_INTERNAL_STATS 8192 /* Use stats structure in net_device */
/* Segmentation offload features */
#define NETIF_F_GSO_SHIFT 16
@@ -349,6 +350,7 @@ struct net_device
struct net_device_stats* (*get_stats)(struct net_device *dev);
+ struct net_device_stats stats;
#ifdef CONFIG_WIRELESS_EXT
/* List of functions to handle Wireless Extensions (instead of ioctl).
diff -r 1ccab0a087b7 net/core/dev.c
--- a/net/core/dev.c Tue Mar 27 13:46:10 2007 +1000
+++ b/net/core/dev.c Tue Mar 27 14:30:05 2007 +1000
@@ -825,7 +825,6 @@ static int default_rebuild_header(struct
return 1;
}
-
/**
* dev_open - prepare an interface for use.
* @dev: device to open
@@ -2120,9 +2119,9 @@ void dev_seq_stop(struct seq_file *seq,
static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
{
- if (dev->get_stats) {
- struct net_device_stats *stats = dev->get_stats(dev);
-
+ struct net_device_stats *stats = dev->get_stats(dev);
+
+ if (stats) {
seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
"%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
dev->name, stats->rx_bytes, stats->rx_packets,
@@ -3146,6 +3145,13 @@ out:
mutex_unlock(&net_todo_run_mutex);
}
+static struct net_device_stats *maybe_internal_stats(struct net_device *dev)
+{
+ if (dev->features & NETIF_F_INTERNAL_STATS)
+ return &dev->stats;
+ return NULL;
+}
+
/**
* alloc_netdev - allocate network device
* @sizeof_priv: size of private data to allocate space for
@@ -3181,6 +3187,7 @@ struct net_device *alloc_netdev(int size
if (sizeof_priv)
dev->priv = netdev_priv(dev);
+ dev->get_stats = maybe_internal_stats;
setup(dev);
strcpy(dev->name, name);
return dev;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Inline net_device_stats
2007-03-28 9:16 [PATCH] Inline net_device_stats Rusty Russell
@ 2007-03-28 15:52 ` Stephen Hemminger
2007-03-28 21:26 ` David Miller
2007-03-29 0:00 ` Rusty Russell
0 siblings, 2 replies; 4+ messages in thread
From: Stephen Hemminger @ 2007-03-28 15:52 UTC (permalink / raw)
To: Rusty Russell; +Cc: netdev
Rusty Russell wrote:
> Hi all,
>
> Does something like this make sense for future drivers?
>
> Cheers,
> Rusty.
> ===
> Network drivers which keep stats allocate their own stats structure
> then write a get_stats() function to return them. It would be nice if
> this were done by default.
>
> 1) Add a new "stats" field to "struct net_device".
> 2) Add a new feature field to say "this driver uses the internal one"
> 3) Have a default "get_stats" which returns NULL if that feature not set.
> 4) Change callers to check result of get_stats call for NULL, not if
> ->get_stats is set.
>
> This should not break backwards compatibility with older drivers, yet
> allow modern drivers to shed some boilerplate code.
>
> Lightly tested: works for a modified lguest network driver.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
> ---
> 0 files changed
>
> diff -r 1ccab0a087b7 arch/s390/appldata/appldata_net_sum.c
> --- a/arch/s390/appldata/appldata_net_sum.c Tue Mar 27 13:46:10 2007 +1000
> +++ b/arch/s390/appldata/appldata_net_sum.c Tue Mar 27 14:28:47 2007 +1000
> @@ -108,10 +108,10 @@ static void appldata_get_net_sum_data(vo
> collisions = 0;
> read_lock(&dev_base_lock);
> for (dev = dev_base; dev != NULL; dev = dev->next) {
> - if (dev->get_stats == NULL) {
> + stats = dev->get_stats(dev);
> + if (stats == NULL) {
> continue;
> }
> - stats = dev->get_stats(dev);
> rx_packets += stats->rx_packets;
> tx_packets += stats->tx_packets;
> rx_bytes += stats->rx_bytes;
> diff -r 1ccab0a087b7 drivers/net/bonding/bond_main.c
> --- a/drivers/net/bonding/bond_main.c Tue Mar 27 13:46:10 2007 +1000
> +++ b/drivers/net/bonding/bond_main.c Tue Mar 27 14:29:08 2007 +1000
> @@ -3621,9 +3621,8 @@ static struct net_device_stats *bond_get
> read_lock_bh(&bond->lock);
>
> bond_for_each_slave(bond, slave, i) {
> - if (slave->dev->get_stats) {
> - sstats = slave->dev->get_stats(slave->dev);
> -
> + sstats = slave->dev->get_stats(slave->dev);
> + if (sstats) {
> stats->rx_packets += sstats->rx_packets;
> stats->rx_bytes += sstats->rx_bytes;
> stats->rx_errors += sstats->rx_errors;
> diff -r 1ccab0a087b7 drivers/parisc/led.c
> --- a/drivers/parisc/led.c Tue Mar 27 13:46:10 2007 +1000
> +++ b/drivers/parisc/led.c Tue Mar 27 14:29:17 2007 +1000
> @@ -372,9 +372,9 @@ static __inline__ int led_get_net_activi
> continue;
> if (LOOPBACK(in_dev->ifa_list->ifa_local))
> continue;
> - if (!dev->get_stats)
> + stats = dev->get_stats(dev);
> + if (!stats)
> continue;
> - stats = dev->get_stats(dev);
> rx_total += stats->rx_packets;
> tx_total += stats->tx_packets;
> }
> diff -r 1ccab0a087b7 include/linux/netdevice.h
> --- a/include/linux/netdevice.h Tue Mar 27 13:46:10 2007 +1000
> +++ b/include/linux/netdevice.h Tue Mar 27 14:21:09 2007 +1000
> @@ -325,6 +325,7 @@ struct net_device
> #define NETIF_F_VLAN_CHALLENGED 1024 /* Device cannot handle VLAN packets */
> #define NETIF_F_GSO 2048 /* Enable software GSO. */
> #define NETIF_F_LLTX 4096 /* LockLess TX */
> +#define NETIF_F_INTERNAL_STATS 8192 /* Use stats structure in net_device */
>
> /* Segmentation offload features */
> #define NETIF_F_GSO_SHIFT 16
> @@ -349,6 +350,7 @@ struct net_device
>
>
> struct net_device_stats* (*get_stats)(struct net_device *dev);
> + struct net_device_stats stats;
>
> #ifdef CONFIG_WIRELESS_EXT
> /* List of functions to handle Wireless Extensions (instead of ioctl).
> diff -r 1ccab0a087b7 net/core/dev.c
> --- a/net/core/dev.c Tue Mar 27 13:46:10 2007 +1000
> +++ b/net/core/dev.c Tue Mar 27 14:30:05 2007 +1000
> @@ -825,7 +825,6 @@ static int default_rebuild_header(struct
> return 1;
> }
>
> -
> /**
> * dev_open - prepare an interface for use.
> * @dev: device to open
> @@ -2120,9 +2119,9 @@ void dev_seq_stop(struct seq_file *seq,
>
> static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
> {
> - if (dev->get_stats) {
> - struct net_device_stats *stats = dev->get_stats(dev);
> -
> + struct net_device_stats *stats = dev->get_stats(dev);
> +
> + if (stats) {
> seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
> "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
> dev->name, stats->rx_bytes, stats->rx_packets,
> @@ -3146,6 +3145,13 @@ out:
> mutex_unlock(&net_todo_run_mutex);
> }
>
> +static struct net_device_stats *maybe_internal_stats(struct net_device *dev)
> +{
> + if (dev->features & NETIF_F_INTERNAL_STATS)
> + return &dev->stats;
> + return NULL;
> +}
> +
> /**
> * alloc_netdev - allocate network device
> * @sizeof_priv: size of private data to allocate space for
> @@ -3181,6 +3187,7 @@ struct net_device *alloc_netdev(int size
> if (sizeof_priv)
> dev->priv = netdev_priv(dev);
>
> + dev->get_stats = maybe_internal_stats;
> setup(dev);
> strcpy(dev->name, name);
> return dev;
>
>
>
It would make sense to do it per-cpu and 64 bit for the non-error counters.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Inline net_device_stats
2007-03-28 15:52 ` Stephen Hemminger
@ 2007-03-28 21:26 ` David Miller
2007-03-29 0:00 ` Rusty Russell
1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2007-03-28 21:26 UTC (permalink / raw)
To: shemminger; +Cc: rusty, netdev
From: Stephen Hemminger <shemminger@osdl.org>
Date: Wed, 28 Mar 2007 08:52:24 -0700
> It would make sense to do it per-cpu and 64 bit for the non-error
> counters.
Good point, but that's a seperate change.
I have no real objection to Rusty's change and if more than
one driver uses this thing it's useful, so I'll apply his
patch.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Inline net_device_stats
2007-03-28 15:52 ` Stephen Hemminger
2007-03-28 21:26 ` David Miller
@ 2007-03-29 0:00 ` Rusty Russell
1 sibling, 0 replies; 4+ messages in thread
From: Rusty Russell @ 2007-03-29 0:00 UTC (permalink / raw)
To: Stephen Hemminger; +Cc: netdev
On Wed, 2007-03-28 at 08:52 -0700, Stephen Hemminger wrote:
> It would make sense to do it per-cpu and 64 bit for the non-error counters.
Well, I looked at the e1000, it doesn't update on every packet anyway,
but seems to d/l from the card occasionally. I assume this is the
method for high-speed drivers, otherwise we should split the tx & rx
parts of the structure.
64 bit introduces potential compatibility problems (exporting via proc).
And per-cpu feels like overkill to me.
Rusty.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-03-29 0:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-28 9:16 [PATCH] Inline net_device_stats Rusty Russell
2007-03-28 15:52 ` Stephen Hemminger
2007-03-28 21:26 ` David Miller
2007-03-29 0:00 ` Rusty Russell
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.