Re: [PATCH 8/8] samples: rust: add SR-IOV driver sample

Zhi Wang
2025-11-20T06:41:59Z

On Wed, 19 Nov 2025 17:19:12 -0500
Peter Colberg <pcolberg@redhat.com> wrote:

> Add a new SR-IOV driver sample that demonstrates how to enable and
> disable the Single Root I/O Virtualization capability for a PCI
> device.
>
> The sample may be exercised using QEMU's 82576 (igb) emulation.
>

snip

> +
> + fn sriov_configure(pdev: &pci::Device<Core>, nr_virtfn: i32) ->
> Result<i32> {
> + assert!(pdev.is_physfn());
> +
> + if nr_virtfn == 0 {
> + dev_info!(
> + pdev.as_ref(),
> + "Disable SR-IOV (PCI ID: {}, 0x{:x}).\n",
> + pdev.vendor_id(),
> + pdev.device_id()
> + );
> + pdev.disable_sriov();
> + } else {
> + dev_info!(
> + pdev.as_ref(),
> + "Enable SR-IOV (PCI ID: {}, 0x{:x}).\n",
> + pdev.vendor_id(),
> + pdev.device_id()
> + );
> + pdev.enable_sriov(nr_virtfn)?;
> + }
> +

IMO, it would be nice to simply demostrate how to reach the driver data
structure (struct SampleDriver) and its members (I think accessing one
dummy member in the SampleDriver is good enough, not something fancy),
which I believe quite many of the drivers need to do so and they can
take this as the kernel recommended approach instead of inventing
something new differently. :)

Z.

> + assert_eq!(pdev.num_vf(), nr_virtfn);
> + Ok(nr_virtfn)
> + }
> +}
> +
> +#[pinned_drop]
> +impl PinnedDrop for SampleDriver {
> + fn drop(self: Pin<&mut Self>) {
> + dev_info!(self.pdev.as_ref(), "Remove Rust SR-IOV driver
> sample.\n");
> + }
> +}
> +
> +kernel::module_pci_driver! {
> + type: SampleDriver,
> + name: "rust_driver_sriov",
> + authors: ["Peter Colberg"],
> + description: "Rust SR-IOV driver",
> + license: "GPL v2",
> +}
>