[PATCH bpf-next 04/15] selftests/bpf: Add send_to_fd_opts helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Geliang Tang <tanggeliang@xxxxxxxxxx>

Similar to network helpers connect_to_addr() and connect_to_fd_opts(), this
patch adds two helpers send_to_addr() send_to_fd_opts() to use sendto()
to send data. They accepts three more parameters "const void *data",
"size_t datalen" and "int flags" for the sending data. send_to_addr() is a
static function at this moment, only send_to_fd_opts() is exported.

The first user of send_to_fd_opts() is fastopen_connect() in
network_helpers.c.

Signed-off-by: Geliang Tang <tanggeliang@xxxxxxxxxx>
---
 tools/testing/selftests/bpf/network_helpers.c | 65 ++++++++++++-------
 tools/testing/selftests/bpf/network_helpers.h |  2 +
 2 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 2a142d713861..311c3a559414 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -206,41 +206,62 @@ void free_fds(int *fds, unsigned int nr_close_fds)
 	}
 }
 
-int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
-		     int timeout_ms)
+static int send_to_addr(int type, const void *data, size_t datalen, int flags,
+			const struct sockaddr_storage *addr, socklen_t addrlen,
+			const struct network_helper_opts *opts)
 {
-	struct sockaddr_storage addr;
-	socklen_t addrlen = sizeof(addr);
-	struct sockaddr_in *addr_in;
 	int fd, ret;
 
-	if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
-		log_err("Failed to get server addr");
+	fd = client_socket(addr->ss_family, type, opts);
+	if (fd < 0) {
+		log_err("Failed to create client socket");
 		return -1;
 	}
 
-	addr_in = (struct sockaddr_in *)&addr;
-	fd = socket(addr_in->sin_family, SOCK_STREAM, 0);
-	if (fd < 0) {
-		log_err("Failed to create client socket");
+	ret = sendto(fd, data, datalen, flags,
+		     (const struct sockaddr *)addr, addrlen);
+	if (ret != datalen) {
+		log_err("Failed to send to server");
+		save_errno_close(fd);
 		return -1;
 	}
 
-	if (settimeo(fd, timeout_ms))
-		goto error_close;
+	return fd;
+}
 
-	ret = sendto(fd, data, data_len, MSG_FASTOPEN, (struct sockaddr *)&addr,
-		     addrlen);
-	if (ret != data_len) {
-		log_err("sendto(data, %u) != %d\n", data_len, ret);
-		goto error_close;
+int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags,
+		    const struct network_helper_opts *opts)
+{
+	struct sockaddr_storage addr;
+	socklen_t addrlen, optlen;
+	int type;
+
+	if (!opts)
+		opts = &default_opts;
+
+	optlen = sizeof(type);
+	if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
+		log_err("getsockopt(SOL_TYPE)");
+		return -1;
 	}
 
-	return fd;
+	addrlen = sizeof(addr);
+	if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
+		log_err("Failed to get server addr");
+		return -1;
+	}
 
-error_close:
-	save_errno_close(fd);
-	return -1;
+	return send_to_addr(type, data, datalen, flags, &addr, addrlen, opts);
+}
+
+int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
+		     int timeout_ms)
+{
+	struct network_helper_opts opts = {
+		.timeout_ms = timeout_ms,
+	};
+
+	return send_to_fd_opts(server_fd, data, data_len, MSG_FASTOPEN, &opts);
 }
 
 int client_socket(int family, int type,
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index cce56955371f..19210a234bc1 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -74,6 +74,8 @@ int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
 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_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
+int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags,
+		    const struct network_helper_opts *opts);
 int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
 		     int timeout_ms);
 int make_sockaddr(int family, const char *addr_str, __u16 port,
-- 
2.43.0





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux