~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/time_tai.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /* Copyright (C) 2022 Linutronix GmbH */
  3 
  4 #include <test_progs.h>
  5 #include <network_helpers.h>
  6 
  7 #include "test_time_tai.skel.h"
  8 
  9 #include <time.h>
 10 #include <stdint.h>
 11 
 12 #define TAI_THRESHOLD   1000000000ULL /* 1s */
 13 #define NSEC_PER_SEC    1000000000ULL
 14 
 15 static __u64 ts_to_ns(const struct timespec *ts)
 16 {
 17         return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
 18 }
 19 
 20 void test_time_tai(void)
 21 {
 22         struct __sk_buff skb = {
 23                 .cb[0] = 0,
 24                 .cb[1] = 0,
 25                 .tstamp = 0,
 26         };
 27         LIBBPF_OPTS(bpf_test_run_opts, topts,
 28                 .data_in = &pkt_v4,
 29                 .data_size_in = sizeof(pkt_v4),
 30                 .ctx_in = &skb,
 31                 .ctx_size_in = sizeof(skb),
 32                 .ctx_out = &skb,
 33                 .ctx_size_out = sizeof(skb),
 34         );
 35         struct test_time_tai *skel;
 36         struct timespec now_tai;
 37         __u64 ts1, ts2, now;
 38         int ret, prog_fd;
 39 
 40         /* Open and load */
 41         skel = test_time_tai__open_and_load();
 42         if (!ASSERT_OK_PTR(skel, "tai_open"))
 43                 return;
 44 
 45         /* Run test program */
 46         prog_fd = bpf_program__fd(skel->progs.time_tai);
 47         ret = bpf_prog_test_run_opts(prog_fd, &topts);
 48         ASSERT_OK(ret, "test_run");
 49 
 50         /* Retrieve generated TAI timestamps */
 51         ts1 = skb.tstamp;
 52         ts2 = skb.cb[0] | ((__u64)skb.cb[1] << 32);
 53 
 54         /* TAI != 0 */
 55         ASSERT_NEQ(ts1, 0, "tai_ts1");
 56         ASSERT_NEQ(ts2, 0, "tai_ts2");
 57 
 58         /* TAI is moving forward only */
 59         ASSERT_GE(ts2, ts1, "tai_forward");
 60 
 61         /* Check for future */
 62         ret = clock_gettime(CLOCK_TAI, &now_tai);
 63         ASSERT_EQ(ret, 0, "tai_gettime");
 64         now = ts_to_ns(&now_tai);
 65 
 66         ASSERT_TRUE(now > ts1, "tai_future_ts1");
 67         ASSERT_TRUE(now > ts2, "tai_future_ts2");
 68 
 69         /* Check for reasonable range */
 70         ASSERT_TRUE(now - ts1 < TAI_THRESHOLD, "tai_range_ts1");
 71         ASSERT_TRUE(now - ts2 < TAI_THRESHOLD, "tai_range_ts2");
 72 
 73         test_time_tai__destroy(skel);
 74 }
 75 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php