public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function
@ 2022-09-02  7:14 Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 01/12] test/helpers: Add `t_bind_ephemeral_port()` function Ammar Faizi
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Hi,

This is revision v2 of "Introducing t_bind_ephemeral_port() function".
After discussing an intermittent bind() issue with Dylan, I decided to
introduce a new helper function, t_bind_ephemeral_port().

## Problem:
We have many places where we need to bind() a socket to any unused port
number. To achieve that, the current approach does one of the following
mechanisms:

  1) Randomly brute force the port number until the bind() syscall
     succeeds.

  2) Use a static port at compile time (randomly chosen too).

This is not reliable and it results in an intermittent issue (test
fails when the selected port is in use).

## Solution:
Setting @addr->sin_port to zero on a bind() syscall lets the kernel
choose a port number that is not in use. The caller then can know the
port number to be bound by invoking a getsockname() syscall after
bind() succeeds.

Wrap this procedure in a new function called t_bind_ephemeral_port().
The selected port will be returned into @addr->sin_port, the caller
can use it later to connect() or whatever they need.

## Patchset summary:
There are 12 patches in this series, summary:
1) Patch #1 introduces a new helper function t_bind_ephemeral_port().
2) Patch #2 to #6 get rid of the port number brute force mechanism.
3) Patch #7 to #12 stop using a static port number.

## Changelog

  v1 -> v2:
    - Fix variable placement in patch #2.
    - Append Reviewed-by tags from Alviro.
    - Append Tested-by tags from Alviro.

Link: https://lore.kernel.org/r/[email protected]
Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---

Ammar Faizi (12):
  test/helpers: Add `t_bind_ephemeral_port()` function
  t/poll-link: Don't brute force the port number
  t/socket-rw: Don't brute force the port number
  t/socket-rw-eagain: Don't brute force the port number
  t/socket-rw-offset: Don't brute force the port number
  t/files-exit-hang-poll: Don't brute force the port number
  t/socket: Don't use a static port number
  t/connect: Don't use a static port number
  t/shutdown: Don't use a static port number
  t/recv-msgall: Don't use a static port number
  t/232c93d07b74: Don't use a static port number
  t/recv-msgall-stream: Don't use a static port number

 test/232c93d07b74.c         | 10 ++++------
 test/accept.c               |  5 +----
 test/files-exit-hang-poll.c | 23 +++--------------------
 test/helpers.c              | 18 ++++++++++++++++++
 test/helpers.h              |  7 +++++++
 test/poll-link.c            | 27 +++++++++------------------
 test/recv-msgall-stream.c   | 22 ++++++++++------------
 test/recv-msgall.c          | 10 ++++------
 test/shutdown.c             |  7 +++----
 test/socket-rw-eagain.c     | 14 ++------------
 test/socket-rw-offset.c     | 13 ++-----------
 test/socket-rw.c            | 13 ++-----------
 test/socket.c               | 11 ++++++-----
 13 files changed, 71 insertions(+), 109 deletions(-)


base-commit: b8c37f02662faa4f2b61840b123201ccc5678fb1
-- 
Ammar Faizi


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 01/12] test/helpers: Add `t_bind_ephemeral_port()` function
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 02/12] t/poll-link: Don't brute force the port number Ammar Faizi
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

This is a prep patch to fix an intermittent issue with the port number.

We have many places where we need to bind() a socket to any unused port
number. To achieve that, the current approach does one of the following
mechanisms:

  1) Randomly brute force the port number until the bind() syscall
     succeeds.

  2) Use a static port at compile time (randomly chosen too).

This is not reliable and it results in an intermittent issue (test
fails when the selected port is in use).

Setting @addr->sin_port to zero on a bind() syscall lets the kernel
choose a port number that is not in use. The caller then can know the
port number to be bound by invoking a getsockname() syscall after
bind() succeeds.

Wrap this procedure in a new function called t_bind_ephemeral_port().
The selected port will be returned into @addr->sin_port, the caller
can use it later to connect() or whatever they need.

Link: https://lore.kernel.org/r/[email protected]
Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/helpers.c | 18 ++++++++++++++++++
 test/helpers.h |  7 +++++++
 2 files changed, 25 insertions(+)

