1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Core time conversion utilities for traffic control.
//!
//! The Linux kernel's traffic control subsystem uses its own time units ("ticks").
//! This module provides utilities to convert between human-readable time values
//! and kernel ticks.
use ;
use LazyLock;
/// Path to the kernel's packet scheduler timing information.
///
/// This file exposes how to convert between scheduler ticks and microseconds,
/// which is essential for configuring latency and jitter values correctly.
pub const PSCHED_PATH: &str = "/proc/net/psched";
/// Standard Ethernet MTU in bytes.
pub const MTU_ETHERNET: u32 = 1_500;
/// Initialize the packet scheduler time base by reading `/proc/net/psched`.
///
/// The Linux kernel's traffic control subsystem uses its own time units ("ticks").
/// This function reads the conversion factor from `/proc/net/psched`, which contains
/// four hex values. The first two represent the ratio of ticks to microseconds.
///
/// # How it works
///
/// The file format is: `t2us us2t clock resolution`
///
/// - `t2us`: ticks to microseconds numerator
/// - `us2t`: ticks to microseconds denominator
/// - The ratio `t2us / us2t` gives us ticks per microsecond
///
/// # Returns
///
/// The number of ticks per microsecond, used by [`usec_to_ticks`].
///
/// # Reference
///
/// Adapted from `iproute2/tc/tc_core.c`.
/// Cached value of ticks per microsecond, initialized lazily on first use.
///
/// This avoids repeatedly reading `/proc/net/psched` for every qdisc configuration.
pub static TICK_IN_USEC: =
new;
/// Convert microseconds to kernel packet scheduler ticks.
///
/// The kernel's netem qdisc expects latency and jitter in ticks, not microseconds.
/// This function performs the conversion using the system's tick rate.