Skip to contentSkip to conversation

net-sysfs: expose IRQ number

PATCH In review Rafał Miłecki 3 participants 4 messages Apr 29, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net-sysfs: expose IRQ number

From: Rafał Miłecki <[email protected]>

Knowing IRQ number makes e.g. reading /proc/interrupts much simpler. It's more reliable than guessing device name used by a driver when calling request_irq().

---
I found a script parsing /proc/interrupts for a given interface name. It wasn't working for me as it assumed request_irq() was called with a device name. It's not a case for all drivers.

I also found some other people looking for a proper solution for that:
https://unix.stackexchange.com/questions/275075/programmatically-determine-the-irqs-associated-with-a-network-interface
https://stackoverflow.com/questions/7516984/retrieving-irq-number-of-a-nic

Let me know if this solution makes sense. I can say it works for me ;)

---
 Documentation/ABI/testing/sysfs-class-net |  7 +++++++
 net/core/net-sysfs.c                      | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)
Documentation/ABI/testing/sysfs-class-net +7 -0
index 664a8f6a634f..33440fe77ca7 100644
@@ -301,3 +301,10 @@ Contact:	[email protected]
 Description:
 		32-bit unsigned integer counting the number of times the link has
 		been down
+
+What:		/sys/class/net/<iface>/irq
+Date:		April 2019
+KernelVersion:	5.2
+Contact:	[email protected]
+Description:
+		IRQ number used by device
net/core/net-sysfs.c +16 -0
index e4fd68389d6f..a3eb7c3f1f37 100644
@@ -512,6 +512,21 @@ static ssize_t phys_switch_id_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(phys_switch_id);
 
+static ssize_t irq_show(struct device *dev, struct device_attribute *attr,
+			char *buf)
+{
+	const struct net_device *netdev = to_net_dev(dev);
+	ssize_t ret;
+
+	if (!rtnl_trylock())
+		return restart_syscall();
+	ret = sprintf(buf, "%d\n", netdev->irq);
+	rtnl_unlock();
+
+	return ret;
+}
+static DEVICE_ATTR_RO(irq);
+
 static struct attribute *net_class_attrs[] __ro_after_init = {
 	&dev_attr_netdev_group.attr,
 	&dev_attr_type.attr,
@@ -542,6 +557,7 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
 	&dev_attr_proto_down.attr,
 	&dev_attr_carrier_up_count.attr,
 	&dev_attr_carrier_down_count.attr,
+	&dev_attr_irq.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(net_class);
Signature · 1 line
2.21.0
#1 lore raw
Quoted 13 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

Knowing IRQ number makes e.g. reading /proc/interrupts much simpler. It's more reliable than guessing device name used by a driver when calling request_irq().

---
I found a script parsing /proc/interrupts for a given interface name. It wasn't working for me as it assumed request_irq() was called with a device name. It's not a case for all drivers.

Quoted 5 lines

I also found some other people looking for a proper solution for that:
https://unix.stackexchange.com/questions/275075/programmatically-determine-the-irqs-associated-with-a-network-interface
https://stackoverflow.com/questions/7516984/retrieving-irq-number-of-a-nic

Let me know if this solution makes sense. I can say it works for me ;)

If parsing /proc/interrupts is problematic, also see the /sys/kernel/irq interface added in commit ecb3f394c5db ("genirq:
Expose interrupt information through sysfs"). Does that address your use-case as well? Though it does the inverse mapping from IRQ to name.

Another practical issue is that many network devices register more than one interrupt. Perhaps one control interrupt, plus one per receive queue and possibly one per transmit queue. Unfortunately those cannot be identified in a driver-independent manner. Because an interface /sys/class/net/$DEV/queues/rx-$i/irq would be useful.

#2 lore raw
In reply to Rafał Miłecki
Quoted 64 lines · On Mon, 29 Apr 2019 08:01:07 +0200 Rafał Miłecki <[email protected]> wrote:

From: Rafał Miłecki <[email protected]>

Knowing IRQ number makes e.g. reading /proc/interrupts much simpler. It's more reliable than guessing device name used by a driver when calling request_irq().

---
I found a script parsing /proc/interrupts for a given interface name. It wasn't working for me as it assumed request_irq() was called with a device name. It's not a case for all drivers.

I also found some other people looking for a proper solution for that:
https://unix.stackexchange.com/questions/275075/programmatically-determine-the-irqs-associated-with-a-network-interface
https://stackoverflow.com/questions/7516984/retrieving-irq-number-of-a-nic

Let me know if this solution makes sense. I can say it works for me ;)

---
 Documentation/ABI/testing/sysfs-class-net |  7 +++++++
 net/core/net-sysfs.c                      | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)
Documentation/ABI/testing/sysfs-class-net +7 -0
index 664a8f6a634f..33440fe77ca7 100644
@@ -301,3 +301,10 @@ Contact:	[email protected]
 Description:
 		32-bit unsigned integer counting the number of times the link has
 		been down
+
+What:		/sys/class/net/<iface>/irq
+Date:		April 2019
+KernelVersion:	5.2
+Contact:	[email protected]
+Description:
+		IRQ number used by device
net/core/net-sysfs.c +15 -0
index e4fd68389d6f..a3eb7c3f1f37 100644
@@ -512,6 +512,21 @@ static ssize_t phys_switch_id_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(phys_switch_id);
 
+static ssize_t irq_show(struct device *dev, struct device_attribute *attr,
+			char *buf)
+{
+	const struct net_device *netdev = to_net_dev(dev);
+	ssize_t ret;
+
+	if (!rtnl_trylock())
+		return restart_syscall();
+	ret = sprintf(buf, "%d\n", netdev->irq);
+	rtnl_unlock();
+
+	return ret;
+}
+static DEVICE_ATTR_RO(irq);
+
 static struct attribute *net_class_attrs[] __ro_after_init = {
 	&dev_attr_netdev_group.attr,
 	&dev_attr_type.attr,

net/core/net-sysfs.c static struct attribute *net_class_attrs[] __ro_after_init = { · line 563

Quoted 7 lines
net/core/net-sysfs.c +1 -0
@@ -542,6 +557,7 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
 	&dev_attr_proto_down.attr,
 	&dev_attr_carrier_up_count.attr,
 	&dev_attr_carrier_down_count.attr,
+	&dev_attr_irq.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(net_class);

Can't you find this on the PCI side already?

$ ls /sys/class/net/eno1/device/msi_irqs/
37  38  39  40  41
#3 lore raw
Quoted 76 lines · Stephen Hemminger
Quoted 64 lines · On Mon, 29 Apr 2019 08:01:07 +0200 Rafał Miłecki <[email protected]> wrote:

From: Rafał Miłecki <[email protected]>

Knowing IRQ number makes e.g. reading /proc/interrupts much simpler. It's more reliable than guessing device name used by a driver when calling request_irq().

---
I found a script parsing /proc/interrupts for a given interface name. It wasn't working for me as it assumed request_irq() was called with a device name. It's not a case for all drivers.

I also found some other people looking for a proper solution for that:
https://unix.stackexchange.com/questions/275075/programmatically-determine-the-irqs-associated-with-a-network-interface
https://stackoverflow.com/questions/7516984/retrieving-irq-number-of-a-nic

Let me know if this solution makes sense. I can say it works for me ;)

---
 Documentation/ABI/testing/sysfs-class-net |  7 +++++++
 net/core/net-sysfs.c                      | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)
Documentation/ABI/testing/sysfs-class-net +7 -0
index 664a8f6a634f..33440fe77ca7 100644
@@ -301,3 +301,10 @@ Contact: [email protected]
 Description:
              32-bit unsigned integer counting the number of times the link has
              been down
+
+What:                /sys/class/net/<iface>/irq
+Date:                April 2019
+KernelVersion:       5.2
+Contact:     [email protected]
+Description:
+             IRQ number used by device
net/core/net-sysfs.c +15 -0
index e4fd68389d6f..a3eb7c3f1f37 100644
@@ -512,6 +512,21 @@ static ssize_t phys_switch_id_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(phys_switch_id);
 
+static ssize_t irq_show(struct device *dev, struct device_attribute *attr,
+                     char *buf)
+{
+     const struct net_device *netdev = to_net_dev(dev);
+     ssize_t ret;
+
+     if (!rtnl_trylock())
+             return restart_syscall();
+     ret = sprintf(buf, "%d\n", netdev->irq);
+     rtnl_unlock();
+
+     return ret;
+}
+static DEVICE_ATTR_RO(irq);
+
 static struct attribute *net_class_attrs[] __ro_after_init = {
      &dev_attr_netdev_group.attr,
      &dev_attr_type.attr,

net/core/net-sysfs.c static struct attribute *net_class_attrs[] __ro_after_init = { · line 563

Quoted 7 lines
net/core/net-sysfs.c +1 -0
@@ -542,6 +557,7 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
      &dev_attr_proto_down.attr,
      &dev_attr_carrier_up_count.attr,
      &dev_attr_carrier_down_count.attr,
+     &dev_attr_irq.attr,
      NULL,
 };
 ATTRIBUTE_GROUPS(net_class);

Can't you find this on the PCI side already?

Quoted 2 lines
$ ls /sys/class/net/eno1/device/msi_irqs/
37  38  39  40  41

I'm dealing with bgmac supported ethernet device on bcma bus (not PCI). I could make bcma bus provide IRQ numbers, but I thought something at net subsystem level will be more generic.

I'm going to review Willem's solution/idea for now.

Signature · 1 line
Rafał