Commit c40a565a authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

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: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200925000440.3858639-1-kafai@fb.com
parent b18c1f0a
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Facebook */ /* Copyright (c) 2019 Facebook */
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
...@@ -12,7 +10,9 @@ ...@@ -12,7 +10,9 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <linux/compiler.h>
#include "network_helpers.h"
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "test_progs.h" #include "test_progs.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
...@@ -43,13 +43,6 @@ static __u32 duration; ...@@ -43,13 +43,6 @@ static __u32 duration;
static __u32 egress_linum_idx = EGRESS_LINUM_IDX; static __u32 egress_linum_idx = EGRESS_LINUM_IDX;
static __u32 ingress_linum_idx = INGRESS_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) static void print_sk(const struct bpf_sock *sk, const char *prefix)
{ {
char src_ip4[24], dst_ip4[24]; char src_ip4[24], dst_ip4[24];
...@@ -252,28 +245,14 @@ static int init_sk_storage(int sk_fd, __u32 pkt_out_cnt) ...@@ -252,28 +245,14 @@ static int init_sk_storage(int sk_fd, __u32 pkt_out_cnt)
static void test(void) static void test(void)
{ {
int listen_fd = -1, cli_fd = -1, accept_fd = -1, epfd, err, i; int listen_fd = -1, cli_fd = -1, accept_fd = -1, err, i;
struct epoll_event ev; socklen_t addrlen = sizeof(struct sockaddr_in6);
socklen_t addrlen;
char buf[DATA_LEN]; 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 */ /* Prepare listen_fd */
listen_fd = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); listen_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
if (CHECK(listen_fd == -1, "socket()", "listen_fd:%d errno:%d\n", /* start_server() has logged the error details */
listen_fd, errno)) if (CHECK_FAIL(listen_fd == -1))
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))
goto done; goto done;
err = getsockname(listen_fd, (struct sockaddr *)&srv_sa6, &addrlen); err = getsockname(listen_fd, (struct sockaddr *)&srv_sa6, &addrlen);
...@@ -282,19 +261,8 @@ static void test(void) ...@@ -282,19 +261,8 @@ static void test(void)
goto done; goto done;
memcpy(&skel->bss->srv_sa6, &srv_sa6, sizeof(srv_sa6)); memcpy(&skel->bss->srv_sa6, &srv_sa6, sizeof(srv_sa6));
err = listen(listen_fd, 1); cli_fd = connect_to_fd(listen_fd, 0);
if (CHECK(err, "listen(listen_fd)", "err:%d errno:%d\n", err, errno)) if (CHECK_FAIL(cli_fd == -1))
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))
goto done; goto done;
err = getsockname(cli_fd, (struct sockaddr *)&cli_sa6, &addrlen); err = getsockname(cli_fd, (struct sockaddr *)&cli_sa6, &addrlen);
...@@ -302,41 +270,12 @@ static void test(void) ...@@ -302,41 +270,12 @@ static void test(void)
err, errno)) err, errno))
goto done; 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); accept_fd = accept(listen_fd, NULL, NULL);
if (CHECK(accept_fd == -1, "accept(listen_fd)", if (CHECK(accept_fd == -1, "accept(listen_fd)",
"accept_fd:%d errno:%d\n", "accept_fd:%d errno:%d\n",
accept_fd, errno)) accept_fd, errno))
goto done; 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)) if (init_sk_storage(accept_fd, 0xeB9F))
goto done; goto done;
...@@ -349,14 +288,6 @@ static void test(void) ...@@ -349,14 +288,6 @@ static void test(void)
"err:%d errno:%d\n", err, errno)) "err:%d errno:%d\n", err, errno))
goto done; 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); err = recv(cli_fd, buf, DATA_LEN, 0);
if (CHECK(err != DATA_LEN, "recv(cli_fd)", "err:%d errno:%d\n", if (CHECK(err != DATA_LEN, "recv(cli_fd)", "err:%d errno:%d\n",
err, errno)) err, errno))
...@@ -383,7 +314,6 @@ static void test(void) ...@@ -383,7 +314,6 @@ static void test(void)
close(cli_fd); close(cli_fd);
if (listen_fd != -1) if (listen_fd != -1)
close(listen_fd); close(listen_fd);
close(epfd);
} }
void test_sock_fields(void) void test_sock_fields(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment