Hello list.  :)

I rewrote the dev_alloc_name() function in the 2.2.14 Linux kernel's
net/core/dev.c module.  It had an apparently artificial limitation on
number of network devices of the same type allowed (100), and was not
implemented in a very efficient manner (which would not be a big deal
under normal circumstances, as this routine wouldn't be called very
often).  Just another drop in the bucket.  :)

This patch has been through a few iterations, and I've been running on
it for almost a month now (i.e. I consider it stable).  I've received
excellent feedback from Ben Canning <[EMAIL PROTECTED]>, and look forward to
more from the great programmers and engineers who are sure to be
perusing this list (rip it up ladies and gentlemen).
-- 

Daniel Rall <[EMAIL PROTECTED]>



PATCH follows:

--- net/core/dev.c-ORIG Mon Apr 17 02:50:31 2000
+++ net/core/dev.c      Sun Apr 30 13:21:06 2000
@@ -17,8 +17,12 @@
  *             David Hinds <[EMAIL PROTECTED]>
  *             Alexey Kuznetsov <[EMAIL PROTECTED]>
  *             Adam Sulmicki <[EMAIL PROTECTED]>
+ *              Daniel Rall <[EMAIL PROTECTED]>
  *
  *     Changes:
+ *              Daniel Rall     :       Support an unlimitted number of
devices
+ *                                      of the same type and improve
the 
+ *                                      efficiency of device name
allocation.
  *             Marcelo Tosatti <[EMAIL PROTECTED]> : dont accept mtu 0 or <
  *             Alan Cox        :       device private ioctl copies fields back.
  *             Alan Cox        :       Transmit queue code does relevant stunts to
@@ -66,6 +70,7 @@
 #include <asm/bitops.h>
 #include <linux/config.h>
 #include <linux/types.h>
+#include <linux/ctype.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/string.h>
@@ -295,23 +300,60 @@
 }
 
 /*
- *     Passed a format string - eg "lt%d" it will try and find a suitable
- *     id. Not efficient for many devices, not called a lot..
+ *     Passed a format string - eg "lt%d" or "eth%d", locates and returns
an 
+ *     unused id.  Assumes the provided device and name have been allocated 
+ *      properly.  Note that a device name is the concatenation of its
type 
+ *      and id.
  */
 
-int dev_alloc_name(struct device *dev, const char *name)
+int dev_alloc_name(struct device *device, const char *name_fmt)
 {
-       int i;
-       /*
-        *      If you need over 100 please also fix the algorithm...
-        */
-       for(i=0;i<100;i++)
+       char *ptr = strrchr(name_fmt, '%');
+
+       if (ptr != NULL)
        {
-               sprintf(dev->name,name,i);
-               if(dev_get(dev->name)==NULL)
-                       return i;
+               int id = 0;
+               size_t type_len = (size_t)(ptr - name_fmt);
+
+               register struct device *dev;
+               int n;
+
+               /*
+                *      Find the device matching the desired device 
+                *      name in the master device list with the 
+                *      highest id.
+                */
+               for (dev = dev_base; dev != NULL; dev = dev->next) 
+               {
+                       /* Be suspicious of the other devices.  Who knows what 
+                          was what has happened to them since we saw them 
+                          last. */
+                       if (dev->name != NULL)
+                       {
+                               /* Check for matching device type. */
+                               if (strncmp(name_fmt, dev->name, type_len) == 0
+                                   && isdigit(dev->name[type_len]))
+                               {
+                                       /* Device type matches, parse id. */
+                                       ptr = (char *)(dev->name + type_len);
+                                       n = (int)simple_strtoul(ptr, NULL, 0);
+
+                                       /* Remeber the parsed id if it's the 
+                                          highest found. */
+                                       if (n > id)
+                                               id = n;
+                               }
+                       }
+               }
+
+               /* Write the new name. */
+               sprintf(device->name, name_fmt, id);
+
+               return id;
        }
-       return -ENFILE; /* Over 100 of the things .. bail out! */
+       else
+               /* No format string indicator found in arg name_fmt. */
+               return -EINVAL;
 }
  
 struct device *dev_alloc(const char *name, int *err)
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]

Reply via email to