Commit fbe3e847 authored by Martin KaFai Lau's avatar Martin KaFai Lau
Browse files

Merge branch 'use network helpers, part 5'



Geliang Tang says:

====================
This patchset uses post_socket_cb callbacks of struct network_helper_opts
to refactor do_test() in bpf_tcp_ca.c.

v5:
 - address Martin's comments in v4 (thanks)
 - add patch 4, use start_server_str in test_dctcp_fallback too
 - ASSERT_* is already used in settcpca, use this helper in cc_cb (patch 3).

v4:
 - address Martin's comments in v3 (thanks).
 - drop 2 patches, keep "type" as the individual arg to start_server_addr,
   connect_to_addr and start_server_str.

v3:
 - Add 4 new patches, 1-3 are cleanups. 4 adds a new helper.
 - address Martin's comments in v2.

v2:
 - rebased on commit "selftests/bpf: Add test for the use of new args in
 cong_control"
====================

Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parents eb4e7726 ed61271a
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -94,7 +94,8 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl
	if (settimeo(fd, opts->timeout_ms))
		goto error_close;

	if (opts->post_socket_cb && opts->post_socket_cb(fd, NULL)) {
	if (opts->post_socket_cb &&
	    opts->post_socket_cb(fd, opts->cb_opts)) {
		log_err("Failed to call post_socket_cb");
		goto error_close;
	}
@@ -118,22 +119,32 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl
	return -1;
}

int start_server(int family, int type, const char *addr_str, __u16 port,
		 int timeout_ms)
int start_server_str(int family, int type, const char *addr_str, __u16 port,
		     const struct network_helper_opts *opts)
{
	struct network_helper_opts opts = {
		.timeout_ms	= timeout_ms,
	};
	struct sockaddr_storage addr;
	socklen_t addrlen;

	if (!opts)
		opts = &default_opts;

	if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
		return -1;

	return __start_server(type, (struct sockaddr *)&addr, addrlen, &opts);
	return __start_server(type, (struct sockaddr *)&addr, addrlen, opts);
}

int start_server(int family, int type, const char *addr_str, __u16 port,
		 int timeout_ms)
{
	struct network_helper_opts opts = {
		.timeout_ms	= timeout_ms,
	};

	return start_server_str(family, type, addr_str, port, &opts);
}

