From: Pavel Begunkov <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: [PATCH liburing v2 3/4] tests/helpers: add t_create_socketpair_ip
Date: Fri, 7 Mar 2025 18:28:55 +0000 [thread overview]
Message-ID: <f298148414a34e3a02c551509be2b308268e3e25.1741372065.git.asml.silence@gmail.com> (raw)
In-Reply-To: <[email protected]>
create_socketpair_ip() is useful for zerocopy tx testing as it needs TCP
sockets and not just AF_UNIX. There will be tx zc tests in more files,
so move the function to helpers.
Signed-off-by: Pavel Begunkov <[email protected]>
---
test/helpers.c | 111 ++++++++++++++++++++++++++++++++++++++
test/helpers.h | 5 ++
test/send-zerocopy.c | 123 ++++---------------------------------------
3 files changed, 127 insertions(+), 112 deletions(-)
diff --git a/test/helpers.c b/test/helpers.c
index 07186911..b2d386f4 100644
--- a/test/helpers.c
+++ b/test/helpers.c
@@ -373,3 +373,114 @@ void *aligned_alloc(size_t alignment, size_t size)
return ret;
}
+
+int t_create_socketpair_ip(struct sockaddr_storage *addr,
+ int *sock_client, int *sock_server,
+ bool ipv6, bool client_connect,
+ bool msg_zc, bool tcp, const char *name)
+{
+ socklen_t addr_size;
+ int family, sock, listen_sock = -1;
+ int ret;
+
+ memset(addr, 0, sizeof(*addr));
+ if (ipv6) {
+ struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)addr;
+
+ family = AF_INET6;
+ saddr->sin6_family = family;
+ saddr->sin6_port = htons(0);
+ addr_size = sizeof(*saddr);
+ } else {
+ struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
+
+ family = AF_INET;
+ saddr->sin_family = family;
+ saddr->sin_port = htons(0);
+ saddr->sin_addr.s_addr = htonl(INADDR_ANY);
+ addr_size = sizeof(*saddr);
+ }
+
+ /* server sock setup */
+ if (tcp) {
+ sock = listen_sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
+ } else {
+ sock = *sock_server = socket(family, SOCK_DGRAM, 0);
+ }
+ if (sock < 0) {
+ perror("socket");
+ return 1;
+ }
+
+ ret = bind(sock, (struct sockaddr *)addr, addr_size);
+ if (ret < 0) {
+ perror("bind");
+ return 1;
+ }
+
+ ret = getsockname(sock, (struct sockaddr *)addr, &addr_size);
+ if (ret < 0) {
+ fprintf(stderr, "getsockname failed %i\n", errno);
+ return 1;
+ }
+
+ if (tcp) {
+ ret = listen(sock, 128);
+ assert(ret != -1);
+ }
+
+ if (ipv6) {
+ struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)addr;
+
+ inet_pton(AF_INET6, name, &(saddr->sin6_addr));
+ } else {
+ struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
+
+ inet_pton(AF_INET, name, &saddr->sin_addr);
+ }
+
+ /* client sock setup */
+ if (tcp) {
+ *sock_client = socket(family, SOCK_STREAM, IPPROTO_TCP);
+ assert(client_connect);
+ } else {
+ *sock_client = socket(family, SOCK_DGRAM, 0);
+ }
+ if (*sock_client < 0) {
+ perror("socket");
+ return 1;
+ }
+ if (client_connect) {
+ ret = connect(*sock_client, (struct sockaddr *)addr, addr_size);
+ if (ret < 0) {
+ perror("connect");
+ return 1;
+ }
+ }
+ if (msg_zc) {
+#ifdef SO_ZEROCOPY
+ int val = 1;
+
+ /*
+ * NOTE: apps must not set SO_ZEROCOPY when using io_uring zc.
+ * It's only here to test interactions with MSG_ZEROCOPY.
+ */
+ if (setsockopt(*sock_client, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
+ perror("setsockopt zc");
+ return 1;
+ }
+#else
+ fprintf(stderr, "no SO_ZEROCOPY\n");
+ return 1;
+#endif
+ }
+ if (tcp) {
+ *sock_server = accept(listen_sock, NULL, NULL);
+ if (!*sock_server) {
+ fprintf(stderr, "can't accept\n");
+ return 1;
+ }
+ close(listen_sock);
+ }
+ return 0;
+}
diff --git a/test/helpers.h b/test/helpers.h
index d0294eba..f8a5c7f2 100644
--- a/test/helpers.h
+++ b/test/helpers.h
@@ -81,6 +81,11 @@ struct iovec *t_create_buffers(size_t buf_num, size_t buf_size);
*/
int t_create_socket_pair(int fd[2], bool stream);
+int t_create_socketpair_ip(struct sockaddr_storage *addr,
+ int *sock_client, int *sock_server,
+ bool ipv6, bool client_connect,
+ bool msg_zc, bool tcp, const char *name);
+
/*
* Helper for setting up a ring and checking for user privs
*/
diff --git a/test/send-zerocopy.c b/test/send-zerocopy.c
index 680481a0..b505e4d0 100644
--- a/test/send-zerocopy.c
+++ b/test/send-zerocopy.c
@@ -258,117 +258,6 @@ static int test_send_faults(int sock_tx, int sock_rx)
return T_EXIT_PASS;
}
-static int create_socketpair_ip(struct sockaddr_storage *addr,
- int *sock_client, int *sock_server,
- bool ipv6, bool client_connect,
- bool msg_zc, bool tcp)
-{
- socklen_t addr_size;
- int family, sock, listen_sock = -1;
- int ret;
-
- memset(addr, 0, sizeof(*addr));
- if (ipv6) {
- struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)addr;
-
- family = AF_INET6;
- saddr->sin6_family = family;
- saddr->sin6_port = htons(0);
- addr_size = sizeof(*saddr);
- } else {
- struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
-
- family = AF_INET;
- saddr->sin_family = family;
- saddr->sin_port = htons(0);
- saddr->sin_addr.s_addr = htonl(INADDR_ANY);
- addr_size = sizeof(*saddr);
- }
-
- /* server sock setup */
- if (tcp) {
- sock = listen_sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
- } else {
- sock = *sock_server = socket(family, SOCK_DGRAM, 0);
- }
- if (sock < 0) {
- perror("socket");
- return 1;
- }
-
- ret = bind(sock, (struct sockaddr *)addr, addr_size);
- if (ret < 0) {
- perror("bind");
- return 1;
- }
-
- ret = getsockname(sock, (struct sockaddr *)addr, &addr_size);
- if (ret < 0) {
- fprintf(stderr, "getsockname failed %i\n", errno);
- return 1;
- }
-
- if (tcp) {
- ret = listen(sock, 128);
- assert(ret != -1);
- }
-
- if (ipv6) {
- struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)addr;
-
- inet_pton(AF_INET6, HOSTV6, &(saddr->sin6_addr));
- } else {
- struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
-
- inet_pton(AF_INET, HOST, &saddr->sin_addr);
- }
-
- /* client sock setup */
- if (tcp) {
- *sock_client = socket(family, SOCK_STREAM, IPPROTO_TCP);
- assert(client_connect);
- } else {
- *sock_client = socket(family, SOCK_DGRAM, 0);
- }
- if (*sock_client < 0) {
- perror("socket");
- return 1;
- }
- if (client_connect) {
- ret = connect(*sock_client, (struct sockaddr *)addr, addr_size);
- if (ret < 0) {
- perror("connect");
- return 1;
- }
- }
- if (msg_zc) {
-#ifdef SO_ZEROCOPY
- int val = 1;
-
- /*
- * NOTE: apps must not set SO_ZEROCOPY when using io_uring zc.
- * It's only here to test interactions with MSG_ZEROCOPY.
- */
- if (setsockopt(*sock_client, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
- perror("setsockopt zc");
- return 1;
- }
-#else
- fprintf(stderr, "no SO_ZEROCOPY\n");
- return 1;
-#endif
- }
- if (tcp) {
- *sock_server = accept(listen_sock, NULL, NULL);
- if (!*sock_server) {
- fprintf(stderr, "can't accept\n");
- return 1;
- }
- close(listen_sock);
- }
- return 0;
-}
-
struct send_conf {
bool fixed_buf;
bool mix_register;
@@ -574,6 +463,16 @@ static int do_test_inet_send(struct io_uring *ring, int sock_client, int sock_se
return 0;
}
+static int create_socketpair_ip(struct sockaddr_storage *addr,
+ int *sock_client, int *sock_server,
+ bool ipv6, bool client_connect,
+ bool msg_zc, bool tcp)
+{
+ return t_create_socketpair_ip(addr, sock_client, sock_server, ipv6,
+ client_connect, msg_zc, tcp,
+ ipv6 ? HOSTV6 : HOST);
+}
+
static int test_inet_send(struct io_uring *ring)
{
struct send_conf conf;
@@ -598,7 +497,7 @@ static int test_inet_send(struct io_uring *ring)
continue;
#endif
ret = create_socketpair_ip(&addr, &sock_client, &sock_server, ipv6,
- client_connect, msg_zc_set, tcp);
+ client_connect, msg_zc_set, tcp);
if (ret) {
fprintf(stderr, "sock prep failed %d\n", ret);
return 1;
--
2.48.1
next prev parent reply other threads:[~2025-03-07 18:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-07 18:28 [PATCH liburing v2 0/4] vectored registered buffer support and tests Pavel Begunkov
2025-03-07 18:28 ` [PATCH liburing v2 1/4] Add vectored registered buffer req init helpers Pavel Begunkov
2025-03-07 18:28 ` [PATCH liburing v2 2/4] test/sendzc: test registered vectored buffers Pavel Begunkov
2025-03-07 18:28 ` Pavel Begunkov [this message]
2025-03-07 18:28 ` [PATCH liburing v2 4/4] tests: targeted registered vector tests Pavel Begunkov
2025-03-07 19:40 ` [PATCH liburing v2 0/4] vectored registered buffer support and tests Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f298148414a34e3a02c551509be2b308268e3e25.1741372065.git.asml.silence@gmail.com \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox