netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Hemminger <shemminger@osdl.org>
To: "David S. Miller" <davem@redhat.com>
Cc: netdev@oss.sgi.com
Subject: [PATCH] (4/4) support large number of network devices -- alloc_name bitmap
Date: Fri, 16 Jan 2004 15:50:07 -0800	[thread overview]
Message-ID: <20040116155007.6b68844f.shemminger@osdl.org> (raw)
In-Reply-To: <20040116154652.04dd3324.shemminger@osdl.org>

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

diff -Nru a/net/core/dev.c b/net/core/dev.c
--- a/net/core/dev.c	Fri Jan 16 15:45:04 2004
+++ b/net/core/dev.c	Fri Jan 16 15:45:04 2004
@@ -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;
 }
 
 

  parent reply	other threads:[~2004-01-16 23:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-16 23:46 [PATCH] (1/4) support large number of network devices Stephen Hemminger
2004-01-16 23:48 ` [PATCH] (2/4) support large number of network devices -- name hash Stephen Hemminger
2004-01-17  2:22   ` YOSHIFUJI Hideaki / 吉藤英明
2004-01-19 19:28     ` Stephen Hemminger
2004-01-22  2:10       ` YOSHIFUJI Hideaki / 吉藤英明
2004-01-16 23:49 ` [PATCH] (3/4) support large number of network devices -- hash ifindex Stephen Hemminger
2004-01-16 23:50 ` Stephen Hemminger [this message]
2004-01-20  5:24 ` [PATCH] (1/4) support large number of network devices David S. Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040116155007.6b68844f.shemminger@osdl.org \
    --to=shemminger@osdl.org \
    --cc=davem@redhat.com \
    --cc=netdev@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).