Skip to content

Commit 7cb027a

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.
1 parent 43c43ad commit 7cb027a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

rust/kernel/time.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,61 @@
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+
/// The milliseconds time unit with a fixed width of 32bit.
18+
///
19+
/// This is used in networking.
20+
pub type Msecs32 = u32;
21+
22+
/// The microseconds time unit.
23+
pub type Usecs = u64;
24+
25+
/// Microseconds per millisecond.
26+
pub const USEC_PER_MSEC: Usecs = 1000;
27+
28+
/// The microseconds time unit with a fixed width of 32bit.
29+
///
30+
/// This is used in networking.
31+
pub type Usecs32 = u32;
32+
33+
/// The nanosecond time unit.
34+
pub type Nsecs = u64;
35+
36+
/// Nanoseconds per microsecond.
37+
pub const NSEC_PER_USEC: Nsecs = 1000;
38+
1439
/// Converts milliseconds to jiffies.
1540
#[inline]
1641
pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
1742
// SAFETY: The `__msecs_to_jiffies` function is always safe to call no
1843
// matter what the argument is.
1944
unsafe { bindings::__msecs_to_jiffies(msecs) }
2045
}
46+
47+
/// Converts jiffies to milliseconds.
48+
#[inline]
49+
pub fn jiffies_to_msecs(jiffies: Jiffies) -> Msecs {
50+
// SAFETY: The `__msecs_to_jiffies` function is always safe to call no
51+
// matter what the argument is.
52+
unsafe { bindings::jiffies_to_msecs(jiffies) }
53+
}
54+
55+
/// Returns the current time in 32bit jiffies.
56+
#[inline]
57+
pub fn jiffies32() -> Jiffies32 {
58+
// SAFETY: It is always atomic to read the lower 32bit of jiffies.
59+
unsafe { bindings::jiffies as u32 }
60+
}
61+
62+
/// Returns the time elapsed since system boot, in nanoseconds. Does include the
63+
/// time the system was suspended.
64+
#[inline]
65+
pub fn ktime_get_boot_fast_ns() -> Nsecs {
66+
// SAFETY: FFI call without safety requirements.
67+
unsafe { bindings::ktime_get_boot_fast_ns() }
68+
}

0 commit comments

Comments
 (0)