Thanks to visit codestin.com
Credit goes to github.com

Skip to content

vmm: Refactor common PCI based device configuration - #7962

Merged
rbradford merged 22 commits into
cloud-hypervisor:mainfrom
rbradford:202604/config-refactoring
Apr 14, 2026
Merged

rbradford merged 22 commits into
cloud-hypervisor:mainfrom
rbradford:202604/config-refactoring

Conversation

@rbradford

Copy link
Copy Markdown
Member

I noticed that we have a lot of common shared PCI configuration options between
our different config structs. By consolidating into a common struct we can then
use this to reduce code duplication but also make it easier for the work to add a
pci_device_id as required in #7631

  • vmm: Introduce a PciDeviceCommonConfig struct
  • option_parser: Introduce parse_subset() tolerating unknown options
  • vmm: config: Implement PciDeviceCommonConfig::parse
  • vmm: Introduce PciDeviceCommonConfig::validate()
  • vmm: config: Switch DiskConfig to use PciDeviceCommonConfig
  • vmm: config: Switch NetConfig to use PciDeviceCommonConfig
  • vmm: config: Switch FsConfig to use PciDeviceCommonConfig
  • vmm: config: Switch GenericVhostUserConfig to use PciDeviceCommonConfig
  • vmm: config: Switch PmemConfig to use PciDeviceCommonConfig
  • vmm: config: Switch DeviceConfig to use PciDeviceCommonConfig
  • vmm: config: Switch UserDeviceConfig to use PciDeviceCommonConfig
  • vmm: config: Switch VdpaConfig to use PciDeviceCommonConfig
  • vmm: config: Switch VsockConfig to use PciDeviceCommonConfig
  • vmm: config: Remove unused error variant
  • vmm: device_manager: Reuse PciDeviceCommonConfig in MetaVirtioDevice

@phip1611

phip1611 commented Apr 4, 2026

Copy link
Copy Markdown
Member

Seems reasonable.

Would it make sense to merge #7631 first to minimize the need for another substantial rebase? It has already gone through several review iterations (since Jan), and requiring further large-scale changes at this stage could negatively impact the contributor experience 🤔

@rbradford
rbradford marked this pull request as ready for review April 4, 2026 07:47
@rbradford
rbradford requested a review from a team as a code owner April 4, 2026 07:47
@rbradford

This comment was marked as outdated.

@phip1611

This comment was marked as outdated.

@rbradford
rbradford force-pushed the 202604/config-refactoring branch 2 times, most recently from 85859df to 6b27998 Compare April 4, 2026 16:19

@phip1611 phip1611 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a very sensible cleanup - thanks! Will wait for Pascal (@scholzp)s approval as this will be the new base for #7631 (which likely will be replaced by #7965)

Comment thread option_parser/src/lib.rs
Comment thread option_parser/src/lib.rs
Comment thread option_parser/src/lib.rs
Comment thread vmm/src/vm_config.rs
Comment thread option_parser/src/lib.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated

@scholzp scholzp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the refactoring you did! I think the improvements speak for them self. Thanks for putting time into this!

I left a few comments.

Comment thread option_parser/src/lib.rs
Comment thread option_parser/src/lib.rs Outdated
Comment thread option_parser/src/lib.rs
Comment thread option_parser/src/lib.rs
Comment thread vmm/src/config.rs Outdated
Comment thread vmm/src/device_manager.rs Outdated
Comment thread option_parser/src/lib.rs
Comment thread vmm/src/device_manager.rs Outdated
Comment thread vmm/src/device_manager.rs
Comment thread vmm/src/vm_config.rs
@phip1611

Copy link
Copy Markdown
Member

ping @rbradford: @scholzp and I'd love to move this PR forward. Apart from the existing remarks, there are no further concerns from our side

@rbradford
rbradford force-pushed the 202604/config-refactoring branch from 6b27998 to 76eff16 Compare April 14, 2026 07:55
@rbradford

Copy link
Copy Markdown
Member Author

ping @rbradford: @scholzp and I'd love to move this PR forward. Apart from the existing remarks, there are no further concerns from our side

PR updated! Thanks for the patience.

@rbradford
rbradford force-pushed the 202604/config-refactoring branch from 76eff16 to 9eaf43e Compare April 14, 2026 08:20
@rbradford
rbradford requested a review from phip1611 April 14, 2026 10:49
Refactor parse() into a version that can control whether to tolerate
unknown options. This can then be used to then parse a subset of the
options.

