Re: [Devel] [PATCH 1/1] Dynamically allocate the loopback device

2007-08-27 Thread Eric W. Biederman
Stephen Hemminger [EMAIL PROTECTED] writes:

 On Fri, 24 Aug 2007 19:55:47 +0400
 Denis V. Lunev [EMAIL PROTECTED] wrote:

 [EMAIL PROTECTED] wrote:
  From: Daniel Lezcano [EMAIL PROTECTED]
  
  Doing this makes loopback.c a better example of how to do a
  simple network device, and it removes the special case
  single static allocation of a struct net_device, hopefully
  making maintenance easier.
  
  Applies against net-2.6.24
  
  Tested on i386, x86_64
  Compiled on ia64, sparc
 
 I think that a small note, that initialization order is changed will be
 good to record. After this, loopback MUST be allocated before any other
 networking subsystem initialization. And this is an important change.
 
 Regards,
 Den

 Yes, this code would break when other drivers are directly linked
 in. 

No. Other drivers don't care at all about the loopback device,
and it isn't a requirement that the loopback device be initialized
before other devices.

The requirement is that the loopback device is allocated before
we start using it.  Which means networking subsystems like
ipv4 and ipv6 care not other network drivers.  In practices this means
before we get very far into the ipv4 subsystem initialization as
ipv4 is always compiled in and is initialized early.

To get the initialization order correct I used fs_initcall instead of
module_init.

When I reflect on it.  I'm not really comfortable with the fact
that we currently start using the loopback device before we
finish initializing and register it.  Although it has worked
for over a decade so I guess early on we don't care about
much more then the address of the loopback device.

From what I can tell the initialization order dependency seems much
less subtle and much more robust then separate rules for allocating
the loopback device.  We have had several patchs recently that
broke (including one merged upstream).  The only way I can see
to break an initialization order dependency is to go deliberately
messing around with initialization order.

Eric

p.s.  My apologies for the late reply some one dropped me off the cc.
And I have been under the weather all week.
-
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [Devel] [PATCH 1/1] Dynamically allocate the loopback device

2007-08-24 Thread Denis V. Lunev
[EMAIL PROTECTED] wrote:
 From: Daniel Lezcano [EMAIL PROTECTED]
 
 Doing this makes loopback.c a better example of how to do a
 simple network device, and it removes the special case
 single static allocation of a struct net_device, hopefully
 making maintenance easier.
 
 Applies against net-2.6.24
 
 Tested on i386, x86_64
 Compiled on ia64, sparc

I think that a small note, that initialization order is changed will be
good to record. After this, loopback MUST be allocated before any other
networking subsystem initialization. And this is an important change.

Regards,
Den
-
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 1/1] Dynamically allocate the loopback device

2007-08-24 Thread dlezcano
From: Daniel Lezcano [EMAIL PROTECTED]

Doing this makes loopback.c a better example of how to do a
simple network device, and it removes the special case
single static allocation of a struct net_device, hopefully
making maintenance easier.

Applies against net-2.6.24

Tested on i386, x86_64
Compiled on ia64, sparc

Signed-off-by: Eric W. Biederman [EMAIL PROTECTED]
Signed-off-by: Daniel Lezcano [EMAIL PROTECTED]
Acked-By: Kirill Korotaev [EMAIL PROTECTED]
Acked-by: Benjamin Thery [EMAIL PROTECTED]
---
 drivers/net/loopback.c   |   63 +++---
 include/linux/netdevice.h|2 +-
 net/core/dst.c   |8 ++--
 net/decnet/dn_dev.c  |4 +-
 net/decnet/dn_route.c|   14 
 net/ipv4/devinet.c   |6 ++--
 net/ipv4/ipconfig.c  |6 ++--
 net/ipv4/ipvs/ip_vs_core.c   |2 +-
 net/ipv4/route.c |   18 +-
 net/ipv4/xfrm4_policy.c  |2 +-
 net/ipv6/addrconf.c  |   15 +---
 net/ipv6/ip6_input.c |2 +-
 net/ipv6/netfilter/ip6t_REJECT.c |2 +-
 net/ipv6/route.c |   15 +++-
 net/ipv6/xfrm6_policy.c  |2 +-
 net/xfrm/xfrm_policy.c   |4 +-
 16 files changed, 89 insertions(+), 76 deletions(-)

diff --git a/drivers/net/loopback.c b/drivers/net/loopback.c
index 5106c23..3642aff 100644
--- a/drivers/net/loopback.c
+++ b/drivers/net/loopback.c
@@ -199,44 +199,57 @@ static const struct ethtool_ops loopback_ethtool_ops = {
.get_rx_csum= always_on,
 };
 
