Message ID | 20240329191907.1808635-5-jrife@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | selftests/bpf: Add sockaddr tests for kernel networking | expand |
On 3/29/24 12:18 PM, Jordan Rife wrote: > +int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr) > +{ > + struct timeval tv; > + struct msghdr hdr; > + struct iovec iov; > + char data[64]; > + fd_set rfds; > + > + FD_ZERO(&rfds); > + FD_SET(sockfd, &rfds); > + > + tv.tv_sec = 2; > + tv.tv_usec = 0; > + > + if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 || The socket fd created by the helpers in network_helpers.c should be a blocking socket and has a 3s default delay. select should not be needed. Try to stay with the default. If the default turned out to be too flaky in the bpf CI in the future, one default change is easier. > + !FD_ISSET(sockfd, &rfds)) > + return -1; > + > + memset(&iov, 0, sizeof(iov)); > + iov.iov_base = data; > + iov.iov_len = sizeof(data); > + > + memset(&hdr, 0, sizeof(hdr)); > + hdr.msg_name = src_addr; > + hdr.msg_namelen = sizeof(struct sockaddr_storage); > + hdr.msg_iov = &iov; > + hdr.msg_iovlen = 1; > + > + return recvmsg(sockfd, &hdr, 0); > +}
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 8eae4afe3ec3d..ef0a94d9e9d5c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -498,6 +498,37 @@ int get_socket_local_port(int sock_fd) return -1; } +int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr) +{ + struct timeval tv; + struct msghdr hdr; + struct iovec iov; + char data[64]; + fd_set rfds; + + FD_ZERO(&rfds); + FD_SET(sockfd, &rfds); + + tv.tv_sec = 2; + tv.tv_usec = 0; + + if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 || + !FD_ISSET(sockfd, &rfds)) + return -1; + + memset(&iov, 0, sizeof(iov)); + iov.iov_base = data; + iov.iov_len = sizeof(data); + + memset(&hdr, 0, sizeof(hdr)); + hdr.msg_name = src_addr; + hdr.msg_namelen = sizeof(struct sockaddr_storage); + hdr.msg_iov = &iov; + hdr.msg_iovlen = 1; + + return recvmsg(sockfd, &hdr, 0); +} + int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, const struct sockaddr_storage *addr2, socklen_t addr2_len, bool cmp_port) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index a4a458c858d59..f88f2470e3fcb 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -61,6 +61,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); int get_socket_local_port(int sock_fd); +int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr); int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, const struct sockaddr_storage *addr2, socklen_t addr2_len, bool cmp_port);
Copy recv_msg_from_client helper from test_sock_addr self test in order to use it in the sock_addr_kern test program. Signed-off-by: Jordan Rife <jrife@google.com> --- tools/testing/selftests/bpf/network_helpers.c | 31 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 32 insertions(+)