For now, the tcp socket lookup will terminate if the socket is reuse port in inet_lhash2_lookup(), which makes the socket is not the best match.
For example, we have socket1 and socket2 both listen on "0.0.0.0:1234", but socket1 bind on "eth0". We create socket1 first, and then socket2. Then, all connections will goto socket2, which is not expected, as socket1 has higher priority.
The 1st patch fix this problem, and the 2nd patch is a selftests for this problem. Without the 1st patch, the selftests will fail with:
$ ./tcp_reuseport.py TAP version 13 1..1 FAIL: wrong assignment not ok 1 tcp_reuseport.test_reuseport_select Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0
With the 1st patch, it will success: $ ./tcp_reuseport.py TAP version 13 1..1 SUCCESS: assigned properly: (<socket.socket fd=6, family=2, type=1, proto=0, laddr=('127.0.0.1', 33787), raddr=('127.0.0.1', 43140)>, ('127.0.0.1', 43140)) SUCCESS: assigned properly: (<socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 33787), raddr=('127.0.0.1', 43146)>, ('127.0.0.1', 43146)) SUCCESS: assigned properly: (<socket.socket fd=6, family=2, type=1, proto=0, laddr=('127.0.0.1', 33787), raddr=('127.0.0.1', 43162)>, ('127.0.0.1', 43162)) ok 1 tcp_reuseport.test_reuseport_select Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
Changes since V2: * use the approach in V1 * add the Fixes tag in the 1st patch * introduce the selftests
Menglong Dong (2): net: tcp: lookup the best matched listen socket selftests/net: test TCP reuseport socket selection
net/ipv4/inet_hashtables.c | 13 +++---- net/ipv6/inet6_hashtables.c | 13 +++---- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/tcp_reuseport.py | 36 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100755 tools/testing/selftests/net/tcp_reuseport.py