summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/network_helpers.c
diff options
context:
space:
mode:
authorMartin KaFai Lau <kafai@fb.com>2020-08-20 22:01:11 +0300
committerAlexei Starovoitov <ast@kernel.org>2020-08-25 00:35:00 +0300
commit8085e1dc1f3c885e1e9c1ef8031b3eabc1cccf25 (patch)
tree4e1e7aa42c32b5c4afcd2a54770f77cfff2329c3 /tools/testing/selftests/bpf/network_helpers.c
parent0813a841566f0962a5551be7749b43c45f0022a0 (diff)
downloadlinux-8085e1dc1f3c885e1e9c1ef8031b3eabc1cccf25.tar.xz
bpf: selftests: Add fastopen_connect to network_helpers
This patch adds a fastopen_connect() helper which will be used in a later test. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200820190111.2886196-1-kafai@fb.com
Diffstat (limited to 'tools/testing/selftests/bpf/network_helpers.c')
-rw-r--r--tools/testing/selftests/bpf/network_helpers.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index f56655690f9b..12ee40284da0 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -104,6 +104,43 @@ error_close:
return -1;
}
+int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
+ int timeout_ms)
+{
+ 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");
+ 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");
+ return -1;
+ }
+
+ if (settimeo(fd, timeout_ms))
+ goto error_close;
+
+ 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;
+ }
+
+ 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)