Skip to content

Commit 0c3015d

Browse files
committed
Avoid ephemeral port exhaustion when binding to a specific IP
This is edumazet's patch. The network client was binding to a specific IP address without using `IP_BIND_ADDRESS_NO_PORT`. This forces the kernel to allocate an exclusive ephemeral port for each connection before `connect()`, leading to "Address already in use" errors when many concurrent connections are attempted (around 60k flows). This commit adds a `setsockopt` call with `IP_BIND_ADDRESS_NO_PORT` before the `bind` call. This allows the kernel to defer port selection until `connect()`, enabling the client to establish many more concurrent connections without exhausting the ephemeral port range. Tested: * Verified with strace that `IP_BIND_ADDRESS_NO_PORT` is called before bind.
1 parent c8ed964 commit 0c3015d

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

socket.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ int socket_connect_one(struct thread *t, int flags)
327327
socket_init_not_established(t, s);
328328
if (t->local_hosts) {
329329
int i = (t->flow_first + t->flow_count) % t->num_local_hosts;
330+
int enable = 1;
331+
setsockopt(s, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &enable,
332+
sizeof(enable));
330333
bind_or_die(s, t->local_hosts[i], t->cb);
331334
}
332335
if (t->fn->fn_pre_connect) {

0 commit comments

Comments
 (0)