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

Peter Colberg
2025-11-20T15:49:37Z

On Thu, Nov 20, 2025 at 08:41:32AM +0200, Zhi Wang wrote:
> 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. :)

Thanks for the suggestion. I added a `private` member to SampleDriver,
similar to the rust_driver_auxiliary sample, and extended the VF-only
path of probe() to demonstrate how to reach the driver data of the PF
device from a VF device.

Peter

>
> 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",
> > +}
> >
>