Skip to content

Commit aacd928

Browse files
ketotekdavem330
authored andcommitted
tcp: bind() use stronger condition for bind_conflict
We must try harder to get unique (addr, port) pairs when doing port autoselection for sockets with SO_REUSEADDR option set. We achieve this by adding a relaxation parameter to inet_csk_bind_conflict. When 'relax' parameter is off we return a conflict whenever the current searched pair (addr, port) is not unique. This tries to address the problems reported in patch: 8d238b2 Revert "tcp: bind() fix when many ports are bound" Tests where ran for creating and binding(0) many sockets on 100 IPs. The results are, on average: * 60000 sockets, 600 ports / IP: * 0.210 s, 620 (IP, port) duplicates without patch * 0.219 s, no duplicates with patch * 100000 sockets, 1000 ports / IP: * 0.371 s, 1720 duplicates without patch * 0.373 s, no duplicates with patch * 200000 sockets, 2000 ports / IP: * 0.766 s, 6900 duplicates without patch * 0.768 s, no duplicates with patch * 500000 sockets, 5000 ports / IP: * 2.227 s, 41500 duplicates without patch * 2.284 s, no duplicates with patch Signed-off-by: Alex Copot <[email protected]> Signed-off-by: Daniel Baluta <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c72e118 commit aacd928

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

include/net/inet6_connection_sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct sock;
2323
struct sockaddr;
2424

2525
extern int inet6_csk_bind_conflict(const struct sock *sk,
26-
const struct inet_bind_bucket *tb);
26+
const struct inet_bind_bucket *tb, bool relax);
2727

2828
extern struct dst_entry* inet6_csk_route_req(struct sock *sk,
2929
const struct request_sock *req);

include/net/inet_connection_sock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct inet_connection_sock_af_ops {
6060
#endif
6161
void (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
6262
int (*bind_conflict)(const struct sock *sk,
63-
const struct inet_bind_bucket *tb);
63+
const struct inet_bind_bucket *tb, bool relax);
6464
};
6565

6666
/** inet_connection_sock - INET connection oriented sock
@@ -245,7 +245,7 @@ extern struct request_sock *inet_csk_search_req(const struct sock *sk,
245245
const __be32 raddr,
246246
const __be32 laddr);
247247
extern int inet_csk_bind_conflict(const struct sock *sk,
248-
const struct inet_bind_bucket *tb);
248+
const struct inet_bind_bucket *tb, bool relax);
249249
extern int inet_csk_get_port(struct sock *sk, unsigned short snum);
250250

251251
extern struct dst_entry* inet_csk_route_req(struct sock *sk,

net/ipv4/inet_connection_sock.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void inet_get_local_port_range(int *low, int *high)
5353
EXPORT_SYMBOL(inet_get_local_port_range);
5454

5555
int inet_csk_bind_conflict(const struct sock *sk,
56-
const struct inet_bind_bucket *tb)
56+
const struct inet_bind_bucket *tb, bool relax)
5757
{
5858
struct sock *sk2;
5959
struct hlist_node *node;
@@ -79,6 +79,14 @@ int inet_csk_bind_conflict(const struct sock *sk,
7979
sk2_rcv_saddr == sk_rcv_saddr(sk))
8080
break;
8181
}
82+
if (!relax && reuse && sk2->sk_reuse &&
83+
sk2->sk_state != TCP_LISTEN) {
84+
const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
85+
86+
if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) ||
87+
sk2_rcv_saddr == sk_rcv_saddr(sk))
88+
break;
89+
}
8290
}
8391
}
8492
return node != NULL;
@@ -122,12 +130,13 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
122130
(tb->num_owners < smallest_size || smallest_size == -1)) {
123131
smallest_size = tb->num_owners;
124132
smallest_rover = rover;
125-
if (atomic_read(&hashinfo->bsockets) > (high - low) + 1) {
133+
if (atomic_read(&hashinfo->bsockets) > (high - low) + 1 &&
134+
!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
126135
snum = smallest_rover;
127136
goto tb_found;
128137
}
129138
}
130-
if (!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb)) {
139+
if (!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
131140
snum = rover;
132141
goto tb_found;
133142
}
@@ -178,12 +187,13 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
178187
goto success;
179188
} else {
180189
ret = 1;
181-
if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb)) {
190+
if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, true)) {
182191
if (sk->sk_reuse && sk->sk_state != TCP_LISTEN &&
183192
smallest_size != -1 && --attempts >= 0) {
184193
spin_unlock(&head->lock);
185194
goto again;
186195
}
196+
187197
goto fail_unlock;
188198
}
189199
}

net/ipv6/inet6_connection_sock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <net/inet6_connection_sock.h>
2929

3030
int inet6_csk_bind_conflict(const struct sock *sk,
31-
const struct inet_bind_bucket *tb)
31+
const struct inet_bind_bucket *tb, bool relax)
3232
{
3333
const struct sock *sk2;
3434
const struct hlist_node *node;

0 commit comments

Comments
 (0)