Skip to content

Commit

Permalink
Merge branch 'use network helpers, part 8'
Browse files Browse the repository at this point in the history
Geliang Tang says:

====================
v11:
 - new patches 2, 4, 6.
 - drop expect_errno from network_helper_opts as Eduard and Martin
   suggested.
 - drop sockmap_ktls patches from this set.

v10:
 - a new patch 10 is added.
 - patches 1-6, 8-9 unchanged, only commit logs updated.
 - "err = -errno" is used in patches 7, 11, 12 to get the real error
   number before checking value of "err".

v9:
 - new patches 5-7, new struct member expect_errno for network_helper_opts.
 - patches 1-4, 8-9 unchanged.
 - update patches 10-11 to make sure all tests pass.

v8:
 - only patch 8 updated, to fix errors reported by CI.

v7:
 - address Martin's comments in v6. (thanks)
 - use MAX(opts->backlog, 0) instead of opts->backlog.
 - use connect_to_fd_opts instead connect_to_fd.
 - more ASSERT_* to check errors.

v6:
 - update patch 6 as Daniel suggested. (thanks)

v5:
 - keep make_server and make_client as Eduard suggested.

v4:
 - a new patch to use make_sockaddr in sockmap_ktls.
 - a new patch to close fd in error path in drop_on_reuseport.
 - drop make_server() in patch 7.
 - drop make_client() too in patch 9.

v3:
 - a new patch to add backlog for network_helper_opts.
 - use start_server_str in sockmap_ktls now, not start_server.

v2:
 - address Eduard's comments in v1. (thanks)
 - fix errors reported by CI.

This patch set uses network helpers in sk_lookup, and drop the local
helpers inetaddr_len() and make_socket().
====================

Signed-off-by: Martin KaFai Lau <[email protected]>
  • Loading branch information
Martin KaFai Lau committed Jul 10, 2024
2 parents eeb23b5 + 9004054 commit ec5b8c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/network_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl
}

if (type == SOCK_STREAM) {
if (listen(fd, 1) < 0) {
if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) {
log_err("Failed to listed on socket");
goto error_close;
}
Expand Down
10 changes: 10 additions & 0 deletions tools/testing/selftests/bpf/network_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ struct network_helper_opts {
int timeout_ms;
bool must_fail;
int proto;
/* +ve: Passed to listen() as-is.
* 0: Default when the test does not set
* a particular value during the struct init.
* It is changed to 1 before passing to listen().
* Most tests only have one on-going connection.
* -ve: It is changed to 0 before passing to listen().
* It is useful to force syncookie without
* changing the "tcp_syncookies" sysctl from 1 to 2.
*/
int backlog;
int (*post_socket_cb)(int fd, void *opts);
void *cb_opts;
};
Expand Down
82 changes: 40 additions & 42 deletions tools/testing/selftests/bpf/prog_tests/sk_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ struct test {
bool reuseport_has_conns; /* Add a connected socket to reuseport group */
};

struct cb_opts {
int family;
int sotype;
bool reuseport;
};

static __u32 duration; /* for CHECK macro */

static bool is_ipv6(const char *ip)
Expand Down Expand Up @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port,
return fd;
}

static int make_server(int sotype, const char *ip, int port,
struct bpf_program *reuseport_prog)
static int setsockopts(int fd, void *opts)
{
struct sockaddr_storage addr = {0};
struct cb_opts *co = (struct cb_opts *)opts;
const int one = 1;
int err, fd = -1;

fd = make_socket(sotype, ip, port, &addr);
if (fd < 0)
return -1;
int err = 0;

/* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */
if (sotype == SOCK_DGRAM) {
if (co->sotype == SOCK_DGRAM) {
err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one,
sizeof(one));
if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) {
Expand All @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port,
}
}

if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) {
if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) {
err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one,
sizeof(one));
if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) {
Expand All @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port,
}
}

if (sotype == SOCK_STREAM) {
if (co->sotype == SOCK_STREAM) {
err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one));
if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) {
Expand All @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port,
}
}