diff --git a/test/helpers.c b/test/helpers.c
index 0146533..4d5c402 100644
--- a/test/helpers.c
+++ b/test/helpers.c
@@ -24,14 +24,32 @@ void *t_malloc(size_t size)
 {
 	void *ret;
 	ret = malloc(size);
 	assert(ret);
 	return ret;
 }
 
+/*
+ * Helper for binding socket to an ephemeral port.
+ * The port number to be bound is returned in @addr->sin_port.
+ */
+int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr)
+{
+	socklen_t addrlen;
+
+	addr->sin_port = 0;
+	if (bind(fd, (struct sockaddr *)addr, sizeof(*addr)))
+		return -errno;
+
+	addrlen = sizeof(*addr);
+	assert(!getsockname(fd, (struct sockaddr *)addr, &addrlen));
+	assert(addr->sin_port != 0);
+	return 0;
+}
+
 /*
  * Helper for allocating size bytes aligned on a boundary.
  */
 void t_posix_memalign(void **memptr, size_t alignment, size_t size)
 {
 	int ret;
 	ret = posix_memalign(memptr, alignment, size);
diff --git a/test/helpers.h b/test/helpers.h
index 6d5726c..9ad9947 100644
--- a/test/helpers.h
+++ b/test/helpers.h
@@ -18,14 +18,21 @@ enum t_setup_ret {
 
 enum t_test_result {
 	T_EXIT_PASS   = 0,
 	T_EXIT_FAIL   = 1,
 	T_EXIT_SKIP   = 77,
 };
 
+/*
+ * Helper for binding socket to an ephemeral port.
+ * The port number to be bound is returned in @addr->sin_port.
+ */
+int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr);
+
+
 /*
  * Helper for allocating memory in tests.
  */
 void *t_malloc(size_t size);
 
 
 /*
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 02/12] t/poll-link: Don't brute force the port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 01/12] test/helpers: Add `t_bind_ephemeral_port()` function Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 03/12] t/socket-rw: " Ammar Faizi
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't brute force the port number, use `t_bind_ephemeral_port()`,
much simpler and reliable for choosing a port number that is not
in use.

v2:
  - While in there, fix variable placements (put them at the top).

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/poll-link.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)

diff --git a/test/poll-link.c b/test/poll-link.c
index 197ad77..39b48f5 100644
--- a/test/poll-link.c
+++ b/test/poll-link.c
@@ -9,14 +9,15 @@
 #include <pthread.h>
 #include <sys/socket.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <poll.h>
 #include <arpa/inet.h>
 
+#include "helpers.h"
 #include "liburing.h"
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
 static int recv_thread_ready = 0;
 static int recv_thread_done = 0;
@@ -46,36 +47,37 @@ struct data {
 	unsigned short port;
 	unsigned int addr;
 	int stop;
 };
 
 static void *send_thread(void *arg)
 {
+	struct sockaddr_in addr;
 	struct data *data = arg;
+	int s0;
 
 	wait_for_var(&recv_thread_ready);
 
-	int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+	s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 	assert(s0 != -1);
 
-	struct sockaddr_in addr;
-
 	addr.sin_family = AF_INET;
 	addr.sin_port = data->port;
 	addr.sin_addr.s_addr = data->addr;
 
 	if (connect(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
 		wait_for_var(&recv_thread_done);
 
 	close(s0);
 	return 0;
 }
 
 void *recv_thread(void *arg)
 {
+	struct sockaddr_in addr = { };
 	struct data *data = arg;
 	struct io_uring_sqe *sqe;
 	struct io_uring ring;
 	int i, ret;
 
 	ret = io_uring_queue_init(8, &ring, 0);
 	assert(ret == 0);
@@ -85,35 +87,25 @@ void *recv_thread(void *arg)
 
 	int32_t val = 1;
 	ret = setsockopt(s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 	assert(ret != -1);
 	ret = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	assert(ret != -1);
 
-	struct sockaddr_in addr;
-
 	addr.sin_family = AF_INET;
 	data->addr = inet_addr("127.0.0.1");
 	addr.sin_addr.s_addr = data->addr;
 
-	i = 0;
-	do {
-		data->port = htons(1025 + (rand() % 64510));
-		addr.sin_port = data->port;
-
-		if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
-			break;
-	} while (++i < 100);
-
-	if (i >= 100) {
-		fprintf(stderr, "Can't find good port, skipped\n");
+	if (t_bind_ephemeral_port(s0, &addr)) {
+		perror("bind");
 		data->stop = 1;
 		signal_var(&recv_thread_ready);
-		goto out;
+		goto err;
 	}
+	data->port = addr.sin_port;
 
 	ret = listen(s0, 128);
 	assert(ret != -1);
 
 	signal_var(&recv_thread_ready);
 
 	sqe = io_uring_get_sqe(&ring);
@@ -154,15 +146,14 @@ void *recv_thread(void *arg)
 					(uint64_t) cqe->user_data, cqe->res,
 					data->expected[idx]);
 			goto err;
 		}
 		io_uring_cqe_seen(&ring, cqe);
 	}
 
-out:
 	signal_var(&recv_thread_done);
 	close(s0);
 	io_uring_queue_exit(&ring);
 	return NULL;
 err:
 	signal_var(&recv_thread_done);
 	close(s0);
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 03/12] t/socket-rw: Don't brute force the port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 01/12] test/helpers: Add `t_bind_ephemeral_port()` function Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 02/12] t/poll-link: Don't brute force the port number Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 04/12] t/socket-rw-eagain: " Ammar Faizi
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't brute force the port number, use `t_bind_ephemeral_port()`,
much simpler and reliable for choosing a port number that is not
in use.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/socket-rw.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/test/socket-rw.c b/test/socket-rw.c
index 4fbf032..6211b01 100644
--- a/test/socket-rw.c
+++ b/test/socket-rw.c
@@ -16,14 +16,15 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include "liburing.h"
+#include "helpers.h"
 
 int main(int argc, char *argv[])
 {
 	int p_fd[2], ret;
 	int32_t recv_s0;
 	int32_t val = 1;
 	struct sockaddr_in addr;
@@ -39,25 +40,15 @@ int main(int argc, char *argv[])
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 	assert(ret != -1);
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	assert(ret != -1);
 
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-	do {
-		addr.sin_port = htons((rand() % 61440) + 4096);
-		ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
-		if (!ret)
-			break;
-		if (errno != EADDRINUSE) {
-			perror("bind");
-			exit(1);
-		}
-	} while (1);
+	assert(!t_bind_ephemeral_port(recv_s0, &addr));
 	ret = listen(recv_s0, 128);
 	assert(ret != -1);
 
 
 	p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 
 	val = 1;
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 04/12] t/socket-rw-eagain: Don't brute force the port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (2 preceding siblings ...)
  2022-09-02  7:14 ` [PATCH liburing v2 03/12] t/socket-rw: " Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 05/12] t/socket-rw-offset: " Ammar Faizi
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't brute force the port number, use `t_bind_ephemeral_port()`,
much simpler and reliable for choosing a port number that is not
in use.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/socket-rw-eagain.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/test/socket-rw-eagain.c b/test/socket-rw-eagain.c
index 2d6a817..a12c70d 100644
--- a/test/socket-rw-eagain.c
+++ b/test/socket-rw-eagain.c
@@ -14,14 +14,15 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include "liburing.h"
+#include "helpers.h"
 
 int main(int argc, char *argv[])
 {
 	int p_fd[2], ret;
 	int32_t recv_s0;
 	int32_t val = 1;
 	struct sockaddr_in addr;
@@ -37,26 +38,15 @@ int main(int argc, char *argv[])
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 	assert(ret != -1);
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	assert(ret != -1);
 
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-	do {
-		addr.sin_port = htons((rand() % 61440) + 4096);
-		ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
-		if (!ret)
-			break;
-		if (errno != EADDRINUSE) {
-			perror("bind");
-			exit(1);
-		}
-	} while (1);
-
+	assert(!t_bind_ephemeral_port(recv_s0, &addr));
 	ret = listen(recv_s0, 128);
 	assert(ret != -1);
 
 	p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 
 	val = 1;
 	ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 05/12] t/socket-rw-offset: Don't brute force the port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (3 preceding siblings ...)
  2022-09-02  7:14 ` [PATCH liburing v2 04/12] t/socket-rw-eagain: " Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:14 ` [PATCH liburing v2 06/12] t/files-exit-hang-poll: " Ammar Faizi
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't brute force the port number, use `t_bind_ephemeral_port()`,
much simpler and reliable for choosing a port number that is not
in use.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/socket-rw-offset.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/test/socket-rw-offset.c b/test/socket-rw-offset.c
index 987b6c9..c422442 100644
--- a/test/socket-rw-offset.c
+++ b/test/socket-rw-offset.c
@@ -16,14 +16,15 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include "liburing.h"
+#include "helpers.h"
 
 int main(int argc, char *argv[])
 {
 	int p_fd[2], ret;
 	int32_t recv_s0;
 	int32_t val = 1;
 	struct sockaddr_in addr;
@@ -39,25 +40,15 @@ int main(int argc, char *argv[])
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 	assert(ret != -1);
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	assert(ret != -1);
 
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-	do {
-		addr.sin_port = htons((rand() % 61440) + 4096);
-		ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
-		if (!ret)
-			break;
-		if (errno != EADDRINUSE) {
-			perror("bind");
-			exit(1);
-		}
-	} while (1);
+	assert(!t_bind_ephemeral_port(recv_s0, &addr));
 	ret = listen(recv_s0, 128);
 	assert(ret != -1);
 
 
 	p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 
 	val = 1;
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 06/12] t/files-exit-hang-poll: Don't brute force the port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (4 preceding siblings ...)
  2022-09-02  7:14 ` [PATCH liburing v2 05/12] t/socket-rw-offset: " Ammar Faizi
@ 2022-09-02  7:14 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 07/12] t/socket: Don't use a static " Ammar Faizi
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't brute force the port number, use `t_bind_ephemeral_port()`,
much simpler and reliable for choosing a port number that is not
in use.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/files-exit-hang-poll.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)

diff --git a/test/files-exit-hang-poll.c b/test/files-exit-hang-poll.c
index 0c609f1..04febc8 100644
--- a/test/files-exit-hang-poll.c
+++ b/test/files-exit-hang-poll.c
@@ -15,16 +15,14 @@
 #include <unistd.h>
 #include <poll.h>
 #include "liburing.h"
 #include "helpers.h"
 
 #define BACKLOG 512
 
-#define PORT 9100
-
 static struct io_uring ring;
 
 static void add_poll(struct io_uring *ring, int fd)
 {
 	struct io_uring_sqe *sqe;
 
 	sqe = io_uring_get_sqe(ring);
@@ -60,15 +58,14 @@ static void alarm_sig(int sig)
 
 int main(int argc, char *argv[])
 {
 	struct sockaddr_in serv_addr;
 	struct io_uring_cqe *cqe;
 	int ret, sock_listen_fd;
 	const int val = 1;
-	int i;
 
 	if (argc > 1)
 		return T_EXIT_SKIP;
 
 	sock_listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
 	if (sock_listen_fd < 0) {
 		perror("socket");
@@ -77,28 +74,17 @@ int main(int argc, char *argv[])
 
 	setsockopt(sock_listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 
 	memset(&serv_addr, 0, sizeof(serv_addr));
 	serv_addr.sin_family = AF_INET;
 	serv_addr.sin_addr.s_addr = INADDR_ANY;
 
-	for (i = 0; i < 100; i++) {
-		serv_addr.sin_port = htons(PORT + i);
-
-		ret = bind(sock_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
-		if (!ret)
-			break;
-		if (errno != EADDRINUSE) {
-			fprintf(stderr, "bind: %s\n", strerror(errno));
-			return T_EXIT_FAIL;
-		}
-		if (i == 99) {
-			printf("Gave up on finding a port, skipping\n");
-			goto skip;
-		}
+	if (t_bind_ephemeral_port(sock_listen_fd, &serv_addr)) {
+		perror("bind");
+		return T_EXIT_FAIL;
 	}
 
 	if (listen(sock_listen_fd, BACKLOG) < 0) {
 		perror("Error listening on socket\n");
 		return T_EXIT_FAIL;
 	}
 
@@ -121,11 +107,8 @@ int main(int argc, char *argv[])
 	if (ret) {
 		fprintf(stderr, "wait_cqe=%d\n", ret);
 		return T_EXIT_FAIL;
 	}
 
 	io_uring_queue_exit(&ring);
 	return T_EXIT_PASS;
-skip:
-	io_uring_queue_exit(&ring);
-	return T_EXIT_SKIP;
 }
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 07/12] t/socket: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (5 preceding siblings ...)
  2022-09-02  7:14 ` [PATCH liburing v2 06/12] t/files-exit-hang-poll: " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 08/12] t/connect: " Ammar Faizi
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/socket.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/test/socket.c b/test/socket.c
index 6a3ea09..94c8e9f 100644
--- a/test/socket.c
+++ b/test/socket.c
@@ -7,53 +7,53 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <pthread.h>
+#include <assert.h>
 
 #include "liburing.h"
 #include "helpers.h"
 
 static char str[] = "This is a test of send and recv over io_uring!";
 
 #define MAX_MSG	128
 
-#define PORT	10202
 #define HOST	"127.0.0.1"
 
 static int no_socket;
+static __be32 g_port;
 
 static int recv_prep(struct io_uring *ring, struct iovec *iov, int *sock,
 		     int registerfiles)
 {
 	struct sockaddr_in saddr;
 	struct io_uring_sqe *sqe;
 	int sockfd, ret, val, use_fd;
 
 	memset(&saddr, 0, sizeof(saddr));
 	saddr.sin_family = AF_INET;
 	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
-	saddr.sin_port = htons(PORT);
 
 	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (sockfd < 0) {
 		perror("socket");
 		return 1;
 	}
 
 	val = 1;
 	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 
-	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
-	if (ret < 0) {
+	if (t_bind_ephemeral_port(sockfd, &saddr)) {
 		perror("bind");
 		goto err;
 	}
+	g_port = saddr.sin_port;
 
 	if (registerfiles) {
 		ret = io_uring_register_files(ring, &sockfd, 1);
 		if (ret) {
 			fprintf(stderr, "file reg failed\n");
 			goto err;
 		}
@@ -240,17 +240,18 @@ static int do_send(int socket_direct, int alloc)
 		ret = io_uring_register_files(&ring, &fd, 1);
 		if (ret) {
 			fprintf(stderr, "file register %d\n", ret);
 			return 1;
 		}
 	}
 
+	assert(g_port != 0);
 	memset(&saddr, 0, sizeof(saddr));
 	saddr.sin_family = AF_INET;
-	saddr.sin_port = htons(PORT);
+	saddr.sin_port = g_port;
 	inet_pton(AF_INET, HOST, &saddr.sin_addr);
 
 	sqe = io_uring_get_sqe(&ring);
 	if (socket_direct) {
 		unsigned file_index = 0;
 		if (alloc)
 			file_index = IORING_FILE_INDEX_ALLOC - 1;
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 08/12] t/connect: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (6 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 07/12] t/socket: Don't use a static " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 09/12] t/shutdown: " Ammar Faizi
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/accept.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/test/accept.c b/test/accept.c
index b35ded4..1821faa 100644
--- a/test/accept.c
+++ b/test/accept.c
@@ -190,19 +190,16 @@ static int start_accept_listen(struct sockaddr_in *addr, int port_off,
 
 	struct sockaddr_in laddr;
 
 	if (!addr)
 		addr = &laddr;
 
 	addr->sin_family = AF_INET;
-	addr->sin_port = htons(0x1235 + port_off);
 	addr->sin_addr.s_addr = inet_addr("127.0.0.1");
-
-	ret = bind(fd, (struct sockaddr*)addr, sizeof(*addr));
-	assert(ret != -1);
+	assert(!t_bind_ephemeral_port(fd, addr));
 	ret = listen(fd, 128);
 	assert(ret != -1);
 
 	return fd;
 }
 
 static int set_client_fd(struct sockaddr_in *addr)
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 09/12] t/shutdown: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (7 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 08/12] t/connect: " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 10/12] t/recv-msgall: " Ammar Faizi
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/shutdown.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/test/shutdown.c b/test/shutdown.c
index 14c7407..064ee36 100644
--- a/test/shutdown.c
+++ b/test/shutdown.c
@@ -15,44 +15,43 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include "liburing.h"
+#include "helpers.h"
 
 static void sig_pipe(int sig)
 {
 }
 
 int main(int argc, char *argv[])
 {
 	int p_fd[2], ret;
 	int32_t recv_s0;
 	int32_t val = 1;
-	struct sockaddr_in addr;
+	struct sockaddr_in addr = { };
 
 	if (argc > 1)
 		return 0;
 
 	srand(getpid());
 
 	recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 	assert(ret != -1);
 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	assert(ret != -1);
 
 	addr.sin_family = AF_INET;
-	addr.sin_port = htons((rand() % 61440) + 4096);
 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
-	ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
-	assert(ret != -1);
+	assert(!t_bind_ephemeral_port(recv_s0, &addr));
 	ret = listen(recv_s0, 128);
 	assert(ret != -1);
 
 	p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 
 	val = 1;
 	ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 10/12] t/recv-msgall: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (8 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 09/12] t/shutdown: " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 11/12] t/232c93d07b74: " Ammar Faizi
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/recv-msgall.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/test/recv-msgall.c b/test/recv-msgall.c
index a6f7cfc..ae123e4 100644
--- a/test/recv-msgall.c
+++ b/test/recv-msgall.c
@@ -12,45 +12,43 @@
 #include <sys/socket.h>
 #include <pthread.h>
 
 #include "liburing.h"
 #include "helpers.h"
 
 #define MAX_MSG	128
-
-#define PORT	10201
 #define HOST	"127.0.0.1"
+static __be16 bind_port;
 
 static int recv_prep(struct io_uring *ring, struct iovec *iov, int *sock,
 		     int use_recvmsg)
 {
 	struct sockaddr_in saddr;
 	struct io_uring_sqe *sqe;
 	int sockfd, ret, val;
 	struct msghdr msg = { };
 
 	memset(&saddr, 0, sizeof(saddr));
 	saddr.sin_family = AF_INET;
 	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
-	saddr.sin_port = htons(PORT);
 
 	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (sockfd < 0) {
 		perror("socket");
 		return 1;
 	}
 
 	val = 1;
 	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 
-	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
-	if (ret < 0) {
+	if (t_bind_ephemeral_port(sockfd, &saddr)) {
 		perror("bind");
 		goto err;
 	}
+	bind_port = saddr.sin_port;
 
 	sqe = io_uring_get_sqe(ring);
 	if (!use_recvmsg) {
 		io_uring_prep_recv(sqe, sockfd, iov->iov_base, iov->iov_len,
 					MSG_WAITALL);
 	} else {
 		msg.msg_namelen = sizeof(struct sockaddr_in);
@@ -161,15 +159,15 @@ static int do_send(void)
 
 	buf = malloc(MAX_MSG * sizeof(int));
 	for (i = 0; i < MAX_MSG; i++)
 		buf[i] = i;
 
 	memset(&saddr, 0, sizeof(saddr));
 	saddr.sin_family = AF_INET;
-	saddr.sin_port = htons(PORT);
+	saddr.sin_port = bind_port;
 	inet_pton(AF_INET, HOST, &saddr.sin_addr);
 
 	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (sockfd < 0) {
 		perror("socket");
 		return 1;
 	}
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 11/12] t/232c93d07b74: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (9 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 10/12] t/recv-msgall: " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02  7:15 ` [PATCH liburing v2 12/12] t/recv-msgall-stream: " Ammar Faizi
  2022-09-02 11:57 ` [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Jens Axboe
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/232c93d07b74.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/test/232c93d07b74.c b/test/232c93d07b74.c
index c99491f..74cc063 100644
--- a/test/232c93d07b74.c
+++ b/test/232c93d07b74.c
@@ -23,19 +23,18 @@
 
 #include "helpers.h"
 #include "liburing.h"
 
 #define RECV_BUFF_SIZE 2
 #define SEND_BUFF_SIZE 3
 
-#define PORT	0x1234
-
 struct params {
 	int tcp;
 	int non_blocking;
+	__be16 bind_port;
 };
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 int rcv_ready = 0;
 
 static void set_rcv_ready(void)
@@ -73,18 +72,17 @@ static void *rcv(void *arg)
 		assert(res != -1);
 		res = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 		assert(res != -1);
 
 		struct sockaddr_in addr;
 
 		addr.sin_family = AF_INET;
-		addr.sin_port = htons(PORT);
 		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
-		res = bind(s0, (struct sockaddr *) &addr, sizeof(addr));
-		assert(res != -1);
+		assert(t_bind_ephemeral_port(s0, &addr) == 0);
+		p->bind_port = addr.sin_port;
 	} else {
 		s0 = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
 		assert(s0 != -1);
 
 		struct sockaddr_un addr;
 		memset(&addr, 0, sizeof(addr));
 
@@ -188,15 +186,15 @@ static void *snd(void *arg)
 		s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 		ret = setsockopt(s0, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
 		assert(ret != -1);
 
 		struct sockaddr_in addr;
 
 		addr.sin_family = AF_INET;
-		addr.sin_port = htons(PORT);
+		addr.sin_port = p->bind_port;
 		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 		ret = connect(s0, (struct sockaddr*) &addr, sizeof(addr));
 		assert(ret != -1);
 	} else {
 		s0 = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
 		assert(s0 != -1);
 
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH liburing v2 12/12] t/recv-msgall-stream: Don't use a static port number
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (10 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 11/12] t/232c93d07b74: " Ammar Faizi
@ 2022-09-02  7:15 ` Ammar Faizi
  2022-09-02 11:57 ` [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Jens Axboe
  12 siblings, 0 replies; 14+ messages in thread
From: Ammar Faizi @ 2022-09-02  7:15 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Ammar Faizi, Dylan Yudaken, Facebook Kernel Team, Pavel Begunkov,
	io-uring Mailing List, GNU/Weeb Mailing List, Kanna Scarlet,
	Muhammad Rizki, Alviro Iskandar Setiawan

From: Ammar Faizi <[email protected]>

Don't use a static port number. It might already be in use, resulting
in a test failure. Use an ephemeral port to make this test reliable.

Cc: Dylan Yudaken <[email protected]>
Cc: Facebook Kernel Team <[email protected]>
Cc: Pavel Begunkov <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Tested-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/recv-msgall-stream.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/test/recv-msgall-stream.c b/test/recv-msgall-stream.c
index a188cc1..65b4d22 100644
--- a/test/recv-msgall-stream.c
+++ b/test/recv-msgall-stream.c
@@ -1,12 +1,13 @@
 /* SPDX-License-Identifier: MIT */
 /*
  * Test MSG_WAITALL for recv/recvmsg and include normal sync versions just
  * for comparison.
  */
+#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <arpa/inet.h>
@@ -15,48 +16,45 @@
 #include <pthread.h>
 
 #include "liburing.h"
 #include "helpers.h"
 
 #define MAX_MSG	128
 
-static int port = 31200;
-
 struct recv_data {
 	pthread_mutex_t mutex;
 	int use_recvmsg;
 	int use_sync;
-	int port;
+	__be16 port;
 };
 
 static int get_conn_sock(struct recv_data *rd, int *sockout)
 {
 	struct sockaddr_in saddr;
 	int sockfd, ret, val;
 
 	memset(&saddr, 0, sizeof(saddr));
 	saddr.sin_family = AF_INET;
 	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
-	saddr.sin_port = htons(rd->port);
 
 	sockfd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 	if (sockfd < 0) {
 		perror("socket");
 		goto err;
 	}
 
 	val = 1;
 	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
 
-	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
-	if (ret < 0) {
+	if (t_bind_ephemeral_port(sockfd, &saddr)) {
 		perror("bind");
 		goto err;
 	}
+	rd->port = saddr.sin_port;
 
 	ret = listen(sockfd, 16);
 	if (ret < 0) {
 		perror("listen");
 		goto err;
 	}
 
@@ -275,26 +273,26 @@ static int do_send(struct recv_data *rd)
 		return 1;
 	}
 
 	buf = malloc(MAX_MSG * sizeof(int));
 	for (i = 0; i < MAX_MSG; i++)
 		buf[i] = i;
 
-	memset(&saddr, 0, sizeof(saddr));
-	saddr.sin_family = AF_INET;
-	saddr.sin_port = htons(rd->port);
-	inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr);
-
 	sockfd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
 	if (sockfd < 0) {
 		perror("socket");
 		return 1;
 	}
 
 	pthread_mutex_lock(&rd->mutex);
+	assert(rd->port != 0);
+	memset(&saddr, 0, sizeof(saddr));
+	saddr.sin_family = AF_INET;
+	saddr.sin_port = rd->port;
+	inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr);
 
 	ret = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
 	if (ret < 0) {
 		perror("connect");
 		return 1;
 	}
 
