Skip to contentSkip to conversation

hmm, handle net devices with udev?

PATCH In review Kay Sievers 3 participants 5 messages Mar 23, 2004 – Mar 24, 2004

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hmm, handle net devices with udev?

Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and I want to bring the question back, if we want to handle net device naming with udev.

With this patch it is actually possible to specify something like this in udev.rules:

KERNEL="dummy*", SYSFS{address}="00:00:00:00:00:00", SYSFS{features}="0x0", NAME="blind%n" KERNEL="eth*", SYSFS{address}="00:0d:60:77:30:91", NAME="private"

and you will get:

[root@pim udev.kay]# cat /proc/net/dev

  Inter-|   Receive                                                | Transmit
   face |bytes    packets errs drop fifo frame compressed multicast|bytes   packets errs drop fifo colls carrier compressed
       lo:    1500     30    0    0    0     0          0         0    1500      30    0    0    0     0       0          0
  private:  278393   1114    0    0    0     0          0         0  153204    1468    0    0    0     0       0          0
     sit0:       0      0    0    0    0     0          0         0       0       0    0    0    0     0       0          0
   blind0:       0      0    0    0    0     0          0         0       0       0    0    0    0     0       0          0

The udevinfo program is also working:

[root@pim udev.kay]# ./udevinfo -a -p /sys/class/net/private

    looking at class device '/sys/class/net/private':
      SYSFS{addr_len}="6"
      SYSFS{address}="00:0d:60:77:30:91"
      SYSFS{broadcast}="ff:ff:ff:ff:ff:ff"
      SYSFS{features}="0x3a9"
      SYSFS{flags}="0x1003"
      SYSFS{ifindex}="2"
      SYSFS{iflink}="2"
      SYSFS{mtu}="1500"
      SYSFS{tx_queue_len}="1000"
      SYSFS{type}="1"

follow the class device's "device"

    looking at the device chain at '/sys/devices/pci0000:00/0000:00:1e.0/0000:02:01.0':
      BUS="pci"
      ID="0000:02:01.0"
      SYSFS{class}="0x020000"
      SYSFS{detach_state}="0"
      SYSFS{device}="0x101e"
      SYSFS{irq}="11"
      SYSFS{subsystem_device}="0x0549"
      SYSFS{subsystem_vendor}="0x1014"
      SYSFS{vendor}="0x8086"

The matching device will be renamed to the given name. The device name will not be put into the udev database, cause the kernel renames the device and the sysfs name disappears.

I like it, cause it plugs in nicely. We have all the naming features and sysfs queries and walks inside of udev. The sysfs timing races are already solved and the management tools are working for net devices too. nameif can only match the MAC address now. udev can match any sysfs value of the device tree the net device is connected to.
But right, net devices do not have device nodes :)

So, please share your thoughts, if we want something like this.

thanks,
Kay

===== namedev.c 1.131 vs edited =====

namedev.c +9 -0
@@ -832,12 +832,21 @@
 		}
 	}
 
+	if (udev->type == 'n') {
+		dbg("no name for net device '%s' configured", udev->kernel_name);
+		return -1;
+	}
+
 	/* no rule was found so we use the kernel name */
 	strfieldcpy(udev->name, udev->kernel_name);
 	goto done;
 
 found:
 	apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
+
+	if (udev->type == 'n')
+		return 0;
+
 	apply_format(udev, udev->symlink, sizeof(udev->symlink), class_dev, sysfs_device);
 	udev->partitions = dev->partitions;
 

===== udev-add.c 1.62 vs edited =====

udev-add.c +68 -18

135 lines not on this page · raw message

===== udev-remove.c 1.26 vs edited =====

udev-remove.c +27 -13
@@ -136,22 +136,36 @@
 	char *temp;
 	int retval;
 
-	memset(&dev, 0, sizeof(dev));
+	memset(&dev, 0x00, sizeof(dev));
 
