Skip to content

Commit c8996c9

Browse files
netoptimizerAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add BPF-helper for accessing CLOCK_TAI
Commit 3dc6ffa ("timekeeping: Introduce fast accessor to clock tai") introduced a fast and NMI-safe accessor for CLOCK_TAI. Especially in time sensitive networks (TSN), where all nodes are synchronized by Precision Time Protocol (PTP), it's helpful to have the possibility to generate timestamps based on CLOCK_TAI instead of CLOCK_MONOTONIC. With a BPF helper for TAI in place, it becomes very convenient to correlate activity across different machines in the network. Use cases for such a BPF helper include functionalities such as Tx launch time (e.g. ETF and TAPRIO Qdiscs) and timestamping. Note: CLOCK_TAI is nothing new per se, only the NMI-safe variant of it is. Signed-off-by: Jesper Dangaard Brouer <[email protected]> [Kurt: Wrote changelog and renamed helper] Signed-off-by: Kurt Kanzenbach <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b2d8ef1 commit c8996c9

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

Diff for: include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,7 @@ extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
23492349
extern const struct bpf_func_proto bpf_tail_call_proto;
23502350
extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
23512351
extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
2352+
extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
23522353
extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
23532354
extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
23542355
extern const struct bpf_func_proto bpf_get_current_comm_proto;

Diff for: include/uapi/linux/bpf.h

+13
Original file line numberDiff line numberDiff line change
@@ -5341,6 +5341,18 @@ union bpf_attr {
53415341
* **-EACCES** if the SYN cookie is not valid.
53425342
*
53435343
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
5344+
*
5345+
* u64 bpf_ktime_get_tai_ns(void)
5346+
* Description
5347+
* A nonsettable system-wide clock derived from wall-clock time but
5348+
* ignoring leap seconds. This clock does not experience
5349+
* discontinuities and backwards jumps caused by NTP inserting leap
5350+
* seconds as CLOCK_REALTIME does.
5351+
*
5352+
* See: **clock_gettime**\ (**CLOCK_TAI**)
5353+
* Return
5354+
* Current *ktime*.
5355+
*
53445356
*/
53455357
#define __BPF_FUNC_MAPPER(FN) \
53465358
FN(unspec), \
@@ -5551,6 +5563,7 @@ union bpf_attr {
55515563
FN(tcp_raw_gen_syncookie_ipv6), \
55525564
FN(tcp_raw_check_syncookie_ipv4), \
55535565
FN(tcp_raw_check_syncookie_ipv6), \
5566+
FN(ktime_get_tai_ns), \
55545567
/* */
55555568

55565569
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

Diff for: kernel/bpf/core.c

+1
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,7 @@ const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
26232623
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
26242624
const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
26252625
const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak;
2626+
const struct bpf_func_proto bpf_ktime_get_tai_ns_proto __weak;
26262627

26272628
const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
26282629
const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;

Diff for: kernel/bpf/helpers.c

+14
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
198198
.ret_type = RET_INTEGER,
199199
};
200200

201+
BPF_CALL_0(bpf_ktime_get_tai_ns)
202+
{
203+
/* NMI safe access to clock tai */
204+
return ktime_get_tai_fast_ns();
205+
}
206+
207+
const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = {
208+
.func = bpf_ktime_get_tai_ns,
209+
.gpl_only = false,
210+
.ret_type = RET_INTEGER,
211+
};
212+
201213
BPF_CALL_0(bpf_get_current_pid_tgid)
202214
{
203215
struct task_struct *task = current;
@@ -1617,6 +1629,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
16171629
return &bpf_ktime_get_ns_proto;
16181630
case BPF_FUNC_ktime_get_boot_ns:
16191631
return &bpf_ktime_get_boot_ns_proto;
1632+
case BPF_FUNC_ktime_get_tai_ns:
1633+
return &bpf_ktime_get_tai_ns_proto;
16201634
case BPF_FUNC_ringbuf_output:
16211635
return &bpf_ringbuf_output_proto;
16221636
case BPF_FUNC_ringbuf_reserve:

Diff for: tools/include/uapi/linux/bpf.h

+13
Original file line numberDiff line numberDiff line change
@@ -5341,6 +5341,18 @@ union bpf_attr {
53415341
* **-EACCES** if the SYN cookie is not valid.
53425342
*
53435343
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
5344+
*
5345+
* u64 bpf_ktime_get_tai_ns(void)
5346+
* Description
5347+
* A nonsettable system-wide clock derived from wall-clock time but
5348+
* ignoring leap seconds. This clock does not experience
5349+
* discontinuities and backwards jumps caused by NTP inserting leap
5350+
* seconds as CLOCK_REALTIME does.
5351+
*
5352+
* See: **clock_gettime**\ (**CLOCK_TAI**)
5353+
* Return
5354+
* Current *ktime*.
5355+
*
53445356
*/
53455357
#define __BPF_FUNC_MAPPER(FN) \
53465358
FN(unspec), \
@@ -5551,6 +5563,7 @@ union bpf_attr {
55515563
FN(tcp_raw_gen_syncookie_ipv6), \
55525564
FN(tcp_raw_check_syncookie_ipv4), \
55535565
FN(tcp_raw_check_syncookie_ipv6), \
5566+
FN(ktime_get_tai_ns), \
55545567
/* */
55555568

55565569
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)