static int reuseport_cb(int fd, const struct post_socket_opts *opts)
static int reuseport_cb(int fd, void *opts)
{
	int on = 1;

@@ -338,9 +349,8 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
	if (settimeo(fd, opts->timeout_ms))
		goto error_close;

	if (opts->cc && opts->cc[0] &&
	    setsockopt(fd, SOL_TCP, TCP_CONGESTION, opts->cc,
		       strlen(opts->cc) + 1))
	if (opts->post_socket_cb &&
	    opts->post_socket_cb(fd, opts->cb_opts))
		goto error_close;

	if (!opts->noconnect)
+4 −4
Original line number Diff line number Diff line
@@ -21,16 +21,14 @@ typedef __u16 __sum16;
#define VIP_NUM 5
#define MAGIC_BYTES 123

struct post_socket_opts {};

struct network_helper_opts {
	const char *cc;
	int timeout_ms;
	bool must_fail;
	bool noconnect;
	int type;
	int proto;
	int (*post_socket_cb)(int fd, const struct post_socket_opts *opts);
	int (*post_socket_cb)(int fd, void *opts);
	void *cb_opts;
};

/* ipv4 test vector */
@@ -50,6 +48,8 @@ struct ipv6_packet {
extern struct ipv6_packet pkt_v6;

int settimeo(int fd, int timeout_ms);
int start_server_str(int family, int type, const char *addr_str, __u16 port,
		     const struct network_helper_opts *opts);
int start_server(int family, int type, const char *addr, __u16 port,
		 int timeout_ms);
int *start_reuseport_server(int family, int type, const char *addr_str,
+77 −15
Original line number Diff line number Diff line
@@ -23,6 +23,10 @@
static const unsigned int total_bytes = 10 * 1024 * 1024;
static int expected_stg = 0xeB9F;

struct cb_opts {
	const char *cc;
};

static int settcpca(int fd, const char *tcp_ca)
{
	int err;
@@ -34,12 +38,14 @@ static int settcpca(int fd, const char *tcp_ca)
	return 0;
}

static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
static void do_test(const struct network_helper_opts *opts,
		    const struct bpf_map *sk_stg_map)
{
	struct cb_opts *cb_opts = (struct cb_opts *)opts->cb_opts;
	int lfd = -1, fd = -1;
	int err;

	lfd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
	lfd = start_server_str(AF_INET6, SOCK_STREAM, NULL, 0, opts);
	if (!ASSERT_NEQ(lfd, -1, "socket"))
		return;

@@ -49,7 +55,7 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
		return;
	}

	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca))
	if (settcpca(fd, cb_opts->cc))
		goto done;

	if (sk_stg_map) {
@@ -81,8 +87,22 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
	close(fd);
}

static int cc_cb(int fd, void *opts)
{
	struct cb_opts *cb_opts = (struct cb_opts *)opts;

	return settcpca(fd, cb_opts->cc);
}

static void test_cubic(void)
{
	struct cb_opts cb_opts = {
		.cc = "bpf_cubic",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct bpf_cubic *cubic_skel;
	struct bpf_link *link;

@@ -96,7 +116,7 @@ static void test_cubic(void)
		return;
	}

	do_test("bpf_cubic", NULL);
	do_test(&opts, NULL);

	ASSERT_EQ(cubic_skel->bss->bpf_cubic_acked_called, 1, "pkts_acked called");

@@ -106,6 +126,13 @@ static void test_cubic(void)

static void test_dctcp(void)
{
	struct cb_opts cb_opts = {
		.cc = "bpf_dctcp",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct bpf_dctcp *dctcp_skel;
	struct bpf_link *link;

@@ -119,7 +146,7 @@ static void test_dctcp(void)
		return;
	}

	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
	do_test(&opts, dctcp_skel->maps.sk_stg_map);
	ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");

	bpf_link__destroy(link);
@@ -172,10 +199,16 @@ static void test_dctcp_fallback(void)
{
	int err, lfd = -1, cli_fd = -1, srv_fd = -1;
	struct network_helper_opts opts = {
		.cc = "cubic",
		.post_socket_cb	= cc_cb,
	};
	struct bpf_dctcp *dctcp_skel;
	struct bpf_link *link = NULL;
	struct cb_opts dctcp = {
		.cc = "bpf_dctcp",
	};
	struct cb_opts cubic = {
		.cc = "cubic",
	};
	char srv_cc[16];
	socklen_t cc_len = sizeof(srv_cc);

@@ -190,11 +223,12 @@ static void test_dctcp_fallback(void)
	if (!ASSERT_OK_PTR(link, "dctcp link"))
		goto done;

	lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
	if (!ASSERT_GE(lfd, 0, "lfd") ||
	    !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
	opts.cb_opts = &dctcp;
	lfd = start_server_str(AF_INET6, SOCK_STREAM, "::1", 0, &opts);
	if (!ASSERT_GE(lfd, 0, "lfd"))
		goto done;

	opts.cb_opts = &cubic;
	cli_fd = connect_to_fd_opts(lfd, &opts);
	if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
		goto done;
@@ -297,6 +331,13 @@ static void test_unsupp_cong_op(void)

static void test_update_ca(void)
{
	struct cb_opts cb_opts = {
		.cc = "tcp_ca_update",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct tcp_ca_update *skel;
	struct bpf_link *link;
	int saved_ca1_cnt;
@@ -309,14 +350,14 @@ static void test_update_ca(void)
	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
	ASSERT_OK_PTR(link, "attach_struct_ops");

	do_test("tcp_ca_update", NULL);
	do_test(&opts, NULL);
	saved_ca1_cnt = skel->bss->ca1_cnt;
	ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");

	err = bpf_link__update_map(link, skel->maps.ca_update_2);
	ASSERT_OK(err, "update_map");

	do_test("tcp_ca_update", NULL);
	do_test(&opts, NULL);
	ASSERT_EQ(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");
	ASSERT_GT(skel->bss->ca2_cnt, 0, "ca2_ca2_cnt");

@@ -326,6 +367,13 @@ static void test_update_ca(void)

static void test_update_wrong(void)
{
	struct cb_opts cb_opts = {
		.cc = "tcp_ca_update",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct tcp_ca_update *skel;
	struct bpf_link *link;
	int saved_ca1_cnt;
@@ -338,14 +386,14 @@ static void test_update_wrong(void)
	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
	ASSERT_OK_PTR(link, "attach_struct_ops");

	do_test("tcp_ca_update", NULL);
	do_test(&opts, NULL);
	saved_ca1_cnt = skel->bss->ca1_cnt;
	ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");

	err = bpf_link__update_map(link, skel->maps.ca_wrong);
	ASSERT_ERR(err, "update_map");

	do_test("tcp_ca_update", NULL);
	do_test(&opts, NULL);
	ASSERT_GT(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");

	bpf_link__destroy(link);
@@ -354,6 +402,13 @@ static void test_update_wrong(void)

static void test_mixed_links(void)
{
	struct cb_opts cb_opts = {
		.cc = "tcp_ca_update",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct tcp_ca_update *skel;
	struct bpf_link *link, *link_nl;
	int err;
@@ -368,7 +423,7 @@ static void test_mixed_links(void)
	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
	ASSERT_OK_PTR(link, "attach_struct_ops");

	do_test("tcp_ca_update", NULL);
	do_test(&opts, NULL);
	ASSERT_GT(skel->bss->ca1_cnt, 0, "ca1_ca1_cnt");

	err = bpf_link__update_map(link, skel->maps.ca_no_link);
@@ -455,6 +510,13 @@ static void test_tcp_ca_kfunc(void)

static void test_cc_cubic(void)
{
	struct cb_opts cb_opts = {
		.cc = "bpf_cc_cubic",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct bpf_cc_cubic *cc_cubic_skel;
	struct bpf_link *link;

@@ -468,7 +530,7 @@ static void test_cc_cubic(void)
		return;
	}

	do_test("bpf_cc_cubic", NULL);
	do_test(&opts, NULL);

	bpf_link__destroy(link);
	bpf_cc_cubic__destroy(cc_cubic_skel);
+1 −1
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ static void *server_thread(void *arg)
	return (void *)(long)err;
}

static int custom_cb(int fd, const struct post_socket_opts *opts)
static int custom_cb(int fd, void *opts)
{
	char buf;
	int err;
+2 −2
Original line number Diff line number Diff line
@@ -139,14 +139,14 @@ static int run_test(int server_fd, int results_fd, bool xdp)
	return ret;
}

static int v6only_true(int fd, const struct post_socket_opts *opts)
static int v6only_true(int fd, void *opts)
{
	int mode = true;

	return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &mode, sizeof(mode));
}

static int v6only_false(int fd, const struct post_socket_opts *opts)
static int v6only_false(int fd, void *opts)
{
	int mode = false;