Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alioth/src/sys/linux/if_tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{ioctl_read, ioctl_write_ptr, ioctl_write_val};

ioctl_write_ptr!(tun_set_iff, ioctl_iow::<c_int>(b'T', 202), ifreq);

ioctl_write_val!(tun_set_offload, ioctl_iow::<c_uint>(b'T', 208));
ioctl_write_val!(tun_set_offload, ioctl_iow::<c_uint>(b'T', 208), TunFeature);

ioctl_read!(tun_get_vnet_hdr_sz, b'T', 215, c_int);

Expand Down
75 changes: 27 additions & 48 deletions alioth/src/sys/linux/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,19 @@ pub const fn ioctl_iowr<T>(type_: u8, nr: u8) -> u32 {

#[macro_export]
macro_rules! ioctl_none {
($name:ident, $type_:expr, $nr:expr, $val:expr) => {
($name:ident, $type_:expr, $nr:expr) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(fd: &F) -> ::std::io::Result<libc::c_int> {
let op = $crate::sys::ioctl::ioctl_io($type_, $nr);
let v = $val as ::libc::c_ulong;
$crate::ffi!(unsafe {
::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, v)
::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, 0)
})
}
};
($name:ident, $type_:expr, $nr:expr) => {
$crate::ioctl_none!($name, $type_, $nr, 0);
};
}

#[macro_export]
macro_rules! ioctl_write_val {
($name:ident, $code:expr) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(
fd: &F,
val: ::libc::c_ulong,
) -> ::std::io::Result<libc::c_int> {
let op = $code;
$crate::ffi!(unsafe {
::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, val)
})
}
};
($name:ident, $code:expr, $ty:ty) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(
Expand All @@ -89,6 +73,9 @@ macro_rules! ioctl_write_val {
})
}
};
($name:ident, $type_:expr, $nr:expr, $ty:ty) => {
$crate::ioctl_write_val!($name, $crate::sys::ioctl::ioctl_io($type_, $nr), $ty);
};
}

#[macro_export]
Expand All @@ -109,22 +96,12 @@ macro_rules! ioctl_write_ptr {
})
}
};

($name:ident, $type_:expr, $nr:expr, $ty:ty) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(
fd: &F,
val: &$ty,
) -> ::std::io::Result<libc::c_int> {
let op = $crate::sys::ioctl::ioctl_iow::<$ty>($type_, $nr);
$crate::ffi!(unsafe {
::libc::ioctl(
::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()),
op as _,
val as *const $ty,
)
})
}
$crate::ioctl_write_ptr!(
$name,
$crate::sys::ioctl::ioctl_iow::<$ty>($type_, $nr),
$ty
);
};
}