@@ -347,15 +345,15 @@ static int test(int use_recvmsg, int use_sync)
 
 	pthread_mutexattr_init(&attr);
 	pthread_mutexattr_setpshared(&attr, 1);
 	pthread_mutex_init(&rd.mutex, &attr);
 	pthread_mutex_lock(&rd.mutex);
 	rd.use_recvmsg = use_recvmsg;
 	rd.use_sync = use_sync;
-	rd.port = port++;
+	rd.port = 0;
 
 	ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
 	if (ret) {
 		fprintf(stderr, "Thread create failed: %d\n", ret);
 		pthread_mutex_unlock(&rd.mutex);
 		return 1;
 	}
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function
  2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
                   ` (11 preceding siblings ...)
  2022-09-02  7:15 ` [PATCH liburing v2 12/12] t/recv-msgall-stream: " Ammar Faizi
@ 2022-09-02 11:57 ` Jens Axboe
  12 siblings, 0 replies; 14+ messages in thread
From: Jens Axboe @ 2022-09-02 11:57 UTC (permalink / raw)
  To: Ammar Faizi
  Cc: Muhammad Rizki, Kanna Scarlet, Dylan Yudaken,
	Alviro Iskandar Setiawan, GNU/Weeb Mailing List,
	io-uring Mailing List, Pavel Begunkov, Facebook Kernel Team

On Fri, 2 Sep 2022 14:14:53 +0700, Ammar Faizi wrote:
> From: Ammar Faizi <[email protected]>
> 
> Hi,
> 
> This is revision v2 of "Introducing t_bind_ephemeral_port() function".
> After discussing an intermittent bind() issue with Dylan, I decided to
> introduce a new helper function, t_bind_ephemeral_port().
> 
> [...]

Applied, thanks!

[01/12] test/helpers: Add `t_bind_ephemeral_port()` function
        commit: 0a2d0af5c1d7daa77bacc203d28a7a79662a928e
[02/12] t/poll-link: Don't brute force the port number
        commit: 6341145b6fe368d123ced2ba9857b3105673f644
[03/12] t/socket-rw: Don't brute force the port number
        commit: e0f0f04de43447c69f442532f5c4d72e9a833481
[04/12] t/socket-rw-eagain: Don't brute force the port number
        commit: 156ef697a9af0efd2e2e0d210de43e45e620fc76
[05/12] t/socket-rw-offset: Don't brute force the port number
        commit: ac7f81db44fc658bb7b99cceb1bee738890a317e
[06/12] t/files-exit-hang-poll: Don't brute force the port number
        commit: ce419aa629af36c0e0b19c155ef385e58bd8f9c8
[07/12] t/socket: Don't use a static port number
        commit: 875630a00f64ede24bf3cd7b3df4a37c45de65e0
[08/12] t/connect: Don't use a static port number
        commit: 7ce01375835e57a47c46be9646f773be1a8d8c3c
[09/12] t/shutdown: Don't use a static port number
        commit: 4736c9392bb910ae5a7d965f1019070864f831d3
[10/12] t/recv-msgall: Don't use a static port number
        commit: 2f8d4bb4259c6e3b6c519c03385c86f14e619ed9
[11/12] t/232c93d07b74: Don't use a static port number
        commit: fab19e78737fd65a411a446b848d427af6706c0e
[12/12] t/recv-msgall-stream: Don't use a static port number
        commit: c93f347ccaeb68c1aa4b377658555f0c7c2576b2

Best regards,
-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-09-02 11:57 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-02  7:14 [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 01/12] test/helpers: Add `t_bind_ephemeral_port()` function Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 02/12] t/poll-link: Don't brute force the port number Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 03/12] t/socket-rw: " Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 04/12] t/socket-rw-eagain: " Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 05/12] t/socket-rw-offset: " Ammar Faizi
2022-09-02  7:14 ` [PATCH liburing v2 06/12] t/files-exit-hang-poll: " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 07/12] t/socket: Don't use a static " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 08/12] t/connect: " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 09/12] t/shutdown: " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 10/12] t/recv-msgall: " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 11/12] t/232c93d07b74: " Ammar Faizi
2022-09-02  7:15 ` [PATCH liburing v2 12/12] t/recv-msgall-stream: " Ammar Faizi
2022-09-02 11:57 ` [PATCH liburing v2 00/12] Introducing t_bind_ephemeral_port() function Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox