Skip to content

Commit faddbdc

Browse files
committed
pci: refactor: use PciSBDF type instead of raw u32
Fix all the places where the SBDF value was passed as a raw u32 to refer to the correct PciSBDF type instead. No functional changes intended. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent c08b28b commit faddbdc

5 files changed

Lines changed: 41 additions & 39 deletions

File tree

src/vmm/src/device_manager/pci_mngr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl PciDevices {
161161

162162
// Create the transport
163163
let mut virtio_device =
164-
VirtioPciDevice::new(id.clone(), mem, device, Arc::new(msix_vectors), sbdf.into())?;
164+
VirtioPciDevice::new(id.clone(), mem, device, Arc::new(msix_vectors), sbdf)?;
165165

166166
// Allocate bars
167167
let mut resource_allocator_lock = vm.resource_allocator();

src/vmm/src/devices/virtio/transport/pci/device.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ impl VirtioPciDevice {
363363
memory: GuestMemoryMmap,
364364
device: Arc<Mutex<dyn VirtioDevice>>,
365365
msix_vectors: Arc<MsixVectorGroup>,
366-
pci_device_bdf: u32,
366+
pci_device_sbdf: PciSBDF,
367367
) -> Result<Self, VirtioPciDeviceError> {
368368
let num_queues = device.lock().expect("Poisoned lock").queues().len();
369369

370370
let msix_config = Arc::new(Mutex::new(MsixConfig::new(
371371
msix_vectors.clone(),
372-
pci_device_bdf,
372+
pci_device_sbdf,
373373
)));
374374
let pci_config = Self::pci_configuration(
375375
device.lock().expect("Poisoned lock").device_type(),
@@ -395,7 +395,7 @@ impl VirtioPciDevice {
395395
let virtio_pci_device = VirtioPciDevice {
396396
id,
397397
sub_id: None,
398-
pci_device_sbdf: pci_device_bdf.into(),
398+
pci_device_sbdf,
399399
configuration: pci_config,
400400
common_config: virtio_common_config,
401401
device,
@@ -415,7 +415,7 @@ impl VirtioPciDevice {
415415
device: Arc<Mutex<dyn VirtioDevice>>,
416416
state: VirtioPciDeviceState,
417417
) -> Result<Self, VirtioPciDeviceError> {
418-
let msix_config = MsixConfig::from_state(state.msix_state, vm.clone(), state.sbdf.into())?;
418+
let msix_config = MsixConfig::from_state(state.msix_state, vm.clone(), state.sbdf)?;
419419
let vectors = msix_config.vectors.clone();
420420
let msix_config = Arc::new(Mutex::new(msix_config));
421421

src/vmm/src/pci/msix.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use vm_memory::ByteValued;
1212

1313
use crate::Vm;
1414
use crate::logger::{debug, error, warn};
15-
use crate::pci::PciCapabilityId;
1615
use crate::pci::configuration::PciCapability;
16+
use crate::pci::{PciCapabilityId, PciSBDF};
1717
use crate::snapshot::Persist;
1818
use crate::vstate::interrupts::{InterruptError, MsixVectorConfig, MsixVectorGroup};
1919

@@ -71,8 +71,8 @@ pub struct MsixConfig {
7171
pub table_entries: Vec<MsixTableEntry>,
7272
/// Pending bit array
7373
pub pba_entries: Vec<u64>,
74-
/// Id of the device using this set of vectors
75-
pub devid: u32,
74+
/// SBDF of the device using this set of vectors
75+
pub sbdf: PciSBDF,
7676
/// Interrupts vectors used
7777
pub vectors: Arc<MsixVectorGroup>,
7878
/// Whether vectors are masked
@@ -86,7 +86,7 @@ impl std::fmt::Debug for MsixConfig {
8686
f.debug_struct("MsixConfig")
8787
.field("table_entries", &self.table_entries)
8888
.field("pba_entries", &self.pba_entries)
89-
.field("devid", &self.devid)
89+
.field("devid", &self.sbdf)
9090
.field("masked", &self.masked)
9191
.field("enabled", &self.enabled)
9292
.finish()
@@ -95,7 +95,7 @@ impl std::fmt::Debug for MsixConfig {
9595

9696
impl MsixConfig {
9797
/// Create a new MSI-X configuration
98-
pub fn new(vectors: Arc<MsixVectorGroup>, devid: u32) -> Self {
98+
pub fn new(vectors: Arc<MsixVectorGroup>, sbdf: PciSBDF) -> Self {
9999
assert!(vectors.num_vectors() <= MAX_MSIX_VECTORS_PER_DEVICE);
100100

101101
let mut table_entries: Vec<MsixTableEntry> = Vec::new();
@@ -107,7 +107,7 @@ impl MsixConfig {
107107
MsixConfig {
108108
table_entries,
109109
pba_entries,
110-
devid,
110+
sbdf,
111111
vectors,
112112
masked: true,
113113
enabled: false,
@@ -118,7 +118,7 @@ impl MsixConfig {
118118
pub fn from_state(
119119
state: MsixConfigState,
120120
vm: Arc<Vm>,
121-
devid: u32,
121+
sbdf: PciSBDF,
122122
) -> Result<Self, InterruptError> {
123123
let vectors = Arc::new(MsixVectorGroup::restore(vm, &state.vectors)?);
124124
if state.enabled && !state.masked {
@@ -131,7 +131,7 @@ impl MsixConfig {
131131
high_addr: table_entry.msg_addr_hi,
132132
low_addr: table_entry.msg_addr_lo,
133133
data: table_entry.msg_data,
134-
devid,
134+
sbdf,
135135
};
136136

137137
vectors.update(idx, config, state.masked, true)?;
@@ -142,7 +142,7 @@ impl MsixConfig {
142142
Ok(MsixConfig {
143143
table_entries: state.table_entries,
144144
pba_entries: state.pba_entries,
145-
devid,
145+
sbdf,
146146
vectors,
147147
masked: state.masked,
148148
enabled: state.enabled,
@@ -171,21 +171,21 @@ impl MsixConfig {
171171
// Update interrupt routing
172172
if old_masked != self.masked || old_enabled != self.enabled {
173173
if self.enabled && !self.masked {
174-
debug!("MSI-X enabled for device 0x{:x}", self.devid);
174+
debug!("MSI-X enabled for device {}", self.sbdf);
175175
for (idx, table_entry) in self.table_entries.iter().enumerate() {
176176
let config = MsixVectorConfig {
177177
high_addr: table_entry.msg_addr_hi,
178178
low_addr: table_entry.msg_addr_lo,
179179
data: table_entry.msg_data,
180-
devid: self.devid,
180+
sbdf: self.sbdf,
181181
};
182182

183183
if let Err(e) = self.vectors.update(idx, config, table_entry.masked(), true) {
184184
error!("Failed updating vector: {:?}", e);
185185
}
186186
}
187187
} else if old_enabled || !old_masked {
188-
debug!("MSI-X disabled for device 0x{:x}", self.devid);
188+
debug!("MSI-X disabled for device {}", self.sbdf);
189189
if let Err(e) = self.vectors.disable() {
190190
error!("Failed disabling irq_fd: {:?}", e);
191191
}
@@ -323,7 +323,7 @@ impl MsixConfig {
323323
high_addr: table_entry.msg_addr_hi,
324324
low_addr: table_entry.msg_addr_lo,
325325
data: table_entry.msg_data,
326-
devid: self.devid,
326+
sbdf: self.sbdf,
327327
};
328328

329329
if let Err(e) = self
@@ -521,13 +521,13 @@ mod tests {
521521
#[test]
522522
#[should_panic]
523523
fn test_too_many_vectors() {
524-
MsixConfig::new(msix_vector_group(2049), 0x42);
524+
MsixConfig::new(msix_vector_group(2049), PciSBDF::from(0x42));
525525
}
526526

527527
#[test]
528528
fn test_new_msix_config() {
529-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
530-
assert_eq!(config.devid, 0x42);
529+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
530+
assert_eq!(config.sbdf, PciSBDF::from(0x42));
531531
assert!(config.masked);
532532
assert!(!config.enabled);
533533
assert_eq!(config.table_entries.len(), 2);
@@ -536,7 +536,7 @@ mod tests {
536536

537537
#[test]
538538
fn test_enable_msix_vectors() {
539-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
539+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
540540

541541
assert!(!config.enabled);
542542
assert!(config.masked);
@@ -563,15 +563,15 @@ mod tests {
563563
#[test]
564564
#[should_panic]
565565
fn test_table_access_read_too_big() {
566-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
566+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
567567
let mut buffer = [0u8; 16];
568568

569569
config.read_table(0, &mut buffer);
570570
}
571571

572572
#[test]
573573
fn test_read_table_past_end() {
574-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
574+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
575575
let mut buffer = [0u8; 8];
576576

577577
// We have 2 vectors (16 bytes each), so we should be able to read up to 32 bytes.
@@ -582,7 +582,7 @@ mod tests {
582582

583583
#[test]
584584
fn test_read_table_bad_length() {
585-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
585+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
586586
let mut buffer = [0u8; 8];
587587

588588
// We can either read 4 or 8 bytes
@@ -608,7 +608,7 @@ mod tests {
608608

609609
#[test]
610610
fn test_access_table() {
611-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
611+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
612612
// enabled and not masked
613613
check_metric_after_block!(
614614
METRICS.interrupts.config_updates,
@@ -725,15 +725,15 @@ mod tests {
725725
#[test]
726726
#[should_panic]
727727
fn test_table_access_write_too_big() {
728-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
728+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
729729
let buffer = [0u8; 16];
730730

731731
config.write_table(0, &buffer);
732732
}
733733

734734
#[test]
735735
fn test_pba_read_too_big() {
736-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
736+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
737737
let mut buffer = [0u8; 16];
738738

739739
config.read_pba(0, &mut buffer);
@@ -742,7 +742,7 @@ mod tests {
742742

743743
#[test]
744744
fn test_pba_invalid_offset() {
745-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
745+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
746746
let mut buffer = [0u8; 8];
747747

748748
// Past the end of the PBA array
@@ -760,22 +760,22 @@ mod tests {
760760
#[test]
761761
#[should_panic]
762762
fn test_set_pba_bit_vector_too_big() {
763-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
763+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
764764

765765
config.set_pba_bit(2048, false);
766766
}
767767

768768
#[test]
769769
#[should_panic]
770770
fn test_get_pba_bit_vector_too_big() {
771-
let config = MsixConfig::new(msix_vector_group(2), 0x42);
771+
let config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
772772

773773
config.get_pba_bit(2048);
774774
}
775775

776776
#[test]
777777
fn test_pba_bit_invalid_vector() {
778-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
778+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
779779

780780
// We have two vectors, so setting the pending bit for the third one
781781
// should be ignored
@@ -788,7 +788,7 @@ mod tests {
788788

789789
#[test]
790790
fn test_pba_read() {
791-
let mut config = MsixConfig::new(msix_vector_group(128), 0x42);
791+
let mut config = MsixConfig::new(msix_vector_group(128), PciSBDF::from(0x42));
792792
let mut buffer = [0u8; 8];
793793

794794
config.set_pba_bit(1, false);
@@ -809,7 +809,7 @@ mod tests {
809809

810810
#[test]
811811
fn test_pending_interrupt() {
812-
let mut config = MsixConfig::new(msix_vector_group(2), 0x42);
812+
let mut config = MsixConfig::new(msix_vector_group(2), PciSBDF::from(0x42));
813813
config.set_pba_bit(1, false);
814814
assert_eq!(config.get_pba_bit(1), 1);
815815
// Enable MSI-X vector and unmask interrupts

src/vmm/src/vstate/interrupts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use vmm_sys_util::eventfd::EventFd;
99

1010
use crate::Vm;
1111
use crate::logger::{IncMetric, METRICS};
12+
use crate::pci::PciSBDF;
1213
use crate::snapshot::Persist;
1314

1415
#[derive(Debug, thiserror::Error, displaydoc::Display)]
@@ -35,8 +36,8 @@ pub struct MsixVectorConfig {
3536
pub low_addr: u32,
3637
/// Data to write to delivery message signaled interrupt.
3738
pub data: u32,
38-
/// Unique ID of the device to delivery message signaled interrupt.
39-
pub devid: u32,
39+
/// SBDF of the device to delivery message signaled interrupt.
40+
pub sbdf: PciSBDF,
4041
}
4142

4243
/// Type that describes an allocated interrupt

src/vmm/src/vstate/vm.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl Vm {
457457
// For the time being, we are using a single PCI segment and a single bus per segment
458458
// so just passing config.devid should be fine.
459459
entry.flags = KVM_MSI_VALID_DEVID;
460-
entry.u.msi.__bindgen_anon_1.devid = config.devid;
460+
entry.u.msi.__bindgen_anon_1.devid = config.sbdf.into();
461461
}
462462

463463
self.common
@@ -556,6 +556,7 @@ pub(crate) mod tests {
556556
use vm_memory::mmap::MmapRegionBuilder;
557557

558558
use super::*;
559+
use crate::pci::PciSBDF;
559560
use crate::snapshot::Persist;
560561
use crate::test_utils::single_region_mem_raw;
561562
use crate::utils::mib_to_bytes;
@@ -751,7 +752,7 @@ pub(crate) mod tests {
751752
high_addr: 0x42,
752753
low_addr: 0x12,
753754
data: 0x12,
754-
devid: 0xafa,
755+
sbdf: PciSBDF::from(0xafa),
755756
};
756757
msix_group.update(0, config, true, true).unwrap();
757758
msix_group.update(4, config, true, true).unwrap_err();
@@ -770,7 +771,7 @@ pub(crate) mod tests {
770771
high_addr: 0x42,
771772
low_addr: 0x13,
772773
data: 0x12,
773-
devid: 0xafa,
774+
sbdf: PciSBDF::from(0xafa),
774775
};
775776
for i in 0..4 {
776777
config.data = 0x12 * i;

0 commit comments

Comments
 (0)