Skip to content

Commit

Permalink
selftests/bpf: Add BPF-helper test for CLOCK_TAI access
Browse files Browse the repository at this point in the history
Add BPF-helper test case for CLOCK_TAI access. The added test verifies that:

 * Timestamps are generated
 * Timestamps are moving forward
 * Timestamps are reasonable

Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
Link: https://lore.kernel.org/r/20220809060803.5773-3-kurt@linutronix.de
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
shifty91 authored and Alexei Starovoitov committed Aug 9, 2022
1 parent c8996c9 commit 64e1582
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/time_tai.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2022 Linutronix GmbH */

#include <test_progs.h>
#include <network_helpers.h>

#include "test_time_tai.skel.h"

#include <time.h>
#include <stdint.h>

#define TAI_THRESHOLD 1000000000ULL /* 1s */
#define NSEC_PER_SEC 1000000000ULL

static __u64 ts_to_ns(const struct timespec *ts)
{
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
}

void test_time_tai(void)
{
struct __sk_buff skb = {
.cb[0] = 0,
.cb[1] = 0,
.tstamp = 0,
};
LIBBPF_OPTS(bpf_test_run_opts, topts,
.data_in = &pkt_v4,
.data_size_in = sizeof(pkt_v4),
.ctx_in = &skb,
.ctx_size_in = sizeof(skb),
.ctx_out = &skb,
.ctx_size_out = sizeof(skb),
);
struct test_time_tai *skel;
struct timespec now_tai;
__u64 ts1, ts2, now;
int ret, prog_fd;

/* Open and load */
skel = test_time_tai__open_and_load();
if (!ASSERT_OK_PTR(skel, "tai_open"))
return;

/* Run test program */
prog_fd = bpf_program__fd(skel->progs.time_tai);
ret = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(ret, "test_run");

/* Retrieve generated TAI timestamps */
ts1 = skb.tstamp;
ts2 = skb.cb[0] | ((__u64)skb.cb[1] << 32);

/* TAI != 0 */
ASSERT_NEQ(ts1, 0, "tai_ts1");
ASSERT_NEQ(ts2, 0, "tai_ts2");

/* TAI is moving forward only */
ASSERT_GT(ts2, ts1, "tai_forward");

/* Check for future */
ret = clock_gettime(CLOCK_TAI, &now_tai);
ASSERT_EQ(ret, 0, "tai_gettime");
now = ts_to_ns(&now_tai);

ASSERT_TRUE(now > ts1, "tai_future_ts1");
ASSERT_TRUE(now > ts2, "tai_future_ts2");

/* Check for reasonable range */
ASSERT_TRUE(now - ts1 < TAI_THRESHOLD, "tai_range_ts1");
ASSERT_TRUE(now - ts2 < TAI_THRESHOLD, "tai_range_ts2");

test_time_tai__destroy(skel);
}
24 changes: 24 additions & 0 deletions tools/testing/selftests/bpf/progs/test_time_tai.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2022 Linutronix GmbH */

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

char _license[] SEC("license") = "GPL";

SEC("tc")
int time_tai(struct __sk_buff *skb)
{
__u64 ts1, ts2;

/* Get TAI timestamps */
ts1 = bpf_ktime_get_tai_ns();
ts2 = bpf_ktime_get_tai_ns();

/* Save TAI timestamps (Note: skb->hwtstamp is read-only) */
skb->tstamp = ts1;
skb->cb[0] = ts2 & 0xffffffff;
skb->cb[1] = ts2 >> 32;

return 0;
}

0 comments on commit 64e1582

Please sign in to comment.