Commit 1ae7a19e authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'use-network-helpers-part-7'

Geliang Tang says:

====================
use network helpers, part 7

From: Geliang Tang <tanggeliang@kylinos.cn>

v6:
 - update ASSERT strings in patch 4 as Eduard suggested. (thanks)

v5:
 - update patch 1, add getsockopt(SO_PROTOCOL) in connect_to_fd() to
fix errors reported by CI.

v4:
 - fix errors reported by CI.

v3:
 - rename start_client to client_socket
 - Use connect_to_addr in connect_to_fd_opt

v2:
 - update patch 2, extract a new helper start_client.
 - drop patch 3, keep must_fail in network_helper_opts.

Drop type and noconnect from network_helper_opts. And use start_server_str
in mptcp and test_tcp_check_syncookie_user.

Patches 1-4 address Martin's comments in the previous series.
====================

Link: https://lore.kernel.org/r/cover.1718932493.git.tanggeliang@kylinos.cn


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents bf977ee4 8cab7cdc
Loading
Loading
Loading
Loading
+48 −52
Original line number Diff line number Diff line
@@ -249,6 +249,34 @@ int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
	return -1;
}

int client_socket(int family, int type,
		  const struct network_helper_opts *opts)
{
	int fd;

	if (!opts)
		opts = &default_opts;

	fd = socket(family, type, opts->proto);
	if (fd < 0) {
		log_err("Failed to create client socket");
		return -1;
	}

	if (settimeo(fd, opts->timeout_ms))
		goto error_close;

	if (opts->post_socket_cb &&
	    opts->post_socket_cb(fd, opts->cb_opts))
		goto error_close;

	return fd;

error_close:
	save_errno_close(fd);
	return -1;
}

static int connect_fd_to_addr(int fd,
			      const struct sockaddr_storage *addr,
			      socklen_t addrlen, const bool must_fail)
@@ -284,15 +312,12 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
	if (!opts)
		opts = &default_opts;

	fd = socket(addr->ss_family, type, opts->proto);
	fd = client_socket(addr->ss_family, type, opts);
	if (fd < 0) {
		log_err("Failed to create client socket");
		return -1;
	}

	if (settimeo(fd, opts->timeout_ms))
		goto error_close;

	if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail))
		goto error_close;

@@ -303,65 +328,21 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
	return -1;
}

int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts)
{
	struct sockaddr_storage addr;
	struct sockaddr_in *addr_in;
	socklen_t addrlen, optlen;
	int fd, type, protocol;
	socklen_t addrlen;

	if (!opts)
		opts = &default_opts;

	optlen = sizeof(type);

	if (opts->type) {
		type = opts->type;
	} else {
		if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
			log_err("getsockopt(SOL_TYPE)");
			return -1;
		}
	}

	if (opts->proto) {
		protocol = opts->proto;
	} else {
		if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
			log_err("getsockopt(SOL_PROTOCOL)");
			return -1;
		}
	}

	addrlen = sizeof(addr);
	if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
		log_err("Failed to get server addr");
		return -1;
	}

	addr_in = (struct sockaddr_in *)&addr;
	fd = socket(addr_in->sin_family, type, protocol);
	if (fd < 0) {
		log_err("Failed to create client socket");
		return -1;
	}

	if (settimeo(fd, opts->timeout_ms))
		goto error_close;

	if (opts->post_socket_cb &&
	    opts->post_socket_cb(fd, opts->cb_opts))
		goto error_close;

	if (!opts->noconnect)
		if (connect_fd_to_addr(fd, &addr, addrlen, opts->must_fail))
			goto error_close;

	return fd;

error_close:
	save_errno_close(fd);
	return -1;
	return connect_to_addr(type, &addr, addrlen, opts);
}

int connect_to_fd(int server_fd, int timeout_ms)
@@ -369,8 +350,23 @@ int connect_to_fd(int server_fd, int timeout_ms)
	struct network_helper_opts opts = {
		.timeout_ms = timeout_ms,
	};
	int type, protocol;
	socklen_t optlen;

	optlen = sizeof(type);
	if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
		log_err("getsockopt(SOL_TYPE)");
		return -1;
	}

	optlen = sizeof(protocol);
	if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
		log_err("getsockopt(SOL_PROTOCOL)");
		return -1;
	}
	opts.proto = protocol;

	return connect_to_fd_opts(server_fd, &opts);
	return connect_to_fd_opts(server_fd, type, &opts);
}

