diff options
author | Alessandro Ghedini <alessandro@cloudflare.com> | 2021-08-25 14:31:57 +0100 |
---|---|---|
committer | Alessandro Ghedini <alessandro@cloudflare.com> | 2021-12-29 14:15:54 +0000 |
commit | a9829853df9192048be39cb9593206b8492748f6 (patch) | |
tree | 35d76ceca21b7e8511edf0bb2e40eb39ec6b9993 /test/sys/test_socket.rs | |
parent | c77a8728b558dc75a9a023c669543bf23dd13a9f (diff) | |
download | nix-a9829853df9192048be39cb9593206b8492748f6.zip |
Add support for the SO_TXTIME sockopt and SCM_TXTIME control message
Diffstat (limited to 'test/sys/test_socket.rs')
-rw-r--r-- | test/sys/test_socket.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 7a861abf..5262d3b8 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -1997,3 +1997,60 @@ mod linux_errqueue { assert_eq!(ext_err.ee_info, 0); } } + +// Disable the test on emulated platforms because it fails in Cirrus-CI. Lack +// of QEMU support is suspected. +#[cfg_attr(qemu, ignore)] +#[cfg(target_os = "linux")] +#[test] +pub fn test_txtime() { + use nix::sys::socket::{ + bind, recvmsg, sendmsg, setsockopt, socket, sockopt, ControlMessage, + MsgFlags, SockFlag, SockType, + }; + use nix::sys::time::TimeValLike; + use nix::time::{ClockId, clock_gettime}; + + require_kernel_version!(test_txtime, ">= 5.8"); + + let std_sa = SocketAddr::from_str("127.0.0.1:6790").unwrap(); + let inet_addr = InetAddr::from_std(&std_sa); + let sock_addr = SockAddr::new_inet(inet_addr); + + let ssock = socket( + AddressFamily::Inet, + SockType::Datagram, + SockFlag::empty(), + None, + ) + .expect("send socket failed"); + + let txtime_cfg = libc::sock_txtime { + clockid: libc::CLOCK_MONOTONIC, + flags: 0, + }; + setsockopt(ssock, sockopt::TxTime, &txtime_cfg).unwrap(); + + let rsock = socket( + AddressFamily::Inet, + SockType::Datagram, + SockFlag::empty(), + None, + ) + .unwrap(); + bind(rsock, &sock_addr).unwrap(); + + let sbuf = [0u8; 2048]; + let iov1 = [nix::sys::uio::IoVec::from_slice(&sbuf)]; + + let now = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap(); + let delay = std::time::Duration::from_secs(1).into(); + let txtime = (now + delay).num_nanoseconds() as u64; + + let cmsg = ControlMessage::TxTime(&txtime); + sendmsg(ssock, &iov1, &[cmsg], MsgFlags::empty(), Some(&sock_addr)).unwrap(); + + let mut rbuf = [0u8; 2048]; + let iov2 = [nix::sys::uio::IoVec::from_mut_slice(&mut rbuf)]; + recvmsg(rsock, &iov2, None, MsgFlags::empty()).unwrap(); +} |