Skip to content

Commit d194b38

Browse files
committed
feat: Add glibc::SOF_TIMESTAMPING_* support
1 parent 150579c commit d194b38

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/sys/socket/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,14 @@ pub enum ControlMessageOwned {
591591
/// # }
592592
/// ```
593593
ScmTimestamp(TimeVal),
594+
/// A set of nanosecond resolution timestamps
595+
///
596+
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
597+
#[cfg(all(target_os = "linux"))]
598+
ScmTimestampsns(Timestamping),
599+
#[cfg(any(
600+
target_os = "linux",
601+
))]
594602
/// Nanoseconds resolution timestamp
595603
///
596604
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
@@ -666,6 +674,13 @@ pub enum ControlMessageOwned {
666674
Unknown(UnknownCmsg),
667675
}
668676

677+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
678+
pub struct Timestamping {
679+
pub system: TimeSpec,
680+
pub hw_trans: TimeSpec,
681+
pub hw_raw: TimeSpec,
682+
}
683+
669684
impl ControlMessageOwned {
670685
/// Decodes a `ControlMessageOwned` from raw bytes.
671686
///
@@ -710,6 +725,19 @@ impl ControlMessageOwned {
710725
let ts: libc::timespec = ptr::read_unaligned(p as *const _);
711726
ControlMessageOwned::ScmTimestampns(TimeSpec::from(ts))
712727
}
728+
#[cfg(all(target_os = "linux"))]
729+
(libc::SOL_SOCKET, libc::SCM_TIMESTAMPING) => {
730+
let ts: libc::timespec = ptr::read_unaligned(p as *const _);
731+
let system = TimeSpec::from(ts);
732+
let tsp = (p as *const libc::timespec).add(1);
733+
let ts: libc::timespec = ptr::read_unaligned(tsp as *const _);
734+
let hw_trans = TimeSpec::from(ts);
735+
let tsp = (p as *const libc::timespec).add(2);
736+
let ts: libc::timespec = ptr::read_unaligned(tsp as *const _);
737+
let hw_raw = TimeSpec::from(ts);
738+
let timestamping = Timestamping { system, hw_trans, hw_raw };
739+
ControlMessageOwned::ScmTimestampsns(timestamping)
740+
}
713741
#[cfg(any(
714742
target_os = "android",
715743
target_os = "freebsd",

src/sys/socket/sockopt.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::os::unix::ffi::OsStrExt;
1616

1717
// Constants
1818
// TCP_CA_NAME_MAX isn't defined in user space include files
19-
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
19+
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
2020
const TCP_CA_NAME_MAX: usize = 16;
2121

2222
/// Helper for implementing `SetSockOpt` for a given socket option. See
@@ -432,7 +432,18 @@ sockopt_impl!(
432432
#[allow(missing_docs)]
433433
// Not documented by Linux!
434434
Ip6tOriginalDst, GetOnly, libc::SOL_IPV6, libc::IP6T_SO_ORIGINAL_DST, libc::sockaddr_in6);
435-
sockopt_impl!(
435+
sockopt_impl!(
436+
/// Specifies exact type of timestamping information collected by the kernel
437+
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
438+
/// Takes a bitwise `or` of libc timestamping options:
439+
/// - [SOF_TIMESTAMPING_TX_HARDWARE][::libc::SOF_TIMESTAMPING_TX_HARDWARE]:
440+
/// - [SOF_TIMESTAMPING_TX_SOFTWARE][::libc::SOF_TIMESTAMPING_TX_SOFTWARE]:
441+
/// - [SOF_TIMESTAMPING_RX_HARDWARE][::libc::SOF_TIMESTAMPING_RX_HARDWARE]:
442+
/// - [SOF_TIMESTAMPING_RX_SOFTWARE][::libc::SOF_TIMESTAMPING_RX_SOFTWARE]:
443+
/// - [SOF_TIMESTAMPING_RAW_HARDWARE][::libc::SOF_TIMESTAMPING_RAW_HARDWARE]:
444+
/// - [SOF_TIMESTAMPING_SOFTWARE][::libc::SOF_TIMESTAMPING_SOFTWARE]
445+
Timestamping, Both, libc::SOL_SOCKET, libc::SO_TIMESTAMPING, u32);
446+
sockopt_impl!(
436447
/// Enable or disable the receiving of the `SO_TIMESTAMP` control message.
437448
ReceiveTimestamp, Both, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
438449
#[cfg(all(target_os = "linux"))]
@@ -463,7 +474,7 @@ sockopt_impl!(
463474
/// Enable or disable the receiving of the `SCM_CREDENTIALS` control
464475
/// message.
465476
PassCred, Both, libc::SOL_SOCKET, libc::SO_PASSCRED, bool);
466-
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
477+
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
467478
sockopt_impl!(
468479
/// This option allows the caller to set the TCP congestion control
469480
/// algorithm to be used, on a per-socket basis.

0 commit comments

Comments
 (0)