if (reuseport_prog) {
if (co->reuseport) {
err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one,
sizeof(one));
if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) {
Expand All @@ -190,19 +191,28 @@ static int make_server(int sotype, const char *ip, int port,
}
}

err = bind(fd, (void *)&addr, inetaddr_len(&addr));
if (CHECK(err, "bind", "failed\n")) {
log_err("failed to bind listen socket");
goto fail;
}
fail:
return err;
}

if (sotype == SOCK_STREAM) {
err = listen(fd, SOMAXCONN);
if (CHECK(err, "make_server", "listen")) {
log_err("failed to listen on port %d", port);
goto fail;
}
}
static int make_server(int sotype, const char *ip, int port,
struct bpf_program *reuseport_prog)
{
struct cb_opts cb_opts = {
.family = is_ipv6(ip) ? AF_INET6 : AF_INET,
.sotype = sotype,
.reuseport = reuseport_prog,
};
struct network_helper_opts opts = {
.backlog = SOMAXCONN,
.post_socket_cb = setsockopts,
.cb_opts = &cb_opts,
};
int err, fd;

fd = start_server_str(cb_opts.family, sotype, ip, port, &opts);
if (!ASSERT_OK_FD(fd, "start_server_str"))
return -1;

/* Late attach reuseport prog so we can have one init path */
if (reuseport_prog) {
Expand Down Expand Up @@ -406,18 +416,12 @@ static int udp_recv_send(int server_fd)
}

/* Reply from original destination address. */
fd = socket(dst_addr->ss_family, SOCK_DGRAM, 0);
if (CHECK(fd < 0, "socket", "failed\n")) {
fd = start_server_addr(SOCK_DGRAM, dst_addr, sizeof(*dst_addr), NULL);
if (!ASSERT_OK_FD(fd, "start_server_addr")) {
log_err("failed to create tx socket");
return -1;
}

ret = bind(fd, (struct sockaddr *)dst_addr, sizeof(*dst_addr));
if (CHECK(ret, "bind", "failed\n")) {
log_err("failed to bind tx socket");
goto out;
}

msg.msg_control = NULL;
msg.msg_controllen = 0;
n = sendmsg(fd, &msg, 0);
Expand Down Expand Up @@ -629,22 +633,16 @@ static void run_lookup_prog(const struct test *t)
* BPF socket lookup.
*/
if (t->reuseport_has_conns) {
struct sockaddr_storage addr = {};
socklen_t len = sizeof(addr);

/* Add an extra socket to reuseport group */
reuse_conn_fd = make_server(t->sotype, t->listen_at.ip,
t->listen_at.port,
t->reuseport_prog);
if (reuse_conn_fd < 0)
goto close;

/* Connect the extra socket to itself */
err = getsockname(reuse_conn_fd, (void *)&addr, &len);
if (CHECK(err, "getsockname", "errno %d\n", errno))
goto close;
err = connect(reuse_conn_fd, (void *)&addr, len);
if (CHECK(err, "connect", "errno %d\n", errno))
/* Connect the extra socket to itself */
err = connect_fd_to_fd(reuse_conn_fd, reuse_conn_fd, 0);
if (!ASSERT_OK(err, "connect_fd_to_fd"))
goto close;
}

Expand Down Expand Up @@ -994,7 +992,7 @@ static void drop_on_reuseport(const struct test *t)

err = update_lookup_map(t->sock_map, SERVER_A, server1);
if (err)
goto detach;
goto close_srv1;

/* second server on destination address we should never reach */
server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port,
Expand Down
9 changes: 9 additions & 0 deletions tools/testing/selftests/bpf/test_progs.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ int test__join_cgroup(const char *path);
___ok; \
})

#define ASSERT_OK_FD(fd, name) ({ \
static int duration = 0; \
int ___fd = (fd); \
bool ___ok = ___fd >= 0; \
CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n", \
___fd, errno); \
___ok; \
})

#define SYS(goto_label, fmt, ...) \
({ \
char cmd[1024]; \
Expand Down

0 comments on commit ec5b8c7

Please sign in to comment.