Skip to content

Instantly share code, notes, and snippets.

@folkertdev
Created July 20, 2022 13:18
Show Gist options
  • Save folkertdev/8dabcc0d7b6ecb233e26d7bce74c2f01 to your computer and use it in GitHub Desktop.
Save folkertdev/8dabcc0d7b6ecb233e26d7bce74c2f01 to your computer and use it in GitHub Desktop.
Get timestamp capabilities of a network interface using ioctl in rust
#[repr(C)]
#[derive(Default)]
struct ethtool_ts_info {
cmd: u32,
so_timestamping: u32,
phc_index: u32,
tx_types: u32,
tx_reserved: [u32; 3],
rx_filters: u32,
rx_reserved: [u32; 3],
}
#[repr(C)]
struct ifreq {
ifrn_name: [u8; 16],
ifru_data: *mut libc::c_void,
__empty_space: [u8; 40 - 8],
}
#[allow(dead_code)]
fn is_timestamping_supported() -> std::io::Result<(bool, bool)> {
// Get time stamping and PHC info
const ETHTOOL_GET_TS_INFO: u32 = 0x00000041;
let mut tsi: ethtool_ts_info = ethtool_ts_info {
cmd: ETHTOOL_GET_TS_INFO,
..Default::default()
};
let mut ifrn_name = [0; 16];
ifrn_name[0] = b'e';
ifrn_name[1] = b'n';
ifrn_name[2] = b'o';
ifrn_name[3] = b'1';
let ifr: ifreq = ifreq {
ifrn_name,
ifru_data: (&mut tsi as *mut _) as *mut libc::c_void,
__empty_space: [0; 40 - 8],
};
let fd = cvt(unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) }).unwrap();
const SIOCETHTOOL: u64 = 0x8946;
cvt(unsafe { libc::ioctl(fd, SIOCETHTOOL, &ifr) }).unwrap();
let supports_software_rx = tsi.so_timestamping & libc::SOF_TIMESTAMPING_RX_SOFTWARE;
let supports_software_tx = tsi.so_timestamping & libc::SOF_TIMESTAMPING_TX_SOFTWARE;
Ok((supports_software_rx != 0, supports_software_tx != 0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment