(1/4) support large number of network devices
Subsystems (2)
PATCH 1/1 (1/4) support large number of network devices
In preparation for the other changes to allow large number of network devices, move the device table head and lock from over in the network drivers setup code into the core netdev code.
drivers/net/Space.c +0 -25
@@ -455,28 +455,3 @@ }device_initcall (net_olddevs_init); - -/* -* The @dev_base list is protected by @dev_base_lock and the rtln -* semaphore. -* -* Pure readers hold dev_base_lock for reading. -* -* Writers must hold the rtnl semaphore while they loop through the -* dev_base list, and hold dev_base_lock for writing when they do the -* actual updates. This allows pure readers to access the list even -* while a writer is preparing to update it. -* -* To put it another way, dev_base_lock is held for writing only to -* protect against pure readers; the rtnl semaphore provides the -* protection against other writers. -* -* See, for example usages, register_netdevice() and -* unregister_netdevice(), which must be called with the rtnl -* semaphore held. -*/ -struct net_device *dev_base; -rwlock_t dev_base_lock =RW_LOCK_UNLOCKED ; - -EXPORT_SYMBOL (dev_base); -EXPORT_SYMBOL (dev_base_lock);
net/core/dev.c +25 -0
@@ -161,6 +161,31 @@#endif /* +* The @dev_base list is protected by @dev_base_lock and the rtln +* semaphore. +* +* Pure readers hold dev_base_lock for reading. +* +* Writers must hold the rtnl semaphore while they loop through the +* dev_base list, and hold dev_base_lock for writing when they do the +* actual updates. This allows pure readers to access the list even +* while a writer is preparing to update it. +* +* To put it another way, dev_base_lock is held for writing only to +* protect against pure readers; the rtnl semaphore provides the +* protection against other writers. +* +* See, for example usages, register_netdevice() and +* unregister_netdevice(), which must be called with the rtnl +* semaphore held. +*/ +struct net_device *dev_base; +rwlock_t dev_base_lock =RW_LOCK_UNLOCKED ; + +EXPORT_SYMBOL (dev_base); +EXPORT_SYMBOL (dev_base_lock); + +/* * Our notifier list */
PATCH 1/1 (2/4) support large number of network devices -- name hash
Add a hash list to speed lookup's of network device name used in dev_alloc_name and by other protocols. Keep a tail pointer as well to allow quick append to the end of the device singly linked device list.
Note: Uses 8bits for hash and the filename hash function
include/linux/netdevice.h +2 -0
@@ -375,6 +375,8 @@atomic_t refcnt;/* delayed register/unregister */ struct list_head todo_list; +/* device name hash chain */ +struct hlist_node name_hlist;/* register/unregister state machine */ enum {NETREG_UNINITIALIZED =0 ,
net/core/dev.c +43 -13
111 lines not on this page · raw message
PATCH 1/1 (3/4) support large number of network devices -- hash ifindex
In reply to Stephen HemmingerAdd an additional hash chain to keep track of network devices by index. This gets used during route manipulation and when allocating a unique ifindex.
include/linux/netdevice.h +2 -0
@@ -377,6 +377,8 @@struct list_head todo_list;/* device name hash chain */ struct hlist_node name_hlist; +/* device index hash chain */ +struct hlist_node index_hlist;/* register/unregister state machine */ enum {NETREG_UNINITIALIZED =0 ,
net/core/dev.c +18 -4
@@ -188,6 +188,7 @@#define NETDEV_HASHBITS 8static struct hlist_head dev_name_head[1 <<NETDEV_HASHBITS ]; +static struct hlist_head dev_index_head[1 <<NETDEV_HASHBITS ];static inline struct hlist_head *dev_name_hash (const char *name) {
@@ -196,6 +197,11 @@return &dev_name_head[hash & ((1 <<NETDEV_HASHBITS )-1 )]; } +static inline struct hlist_head *dev_index_hash (int ifindex) +{ +return &dev_index_head[ifindex & ((1 <<NETDEV_HASHBITS )-1 )]; +} +/* * Our notifier list */
@@ -531,12 +537,15 @@struct net_device *__dev_get_by_index (int ifindex) { -struct net_device *dev; +struct hlist_node *p; -for (dev = dev_base; dev; dev = dev->next) +hlist_for_each (p,dev_index_hash (ifindex)) { +struct net_device *dev + = hlist_entry(p, struct net_device, index_hlist);if (dev->ifindex == ifindex) -break ; -return dev; +return dev; + } +return NULL ; }
@@ -2819,6 +2828,7 @@ *dev_tail = dev; dev_tail = &dev->next;hlist_add_head (&dev->name_hlist, head); +hlist_add_head (&dev->index_hlist,dev_index_hash (dev->ifindex));dev_hold (dev); dev->reg_state =NETREG_REGISTERING ;write_unlock_bh (&dev_base_lock);
@@ -3041,6 +3051,7 @@if (d == dev) {write_lock_bh (&dev_base_lock);hlist_del (&dev->name_hlist); +hlist_del (&dev->index_hlist);if (dev_tail == &dev->next) dev_tail = dp; *dp = d->next;
@@ -3121,6 +3132,9 @@for (i =0 ; i <ARRAY_SIZE (dev_name_head); i++)INIT_HLIST_HEAD (&dev_name_head[i]); + +for (i =0 ; i <ARRAY_SIZE (dev_index_head); i++) +INIT_HLIST_HEAD (&dev_index_head[i]);/* * Initialise the packet receive queues.
PATCH 1/1 (4/4) support large number of network devices -- alloc_name bitmap
In reply to Stephen HemmingerConvert dev_alloc_name from O(n^2) algorithm with a limit of 100 of same prefix; to a two pass O(n) algorithim with a limit of 32k devices with the same prefix.
This version finds the first free slot, and handles overflow correctly for long names.
net/core/dev.c +36 -12
@@ -698,8 +698,11 @@int dev_alloc_name (struct net_device *dev,const char *name) {int i; -char buf[32 ]; -char *p; +char buf[IFNAMSIZ ]; +const char *p; +const int max_netdevices =8 *PAGE_SIZE ; +long *inuse; +struct net_device *d;/* * Verify the string as this thing may have come from
@@ -707,20 +710,41 @@* characters, or no "%" characters at all. */ p =strchr (name,'%' ); -if (p && (p[1 ] !='d' ||strchr (p +2 ,'%' ))) +if (p && ((p[1 ] !='d' ||strchr (p +2 ,'%' )) + || (p - name) >IFNAMSIZ -2 ))return -EINVAL ; -/* -* If you need over 100 please also fix the algorithm... -*/ -for (i =0 ; i <100 ; i++) { +/* Use one page as a bit array of possible slots */ + inuse = (long *)get_zeroed_page (GFP_ATOMIC ); +if (!inuse) +return -ENOMEM ; + +for (d = dev_base; d; d = d->next) { +if (!sscanf (d->name, name, &i)) +continue ; +if (i <0 || i >= max_netdevices) +continue ; + +/* avoid cases where sscanf is not exact inverse of printf */ snprintf (buf,sizeof (buf), name, i); -if (!__dev_get_by_name (buf)) { -strcpy (dev->name, buf); -return i; - } +if (!strncmp (buf, d->name,IFNAMSIZ )) +set_bit (i, inuse); } -return -ENFILE ;/* Over 100 of the things .. bail out! */ + + i =find_first_zero_bit (inuse, max_netdevices); +free_page ((unsigned long ) inuse); + +snprintf (buf,sizeof (buf), name, i); +if (!__dev_get_by_name (buf)) { +strlcpy (dev->name, buf,IFNAMSIZ ); +return i; + } + +/* It is possible to run out of possible slots +* when the name is long and there isn't enough space left +* for the digits, or if all bits are used. +*/ +return -ENFILE ; }
Re: [PATCH] (2/4) support large number of network devices -- name hash
In reply to Stephen HemmingerIn article <[email protected]> (at Fri, 16 Jan 2004 15:48:14 -0800), Stephen Hemminger <[email protected]> says:
net/core/dev.c line 194
Quoted 1 line
net/core/dev.c +1
-0+size_t len =min (strlen (name),(size_t )(IFNAMSIZ -1 ));
strnlen(name, IFNAMESIZ) ?
Signature · 2 lines
Hideaki YOSHIFUJI @ USAGI Project <[email protected]> GPG FP: 9022 65EB 1ECF 3AD1 0BDF 80D8 4807 F894 E062 0EEA
Re: [PATCH] (2/4) support large number of network devices -- name hash
Use strnlen, great idea, thanks.
net/core/dev.c +2 -2
@@ -192,8 +192,8 @@static inline struct hlist_head *dev_name_hash (const char *name) { -size_t len =min (strlen (name),(size_t )(IFNAMSIZ -1 )); -unsigned hash =full_name_hash (name, len); +unsigned hash =full_name_hash (name, +strnlen (name,IFNAMSIZ -1 ));return &dev_name_head[hash & ((1 <<NETDEV_HASHBITS )-1 )]; }
I think I'm going to defer this change until the next 2.6.x, we have enough crap in there now I believe :-)
Please remeber to resend this as I do want to integrate your work.
Thanks Stephen.
Re: [PATCH] (2/4) support large number of network devices -- name hash
In reply to Stephen HemmingerIn article <[email protected]> (at Mon, 19 Jan 2004 11:28:50 -0800), Stephen Hemminger <[email protected]> says:
net/core/dev.c line 196
Quoted 6 lines
Use strnlen, great idea, thanks.
net/core/dev.c +2
-2-size_t len =min (strlen (name),(size_t )(IFNAMSIZ -1 )); -unsigned hash =full_name_hash (name, len); +unsigned hash =full_name_hash (name, +strnlen (name,IFNAMSIZ -1 ));
I prefer IFNAMSIZ over IFNAMSIZ-1 here bacause maximum length of valid name is IFNAMSIZ-1.
strnlen(3):
size_t strnlen(const char *s, size_t maxlen);
:
The strnlen function returns strlen(s), if that is less than maxlen, or maxlen if there is no '\0' character among the first maxlen characters pointed to by s.
If strnlen(name,IFNAMSIZ) returns IFNAMSIZ, name is not terminated. while strnlen(name,IFNAMSIZ-1) does not tell whether it is terminated or not.
--yoshfuji