Expand Down Expand Up @@ -200,13 +177,13 @@ macro_rules! ioctl_writeread {

#[macro_export]
macro_rules! ioctl_writeread_buf {
($name:ident, $type_:expr, $nr:expr, $ty:ident) => {
($name:ident, $code:expr, $ty:ident) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd, const N: usize>(
fd: &F,
val: &mut $ty<N>,
) -> ::std::io::Result<libc::c_int> {
let op = $crate::sys::ioctl::ioctl_iowr::<$ty<0>>($type_, $nr);
let op = $code;
$crate::ffi!(unsafe {
::libc::ioctl(
::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()),
Expand All @@ -216,6 +193,13 @@ macro_rules! ioctl_writeread_buf {
})
}
};
($name:ident, $type_:expr, $nr:expr, $ty:ident) => {
$crate::ioctl_writeread_buf!(
$name,
$crate::sys::ioctl::ioctl_iowr::<$ty<0>>($type_, $nr),
$ty
);
};
}

#[macro_export]
Expand All @@ -224,19 +208,7 @@ macro_rules! ioctl_read {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(fd: &F) -> ::std::io::Result<$ty> {
let mut val = ::core::mem::MaybeUninit::<$ty>::uninit();
$crate::ffi!(::libc::ioctl(
::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()),
$code as _,
val.as_mut_ptr()
))?;
::std::io::Result::Ok(val.assume_init())
}
};
($name:ident, $type_:expr, $nr:expr, $ty:ty) => {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn $name<F: ::std::os::fd::AsFd>(fd: &F) -> ::std::io::Result<$ty> {
let mut val = ::core::mem::MaybeUninit::<$ty>::uninit();
let op = $crate::sys::ioctl::ioctl_ior::<$ty>($type_, $nr);
let op = $code;
$crate::ffi!(unsafe {
::libc::ioctl(
::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()),
Expand All @@ -247,6 +219,13 @@ macro_rules! ioctl_read {
::std::io::Result::Ok(unsafe { val.assume_init() })
}
};
($name:ident, $type_:expr, $nr:expr, $ty:ty) => {
$crate::ioctl_read!(
$name,
$crate::sys::ioctl::ioctl_ior::<$ty>($type_, $nr),
$ty
);
};
}

#[cfg(test)]
Expand Down
21 changes: 12 additions & 9 deletions alioth/src/sys/linux/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use bitflags::bitflags;

#[cfg(target_arch = "x86_64")]
use crate::ioctl_writeread_buf;
use crate::sys::ioctl::ioctl_ior;
#[cfg(target_arch = "x86_64")]
use crate::sys::ioctl::ioctl_iowr;
use crate::sys::ioctl::{ioctl_io, ioctl_ior};
use crate::{
c_enum, ioctl_none, ioctl_read, ioctl_write_buf, ioctl_write_ptr, ioctl_write_val,
ioctl_writeread,
Expand Down Expand Up @@ -506,6 +506,7 @@ c_enum! {
pub struct KvmCap(u32);
{
IRQFD = 32;
KVMCLOCK_CTRL = 76;
SIGNAL_MSI = 77;
ARM_PSCI_0_2 = 102;
X2APIC_API = 129;
Expand Down Expand Up @@ -675,22 +676,22 @@ bitflags! {
}
}

ioctl_none!(kvm_get_api_version, KVMIO, 0x00, 0);
ioctl_write_val!(kvm_create_vm, ioctl_io(KVMIO, 0x01), KvmVmType);
ioctl_write_val!(kvm_check_extension, ioctl_io(KVMIO, 0x03), KvmCap);
ioctl_none!(kvm_get_vcpu_mmap_size, KVMIO, 0x04, 0);
ioctl_none!(kvm_get_api_version, KVMIO, 0x00);
ioctl_write_val!(kvm_create_vm, KVMIO, 0x01, KvmVmType);
ioctl_write_val!(kvm_check_extension, KVMIO, 0x03, KvmCap);
ioctl_none!(kvm_get_vcpu_mmap_size, KVMIO, 0x04);
#[cfg(target_arch = "x86_64")]
ioctl_writeread_buf!(kvm_get_supported_cpuid, KVMIO, 0x05, KvmCpuid2);

ioctl_write_val!(kvm_create_vcpu, ioctl_io(KVMIO, 0x41), u32);
ioctl_write_val!(kvm_create_vcpu, KVMIO, 0x41, u32);
ioctl_write_ptr!(
kvm_set_user_memory_region,
KVMIO,
0x46,
KvmUserspaceMemoryRegion
);
#[cfg(target_arch = "x86_64")]
ioctl_write_val!(kvm_set_tss_addr, ioctl_io(KVMIO, 0x47));
ioctl_write_val!(kvm_set_tss_addr, KVMIO, 0x47, u64);
#[cfg(target_arch = "x86_64")]
ioctl_write_ptr!(kvm_set_identity_map_addr, KVMIO, 0x48, u64);
ioctl_write_ptr!(
Expand All @@ -701,13 +702,13 @@ ioctl_write_ptr!(
);

#[cfg(target_arch = "x86_64")]
ioctl_none!(kvm_create_irqchip, KVMIO, 0x60, 0);
ioctl_none!(kvm_create_irqchip, KVMIO, 0x60);
ioctl_write_buf!(kvm_set_gsi_routing, KVMIO, 0x6a, KvmIrqRouting);

ioctl_write_ptr!(kvm_irqfd, KVMIO, 0x76, KvmIrqfd);
ioctl_write_ptr!(kvm_ioeventfd, KVMIO, 0x79, KvmIoEventFd);

ioctl_none!(kvm_run, KVMIO, 0x80, 0);
ioctl_none!(kvm_run, KVMIO, 0x80);
#[cfg(target_arch = "x86_64")]
ioctl_read!(kvm_get_regs, KVMIO, 0x81, KvmRegs);
#[cfg(target_arch = "x86_64")]
Expand All @@ -730,6 +731,8 @@ ioctl_write_ptr!(kvm_get_one_reg, KVMIO, 0xab, KvmOneReg);
#[cfg(not(target_arch = "x86_64"))]
ioctl_write_ptr!(kvm_set_one_reg, KVMIO, 0xac, KvmOneReg);

ioctl_none!(kvm_kvmclock_ctrl, KVMIO, 0xad);

#[cfg(target_arch = "aarch64")]
ioctl_write_ptr!(kvm_arm_vcpu_init, KVMIO, 0xae, KvmVcpuInit);
#[cfg(target_arch = "aarch64")]
Expand Down
2 changes: 1 addition & 1 deletion alioth/src/sys/linux/vfio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ ioctl_writeread!(

ioctl_write_buf!(vfio_device_set_irqs, ioctl_io(VFIO_TYPE, 110), VfioIrqSet);

ioctl_none!(vfio_device_reset, VFIO_TYPE, 111, 0);
ioctl_none!(vfio_device_reset, VFIO_TYPE, 111);

ioctl_write_ptr!(
vfio_device_bind_iommufd,
Expand Down
2 changes: 1 addition & 1 deletion alioth/src/sys/linux/vhost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bitflags! {

ioctl_read!(vhost_get_features, VHOST_VIRTIO, 0x00, u64);
ioctl_write_ptr!(vhost_set_features, VHOST_VIRTIO, 0x00, u64);
ioctl_none!(vhost_set_owner, VHOST_VIRTIO, 0x01, 0);
ioctl_none!(vhost_set_owner, VHOST_VIRTIO, 0x01);

ioctl_write_buf!(
vhost_set_mem_table,
Expand Down
8 changes: 4 additions & 4 deletions alioth/src/virtio/dev/net/tap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,17 @@ fn detect_tap_offload(tap: &impl AsFd) -> NetFeature {
| NetFeature::GUEST_UFO
| NetFeature::GUEST_USO4
| NetFeature::GUEST_USO6;
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() {
return dev_feat;
}
tap_feature &= !(TunFeature::USO4 | TunFeature::USO6);
dev_feat &= !(NetFeature::GUEST_USO4 | NetFeature::GUEST_USO6);
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() {
return dev_feat;
}
tap_feature &= !(TunFeature::UFO);
dev_feat &= !NetFeature::GUEST_UFO;
if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() {
if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() {
return dev_feat;
}
NetFeature::empty()
Expand Down Expand Up @@ -482,6 +482,6 @@ fn enable_tap_offload(tap: &mut File, feature: NetFeature) -> Result<()> {
if feature.contains(NetFeature::GUEST_USO6) {
tap_feature |= TunFeature::USO6;
}
unsafe { tun_set_offload(tap, tap_feature.bits()) }?;
unsafe { tun_set_offload(tap, tap_feature) }?;
Ok(())
}