[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200922070512.1923268-1-kafai@fb.com>
Date: Tue, 22 Sep 2020 00:05:12 -0700
From: Martin KaFai Lau <kafai@...com>
To: <bpf@...r.kernel.org>
CC: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, <kernel-team@...com>,
Lorenz Bauer <lmb@...udflare.com>, <netdev@...r.kernel.org>
Subject: [PATCH v3 bpf-next 10/11] bpf: selftest: Use network_helpers in the sock_fields test
This patch uses start_server() and connect_to_fd() from network_helpers.h
to remove the network testing boiler plate codes. epoll is no longer
needed also since the timeout has already been taken care of also.
Signed-off-by: Martin KaFai Lau <kafai@...com>
---
.../selftests/bpf/prog_tests/sock_fields.c | 86 ++-----------------
1 file changed, 9 insertions(+), 77 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sock_fields.c b/tools/testing/selftests/bpf/prog_tests/sock_fields.c
index cb6d41bc79a4..23d14e2d0d28 100644
--- a/tools/testing/selftests/bpf/prog_tests/sock_fields.c
+++ b/tools/testing/selftests/bpf/prog_tests/sock_fields.c
@@ -12,7 +12,9 @@
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
+#include <linux/compiler.h>
+#include "network_helpers.h"
#include "cgroup_helpers.h"
#include "test_progs.h"
#include "bpf_rlimit.h"
@@ -43,13 +45,6 @@ static __u32 duration;
static __u32 egress_linum_idx = EGRESS_LINUM_IDX;
static __u32 ingress_linum_idx = INGRESS_LINUM_IDX;
-static void init_loopback6(struct sockaddr_in6 *sa6)
-{
- memset(sa6, 0, sizeof(*sa6));
- sa6->sin6_family = AF_INET6;
- sa6->sin6_addr = in6addr_loopback;
-}
-
static void print_sk(const struct bpf_sock *sk, const char *prefix)
{
char src_ip4[24], dst_ip4[24];
@@ -253,28 +248,14 @@ static int init_sk_storage(int sk_fd, __u32 pkt_out_cnt)
static void do_test(void)
{
- int listen_fd = -1, cli_fd = -1, accept_fd = -1, epfd, err, i;
- struct epoll_event ev;
- socklen_t addrlen;
+ int listen_fd = -1, cli_fd = -1, accept_fd = -1, err, i;
+ socklen_t addrlen = sizeof(struct sockaddr_in6);
char buf[DATA_LEN];
- addrlen = sizeof(struct sockaddr_in6);
- ev.events = EPOLLIN;
-
- epfd = epoll_create(1);
- if (CHECK(epfd == -1, "epoll_create()", "epfd:%d errno:%d\n",
- epfd, errno))
- return;
-
/* Prepare listen_fd */
- listen_fd = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0);
- if (CHECK(listen_fd == -1, "socket()", "listen_fd:%d errno:%d\n",
- listen_fd, errno))
- goto done;
-
- init_loopback6(&srv_sa6);
- err = bind(listen_fd, (struct sockaddr *)&srv_sa6, sizeof(srv_sa6));
- if (CHECK(err, "bind(listen_fd)", "err:%d errno:%d\n", err, errno))
+ listen_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
+ /* start_server() has logged the error details */
+ if (CHECK_FAIL(listen_fd == -1))
goto done;
err = getsockname(listen_fd, (struct sockaddr *)&srv_sa6, &addrlen);
@@ -283,19 +264,8 @@ static void do_test(void)
goto done;
memcpy(&skel->bss->srv_sa6, &srv_sa6, sizeof(srv_sa6));
- err = listen(listen_fd, 1);
- if (CHECK(err, "listen(listen_fd)", "err:%d errno:%d\n", err, errno))
- goto done;
-
- /* Prepare cli_fd */
- cli_fd = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0);
- if (CHECK(cli_fd == -1, "socket()", "cli_fd:%d errno:%d\n", cli_fd,
- errno))
- goto done;
-
- init_loopback6(&cli_sa6);
- err = bind(cli_fd, (struct sockaddr *)&cli_sa6, sizeof(cli_sa6));
- if (CHECK(err, "bind(cli_fd)", "err:%d errno:%d\n", err, errno))
+ cli_fd = connect_to_fd(listen_fd, 0);
+ if (CHECK_FAIL(cli_fd == -1))
goto done;
err = getsockname(cli_fd, (struct sockaddr *)&cli_sa6, &addrlen);
@@ -303,41 +273,12 @@ static void do_test(void)
err, errno))
goto done;
- /* Connect from cli_sa6 to srv_sa6 */
- err = connect(cli_fd, (struct sockaddr *)&srv_sa6, addrlen);
- printf("srv_sa6.sin6_port:%u cli_sa6.sin6_port:%u\n\n",
- ntohs(srv_sa6.sin6_port), ntohs(cli_sa6.sin6_port));
- if (CHECK(err && errno != EINPROGRESS,
- "connect(cli_fd)", "err:%d errno:%d\n", err, errno))
- goto done;
-
- ev.data.fd = listen_fd;
- err = epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &ev);
- if (CHECK(err, "epoll_ctl(EPOLL_CTL_ADD, listen_fd)",
- "err:%d errno:%d\n", err, errno))
- goto done;
-
- /* Accept the connection */
- /* Have some timeout in accept(listen_fd). Just in case. */
- err = epoll_wait(epfd, &ev, 1, 1000);
- if (CHECK(err != 1 || ev.data.fd != listen_fd,
- "epoll_wait(listen_fd)",
- "err:%d errno:%d ev.data.fd:%d listen_fd:%d\n",
- err, errno, ev.data.fd, listen_fd))
- goto done;
-
accept_fd = accept(listen_fd, NULL, NULL);
if (CHECK(accept_fd == -1, "accept(listen_fd)",
"accept_fd:%d errno:%d\n",
accept_fd, errno))
goto done;
- ev.data.fd = cli_fd;
- err = epoll_ctl(epfd, EPOLL_CTL_ADD, cli_fd, &ev);
- if (CHECK(err, "epoll_ctl(EPOLL_CTL_ADD, cli_fd)",
- "err:%d errno:%d\n", err, errno))
- goto done;
-
if (init_sk_storage(accept_fd, 0xeB9F))
goto done;
@@ -350,14 +291,6 @@ static void do_test(void)
"err:%d errno:%d\n", err, errno))
goto done;
- /* Have some timeout in recv(cli_fd). Just in case. */
- err = epoll_wait(epfd, &ev, 1, 1000);
- if (CHECK(err != 1 || ev.data.fd != cli_fd,
- "epoll_wait(cli_fd)",
- "err:%d errno:%d ev.data.fd:%d cli_fd:%d\n",
- err, errno, ev.data.fd, cli_fd))
- goto done;
-
err = recv(cli_fd, buf, DATA_LEN, 0);
if (CHECK(err != DATA_LEN, "recv(cli_fd)", "err:%d errno:%d\n",
err, errno))
@@ -384,7 +317,6 @@ static void do_test(void)
close(cli_fd);
if (listen_fd != -1)
close(listen_fd);
- close(epfd);
}
void test_sock_fields(void)
--
2.24.1
Powered by blists - more mailing lists