Skip to content

Commit 830bab6

Browse files
committed
Support TIMESTAMPNS for linux
1 parent 3f8a66d commit 830bab6

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased] - ReleaseDate
77
### Added
8+
- Added TIMESTAMPNS support for linux
9+
(#[1402](https://github.com/nix-rust/nix/pull/1402))
810

911
### Changed
1012
- Made `forkpty` unsafe, like `fork`

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ targets = [
3232
]
3333

3434
[dependencies]
35-
libc = { version = "0.2.82", features = [ "extra_traits" ] }
35+
libc = { version = "0.2.93", features = [ "extra_traits" ] }
3636
bitflags = "1.1"
3737
cfg-if = "1.0"
3838

src/sys/socket/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
77
CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_LEN};
88
use std::{mem, ptr, slice};
99
use std::os::unix::io::RawFd;
10+
#[cfg(all(target_os = "linux"))]
11+
use crate::sys::time::TimeSpec;
1012
use crate::sys::time::TimeVal;
1113
use crate::sys::uio::IoVec;
1214

@@ -554,6 +556,11 @@ pub enum ControlMessageOwned {
554556
/// # }
555557
/// ```
556558
ScmTimestamp(TimeVal),
559+
/// Nanoseconds resolution timestamp
560+
///
561+
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
562+
#[cfg(all(target_os = "linux"))]
563+
ScmTimestampns(TimeSpec),
557564
#[cfg(any(
558565
target_os = "android",
559566
target_os = "ios",
@@ -645,6 +652,11 @@ impl ControlMessageOwned {
645652
let tv: libc::timeval = ptr::read_unaligned(p as *const _);
646653
ControlMessageOwned::ScmTimestamp(TimeVal::from(tv))
647654
},
655+
#[cfg(all(target_os = "linux"))]
656+
(libc::SOL_SOCKET, libc::SCM_TIMESTAMPNS) => {
657+
let ts: libc::timespec = ptr::read_unaligned(p as *const _);
658+
ControlMessageOwned::ScmTimestampns(TimeSpec::from(ts))
659+
}
648660
#[cfg(any(
649661
target_os = "android",
650662
target_os = "freebsd",

src/sys/socket/sockopt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ sockopt_impl!(Both, BindToDevice, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, OsStr
272272
#[cfg(any(target_os = "android", target_os = "linux"))]
273273
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::sockaddr_in);
274274
sockopt_impl!(Both, ReceiveTimestamp, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
275+
#[cfg(all(target_os = "linux"))]
276+
sockopt_impl!(Both, ReceiveTimestampns, libc::SOL_SOCKET, libc::SO_TIMESTAMPNS, bool);
275277
#[cfg(any(target_os = "android", target_os = "linux"))]
276278
sockopt_impl!(Both, IpTransparent, libc::SOL_IP, libc::IP_TRANSPARENT, bool);
277279
#[cfg(target_os = "openbsd")]

test/sys/test_socket.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,3 +1535,111 @@ pub fn test_vsock() {
15351535
close(s1).unwrap();
15361536
thr.join().unwrap();
15371537
}
1538+
1539+
// Disable the test on emulated platforms because it fails in Cirrus-CI. Lack of QEMU
1540+
// support is suspected.
1541+
#[cfg_attr(not(any(target_arch = "x86_64")), ignore)]
1542+
#[cfg(all(target_os = "linux"))]
1543+
#[test]
1544+
fn test_recvmsg_timestampns() {
1545+
use nix::sys::socket::*;
1546+
use nix::sys::uio::IoVec;
1547+
use nix::sys::time::*;
1548+
use std::time::*;
1549+
1550+
// Set up
1551+
let message = "Ohayō!".as_bytes();
1552+
let in_socket = socket(
1553+
AddressFamily::Inet,
1554+
SockType::Datagram,
1555+
SockFlag::empty(),
1556+
None).unwrap();
1557+
setsockopt(in_socket, sockopt::ReceiveTimestampns, &true).unwrap();
1558+
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
1559+
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
1560+
let address = getsockname(in_socket).unwrap();
1561+
// Get initial time
1562+
let time0 = SystemTime::now();
1563+
// Send the message
1564+
let iov = [IoVec::from_slice(message)];
1565+
let flags = MsgFlags::empty();
1566+
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap();
1567+
assert_eq!(message.len(), l);
1568+
// Receive the message
1569+
let mut buffer = vec![0u8; message.len()];
1570+
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
1571+
let iov = [IoVec::from_mut_slice(&mut buffer)];
1572+
let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
1573+
let rtime = match r.cmsgs().next() {
1574+
Some(ControlMessageOwned::ScmTimestampns(rtime)) => rtime,
1575+
Some(_) => panic!("Unexpected control message"),
1576+
None => panic!("No control message")
1577+
};
1578+
// Check the final time
1579+
let time1 = SystemTime::now();
1580+
// the packet's received timestamp should lie in-between the two system
1581+
// times, unless the system clock was adjusted in the meantime.
1582+
let rduration = Duration::new(rtime.tv_sec() as u64,
1583+
rtime.tv_nsec() as u32);
1584+
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration);
1585+
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap());
1586+
// Close socket
1587+
nix::unistd::close(in_socket).unwrap();
1588+
}
1589+
1590+
// Disable the test on emulated platforms because it fails in Cirrus-CI. Lack of QEMU
1591+
// support is suspected.
1592+
#[cfg_attr(not(any(target_arch = "x86_64")), ignore)]
1593+
#[cfg(all(target_os = "linux"))]
1594+
#[test]
1595+
fn test_recvmmsg_timestampns() {
1596+
use nix::sys::socket::*;
1597+
use nix::sys::uio::IoVec;
1598+
use nix::sys::time::*;
1599+
use std::time::*;
1600+
1601+
// Set up
1602+
let message = "Ohayō!".as_bytes();
1603+
let in_socket = socket(
1604+
AddressFamily::Inet,
1605+
SockType::Datagram,
1606+
SockFlag::empty(),
1607+
None).unwrap();
1608+
setsockopt(in_socket, sockopt::ReceiveTimestampns, &true).unwrap();
1609+
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
1610+
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
1611+
let address = getsockname(in_socket).unwrap();
1612+
// Get initial time
1613+
let time0 = SystemTime::now();
1614+
// Send the message
1615+
let iov = [IoVec::from_slice(message)];
1616+
let flags = MsgFlags::empty();
1617+
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap();
1618+
assert_eq!(message.len(), l);
1619+
// Receive the message
1620+
let mut buffer = vec![0u8; message.len()];
1621+
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
1622+
let iov = [IoVec::from_mut_slice(&mut buffer)];
1623+
let mut data = vec![
1624+
RecvMmsgData {
1625+
iov,
1626+
cmsg_buffer: Some(&mut cmsgspace),
1627+
},
1628+
];
1629+
let r = recvmmsg(in_socket, &mut data, flags, None).unwrap();
1630+
let rtime = match r[0].cmsgs().next() {
1631+
Some(ControlMessageOwned::ScmTimestampns(rtime)) => rtime,
1632+
Some(_) => panic!("Unexpected control message"),
1633+
None => panic!("No control message")
1634+
};
1635+
// Check the final time
1636+
let time1 = SystemTime::now();
1637+
// the packet's received timestamp should lie in-between the two system
1638+
// times, unless the system clock was adjusted in the meantime.
1639+
let rduration = Duration::new(rtime.tv_sec() as u64,
1640+
rtime.tv_nsec() as u32);
1641+
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration);
1642+
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap());
1643+
// Close socket
1644+
nix::unistd::close(in_socket).unwrap();
1645+
}

0 commit comments

Comments
 (0)