From: bimmy.pujari@intel.com
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, mchehab@kernel.org, ast@kernel.org,
daniel@iogearbox.net, kafai@fb.com, maze@google.com,
bimmy.pujari@intel.com, ashkan.nikravesh@intel.com,
Daniel.A.Alvarez@intel.com
Subject: [PATCH bpf-next v7 2/2] selftests/bpf: Selftest for real time helper
Date: Wed, 30 Sep 2020 19:05:04 -0700 [thread overview]
Message-ID: <20201001020504.18151-2-bimmy.pujari@intel.com> (raw)
In-Reply-To: <20201001020504.18151-1-bimmy.pujari@intel.com>
From: Bimmy Pujari <bimmy.pujari@intel.com>
Add test validating that bpf_ktime_get_real_ns works fine.
Signed-off-by: Bimmy Pujari <bimmy.pujari@intel.com>
---
.../selftests/bpf/prog_tests/ktime_real.c | 42 +++++++++++++++++++
.../bpf/progs/test_ktime_get_real_ns.c | 36 ++++++++++++++++
2 files changed, 78 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/ktime_real.c
create mode 100644 tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c
diff --git a/tools/testing/selftests/bpf/prog_tests/ktime_real.c b/tools/testing/selftests/bpf/prog_tests/ktime_real.c
new file mode 100644
index 000000000000..85235f2786b2
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/ktime_real.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+
+static void *time_thread(void *arg)
+{
+ __u32 duration, retval;
+ int err, prog_fd = *(u32 *) arg;
+
+ err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
+ NULL, NULL, &retval, &duration);
+ CHECK(err || retval, "",
+ "err %d errno %d retval %d duration %d\n",
+ err, errno, retval, duration);
+ pthread_exit(arg);
+}
+
+void test_ktime_real(void)
+{
+ const char *file = "./test_ktime_get_real_ns.o";
+ struct bpf_object *obj = NULL;
+ pthread_t thread_id;
+ int prog_fd;
+ int err = 0;
+ void *ret;
+
+ err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
+ if (CHECK_FAIL(err)) {
+ printf("test_ktime_get_real_ns:bpf_prog_load errno %d\n", errno);
+ goto close_prog;
+ }
+
+ if (CHECK_FAIL(pthread_create(&thread_id, NULL,
+ &time_thread, &prog_fd)))
+ goto close_prog;
+
+ if (CHECK_FAIL(pthread_join(thread_id, &ret) ||
+ ret != (void *)&prog_fd))
+ goto close_prog;
+close_prog:
+ bpf_object__close(obj);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c b/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c
new file mode 100644
index 000000000000..37132c59de61
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ktime_get_real_ns.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <linux/version.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, unsigned long long);
+} time_map SEC(".maps");
+
+SEC("realtime_helper")
+int realtime_helper_test(struct __sk_buff *skb)
+{
+ unsigned long long *lasttime;
+ unsigned long long curtime;
+ int key = 0;
+ int err = 0;
+
+ lasttime = bpf_map_lookup_elem(&time_map, &key);
+ if (!lasttime)
+ goto err;
+
+ curtime = bpf_ktime_get_real_ns();
+ if (curtime <= *lasttime) {
+ err = 1;
+ goto err;
+ }
+ *lasttime = curtime;
+
+err:
+ return err;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.17.1
next prev parent reply other threads:[~2020-10-01 2:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-01 2:05 [PATCH bpf-next v7 1/2] bpf: Add bpf_ktime_get_real_ns bimmy.pujari
2020-10-01 2:05 ` bimmy.pujari [this message]
2020-10-01 5:35 ` [PATCH bpf-next v7 2/2] selftests/bpf: Selftest for real time helper Alexei Starovoitov
2020-10-01 21:52 ` Pujari, Bimmy
2020-10-01 22:50 ` Alexei Starovoitov
2020-10-05 17:36 ` Maciej Żenczykowski
2020-10-07 16:33 ` David Ahern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201001020504.18151-2-bimmy.pujari@intel.com \
--to=bimmy.pujari@intel.com \
--cc=Daniel.A.Alvarez@intel.com \
--cc=ashkan.nikravesh@intel.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=maze@google.com \
--cc=mchehab@kernel.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).