Skip to content

Commit 08fc8c3

Browse files
author
Valentin Obst
committed
rust/kernel: add time primitives for net/tcp
In net/tcp time values are usually 32bit wide unsigned integers, and either in units of jiffies, microseconds or milliseconds. Add types, constants, and functions to work with 32bit time values. This is, for example, used in the CUBIC and BIC CCAs. Signed-off-by: Valentin Obst <[email protected]>
1 parent a13e700 commit 08fc8c3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

rust/kernel/time.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,84 @@
88
/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
99
pub type Jiffies = core::ffi::c_ulong;
1010

11+
/// Jiffies, but with a fixed width of 32bit.
12+
pub type Jiffies32 = u32;
13+
1114
/// The millisecond time unit.
1215
pub type Msecs = core::ffi::c_uint;
1316

17+
/// Milliseconds per second.
18+
pub const MSEC_PER_SEC: Msecs = 1000;
19+
20+
/// The milliseconds time unit with a fixed width of 32bit.
21+
///
22+
/// This is used in networking.
23+
pub type Msecs32 = u32;
24+
25+
/// The microseconds time unit.
26+
pub type Usecs = u64;
27+
28+
/// Microseconds per millisecond.
29+
pub const USEC_PER_MSEC: Usecs = 1000;
30+
31+
/// Microseconds per second.
32+
pub const USEC_PER_SEC: Usecs = 1_000_000;
33+
34+
/// The microseconds time unit with a fixed width of 32bit.
35+
///
36+
/// This is used in networking.
37+
pub type Usecs32 = u32;
38+
39+
/// The nanosecond time unit.
40+
pub type Nsecs = u64;
41+
42+
/// Nanoseconds per microsecond.
43+
pub const NSEC_PER_USEC: Nsecs = 1000;
44+
45+
/// Nanoseconds per millisecond.
46+
pub const NSEC_PER_MSEC: Nsecs = 1_000_000;
47+
1448
/// Converts milliseconds to jiffies.
1549
#[inline]
1650
pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
1751
// SAFETY: The `__msecs_to_jiffies` function is always safe to call no
1852
// matter what the argument is.
1953
unsafe { bindings::__msecs_to_jiffies(msecs) }
2054
}
55+
56+
/// Converts jiffies to milliseconds.
57+
#[inline]
58+
pub fn jiffies_to_msecs(jiffies: Jiffies) -> Msecs {
59+
// SAFETY: The `__msecs_to_jiffies` function is always safe to call no
60+
// matter what the argument is.
61+
unsafe { bindings::jiffies_to_msecs(jiffies) }
62+
}
63+
64+
/// Returns the current time in 32bit jiffies.
65+
#[inline]
66+
pub fn jiffies32() -> Jiffies32 {
67+
// SAFETY: It is always atomic to read the lower 32bit of jiffies.
68+
unsafe { bindings::jiffies as u32 }
69+
}
70+
71+
/// Returns the time elapsed since system boot, in nanoseconds. Does include the
72+
/// time the system was suspended.
73+
#[inline]
74+
pub fn ktime_get_boot_fast_ns() -> Nsecs {
75+
// SAFETY: FFI call without safety requirements.
76+
unsafe { bindings::ktime_get_boot_fast_ns() }
77+
}
78+
79+
/// Returns the time elapsed since system boot, in 32bit microseconds. Does
80+
/// include the time the system was suspended.
81+
#[inline]
82+
pub fn ktime_get_boot_fast_us32() -> Usecs32 {
83+
(ktime_get_boot_fast_ns() / NSEC_PER_USEC) as Usecs32
84+
}
85+
86+
/// Returns the time elapsed since system boot, in 32bit milliseconds. Does
87+
/// include the time the system was suspended.
88+
#[inline]
89+
pub fn ktime_get_boot_fast_ms32() -> Msecs32 {
90+
(ktime_get_boot_fast_ns() / NSEC_PER_MSEC) as Msecs32
91+
}

0 commit comments

Comments
 (0)