int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
+3 −3
Original line number Diff line number Diff line
@@ -24,8 +24,6 @@ typedef __u16 __sum16;
struct network_helper_opts {
	int timeout_ms;
	bool must_fail;
	bool noconnect;
	int type;
	int proto;
	int (*post_socket_cb)(int fd, void *opts);
	void *cb_opts;
@@ -58,10 +56,12 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
		      const struct network_helper_opts *opts);
void free_fds(int *fds, unsigned int nr_close_fds);
int client_socket(int family, int type,
		  const struct network_helper_opts *opts);
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
		    const struct network_helper_opts *opts);
int connect_to_fd(int server_fd, int timeout_ms);
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts);
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
		     int timeout_ms);
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ static bool start_test(char *addr_str,
		goto err;

	/* connect to server */
	*cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
	*cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts);
	if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
		goto err;

+2 −2
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid)
		goto out;
	}

	fd = connect_to_fd_opts(server_fd, &opts);
	fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
	if (fd < 0)
		err = -1;
	else
@@ -52,7 +52,7 @@ void test_cgroup_v1v2(void)
	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
	if (!ASSERT_GE(server_fd, 0, "server_fd"))
		return;
	client_fd = connect_to_fd_opts(server_fd, &opts);
	client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
	if (!ASSERT_GE(client_fd, 0, "client_fd")) {
		close(server_fd);
		return;
+6 −8
Original line number Diff line number Diff line
@@ -158,15 +158,13 @@ static int send_frags6(int client)

void test_bpf_ip_check_defrag_ok(bool ipv6)
{
	int family = ipv6 ? AF_INET6 : AF_INET;
	struct network_helper_opts rx_opts = {
		.timeout_ms = 1000,
		.noconnect = true,
	};
	struct network_helper_opts tx_ops = {
		.timeout_ms = 1000,
		.type = SOCK_RAW,
		.proto = IPPROTO_RAW,
		.noconnect = true,
	};
	struct sockaddr_storage caddr;
	struct ip_check_defrag *skel;
@@ -192,7 +190,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6)
	nstoken = open_netns(NS1);
	if (!ASSERT_OK_PTR(nstoken, "setns ns1"))
		goto out;
	srv_fd = start_server(ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, NULL, SERVER_PORT, 0);
	srv_fd = start_server(family, SOCK_DGRAM, NULL, SERVER_PORT, 0);
	close_netns(nstoken);
	if (!ASSERT_GE(srv_fd, 0, "start_server"))
		goto out;
@@ -201,18 +199,18 @@ void test_bpf_ip_check_defrag_ok(bool ipv6)
	nstoken = open_netns(NS0);
	if (!ASSERT_OK_PTR(nstoken, "setns ns0"))
		goto out;
	client_tx_fd = connect_to_fd_opts(srv_fd, &tx_ops);
	client_tx_fd = client_socket(family, SOCK_RAW, &tx_ops);
	close_netns(nstoken);
	if (!ASSERT_GE(client_tx_fd, 0, "connect_to_fd_opts"))
	if (!ASSERT_GE(client_tx_fd, 0, "client_socket"))
		goto out;

	/* Open rx socket in ns0 */
	nstoken = open_netns(NS0);
	if (!ASSERT_OK_PTR(nstoken, "setns ns0"))
		goto out;
	client_rx_fd = connect_to_fd_opts(srv_fd, &rx_opts);
	client_rx_fd = client_socket(family, SOCK_DGRAM, &rx_opts);
	close_netns(nstoken);
	if (!ASSERT_GE(client_rx_fd, 0, "connect_to_fd_opts"))
	if (!ASSERT_GE(client_rx_fd, 0, "client_socket"))
		goto out;

	/* Bind rx socket to a premeditated port */
Loading