-	retval = udevdb_get_dev(path, &dev);
-	if (retval) {
-		dbg("'%s' not found in database, falling back on default name", path);
-		temp = strrchr(path, '/');
-		if (temp == NULL)
-			return -ENODEV;
-		strfieldcpy(dev.name, &temp[1]);
-	}
+	dev.type = get_device_type(path, subsystem);
+
+	switch (dev.type) {
+	case 'b':
+	case 'c':
+		retval = udevdb_get_dev(path, &dev);
+		if (retval) {
+			dbg("'%s' not found in database, falling back on default name", path);
+			temp = strrchr(path, '/');
+			if (temp == NULL)
+				return -ENODEV;
+			strfieldcpy(dev.name, &temp[1]);
+		}
 
-	dbg("name is '%s'", dev.name);
-	udevdb_delete_dev(path);
+		dbg("name='%s'", dev.name);
+		udevdb_delete_dev(path);
+		sysbus_send_remove(dev.name, path);
+		retval = delete_node(&dev);
+		break;
 
-	sysbus_send_remove(dev.name, path);
+	case 'n':
+		retval = 0;
+		break;
+
+	default:
+		dbg("unknown device type '%c'", dev.type);
+		retval = -EINVAL;
+	}
 
-	retval = delete_node(&dev);
 	return retval;
 }

===== udev.c 1.53 vs edited =====

udev.c +14 -15
@@ -70,7 +70,6 @@
 }
 
 static char *subsystem_blacklist[] = {
-	"net",
 	"scsi_host",
 	"scsi_device",
 	"usb_host",
@@ -87,6 +86,7 @@
 	int retval = -EINVAL;
 	int i;
 	struct sigaction act;
+	const int nofake = 0;
 
 	action = get_action();
 	if (!action) {
@@ -140,19 +140,21 @@
 	sigaction(SIGINT, &act, NULL);
 	sigaction(SIGTERM, &act, NULL);
 
-	/* initialize the naming deamon */
-	namedev_init();
-
-	if (strcmp(action, "add") == 0)
-		retval = udev_add_device(devpath, subsystem, 0);
+	if (strcmp(action, "add") == 0) {
+		namedev_init();
+		retval = udev_add_device(devpath, subsystem, nofake);
+		goto action_done;
+	}
 
-	else if (strcmp(action, "remove") == 0)
+	if (strcmp(action, "remove") == 0) {
 		retval = udev_remove_device(devpath, subsystem);
-
-	else {
-		dbg("unknown action '%s'", action);
-		retval = -EINVAL;
+		goto action_done;
 	}
+
+	dbg("unknown action '%s'", action);
+	retval = -EINVAL;
+
+action_done:
 	udevdb_exit();
 
 exit_sysbus:
@@ -160,10 +162,7 @@
 	sysbus_disconnect();
 
 exit:
-	if (retval > 0)
-		retval = 0;
-
-	return -retval;
+	return retval;
 }
 
 int main(int argc, char *argv[], char *envp[])

===== udev_lib.c 1.1 vs edited =====

udev_lib.c +16 -0
@@ -70,6 +70,22 @@
 	return subsystem;
 }
 
+char get_device_type(const char *path, const char *subsystem)
+{
+	if (strcmp(subsystem, "block") == 0 ||
+	    strstr(path, "/block/") != NULL)
+		return 'b';
+
+	if (strcmp(subsystem, "net") == 0 ||
+	    strstr(path, "/class/net/") != NULL)
+		return 'n';
+
+	if (strstr(path, "/class/") != NULL)
+		return 'c';
+
+	return '\0';
+}
+
 int file_map(const char *filename, char **buf, size_t *bufsize)
 {
 	struct stat stats;

===== udev_lib.h 1.1 vs edited =====

udev_lib.h +1 -0
@@ -70,6 +70,7 @@
 extern char *get_devpath(void);
 extern char *get_seqnum(void);
 extern char *get_subsystem(char *subsystem);
+extern char get_device_type(const char *path, const char *subsystem);
 extern int file_map(const char *filename, char **buf, size_t *bufsize);
 extern void file_unmap(char *buf, size_t bufsize);
 extern size_t buf_get_line(char *buf, size_t buflen, size_t cur);

===== udevinfo.c 1.19 vs edited =====

udevinfo.c +17 -10
@@ -135,6 +135,10 @@
 	struct sysfs_device *sysfs_dev;
 	struct sysfs_device *sysfs_dev_parent;
 	int retval = 0;
+	char type;
+
+	type = get_device_type(path, "");
+	dbg("device type is %c", type);
 
 	/*  get the class dev */
 	class_dev = sysfs_open_class_device_path(path);
@@ -143,22 +147,25 @@
 		return -1;
 	}
 
-	/* read the 'dev' file for major/minor*/
-	attr = sysfs_get_classdev_attr(class_dev, "dev");
-	if (attr == NULL) {
-		printf("couldn't get the \"dev\" file\n");
-		retval = -1;
-		goto exit;
-	}
-
 	printf("\nudevinfo starts with the device the node belongs to and then walks up the\n"
 	       "device chain, to print for every device found, all possibly useful attributes\n"
 	       "in the udev key format.\n"
 	       "Only attributes within one device section may be used together in one rule,\n"
 	       "to match the device for which the node will be created.\n"
 	       "\n");
-	printf("device '%s' has major:minor %s", class_dev->path, attr->value);
-	sysfs_close_attribute(attr);
+
+	if (type == 'b' || type =='c') {
+		/* read the 'dev' file for major/minor*/
+		attr = sysfs_get_classdev_attr(class_dev, "dev");
+		if (attr == NULL) {
+			printf("couldn't get the \"dev\" file\n");
+			retval = -1;
+			goto exit;
+		}
+
+		printf("device '%s' has major:minor %s", class_dev->path, attr->value);
+		sysfs_close_attribute(attr);
+	}
 
 	/* open sysfs class device directory and print all attributes */
 	printf("  looking at class device '%s':\n", class_dev->path);
#1 lore raw
Quoted 1 line · Kay Sievers

So, please share your thoughts, if we want something like this.

I really like it.

Signature · 13 lines
ciao, |
Marco | [5280 spNBWuo5XxGFs]


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#2 lore raw
In reply to Kay Sievers
Quoted 3 lines · Kay Sievers

Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and I want to bring the question back, if we want to handle net device naming with udev.

Ok, in looking at this patch, I think we should do this, it's just too simple not to :)

I wanted to apply this patch, but there are too many big conflicts with the current bk tree. Care to fix up the merge issues and send me a new one?

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Kay Sievers
Quoted 11 lines · Greg KH
Quoted 3 lines · Kay Sievers

Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and I want to bring the question back, if we want to handle net device naming with udev.

Ok, in looking at this patch, I think we should do this, it's just too simple not to :)

I wanted to apply this patch, but there are too many big conflicts with the current bk tree. Care to fix up the merge issues and send me a new one?

Here is the actual version.

thanks,
Kay

===== namedev.c 1.135 vs edited =====

namedev.c +10 -0
@@ -836,12 +836,22 @@
 		}
 	}
 
