在Linux
内核2.6.13-rc3
以前,驱动和设备之间的绑定和解绑只能通过insmod(modprobe)
和rmmod
来实现,但是这种实现方法有一个弊端,就是一旦绑定或者解绑定都是针对驱动与其所支持的所有设备之间进行,无法实现驱动单独绑定或者解绑定一个设备。然而,在Linux
内核2.6.13-rc3
以后,提供了在用户空间动态的绑定和解绑定驱动与设备之间关系的功能,这样就解决了前面提到的无法实现驱动单独绑定或者解绑定一个设备的问题。
PCI
(peripheral component interconnect
,外围设备互联)总线由PCI Host Bridge
(PCI
主桥)或者PCI Bus Bridge
(PCI
桥)管理,用来连接各类PCI
设备,如声卡、网卡和PCI
桥等。在一个处理器系统中,可以通过PCI
桥扩展PCI
总线,形成PCI
总线树型结构,如下图:Bus0
上的Device2
是一个PCI
桥,由该桥拓展出Bus1
。
在处理器系统中有几个HOST
主桥,就有几棵PCI
总线树,而每一棵PCI
总线树都与一个PCI Domain
(PCI
总线域)对应,上图就是一个总线域。每个PCI
设备都有一个vendor ID
(供应商号)和device ID
(设备号)。
lspci
用来显示所有的PCI
总线和连接在PCI
总线上的设备,在一台Linux
设备上敲一敲lspci
,通常得到类似如下信息:
[root@localhost ~]# lspci
00:00.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse Root Complex
00:00.2 IOMMU: Advanced Micro Devices, Inc. [AMD] Starship/Matisse IOMMU
00:01.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:01.1 PCI bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse GPP Bridge
00:02.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:03.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:04.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:05.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:07.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:07.1 PCI bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse Internal PCIe GPP Bridge 0 to bus[E:B]
00:08.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse PCIe Dummy Host Bridge
00:08.1 PCI bridge: Advanced Micro Devices, Inc. [AMD] Starship/Matisse Internal PCIe GPP Bridge 0 to bus[E:B]
00:14.0 SMBus: Advanced Micro Devices, Inc. [AMD] FCH SMBus Controller (rev 61)
00:14.3 ISA bridge: Advanced Micro Devices, Inc. [AMD] FCH LPC Bridge (rev 51)
00:18.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 0
00:18.1 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 1
00:18.2 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 2
00:18.3 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 3
00:18.4 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 4
00:18.5 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 5
00:18.6 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 6
00:18.7 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 7
00:19.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 0
00:19.1 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 1
00:19.2 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 2
00:19.3 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 3
00:19.4 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 4
00:19.5 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 5
00:19.6 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 6
00:19.7 Host bridge: Advanced Micro Devices, Inc. [AMD] Starship Device 24; Function 7
01:00.0 Ethernet controller: Corigine, Inc. Device 4000
......
用其中最常见的以太网卡信息来剖析下各个参数的含义:
01:00.0 Ethernet controller: Corigine, Inc. Device 4000
PCI
总线域为0000
,bus
总线号为02
,槽位号为01
,功能号为0
(lspci
默认省略总线域0000
,可以加参数-D
显示)。Device classes,Device subclasses,Program interfaces
(一般省略),通过加参数 -nn
可以看到Ethernet controller
对应的设备类型号为0200
,其中02
指Device classes
,00
指Device subclasses
,通过The PCI ID Repository
(http://pci-ids.ucw.cz/read/PD/02/00
)在线网站也可以查看。[root@localhost ~]# lspci -nn -D -s 01:00.0
0000:01:00.0 Ethernet controller [0200]: Corigine, Inc. Device [1da8:4000]
Corigine
公司,如上面所示,对应了供应商号1da8
和设备号4000
,可通过如下方式查看,也可以通过device hunt``(https://devicehunt.com/)
在线网站查看。[root@localhost ~]# lspci -nvmms 01:00.0
Slot: 01:00.0
Class: 0200
Vendor: 1da8
Device: 4000
SVendor: 1da8
SDevice: 0bf9
PhySlot: 2
NUMANode: 0
IOMMUGroup: 13
注意 :在
Linux
中,PCI
设备的设备名称(Device Name
)通常以domain:bus:slot:function
的形式来表示,如图所示:其中冒号分隔开的各个数字具有以下含义:
domain
:表示PCI
设备所在的PCI
总线域(Domain
),通常为一个16
位的十六进制数,用于区分不同的PCI
总线域。在大多数情况下,这个值为0000
。bus
:表示PCI
设备所在的总线(Bus
),通常为一个8
位的十六进制数,用于区分不同的总线。一个系统可以具有多个总线。slot
:表示PCI
设备所在的插槽(Slot
),通常为一个5
位的十六进制数,用于区分不同的插槽。一个总线上可以有多个插槽。function
:表示PCI
设备的功能(Function
),通常为一个3
位的十六进制数,用于区分同一插槽上的不同功能。一个插槽上可以有多个功能。一个PCI
物理设备可以实现多个功能设备,且逻辑功能相互独立,其实就是 硬件虚拟化 。通过这种编号方式,可以唯一标识一个
PCI
设备的位置信息。在上述示例中,"0000:01:00.0" 表示该设备位于 PCI 总线域 0000,总线 01,插槽 00,功能 0 。请注意,这些数字可能会因系统配置而有所不同,具体取决于你的系统和相应的
PCI
设备。
此处,我们以PCI总线的Corigine
网卡设备及其驱动为例,讲述网卡设备及其驱动的绑定和解绑定过程。首先,执行lspci
显示所用机器上的所有网卡设备:
[root@localhost ~]# lspci | grep Eth
01:00.0 Ethernet controller: Corigine, Inc. Device 4000
21:00.0 Ethernet controller: Corigine, Inc. Network Flow Processor 3800
41:00.0 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
41:00.1 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
41:00.2 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
41:00.3 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
对于所有的Corigine
网卡设备,都可以在nfp
驱动下看到。其中,bind
和unbind
文件就是涉及到绑定和解绑的关键文件。
[root@localhost ~]# ls -l /sys/bus/pci/drivers/nfp
total 0
lrwxrwxrwx 1 root root 0 Nov 14 05:17 0000:01:00.0 - > ../../../../devices/pci0000:00/0000:00:01.1/0000:01:00.0
lrwxrwxrwx 1 root root 0 Nov 14 05:17 0000:21:00.0 - > ../../../../devices/pci0000:20/0000:20:03.2/0000:21:00.0
--w------- 1 root root 4096 Nov 14 05:17 bind
lrwxrwxrwx 1 root root 0 Jun 28 07:58 module - > ../../../../module/nfp
--w------- 1 root root 4096 Nov 14 05:17 new_id
--w------- 1 root root 4096 Nov 14 05:17 remove_id
--w------- 1 root root 4096 Jun 8 17:34 uevent
--w------- 1 root root 4096 Nov 14 05:17 unbind
这里我们以PCI
总线BDF
号为01:00.0
的Corigine
网卡为例,解绑该设备,只需将设备的带PCI
总线域的BDF
号写入/sys/bus/pci/drivers/nfp/unbind
(不同的设备,驱动不同,即这里的nfp
)即可:
[root@localhost ~]# echo 0000:01:00.0 > /sys/bus/pci/drivers/nfp/unbind
# 解除绑定成功,再查看目录下文件,该驱动下不再有对应的设备。
[root@localhost ~]# ls -l /sys/bus/pci/drivers/nfp
total 0
lrwxrwxrwx 1 root root 0 Nov 14 05:17 0000:21:00.0 - > ../../../../devices/pci0000:20/0000:20:03.2/0000:21:00.0
--w------- 1 root root 4096 Nov 14 05:17 bind
lrwxrwxrwx 1 root root 0 Jun 28 07:58 module - > ../../../../module/nfp
--w------- 1 root root 4096 Nov 14 05:17 new_id
--w------- 1 root root 4096 Nov 14 05:17 remove_id
--w------- 1 root root 4096 Jun 8 17:34 uevent
--w------- 1 root root 4096 Nov 14 05:23 unbind
执行解绑定操作以后,我们可以通过lshw
命令查看PCI
设备:
[root@localhost ~]# lshw -c network -businfo
Bus info Device Class Description
=======================================================
pci@0000:01:00.0 /dev/fb0 network Corigine, Inc.
pci@0000:21:00.0 ens1np0 network Corigine, Inc.
pci@0000:41:00.0 eno1 network I350 Gigabit Network Connection
pci@0000:41:00.1 ens16f1 network I350 Gigabit Network Connection
pci@0000:41:00.2 ens16f2 network I350 Gigabit Network Connection
pci@0000:41:00.3 ens16f3 network I350 Gigabit Network Connection
pci@0000:21:00.0 ens1np1 network Ethernet interface
通过上述结果可知,BDF
号为01:00.0
的设备,在解绑定nfp
驱动和该设备以后,该设备接口名称已无法正常显示,而同样使用nfp
驱动的其他PCI
设备,如上面BDF
号为21:00.0
的设备,则接口名称正常显示,说明驱动和该设备正常绑定。另外,也可通过如下方式对比两个PCI
设备此时的区别:
[root@localhost ~]# echo 0000:01:00.0 > /sys/bus/pci/drivers/nfp/unbind
[root@localhost ~]# lspci -s 01:00.0 -v
01:00.0 Ethernet controller: Corigine, Inc. Device 4000
Subsystem: Corigine, Inc. Device 0bf9
Physical Slot: 2
Flags: fast devsel, IRQ 117, NUMA node 0, IOMMU group 13
Memory at 340a0000000 (64-bit, prefetchable) [size=128M]
Memory at 340a8000000 (64-bit, prefetchable) [size=64M]
Memory at 340ac000000 (64-bit, prefetchable) [size=16M]
Expansion ROM at f5000000 [disabled] [size=16M]
Capabilities: [80] Power Management version 3
Capabilities: [b0] MSI-X: Enable- Count=256 Masked-
Capabilities: [c0] Express Endpoint, MSI 00
Capabilities: [100] Advanced Error Reporting
Capabilities: [140] Alternative Routing-ID Interpretation (ARI)
Capabilities: [150] Device Serial Number 88-3c-c5-a0-18-6d-10-ff
Capabilities: [200] Single Root I/O Virtualization (SR-IOV)
Capabilities: [300] Secondary PCI Express
Kernel modules: nfp
[root@localhost ~]# lspci -s 21:00.0 -v
21:00.0 Ethernet controller: Corigine, Inc. Network Flow Processor 3800
Subsystem: Corigine, Inc. Device 7feb
Physical Slot: 1
Flags: bus master, fast devsel, latency 0, IRQ 213, NUMA node 0, IOMMU group 29
Memory at 2c070000000 (64-bit, prefetchable) [size=128M]
Memory at 2c078000000 (64-bit, prefetchable) [size=64M]
Memory at 2c07c000000 (64-bit, prefetchable) [size=16M]
Expansion ROM at b0000000 [disabled] [size=16M]
Capabilities: [88] Power Management version 3
Capabilities: [b0] MSI-X: Enable+ Count=64 Masked-
Capabilities: [c0] Express Endpoint, MSI 00
Capabilities: [100] Advanced Error Reporting
Capabilities: [140] Alternative Routing-ID Interpretation (ARI)
Capabilities: [150] Device Serial Number 88-3c-c5-a0-32-1e-10-ff
Capabilities: [160] Power Budgeting < ? >
Capabilities: [1b8] Latency Tolerance Reporting
Capabilities: [200] Single Root I/O Virtualization (SR-IOV)
Capabilities: [300] Secondary PCI Express
Capabilities: [400] Vendor Specific Information: ID=0001 Rev=1 Len=010 < ? >
Kernel driver in use: nfp
Kernel modules: nfp
根据上述结果可知,BDF
号为01:00.0
的PCI
设备较BDF
号为21:00.0
的PCI
设备明显少了一行Kernel driver in use: nfp
,说明BDF
号为01:00.0
的PCI
设备已与nfp
驱动解绑定,即nfp
驱动未在使用中。而BDF
号为21:00.0
的PCI
设备则正在与nfp
驱动绑定中,即nfp
驱动正在使用中。
绑定一个网卡设备,和解绑类似,将设备的带PCI
总线域的BDF
号写入/sys/bus/pci/drivers/nfp/bind
(不同的设备,驱动不同,即这里的nfp
):
[root@localhost ~]# echo 0000:01:00.0 > /sys/bus/pci/drivers/nfp/bind
[root@localhost ~]# ls -l /sys/bus/pci/drivers/nfp
total 0
lrwxrwxrwx 1 root root 0 Nov 14 05:28 0000:01:00.0 - > ../../../../devices/pci0000:00/0000:00:01.1/0000:01:00.0
lrwxrwxrwx 1 root root 0 Nov 14 05:17 0000:21:00.0 - > ../../../../devices/pci0000:20/0000:20:03.2/0000:21:00.0
--w------- 1 root root 4096 Nov 14 05:27 bind
lrwxrwxrwx 1 root root 0 Jun 28 07:58 module - > ../../../../module/nfp
--w------- 1 root root 4096 Nov 14 05:17 new_id
--w------- 1 root root 4096 Nov 14 05:17 remove_id
--w------- 1 root root 4096 Jun 8 17:34 uevent
--w------- 1 root root 4096 Nov 14 05:23 unbind
执行绑定操作以后,我们可以通过lshw
命令查看PCI
设备:
[root@localhost ~]# lshw -c network -businfo
Bus info Device Class Description
=======================================================
pci@0000:01:00.0 ens2np0 network Corigine, Inc.
pci@0000:21:00.0 ens1np0 network Corigine, Inc.
pci@0000:41:00.0 eno1 network I350 Gigabit Network Connection
pci@0000:41:00.1 ens16f1 network I350 Gigabit Network Connection
pci@0000:41:00.2 ens16f2 network I350 Gigabit Network Connection
pci@0000:41:00.3 ens16f3 network I350 Gigabit Network Connection
pci@0000:21:00.0 ens1np1 network Ethernet interface
pci@0000:01:00.0 ens2np1 network Ethernet interface
通过上述结果可知,BDF
号为01:00.0
的设备,在绑定nfp
驱动和该设备以后,该设备接口名称已正常显示,且使用如下命令时,也显示nfp
驱动正在使用中。
[root@localhost ~]# echo 0000:01:00.0 > /sys/bus/pci/drivers/nfp/bind
[root@localhost ~]# lspci -s 01:00.0 -v
01:00.0 Ethernet controller: Corigine, Inc. Device 4000
Subsystem: Corigine, Inc. Device 0bf9
Physical Slot: 2
Flags: bus master, fast devsel, latency 0, IRQ 117, NUMA node 0, IOMMU group 13
Memory at 340a0000000 (64-bit, prefetchable) [size=128M]
Memory at 340a8000000 (64-bit, prefetchable) [size=64M]
Memory at 340ac000000 (64-bit, prefetchable) [size=16M]
Expansion ROM at f5000000 [disabled] [size=16M]
Capabilities: [80] Power Management version 3
Capabilities: [b0] MSI-X: Enable+ Count=256 Masked-
Capabilities: [c0] Express Endpoint, MSI 00
Capabilities: [100] Advanced Error Reporting
Capabilities: [140] Alternative Routing-ID Interpretation (ARI)
Capabilities: [150] Device Serial Number 88-3c-c5-a0-18-6d-10-ff
Capabilities: [200] Single Root I/O Virtualization (SR-IOV)
Capabilities: [300] Secondary PCI Express
Kernel driver in use: nfp
Kernel modules: nfp
bind
是PCI
设备绑定驱动过程中会使用到的文件,unbind
是解绑PCI
设备与驱动过程中会使用到的文件。具体的绑定与解绑的过程就是向这两个文件中写入规定格式的数据完成的。
对 bind
文件写入每一个接口的 PCI
号意味着我们可以将一个网卡上的不同口绑定到不同的驱动上。对unbind
文件写入接口的 PCI
号就会解除当前绑定的驱动。一个接口可以不绑定到任何驱动上面,不过我们常常不会这样去做。
这里,我们只讲了bind
和unbind
文件,其实PCI
设备接口绑定驱动可能还会与new_id
文件有关,new_id
主要用于动态的向PCI
设备驱动中添加一个新的设备ID
,这种功能允许驱动添加更多的硬件而非仅包含在编译时驱动静态支持设备 ID
列表中的硬件。 写入这个文件的格式中,Vendor Id 与 Device ID 字段是必须的,其它的字段可以不指定 。格式如下:
[root@localhost ~]# echo "1da8 4000" > /sys/bus/pci/drivers/nfp/new_id
成功添加一个设备 ID
时,驱动会尝试 probe
系统中匹配到的设备并尝试绑定到它之上。
既然有添加新的PCI
设备的文件,那一定有移除新的PCI
设备的文件,即与new_id
位于同一目录下的remove_id
。
remove_id
的写入格式与 new_id
的写入格式相同。写入 remove_id
可以用来确保内核不会自动 probe
匹配到这个驱动的设备。格式如下:
[root@localhost ~]# echo "1da8 4000" > /sys/bus/pci/drivers/nfp/remove_id
至此,关于驱动与单个PCI
设备的绑定和解绑定就介绍完了。
全部0条评论
快来发表一下你的评论吧 !