Commit 01c2f776 authored by Geliang Tang's avatar Geliang Tang Committed by Andrii Nakryiko

selftests/bpf: Drop make_socket in sk_lookup

This patch uses the public network helers client_socket() + make_sockaddr()
in sk_lookup.c to create the client socket, set the timeout sockopts, and
make the connecting address. The local defined function make_socket()
can be dropped then.
Signed-off-by: default avatarGeliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/588771977ac48c27f73526d8421a84b91d7cf218.1721475357.git.tanggeliang@kylinos.cnSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parent af994e31
......@@ -47,8 +47,6 @@
#define INT_IP6 "fd00::2"
#define INT_PORT 8008
#define IO_TIMEOUT_SEC 3
enum server {
SERVER_A = 0,
SERVER_B = 1,
......@@ -114,40 +112,6 @@ static socklen_t inetaddr_len(const struct sockaddr_storage *addr)
addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0);
}
static int make_socket(int sotype, const char *ip, int port,
struct sockaddr_storage *addr)
{
struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC };
int err, family, fd;
family = is_ipv6(ip) ? AF_INET6 : AF_INET;
err = make_sockaddr(family, ip, port, addr, NULL);
if (CHECK(err, "make_address", "failed\n"))
return -1;
fd = socket(addr->ss_family, sotype, 0);
if (CHECK(fd < 0, "socket", "failed\n")) {
log_err("failed to make socket");
return -1;
}
err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) {
log_err("failed to set SNDTIMEO");
close(fd);
return -1;
}
err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) {
log_err("failed to set RCVTIMEO");
close(fd);
return -1;
}
return fd;
}
static int setsockopts(int fd, void *opts)
{
struct cb_opts *co = (struct cb_opts *)opts;
......@@ -842,6 +806,7 @@ static void test_redirect_lookup(struct test_sk_lookup *skel)
static void drop_on_lookup(const struct test *t)
{
int family = is_ipv6(t->connect_to.ip) ? AF_INET6 : AF_INET;
struct sockaddr_storage dst = {};
int client_fd, server_fd, err;
struct bpf_link *lookup_link;
......@@ -856,11 +821,13 @@ static void drop_on_lookup(const struct test *t)
if (server_fd < 0)
goto detach;
client_fd = make_socket(t->sotype, t->connect_to.ip,
t->connect_to.port, &dst);
if (client_fd < 0)
client_fd = client_socket(family, t->sotype, NULL);
if (!ASSERT_OK_FD(client_fd, "client_socket"))
goto close_srv;
err = make_sockaddr(family, t->connect_to.ip, t->connect_to.port, &dst, NULL);
if (!ASSERT_OK(err, "make_sockaddr"))
goto close_all;
err = connect(client_fd, (void *)&dst, inetaddr_len(&dst));
if (t->sotype == SOCK_DGRAM) {
err = send_byte(client_fd);
......@@ -956,6 +923,7 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel)
static void drop_on_reuseport(const struct test *t)
{
int family = is_ipv6(t->connect_to.ip) ? AF_INET6 : AF_INET;
struct sockaddr_storage dst = { 0 };
int client, server1, server2, err;
struct bpf_link *lookup_link;
......@@ -980,11 +948,13 @@ static void drop_on_reuseport(const struct test *t)
if (server2 < 0)
goto close_srv1;
client = make_socket(t->sotype, t->connect_to.ip,
t->connect_to.port, &dst);
if (client < 0)
client = client_socket(family, t->sotype, NULL);
if (!ASSERT_OK_FD(client, "client_socket"))
goto close_srv2;
err = make_sockaddr(family, t->connect_to.ip, t->connect_to.port, &dst, NULL);
if (!ASSERT_OK(err, "make_sockaddr"))
goto close_all;
err = connect(client, (void *)&dst, inetaddr_len(&dst));
if (t->sotype == SOCK_DGRAM) {
err = send_byte(client);
......@@ -1228,10 +1198,13 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t)
if (err)
goto out_close_server;
client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst);
if (client_fd < 0)
client_fd = client_socket(AF_INET, SOCK_STREAM, NULL);
if (!ASSERT_OK_FD(client_fd, "client_socket"))
goto out_close_server;
err = make_sockaddr(AF_INET, EXT_IP4, EXT_PORT, &dst, NULL);
if (!ASSERT_OK(err, "make_sockaddr"))
goto out_close_client;
err = connect(client_fd, (void *)&dst, inetaddr_len(&dst));
if (CHECK(err && !t->expect_errno, "connect",
"unexpected error %d\n", errno))
......
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