From: Alexander Duyck <alexander.duyck@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com,
john.fastabend@gmail.com, kernel-team@fb.com,
netdev@vger.kernel.org, edumazet@google.com, brakmo@fb.com,
alexanderduyck@fb.com
Subject: [bpf-next PATCH 3/4] selftests/bpf: Replace EXPECT_EQ with ASSERT_EQ and refactor verify_results
Date: Tue, 27 Oct 2020 18:47:20 -0700 [thread overview]
Message-ID: <160384964061.698509.7299229987872061758.stgit@localhost.localdomain> (raw)
In-Reply-To: <160384954046.698509.132709669068189999.stgit@localhost.localdomain>
From: Alexander Duyck <alexanderduyck@fb.com>
There is already logic in test_progs.h for asserting that a value is
expected to be another value. So instead of reinventing it we should just
make use of ASSERT_EQ in tcpbpf_user.c. This will allow for better
debugging and integrates much more closely with the test_progs framework.
In addition we can refactor the code a bit to merge together the two
verify functions and tie them together into a single function. Doing this
helps to clean the code up a bit and makes it more readable as all the
verification is now done in one function.
Lastly we can relocate the verification to the end of the run_test since it
is logically part of the test itself. With this we can drop the need for a
return value from run_test since verification becomes the last step of the
call and then immediately following is the tear down of the test setup.
Signed-off-by: Alexander Duyck <alexanderduyck@fb.com>
---
.../testing/selftests/bpf/prog_tests/tcpbpf_user.c | 119 ++++++++------------
1 file changed, 46 insertions(+), 73 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c
index 71ab82e37eb7..4e1190894e1e 100644
--- a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c
+++ b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c
@@ -11,6 +11,7 @@
static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+static __u32 duration;
static void *server_thread(void *arg)
{
@@ -61,66 +62,57 @@ static void *server_thread(void *arg)
return (void *)(long)err;
}
-#define EXPECT_EQ(expected, actual, fmt) \
- do { \
- if ((expected) != (actual)) { \
- fprintf(stderr, " Value of: " #actual "\n" \
- " Actual: %" fmt "\n" \
- " Expected: %" fmt "\n", \
- (actual), (expected)); \
- ret--; \
- } \
- } while (0)
-
-int verify_result(const struct tcpbpf_globals *result)
-{
- __u32 expected_events;
- int ret = 0;
-
- expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
- (1 << BPF_SOCK_OPS_RWND_INIT) |
- (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
- (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
- (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
- (1 << BPF_SOCK_OPS_NEEDS_ECN) |
- (1 << BPF_SOCK_OPS_STATE_CB) |
- (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
-
- EXPECT_EQ(expected_events, result->event_map, "#" PRIx32);
- EXPECT_EQ(501ULL, result->bytes_received, "llu");
- EXPECT_EQ(1002ULL, result->bytes_acked, "llu");
- EXPECT_EQ(1, result->data_segs_in, PRIu32);
- EXPECT_EQ(1, result->data_segs_out, PRIu32);
- EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32);
- EXPECT_EQ(0, result->good_cb_test_rv, PRIu32);
- EXPECT_EQ(1, result->num_listen, PRIu32);
-
- /* 3 comes from one listening socket + both ends of the connection */
- EXPECT_EQ(3, result->num_close_events, PRIu32);
-
- return ret;
-}
-
-int verify_sockopt_result(int sock_map_fd)
+static void verify_result(int map_fd, int sock_map_fd)
{
+ __u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
+ (1 << BPF_SOCK_OPS_RWND_INIT) |
+ (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
+ (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
+ (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
+ (1 << BPF_SOCK_OPS_NEEDS_ECN) |
+ (1 << BPF_SOCK_OPS_STATE_CB) |
+ (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
+ struct tcpbpf_globals result = { 0 };
__u32 key = 0;
- int ret = 0;
int res;
int rv;
+ rv = bpf_map_lookup_elem(map_fd, &key, &result);
+ if (CHECK_FAIL(rv != 0)) {
+ fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv);
+ return;
+ }
+
+ /* check global map */
+ CHECK(expected_events != result.event_map, "event_map",
+ "unexpected event_map: actual %#" PRIx32" != expected %#" PRIx32 "\n",
+ result.event_map, expected_events);
+
+ ASSERT_EQ(result.bytes_received, 501, "bytes_received");
+ ASSERT_EQ(result.bytes_acked, 1002, "bytes_acked");
+ ASSERT_EQ(result.data_segs_in, 1, "data_segs_in");
+ ASSERT_EQ(result.data_segs_out, 1, "data_segs_out");
+ ASSERT_EQ(result.bad_cb_test_rv, 0x80, "bad_cb_test_rv");
+ ASSERT_EQ(result.good_cb_test_rv, 0, "good_cb_test_rv");
+ ASSERT_EQ(result.num_listen, 1, "num_listen");
+
+ /* 3 comes from one listening socket + both ends of the connection */
+ ASSERT_EQ(result.num_close_events, 3, "num_close_events");
+
/* check setsockopt for SAVE_SYN */
+ key = 0;
rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
- EXPECT_EQ(0, rv, "d");
- EXPECT_EQ(0, res, "d");
- key = 1;
+ ASSERT_EQ(rv, 0, "rv");
+ ASSERT_EQ(res, 0, "res");
+
/* check getsockopt for SAVED_SYN */
+ key = 1;
rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
- EXPECT_EQ(0, rv, "d");
- EXPECT_EQ(1, res, "d");
- return ret;
+ ASSERT_EQ(rv, 0, "rv");
+ ASSERT_EQ(res, 1, "res");
}
-static int run_test(void)
+static void run_test(int map_fd, int sock_map_fd)
{
int server_fd, client_fd;
void *server_err;
@@ -131,7 +123,7 @@ static int run_test(void)
server_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
if (CHECK_FAIL(server_fd < 0))
- return err;
+ return;
pthread_mutex_lock(&server_started_mtx);
if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
@@ -163,17 +155,17 @@ static int run_test(void)
close(client_fd);
close_server_fd:
close(server_fd);
- return err;
+
+ if (!err)
+ verify_result(map_fd, sock_map_fd);
}
void test_tcpbpf_user(void)
{
const char *file = "test_tcpbpf_kern.o";
int prog_fd, map_fd, sock_map_fd;
- struct tcpbpf_globals g = {0};
struct bpf_object *obj;
int cg_fd = -1;
- __u32 key = 0;
int rv;
cg_fd = cgroup_setup_and_join(CG_NAME);
@@ -200,26 +192,7 @@ void test_tcpbpf_user(void)
if (CHECK_FAIL(sock_map_fd < 0))
goto err;
- if (run_test()) {
- fprintf(stderr, "FAILED: TCP server\n");
- goto err;
- }
-
- rv = bpf_map_lookup_elem(map_fd, &key, &g);
- if (CHECK_FAIL(rv != 0)) {
- fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv);
- goto err;
- }
-
- if (CHECK_FAIL(verify_result(&g))) {
- fprintf(stderr, "FAILED: Wrong stats\n");
- goto err;
- }
-
- if (CHECK_FAIL(verify_sockopt_result(sock_map_fd))) {
- fprintf(stderr, "FAILED: Wrong sockopt stats\n");
- goto err;
- }
+ run_test(map_fd, sock_map_fd);
err:
bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
close(cg_fd);
next prev parent reply other threads:[~2020-10-29 0:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-28 1:46 [bpf-next PATCH 0/4] selftests/bpf: Migrate test_tcpbpf_user to be a part of test_progs framework Alexander Duyck
2020-10-28 1:47 ` [bpf-next PATCH 1/4] selftests/bpf: Move test_tcppbf_user into test_progs Alexander Duyck
2020-10-29 23:27 ` Andrii Nakryiko
2020-10-30 0:39 ` Alexander Duyck
2020-10-28 1:47 ` [bpf-next PATCH 2/4] selftests/bpf: Drop python client/server in favor of threads Alexander Duyck
2020-10-29 1:51 ` Martin KaFai Lau
2020-10-29 16:58 ` Alexander Duyck
2020-10-29 18:12 ` Martin KaFai Lau
2020-10-29 19:51 ` Alexander Duyck
2020-10-28 1:47 ` Alexander Duyck [this message]
2020-10-28 1:47 ` [bpf-next PATCH 4/4] selftests/bpf: Migrate tcpbpf_user.c to use BPF skeleton Alexander Duyck
2020-10-29 23:20 ` Andrii Nakryiko
2020-10-30 0:42 ` Alexander Duyck
2020-10-30 2:30 ` Andrii Nakryiko
2020-10-30 15:50 ` Alexander Duyck
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=160384964061.698509.7299229987872061758.stgit@localhost.localdomain \
--to=alexander.duyck@gmail.com \
--cc=alexanderduyck@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brakmo@fb.com \
--cc=daniel@iogearbox.net \
--cc=edumazet@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kernel-team@fb.com \
--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).