+	/* no rule was found for the net device */
+	if (udev->type == 'n') {
+		dbg("no name for net device '%s' configured", udev->kernel_name);
+		return -1;
+	}
+
 	/* no rule was found so we use the kernel name */
 	strfieldcpy(udev->name, udev->kernel_name);
 	goto done;
 
 found:
 	apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
+
+	if (udev->type == 'n')
+		return 0;
+
 	udev->partitions = dev->partitions;
 	strfieldcpy(udev->config_file, dev->config_file);
 	udev->config_line = dev->config_line;

===== udev-add.c 1.65 vs edited =====

udev-add.c +68 -18

135 lines not on this page · raw message

===== udev-remove.c 1.28 vs edited =====

udev-remove.c +29 -13

51 lines not on this page · raw message

===== udev.c 1.55 vs edited =====

udev.c +13 -13
@@ -68,7 +68,6 @@
 }
 
 static char *subsystem_blacklist[] = {
-	"net",
 	"scsi_host",
 	"scsi_device",
 	"usb_host",
@@ -85,6 +84,7 @@
 	int retval = -EINVAL;
 	int i;
 	struct sigaction act;
+	const int nofake = 0;
 
 	action = get_action();
 	if (!action) {
@@ -137,23 +137,23 @@
 
 	if (strcmp(action, "add") == 0) {
 		namedev_init();
-		retval = udev_add_device(devpath, subsystem, 0);
-	} else {
-		if (strcmp(action, "remove") == 0) {
-			retval = udev_remove_device(devpath, subsystem);
-		} else {
-			dbg("unknown action '%s'", action);
-			retval = -EINVAL;
-		}
+		retval = udev_add_device(devpath, subsystem, nofake);
+		goto action_done;
+	}
+
+	if (strcmp(action, "remove") == 0) {
+		retval = udev_remove_device(devpath, subsystem);
+		goto action_done;
 	}
 
+	dbg("unknown action '%s'", action);
+	retval = -EINVAL;
+
+action_done:
 	udevdb_exit();
 
 exit:
-	if (retval > 0)
-		retval = 0;
-
-	return -retval;
+	return retval;
 }
 
 int main(int argc, char *argv[], char *envp[])

===== udev_lib.c 1.2 vs edited =====

udev_lib.c +16 -0
@@ -81,6 +81,22 @@
 	return subsystem;
 }
 
