Skip to content

Commit 26fd3ce

Browse files
vireshkstefano-garzarella
authored andcommitted
vhost_user: Simplify VhostUserSingleMemoryRegion
VhostUserSingleMemoryRegion contains an extra padding before VhostUserMemoryRegion, and everything else remains the same. Lets reuse the same structure instead of duplicating implementation here. Signed-off-by: Viresh Kumar <[email protected]>
1 parent dd4597a commit 26fd3ce

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

crates/vhost/src/vhost_user/message.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use std::fmt::Debug;
1313
use std::marker::PhantomData;
14+
use std::ops::Deref;
1415

1516
use vm_memory::ByteValued;
1617

@@ -528,44 +529,36 @@ pub type VhostUserMemoryPayload = Vec<VhostUserMemoryRegion>;
528529
pub struct VhostUserSingleMemoryRegion {
529530
/// Padding for correct alignment
530531
padding: u64,
531-
/// Guest physical address of the memory region.
532-
pub guest_phys_addr: u64,
533-
/// Size of the memory region.
534-
pub memory_size: u64,
535-
/// Virtual address in the current process.
536-
pub user_addr: u64,
537-
/// Offset where region starts in the mapped memory.
538-
pub mmap_offset: u64,
532+
/// General memory region
533+
region: VhostUserMemoryRegion,
534+
}
535+
536+
impl Deref for VhostUserSingleMemoryRegion {
537+
type Target = VhostUserMemoryRegion;
538+
539+
fn deref(&self) -> &VhostUserMemoryRegion {
540+
&self.region
541+
}
539542
}
540543

541544
impl VhostUserSingleMemoryRegion {
542545
/// Create a new instance.
543546
pub fn new(guest_phys_addr: u64, memory_size: u64, user_addr: u64, mmap_offset: u64) -> Self {
544547
VhostUserSingleMemoryRegion {
545548
padding: 0,
546-
guest_phys_addr,
547-
memory_size,
548-
user_addr,
549-
mmap_offset,
549+
region: VhostUserMemoryRegion::new(
550+
guest_phys_addr,
551+
memory_size,
552+
user_addr,
553+
mmap_offset,
554+
),
550555
}
551556
}
552557
}
553558

554559
// SAFETY: Safe because all fields of VhostUserSingleMemoryRegion are POD.
555560
unsafe impl ByteValued for VhostUserSingleMemoryRegion {}
556-
557-
impl VhostUserMsgValidator for VhostUserSingleMemoryRegion {
558-
fn is_valid(&self) -> bool {
559-
if self.memory_size == 0
560-
|| self.guest_phys_addr.checked_add(self.memory_size).is_none()
561-
|| self.user_addr.checked_add(self.memory_size).is_none()
562-
|| self.mmap_offset.checked_add(self.memory_size).is_none()
563-
{
564-
return false;
565-
}
566-
true
567-
}
568-
}
561+
impl VhostUserMsgValidator for VhostUserSingleMemoryRegion {}
569562

570563
/// Vring state descriptor.
571564
#[repr(packed)]

0 commit comments

Comments
 (0)