Signed-off-by: Rob Bradford <[email protected]>
Add an OptionParser::add_all method that takes a slice of option names
and use that to add to the set of parameters that the parser works on.

Signed-off-by: Rob Bradford <[email protected]>
Autogenerated with Claude Opus 4.6 and reviewed with human eyes.

Signed-off-by: Rob Bradford <[email protected]>
The error message for the InvalidSyntax was copied from UnknownOption.
Correct it to "invalid syntax".

Signed-off-by: Rob Bradford <[email protected]>
Add unit tests generated with Claude Opus 4.6 and reviewed by human
eyes.

Signed-off-by: Rob Bradford <[email protected]>
This unit test was trying to test with extra "="s in the input but was
instead testing using an unknown option. Add the option to the parser to
not hit that incorrect error.

Signed-off-by: Rob Bradford <[email protected]>
Introduce a common struct that can encompass all the config fields
used by devices that are PCI based. The use of `skip_serializing_if`
means that the iommu field will only be included if set (otherwise
falling back to default false). This neatly handles the devices that
don't support an iommu.

Signed-off-by: Rob Bradford <[email protected]>
This parses a subset of the device configuration options used for
devices that are PCI based.

Signed-off-by: Rob Bradford <[email protected]>
Implement some common PCI segment validation. This can be used to reduce
duplication across the different validation methods.

Signed-off-by: Rob Bradford <[email protected]>
Switch DiskConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
Switch NetConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
Switch FsConfig over to using the newly extracted struct members as used
by all PCI based devices. The use of #[serde(flatten)] means that this
change has no impact on the JSON format that the data is stored as.

As virtio-fs does not support being placed behind an IOMMU an error is
now raised if iommu is set. This option is not exposed via the CLI but
could happen with a miscontructed JSON/API call.

Signed-off-by: Rob Bradford <[email protected]>
Switch GenericVhostUserConfig over to using the newly extracted struct
members as used by all PCI based devices. The use of #[serde(flatten)]
means that this change has no impact on the JSON format that the data is
stored as.

As generic vhost-user devices do not support being placed behind an
IOMMU an error is now raised if iommu is set. This can't happen via the
CLI but could via the JSON/API.

Signed-off-by: Rob Bradford <[email protected]>
Switch PmemConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
Switch DeviceConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
Switch UserDeviceConfig over to using the newly extracted struct members
as used by all PCI based devices. The use of #[serde(flatten)] means
that this change has no impact on the JSON format that the data is
stored as.

As VFIO user devices do not support being placed behind an IOMMU an
error is now raised if iommu is set. This can't happen via the CLI but
could via the JSON/API.

Signed-off-by: Rob Bradford <[email protected]>
Switch VdpaConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
Switch VsockConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <[email protected]>
The IommuNotSupportedOnSegment variant is no longer needed as the common
PciDeviceCommonConfig::validate() handles this case with the
OnIommuSegment variant along with more use of the IommuNotSupported
error variant.

Signed-off-by: Rob Bradford <[email protected]>
This can then be used with the OptionParser::add_all() API to reduce the
number of locations the same options are added to the parser. The only
quirk is that some devices do not support an IOMMU (because they are
vhost-user / vfio-user based). There are two different versions of the
array to support that.

Signed-off-by: Rob Bradford <[email protected]>
This struct has the same members and it can be reused to reduce
complexity now and if other common PCI related fields need to be
added in the future.

Signed-off-by: Rob Bradford <[email protected]>
Use a more idiomatic Rust approach when establishing an autogenerated ID
when none is set.

Signed-off-by: Rob Bradford <[email protected]>
@rbradford
rbradford force-pushed the 202604/config-refactoring branch from 9eaf43e to 9905375 Compare April 14, 2026 12:38

@phip1611 phip1611 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Seems you ignored a few remarks, such as adding documentation to new pub fn - was this intentional?

Sorry, Github UI was confusing. Wonderful contribution!

@scholzp scholzp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you very much! :)

@rbradford
rbradford added this pull request to the merge queue Apr 14, 2026
Merged via the queue into cloud-hypervisor:main with commit cc7e56f Apr 14, 2026
38 checks passed
@rbradford
rbradford deleted the 202604/config-refactoring branch June 12, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants