From: Lingpeng Chen <forrest0579@gmail.com>
To: bpf <bpf@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
"David S . Miller" <davem@davemloft.net>,
netdev@vger.kernel.org, Petar Penkov <ppenkov.kernel@gmail.com>,
Lingpeng Chen <forrest0579@gmail.com>
Subject: [PATCH v3 bpf-next 3/3] selftests/bpf: add selftest for get_netns_id helper
Date: Thu, 20 Feb 2020 07:10:54 +0000 [thread overview]
Message-ID: <20200220071054.12499-4-forrest0579@gmail.com> (raw)
In-Reply-To: <20200220071054.12499-1-forrest0579@gmail.com>
adding selftest for new bpf helper function get_netns_id
Signed-off-by: Lingpeng Chen <forrest0579@gmail.com>
---
.../selftests/bpf/progs/test_tcpbpf_kern.c | 11 +++++
.../testing/selftests/bpf/test_tcpbpf_user.c | 46 ++++++++++++++++++-
2 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c b/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
index 1f1966e86e9f..d7d851ddd2cc 100644
--- a/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
+++ b/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
@@ -28,6 +28,13 @@ struct {
__type(value, int);
} sockopt_results SEC(".maps");
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} netns_number SEC(".maps");
+
static inline void update_event_map(int event)
{
__u32 key = 0;
@@ -61,6 +68,7 @@ int bpf_testcb(struct bpf_sock_ops *skops)
int rv = -1;
int v = 0;
int op;
+ __u64 netns_id;
op = (int) skops->op;
@@ -144,6 +152,9 @@ int bpf_testcb(struct bpf_sock_ops *skops)
__u32 key = 0;
bpf_map_update_elem(&sockopt_results, &key, &v, BPF_ANY);
+
+ netns_id = bpf_get_netns_id(skops);
+ bpf_map_update_elem(&netns_number, &key, &netns_id, BPF_ANY);
break;
default:
rv = -1;
diff --git a/tools/testing/selftests/bpf/test_tcpbpf_user.c b/tools/testing/selftests/bpf/test_tcpbpf_user.c
index 3ae127620463..fef2f4d77ecc 100644
--- a/tools/testing/selftests/bpf/test_tcpbpf_user.c
+++ b/tools/testing/selftests/bpf/test_tcpbpf_user.c
@@ -76,6 +76,41 @@ int verify_sockopt_result(int sock_map_fd)
return ret;
}
+int verify_netns(__u64 netns_id)
+{
+ char buf1[40];
+ char buf2[40];
+ int ret = 0;
+ ssize_t len = 0;
+
+ len = readlink("/proc/self/ns/net", buf1, 39);
+ sprintf(buf2, "net:[%llu]", netns_id);
+
+ if (len <= 0) {
+ printf("FAILED: readlink /proc/self/ns/net");
+ return ret;
+ }
+
+ if (strncmp(buf1, buf2, len)) {
+ printf("FAILED: netns don't match");
+ ret = 1;
+ }
+ return ret;
+}
+
+int verify_netns_result(int netns_map_fd)
+{
+ __u32 key = 0;
+ __u64 res = 0;
+ int ret = 0;
+ int rv;
+
+ rv = bpf_map_lookup_elem(netns_map_fd, &key, &res);
+ EXPECT_EQ(0, rv, "d");
+
+ return verify_netns(res);
+}
+
static int bpf_find_map(const char *test, struct bpf_object *obj,
const char *name)
{
@@ -92,7 +127,7 @@ static int bpf_find_map(const char *test, struct bpf_object *obj,
int main(int argc, char **argv)
{
const char *file = "test_tcpbpf_kern.o";
- int prog_fd, map_fd, sock_map_fd;
+ int prog_fd, map_fd, sock_map_fd, netns_map_fd;
struct tcpbpf_globals g = {0};
const char *cg_path = "/foo";
int error = EXIT_FAILURE;
@@ -137,6 +172,10 @@ int main(int argc, char **argv)
if (sock_map_fd < 0)
goto err;
+ netns_map_fd = bpf_find_map(__func__, obj, "netns_number");
+ if (netns_map_fd < 0)
+ goto err;
+
retry_lookup:
rv = bpf_map_lookup_elem(map_fd, &key, &g);
if (rv != 0) {
@@ -161,6 +200,11 @@ int main(int argc, char **argv)
goto err;
}
+ if (verify_netns_result(netns_map_fd)) {
+ printf("FAILED: Wrong netns stats\n");
+ goto err;
+ }
+
printf("PASSED!\n");
error = 0;
err:
--
2.20.1
next prev parent reply other threads:[~2020-02-20 7:12 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-06 8:35 [PATCH bpf-next 0/2] bpf: Add sock ops get netns helpers Lingpeng Chen
2020-02-06 8:35 ` [PATCH bpf-next 1/2] " Lingpeng Chen
2020-02-06 18:48 ` Petar Penkov
2020-02-08 0:14 ` kbuild test robot
2020-02-06 8:35 ` [PATCH bpf-next 2/2] bpf: Sync uapi bpf.h to tools/ Lingpeng Chen
2020-02-18 9:15 ` [PATCH v2 bpf-next 0/3] bpf: Add sock_ops_get_netns helpers Lingpeng Chen
2020-02-18 9:15 ` [PATCH v2 bpf-next 1/3] bpf: Add sock ops get netns helpers Lingpeng Chen
2020-02-20 0:04 ` Daniel Borkmann
2020-02-20 7:10 ` [PATCH v3 bpf-next 0/3] bpf: Add get_netns_id helper for sock_ops Lingpeng Chen
2020-02-20 7:10 ` [PATCH v3 bpf-next 1/3] bpf: Add get_netns_id helper function " Lingpeng Chen
2020-02-24 23:48 ` Song Liu
2020-02-25 4:45 ` [PATCH v4 bpf-next 0/3] bpf: Add get_netns_id helper " Lingpeng Chen
2020-02-25 4:45 ` [PATCH v4 bpf-next 1/3] bpf: Add get_netns_id helper function " Lingpeng Chen
2020-02-25 5:54 ` Song Liu
2020-02-25 4:45 ` [PATCH v4 bpf-next 2/3] bpf: Sync uapi bpf.h to tools/ Lingpeng Chen
2020-02-25 4:45 ` [PATCH v4 bpf-next 3/3] selftests/bpf: add selftest for get_netns_id helper Lingpeng Chen
2020-02-25 6:13 ` Andrii Nakryiko
[not found] ` <CAH+Qyb+rQeebkb1TtLuNHPLmf-VRLqj1yvsHXtaqfzHKMA4azQ@mail.gmail.com>
2020-02-25 17:13 ` Andrii Nakryiko
[not found] ` <CAH+Qyb+-Q7OSrobdojRiep5cmnzwfMnGJ2HPfjvEPiTPtse+LQ@mail.gmail.com>
2020-02-26 4:35 ` Andrii Nakryiko
2020-03-02 8:23 ` [PATCH v4 bpf-next 0/3] bpf: Add get_netns_id helper for sock_ops Forrest Chen
2020-02-20 7:10 ` [PATCH v3 bpf-next 2/3] bpf: Sync uapi bpf.h to tools/ Lingpeng Chen
2020-02-24 23:49 ` Song Liu
2020-02-20 7:10 ` Lingpeng Chen [this message]
2020-02-24 23:55 ` [PATCH v3 bpf-next 3/3] selftests/bpf: add selftest for get_netns_id helper Song Liu
2020-02-18 9:15 ` [PATCH v2 bpf-next 2/3] bpf: Sync uapi bpf.h to tools/ Lingpeng Chen
2020-02-18 9:15 ` [PATCH v2 bpf-next 3/3] selftests/bpf: add selftest for sock_ops_get_netns helper Lingpeng Chen
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=20200220071054.12499-4-forrest0579@gmail.com \
--to=forrest0579@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=john.fastabend@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=ppenkov.kernel@gmail.com \
/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).