+char get_device_type(const char *path, const char *subsystem)
+{
+	if (strcmp(subsystem, "block") == 0 ||
+	    strstr(path, "/block/") != NULL)
+		return 'b';
+
+	if (strcmp(subsystem, "net") == 0 ||
+	    strstr(path, "/class/net/") != NULL)
+		return 'n';
+
+	if (strstr(path, "/class/") != NULL)
+		return 'c';
+
+	return '\0';
+}
+
 int file_map(const char *filename, char **buf, size_t *bufsize)
 {
 	struct stat stats;

===== udev_lib.h 1.2 vs edited =====

udev_lib.h +1 -0
@@ -71,6 +71,7 @@
 extern char *get_devnode(void);
 extern char *get_seqnum(void);
 extern char *get_subsystem(char *subsystem);
+extern char get_device_type(const char *path, const char *subsystem);
 extern int file_map(const char *filename, char **buf, size_t *bufsize);
 extern void file_unmap(char *buf, size_t bufsize);
 extern size_t buf_get_line(char *buf, size_t buflen, size_t cur);

===== udevinfo.c 1.21 vs edited =====

udevinfo.c +15 -9
@@ -138,6 +138,10 @@
 	struct sysfs_device *sysfs_dev;
 	struct sysfs_device *sysfs_dev_parent;
 	int retval = 0;
+	char type;
+
+	type = get_device_type(path, "");
+	dbg("device type is %c", type);
 
 	/*  get the class dev */
 	class_dev = sysfs_open_class_device_path(path);
@@ -146,21 +150,23 @@
 		return -1;
 	}
 
-	/* read the 'dev' file for major/minor*/
-	attr = sysfs_get_classdev_attr(class_dev, "dev");
-	if (attr == NULL) {
-		printf("couldn't get the \"dev\" file\n");
-		retval = -1;
-		goto exit;
-	}
-
 	printf("\nudevinfo starts with the device the node belongs to and then walks up the\n"
 	       "device chain, to print for every device found, all possibly useful attributes\n"
 	       "in the udev key format.\n"
 	       "Only attributes within one device section may be used together in one rule,\n"
 	       "to match the device for which the node will be created.\n"
 	       "\n");
-	printf("device '%s' has major:minor %s", class_dev->path, attr->value);
+
+	if (type == 'b' || type =='c') {
+		/* read the 'dev' file for major/minor*/
+		attr = sysfs_get_classdev_attr(class_dev, "dev");
+		if (attr == NULL) {
+			printf("couldn't get the \"dev\" file\n");
+			retval = -1;
+			goto exit;
+		}
+		printf("device '%s' has major:minor %s", class_dev->path, attr->value);
+	}
 
 	/* open sysfs class device directory and print all attributes */
 	printf("  looking at class device '%s':\n", class_dev->path);

===== test/udev-test.pl 1.52 vs edited =====

test/udev-test.pl +55 -55

440 lines not on this page · raw message

#4 lore raw
In reply to Kay Sievers
Quoted 6 lines · Kay Sievers
Quoted 5 lines · Greg KH
Quoted 3 lines · Kay Sievers

Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and I want to bring the question back, if we want to handle net device naming with udev.

Quoted 8 lines
Quoted 6 lines

Ok, in looking at this patch, I think we should do this, it's just too simple not to :)

I wanted to apply this patch, but there are too many big conflicts with the current bk tree. Care to fix up the merge issues and send me a new one?

Here is the actual version.

Looks good, I've applied it.

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel