为什么 dev.bus 在我的设备中为 NULL?

发布于 2024-11-08 18:12:33 字数 348 浏览 0 评论 0原文

我试图了解Linux设备/驱动程序模型是如何工作的,为此我编写了一个小模块。这个模块很简单,通过函数dev_get_by_name(&init_net, "eth0")检索指向struct net_device(我们称之为netdev)的指针。为什么netdev->dev.bus的值为NULL?该指针是否应该代表我的设备所连接的 bus_type 结构?然而,字段netdev->parent->bus不是NULL,但它应该代表eth控制器的总线......有什么解释吗?

i'm trying to understand how linux device/driver model works and to do this i've written a little module. This module is simple, retrieves a pointer to a struct net_device (let's call it netdev) by the function dev_get_by_name(&init_net, "eth0"). Why the value of netdev->dev.bus is NULL? Should that pointer represent the bus_type structure on which my device is attached? The field netdev->parent->bus is however not NULL but it should represent the bus for eth controller...any explanation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-11-15 18:12:33

这是因为你的 eth 设备,或者更好地说它在内核中的设备“对象”,不是总线,因此它的总线值是未初始化的。但其父设备通常位于总线上,并且父设备知道其所在的总线就足够了,因为两个设备最终在驱动程序初始化期间链接。

让我们看一个例子:这是我在 sysfs 中的 eth0 设备的内容(注意设备字段):

$ ll /sys/class/net/eth0/
total 0
-r--r--r-- 1 root root 4096 May 20 11:10 address
-r--r--r-- 1 root root 4096 May 20 11:10 addr_len
-r--r--r-- 1 root root 4096 May 20 11:10 broadcast
-r--r--r-- 1 root root 4096 May 20 11:10 carrier
lrwxrwxrwx 1 root root    0 May 20 11:10 device -> ../../../devices/pci0000:00/0000:00:19.0
-r--r--r-- 1 root root 4096 May 20 11:10 dev_id
-r--r--r-- 1 root root 4096 May 20 11:10 dormant
-r--r--r-- 1 root root 4096 May 20 11:10 features
-rw-r--r-- 1 root root 4096 May 20 11:10 flags
-rw-r--r-- 1 root root 4096 May 20 11:10 ifalias
-r--r--r-- 1 root root 4096 May 20 11:10 ifindex
-r--r--r-- 1 root root 4096 May 20 11:10 iflink
-r--r--r-- 1 root root 4096 May 20 11:10 link_mode
-rw-r--r-- 1 root root 4096 May 20 11:10 mtu
-r--r--r-- 1 root root 4096 May 20 11:10 operstate
drwxr-xr-x 2 root root    0 May 20 11:10 power
drwxr-xr-x 2 root root    0 May 20 11:10 statistics
lrwxrwxrwx 1 root root    0 May 20 11:10 subsystem -> ../../net
-rw-r--r-- 1 root root 4096 May 20 11:10 tx_queue_len
-r--r--r-- 1 root root 4096 May 20 11:10 type
-rw-r--r-- 1 root root 4096 May 20 11:10 uevent

设备的链接是根据驱动程序探测函数的这段代码创建的,其中 netdev 是网络设备,并且pdev 关联的 PCI 设备:

SET_NETDEV_DEV(netdev, &pdev->dev);

根据文档,这是:

/* Set the sysfs physical device reference for the network logical device
 * if set prior to registration will cause a symlink during initialization.
 */
#define SET_NETDEV_DEV(net, pdev)   ((net)->dev.parent = (pdev))

这是我在相应 PCI 设备中的内容,由 SET_NETDEV_DEV 设置(您可以在其中注意到总线字段):

$ ll /sys/devices/pci0000\:00/0000\:00\:19.0/
total 0
-rw-r--r-- 1 root root   4096 May 20 11:54 broken_parity_status
lrwxrwxrwx 1 root root      0 May 20 11:22 bus -> ../../../bus/pci
-r--r--r-- 1 root root   4096 May 20 11:07 class
-rw-r--r-- 1 root root    256 May 20 11:22 config
-r--r--r-- 1 root root   4096 May 20 11:54 device
lrwxrwxrwx 1 root root      0 May 20 11:22 driver -> ../../../bus/pci/drivers/e1000e
-rw------- 1 root root   4096 May 20 11:22 enable
-r--r--r-- 1 root root   4096 May 20 11:07 irq
-r--r--r-- 1 root root   4096 May 20 11:54 local_cpulist
-r--r--r-- 1 root root   4096 May 20 11:07 local_cpus
-r--r--r-- 1 root root   4096 May 20 11:22 modalias
-rw-r--r-- 1 root root   4096 May 20 11:22 msi_bus
lrwxrwxrwx 1 root root      0 May 20 11:22 net:eth0 -> ../../../class/net/eth0
drwxr-xr-x 2 root root      0 May 20 11:11 power
-r--r--r-- 1 root root   4096 May 20 11:22 resource
-rw------- 1 root root 131072 May 20 11:22 resource0
-rw------- 1 root root   4096 May 20 11:22 resource1
-rw------- 1 root root     32 May 20 11:22 resource2
lrwxrwxrwx 1 root root      0 May 20 11:22 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root   4096 May 20 11:22 subsystem_device
-r--r--r-- 1 root root   4096 May 20 11:22 subsystem_vendor
-rw-r--r-- 1 root root   4096 May 20 11:22 uevent
-r--r--r-- 1 root root   4096 May 20 11:22 vendor

我希望这能够澄清情况。

This is because your eth device, or better said its device "object" in the kernel, is not a bus and thus its bus value is left unitialized. But its parent device usually is on a bus and it is sufficient that the parent device knows the bus it is on, since both device eventually are linked during the driver initialization.

Let's have a look at an example: here is what I have in sysfs for my eth0 device (notice the device field):

$ ll /sys/class/net/eth0/
total 0
-r--r--r-- 1 root root 4096 May 20 11:10 address
-r--r--r-- 1 root root 4096 May 20 11:10 addr_len
-r--r--r-- 1 root root 4096 May 20 11:10 broadcast
-r--r--r-- 1 root root 4096 May 20 11:10 carrier
lrwxrwxrwx 1 root root    0 May 20 11:10 device -> ../../../devices/pci0000:00/0000:00:19.0
-r--r--r-- 1 root root 4096 May 20 11:10 dev_id
-r--r--r-- 1 root root 4096 May 20 11:10 dormant
-r--r--r-- 1 root root 4096 May 20 11:10 features
-rw-r--r-- 1 root root 4096 May 20 11:10 flags
-rw-r--r-- 1 root root 4096 May 20 11:10 ifalias
-r--r--r-- 1 root root 4096 May 20 11:10 ifindex
-r--r--r-- 1 root root 4096 May 20 11:10 iflink
-r--r--r-- 1 root root 4096 May 20 11:10 link_mode
-rw-r--r-- 1 root root 4096 May 20 11:10 mtu
-r--r--r-- 1 root root 4096 May 20 11:10 operstate
drwxr-xr-x 2 root root    0 May 20 11:10 power
drwxr-xr-x 2 root root    0 May 20 11:10 statistics
lrwxrwxrwx 1 root root    0 May 20 11:10 subsystem -> ../../net
-rw-r--r-- 1 root root 4096 May 20 11:10 tx_queue_len
-r--r--r-- 1 root root 4096 May 20 11:10 type
-rw-r--r-- 1 root root 4096 May 20 11:10 uevent

The link for the device is created from this code from the driver probe function, where netdev is the network device, and pdev the associated PCI device:

SET_NETDEV_DEV(netdev, &pdev->dev);

Which according to the documentation is:

/* Set the sysfs physical device reference for the network logical device
 * if set prior to registration will cause a symlink during initialization.
 */
#define SET_NETDEV_DEV(net, pdev)   ((net)->dev.parent = (pdev))

And here is what I have in the corresponding PCI device, that was set by SET_NETDEV_DEV (where you can notice the bus field):

$ ll /sys/devices/pci0000\:00/0000\:00\:19.0/
total 0
-rw-r--r-- 1 root root   4096 May 20 11:54 broken_parity_status
lrwxrwxrwx 1 root root      0 May 20 11:22 bus -> ../../../bus/pci
-r--r--r-- 1 root root   4096 May 20 11:07 class
-rw-r--r-- 1 root root    256 May 20 11:22 config
-r--r--r-- 1 root root   4096 May 20 11:54 device
lrwxrwxrwx 1 root root      0 May 20 11:22 driver -> ../../../bus/pci/drivers/e1000e
-rw------- 1 root root   4096 May 20 11:22 enable
-r--r--r-- 1 root root   4096 May 20 11:07 irq
-r--r--r-- 1 root root   4096 May 20 11:54 local_cpulist
-r--r--r-- 1 root root   4096 May 20 11:07 local_cpus
-r--r--r-- 1 root root   4096 May 20 11:22 modalias
-rw-r--r-- 1 root root   4096 May 20 11:22 msi_bus
lrwxrwxrwx 1 root root      0 May 20 11:22 net:eth0 -> ../../../class/net/eth0
drwxr-xr-x 2 root root      0 May 20 11:11 power
-r--r--r-- 1 root root   4096 May 20 11:22 resource
-rw------- 1 root root 131072 May 20 11:22 resource0
-rw------- 1 root root   4096 May 20 11:22 resource1
-rw------- 1 root root     32 May 20 11:22 resource2
lrwxrwxrwx 1 root root      0 May 20 11:22 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root   4096 May 20 11:22 subsystem_device
-r--r--r-- 1 root root   4096 May 20 11:22 subsystem_vendor
-rw-r--r-- 1 root root   4096 May 20 11:22 uevent
-r--r--r-- 1 root root   4096 May 20 11:22 vendor

I hope this clarifies the situtation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文