-/*
- * The loopback device is special. There is only one instance and
- * it is statically allocated. Don't do this for other devices.
- */
-struct net_device loopback_dev = {
-   .name   = lo,
-   .get_stats  = get_stats,
-   .mtu= (16 * 1024) + 20 + 20 + 12,
-   .hard_start_xmit= loopback_xmit,
-   .hard_header= eth_header,
-   .hard_header_cache  = eth_header_cache,
-   .header_cache_update= eth_header_cache_update,
-   .hard_header_len= ETH_HLEN, /* 14   */
-   .addr_len   = ETH_ALEN, /* 6*/
-   .tx_queue_len   = 0,
-   .type   = ARPHRD_LOOPBACK,  /* 0x0001*/
-   .rebuild_header = eth_rebuild_header,
-   .flags  = IFF_LOOPBACK,
-   .features   = NETIF_F_SG | NETIF_F_FRAGLIST
+static void loopback_setup(struct net_device *dev)
+{
+   dev-get_stats  = get_stats;
+   dev-mtu= (16 * 1024) + 20 + 20 + 12;
+   dev-hard_start_xmit= loopback_xmit;
+   dev-hard_header= eth_header;
+   dev-hard_header_cache  = eth_header_cache;
+   dev-header_cache_update = eth_header_cache_update;
+   dev-hard_header_len= ETH_HLEN; /* 14   */
+   dev-addr_len   = ETH_ALEN; /* 6*/
+   dev-tx_queue_len   = 0;
+   dev-type   = ARPHRD_LOOPBACK;  /* 0x0001*/
+   dev-rebuild_header = eth_rebuild_header;
+   dev-flags  = IFF_LOOPBACK;
+   dev-features   = NETIF_F_SG | NETIF_F_FRAGLIST
 #ifdef LOOPBACK_TSO
  | NETIF_F_TSO
 #endif
  | NETIF_F_NO_CSUM | NETIF_F_HIGHDMA
- | NETIF_F_LLTX,
-   .ethtool_ops= loopback_ethtool_ops,
-};
+ | NETIF_F_LLTX;
+   dev-ethtool_ops= loopback_ethtool_ops;
+}
 
 /* Setup and register the loopback device. */
 static int __init loopback_init(void)
 {
-   int err = register_netdev(loopback_dev);
+   struct net_device *dev;
+   int err;
+   
+   err = -ENOMEM;
+   dev = alloc_netdev(0, lo, loopback_setup);
+   if (!dev)
+   goto out;
+
+   err = register_netdev(dev);
+   if (err)
+   goto out_free_netdev;
 
+   err = 0;
+   loopback_dev = dev;
+
+out:
if (err)
panic(loopback: Failed to register netdevice: %d\n, err);
-
return err;
+out_free_netdev:
+   free_netdev(dev);
+   goto out;
 };
 
-module_init(loopback_init);
+fs_initcall(loopback_init);
 
+struct net_device *loopback_dev;
 EXPORT_SYMBOL(loopback_dev);
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 8d12f02..7cd0641 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -680,7 +680,7 @@ struct packet_type {
 #include linux/interrupt.h
 #include linux/notifier.h
 
-extern struct net_device   loopback_dev;   /* The loopback 
*/
+extern struct net_device   *loopback_dev;  /* The loopback 
*/
 extern struct list_headdev_base_head;  /* All 
devices */
 extern rwlock_tdev_base_lock;  

Re: [Devel] [PATCH 1/1] Dynamically allocate the loopback device

2007-08-24 Thread Daniel Lezcano

Denis V. Lunev wrote:

[EMAIL PROTECTED] wrote:

From: Daniel Lezcano [EMAIL PROTECTED]

Doing this makes loopback.c a better example of how to do a
simple network device, and it removes the special case
single static allocation of a struct net_device, hopefully
making maintenance easier.

Applies against net-2.6.24

Tested on i386, x86_64
Compiled on ia64, sparc


I think that a small note, that initialization order is changed will be
good to record. After this, loopback MUST be allocated before any other
networking subsystem initialization. And this is an important change.

Regards,
Den



Thanks Denis to point that.

-- Daniel

-
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [Devel] [PATCH 1/1] Dynamically allocate the loopback device

2007-08-24 Thread Stephen Hemminger
On Fri, 24 Aug 2007 19:55:47 +0400
Denis V. Lunev [EMAIL PROTECTED] wrote:

 [EMAIL PROTECTED] wrote:
  From: Daniel Lezcano [EMAIL PROTECTED]
  
  Doing this makes loopback.c a better example of how to do a
  simple network device, and it removes the special case
  single static allocation of a struct net_device, hopefully
  making maintenance easier.
  
  Applies against net-2.6.24
  
  Tested on i386, x86_64
  Compiled on ia64, sparc
 
 I think that a small note, that initialization order is changed will be
 good to record. After this, loopback MUST be allocated before any other
 networking subsystem initialization. And this is an important change.
 
 Regards,
 Den

Yes, this code would break when other drivers are directly linked
in. 
-
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [Devel] [PATCH 1/1] Dynamically allocate the loopback device

2007-08-24 Thread Denis V. Lunev
no, and this is important. Loopback is initialized in fs_initcall which
is called sufficiently before module_init.

I have checked the code and do not see initialization order mistakes
right now. But, from now on, maintainer should pay attention for this
unfortunate consequence :(

Regards,
Den

Stephen Hemminger wrote:
 On Fri, 24 Aug 2007 19:55:47 +0400
 Denis V. Lunev [EMAIL PROTECTED] wrote:
 
 [EMAIL PROTECTED] wrote:
 From: Daniel Lezcano [EMAIL PROTECTED]

 Doing this makes loopback.c a better example of how to do a
 simple network device, and it removes the special case
 single static allocation of a struct net_device, hopefully
 making maintenance easier.

 Applies against net-2.6.24

 Tested on i386, x86_64
 Compiled on ia64, sparc
 I think that a small note, that initialization order is changed will be
 good to record. After this, loopback MUST be allocated before any other
 networking subsystem initialization. And this is an important change.

 Regards,
 Den
 
 Yes, this code would break when other drivers are directly linked
 in. 
 ___
 Containers mailing list
 [EMAIL PROTECTED]
 https://lists.linux-foundation.org/mailman/listinfo/containers
 
 ___
 Devel mailing list
 [EMAIL PROTECTED]
 https://openvz.org/mailman/listinfo/devel
 

-
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html