netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] support for large number of network devices.
@ 2004-01-13 23:46 Stephen Hemminger
  2004-01-13 23:59 ` David S. Miller
  2004-01-14  0:23 ` Ben Greear
  0 siblings, 2 replies; 17+ messages in thread
From: Stephen Hemminger @ 2004-01-13 23:46 UTC (permalink / raw)
  To: David S. Miller; +Cc: netdev

When using pseudo network devices, and really big machines; there is
sometimes a need to have a lot of network devices.  This replaces the
existing 2.6.1 limit of 100 entries an was O(n^2)
with a algorithm that will handle up to 32768 entries with O(n) behaviour.

Does need a temporary page, but that shouldn't be a big deal.
It has the same semantics, it will find the first empty name and use it.

diff -Nru a/net/core/dev.c b/net/core/dev.c
--- a/net/core/dev.c	Tue Jan 13 15:42:19 2004
+++ b/net/core/dev.c	Tue Jan 13 15:42:19 2004
@@ -650,8 +650,11 @@
 int dev_alloc_name(struct net_device *dev, const char *name)
 {
 	int i;
-	char buf[32];
 	char *p;
+	const int max_netdevices = 8*PAGE_SIZE;
+	long *inuse;
+	struct net_device *d;
+	char buf[IFNAMSIZ];
 
 	/*
 	 * Verify the string as this thing may have come from
@@ -662,17 +665,34 @@
 	if (p && (p[1] != 'd' || strchr(p + 2, '%')))
 		return -EINVAL;
 
-	/*
-	 * If you need over 100 please also fix the algorithm...
-	 */
-	for (i = 0; i < 100; i++) {
-		snprintf(buf, sizeof(buf), name, i);
-		if (!__dev_get_by_name(buf)) {
-			strcpy(dev->name, buf);
-			return 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)) {
+			if (i < 0 || i >= max_netdevices)
+				continue;
+
+			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)) {
+		strcpy(dev->name, buf);
+		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;
 }
 
 

^ permalink raw reply	[flat|nested] 17+ messages in thread
* Re: [PATCH] support for large number of network devices.
@ 2004-01-14 21:39 Jean Tourrilhes
  0 siblings, 0 replies; 17+ messages in thread
From: Jean Tourrilhes @ 2004-01-14 21:39 UTC (permalink / raw)
  To: Stephen Hemminger, David S. Miller, netdev

Stephen Hemminger wrote :
> 
> When using pseudo network devices, and really big machines; there is
> sometimes a need to have a lot of network devices.  This replaces the
> existing 2.6.1 limit of 100 entries an was O(n^2)
> with a algorithm that will handle up to 32768 entries with O(n) behaviour.

	You may want to be careful about buffer overflow in
dev->name. The old code did not check for it, because it was replacing
'%d' with a most 2 char ('0' to '99').
	The new code may create overflow for device names such as :
		'reallylongname%d'
	And you don't seem the catch that (unless I overlooked something).

	The problem is more messy that it looks like, because there is
no sane way to handle overflow. You can return an error to the driver,
and the driver may bail out properly (or crash), but the end user has
no way to overcome the issue and get its card loaded (short of editing
the driver and recompiling the kernel).
	So, there is a bit of auding to do first. I know for example
the HostAP create such long names.
	But that's only my humble opinion ;-)

	Jean

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2004-01-15 19:40 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-13 23:46 [PATCH] support for large number of network devices Stephen Hemminger
2004-01-13 23:59 ` David S. Miller
2004-01-14  0:13   ` Stephen Hemminger
2004-01-14  7:13     ` Matt Mackall
2004-01-14 19:37       ` Stephen Hemminger
2004-01-14 19:51         ` Matt Mackall
2004-01-14 20:11         ` David S. Miller
2004-01-15  0:24           ` Stephen Hemminger
2004-01-15  8:46             ` David S. Miller
2004-01-15 17:52               ` Stephen Hemminger
2004-01-15 19:40                 ` David S. Miller
2004-01-14  0:23 ` Ben Greear
2004-01-14  0:38   ` Stephen Hemminger
2004-01-14  1:55     ` Ben Greear
2004-01-14  7:18       ` David S. Miller
2004-01-14  7:54         ` Ben Greear
  -- strict thread matches above, loose matches on Subject: below --
2004-01-14 21:39 Jean Tourrilhes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).