Skip to content

Commit 1302519

Browse files
committed
feat: add a FreeBSD platform (pciconf/devctl/camcontrol/fstat)
Implement the Platform interface with FreeBSD's native tools: - pciconf -l enumerates devices. Both output formats are parsed: the packed chip=/card= fields of 14.3 and earlier, and the split vendor=/device= fields of 15.x and 14.4. - devctl detach / devctl set driver implement unbind and bind for kernel drivers. nic_uio cannot be attached that way: it only probes devices listed in the hw.nic_uio.bdfs tunable. Binding to nic_uio registers the device there, drops any driver pinned by an earlier set driver, and (re)loads the module, which detaches the previous driver and claims the device by itself. Binding back to a kernel driver removes the bdfs entry again. - camcontrol devlist maps an nvmeX controller to its CAM disk (ndaY), so the fstat in-use check also sees users of the disk device. - kldstat reports whether nic_uio is loaded. - scan_devices() honors the --device class-filter bypass, matching the Linux platform. - pciconf -w enables bus-mastering once the nic_uio attach is verified through pciconf -l. A failed write is reported instead of ignored. Helpers convert between the pciX:B:S:F selector and the domain:bus:device.function bdf form. nic_uio is added to KNOWN_DRIVERS, completion, and the docs. User-facing text drops its Linux assumptions: the --bind help says driver file instead of .ko file, and the memlock warning says DMA mapping instead of VFIO_IOMMU_MAP_DMA. Unit tests cover both pciconf formats, the fstat heuristic, kldstat probing, camcontrol parsing, the nic_uio bdfs flow, and the devctl bind for kernel drivers. Signed-off-by: Jaeyoon Choi <j_yoon.choi@samsung.com>
1 parent 7fc744d commit 1302519

4 files changed

Lines changed: 515 additions & 23 deletions

File tree

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@
77
[![Test](https://github.com/xnvme/devbind/actions/workflows/test.yml/badge.svg)](https://github.com/xnvme/devbind/actions/workflows/test.yml)
88

99
`devbind` is a small CLI for binding and unbinding PCI devices to a
10-
chosen kernel driver via sysfs. The typical use is moving a device
11-
between its native driver (e.g. `nvme`) and a user space driver
12-
framework (`vfio-pci`, `uio_pci_generic`) for DPDK/SPDK and xNVMe/uPCIe
13-
workloads. `devbind --list` also reports the process `RLIMIT_MEMLOCK`
14-
and warns when the soft limit is below the 64 MiB threshold those
15-
frameworks inherit.
10+
chosen kernel driver. The typical use is moving a device between its
11+
native driver (e.g. `nvme`) and a user space driver framework for
12+
DPDK/SPDK and xNVMe/uPCIe workloads. `devbind --list` also reports the
13+
process `RLIMIT_MEMLOCK` and warns when the soft limit is below the
14+
64 MiB threshold those frameworks inherit.
15+
16+
Both **Linux** and **FreeBSD** are supported; the platform-specific
17+
operations are handled by a platform implementation selected at runtime:
18+
19+
| | Linux | FreeBSD |
20+
|---|---|---|
21+
| enumerate / inspect | `lspci`, sysfs | `pciconf -l`, `camcontrol` |
22+
| user space framework | `vfio-pci`, `uio_pci_generic` | `nic_uio` |
23+
| unbind | sysfs `driver/unbind` | `devctl detach` |
24+
| bind (kernel driver) | sysfs `drivers/<drv>/bind` | `devctl set driver` |
25+
| bind (user space framework) | sysfs `driver_override` + `bind` | `hw.nic_uio.bdfs` + `kldload` |
26+
27+
(The Linux-only `iommugroup` is reported as `None` on FreeBSD.)
1628

1729
## Install
1830

@@ -33,7 +45,7 @@ curl -fsSL https://raw.githubusercontent.com/xnvme/devbind/main/src/devbind/devb
3345
devbind --print-completion bash > ~/.local/share/bash-completion/completions/devbind
3446
```
3547

36-
Open a new shell (or `source` the file) and tab-completion is live: `devbind --bind <TAB>` lists `nvme vfio-pci vfio-noiommu uio_pci_generic`.
48+
Open a new shell (or `source` the file) and tab-completion is live: `devbind --bind <TAB>` lists `nvme vfio-pci vfio-noiommu uio_pci_generic nic_uio`.
3749

3850
## Usage
3951

@@ -43,7 +55,7 @@ usage: devbind [-h] [--version] [--classcode CLASSCODE] [--device DEVICE]
4355
[--list] [--unbind] [--bind BIND] [--verbose]
4456
[--print-completion SHELL]
4557
46-
Inspect and control PCI device-driver binding in Linux
58+
Inspect and control PCI device-driver binding on Linux and FreeBSD
4759
4860
options:
4961
-h, --help show this help message and exit
@@ -55,8 +67,8 @@ options:
5567
association.
5668
--unbind Unbind if bound.
5769
--bind BIND Unbind if bound; then bind to the given driver-name
58-
[nvme, vfio-pci, uio_pci_generic] or to a .ko driver
59-
file (path)
70+
[nvme, vfio-pci, uio_pci_generic, nic_uio] or to a
71+
driver file (path)
6072
--verbose Enable verbose logging
6173
--print-completion SHELL
6274
Print shell completion script to stdout and exit
@@ -71,6 +83,14 @@ sudo devbind --bind nvme --device 0000:01:00.0 # rebind to the native driv
7183
sudo devbind --unbind --device 0000:01:00.0 # unbind without rebinding
7284
```
7385

86+
On FreeBSD, bind to `nic_uio` instead of `vfio-pci`/`uio_pci_generic`
87+
(`--device` always takes the `domain:bus:device.function` form `0000:01:00.0` on both platforms):
88+
89+
```
90+
sudo devbind --bind nic_uio --device 0000:01:00.0 # hand device to DPDK/SPDK
91+
sudo devbind --bind nvme --device 0000:01:00.0 # rebind to the native driver
92+
```
93+
7494
`devbind --list` sample output (stock WSL host, no NVMe devices visible):
7595

7696
```

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55
[project]
66
name = "devbind"
77
dynamic = ["version"]
8-
description = "Inspect and control PCI device-driver binding in Linux"
8+
description = "Inspect and control PCI device-driver binding on Linux and FreeBSD"
99
readme = "README.md"
1010
license = "BSD-3-Clause"
1111
requires-python = ">=3.10"
@@ -18,6 +18,7 @@ classifiers = [
1818
"Intended Audience :: Developers",
1919
"Intended Audience :: System Administrators",
2020
"Operating System :: POSIX :: Linux",
21+
"Operating System :: POSIX :: BSD :: FreeBSD",
2122
"Programming Language :: Python :: 3",
2223
"Programming Language :: Python :: 3.10",
2324
"Programming Language :: Python :: 3.11",

0 commit comments

Comments
 (0)