Skip to content

Commit 538aaf9

Browse files
joannekoongkuba-moo
authored andcommitted
selftests: Add test for timing a bind request to a port with a populated bhash entry
This test populates the bhash table for a given port with MAX_THREADS * MAX_CONNECTIONS sockets, and then times how long a bind request on the port takes. When populating the bhash table, we create the sockets and then bind the sockets to the same address and port (SO_REUSEADDR and SO_REUSEPORT are set). When timing how long a bind on the port takes, we bind on a different address without SO_REUSEPORT set. We do not set SO_REUSEPORT because we are interested in the case where the bind request does not go through the tb->fastreuseport path, which is fragile (eg tb->fastreuseport path does not work if binding with a different uid). To run the test locally, I did: * ulimit -n 65535000 * ip addr add 2001:0db8:0:f101::1 dev eth0 * ./bind_bhash_test 443 Signed-off-by: Joanne Koong <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d5a42de commit 538aaf9

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

tools/testing/selftests/net/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ gro
3636
ioam6_parser
3737
toeplitz
3838
cmsg_sender
39+
bind_bhash_test

tools/testing/selftests/net/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ TEST_GEN_FILES += toeplitz
5959
TEST_GEN_FILES += cmsg_sender
6060
TEST_GEN_FILES += stress_reuseport_listen
6161
TEST_PROGS += test_vxlan_vnifiltering.sh
62+
TEST_GEN_FILES += bind_bhash_test
6263

6364
TEST_FILES := settings
6465

@@ -69,4 +70,5 @@ include bpf/Makefile
6970

7071
$(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma
7172
$(OUTPUT)/tcp_mmap: LDLIBS += -lpthread
73+
$(OUTPUT)/bind_bhash_test: LDLIBS += -lpthread
7274
$(OUTPUT)/tcp_inq: LDLIBS += -lpthread
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* This times how long it takes to bind to a port when the port already
4+
* has multiple sockets in its bhash table.
5+
*
6+
* In the setup(), we populate the port's bhash table with
7+
* MAX_THREADS * MAX_CONNECTIONS number of entries.
8+
*/
9+
10+
#include <unistd.h>
11+
#include <stdio.h>
12+
#include <netdb.h>
13+
#include <pthread.h>
14+
15+
#define MAX_THREADS 600
16+
#define MAX_CONNECTIONS 40
17+
18+
static const char *bind_addr = "::1";
19+
static const char *port;
20+
21+
static int fd_array[MAX_THREADS][MAX_CONNECTIONS];
22+
23+
static int bind_socket(int opt, const char *addr)
24+
{
25+
struct addrinfo *res, hint = {};
26+
int sock_fd, reuse = 1, err;
27+
28+
sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
29+
if (sock_fd < 0) {
30+
perror("socket fd err");
31+
return -1;
32+
}
33+
34+
hint.ai_family = AF_INET6;
35+
hint.ai_socktype = SOCK_STREAM;
36+
37+
err = getaddrinfo(addr, port, &hint, &res);
38+
if (err) {
39+
perror("getaddrinfo failed");
40+
return -1;
41+
}
42+
43+
if (opt) {
44+
err = setsockopt(sock_fd, SOL_SOCKET, opt, &reuse, sizeof(reuse));
45+
if (err) {
46+
perror("setsockopt failed");
47+
return -1;
48+
}
49+
}
50+
51+
err = bind(sock_fd, res->ai_addr, res->ai_addrlen);
52+
if (err) {
53+
perror("failed to bind to port");
54+
return -1;
55+
}
56+
57+
return sock_fd;
58+
}
59+
60+
static void *setup(void *arg)
61+
{
62+
int sock_fd, i;
63+
int *array = (int *)arg;
64+
65+
for (i = 0; i < MAX_CONNECTIONS; i++) {
66+
sock_fd = bind_socket(SO_REUSEADDR | SO_REUSEPORT, bind_addr);
67+
if (sock_fd < 0)
68+
return NULL;
69+
array[i] = sock_fd;
70+
}
71+
72+
return NULL;
73+
}
74+
75+
int main(int argc, const char *argv[])
76+
{
77+
int listener_fd, sock_fd, i, j;
78+
pthread_t tid[MAX_THREADS];
79+
clock_t begin, end;
80+
81+
if (argc != 2) {
82+
printf("Usage: listener <port>\n");
83+
return -1;
84+
}
85+
86+
port = argv[1];
87+
88+
listener_fd = bind_socket(SO_REUSEADDR | SO_REUSEPORT, bind_addr);
89+
if (listen(listener_fd, 100) < 0) {
90+
perror("listen failed");
91+
return -1;
92+
}
93+
94+
/* Set up threads to populate the bhash table entry for the port */
95+
for (i = 0; i < MAX_THREADS; i++)
96+
pthread_create(&tid[i], NULL, setup, fd_array[i]);
97+
98+
for (i = 0; i < MAX_THREADS; i++)
99+
pthread_join(tid[i], NULL);
100+
101+
begin = clock();
102+
103+
/* Bind to the same port on a different address */
104+
sock_fd = bind_socket(0, "2001:0db8:0:f101::1");
105+
106+
end = clock();
107+
108+
printf("time spent = %f\n", (double)(end - begin) / CLOCKS_PER_SEC);
109+
110+
/* clean up */
111+
close(sock_fd);
112+
close(listener_fd);
113+
for (i = 0; i < MAX_THREADS; i++) {
114+
for (j = 0; i < MAX_THREADS; i++)
115+
close(fd_array[i][j]);
116+
}
117+
118+
return 0;
119+
}

0 commit comments

Comments
 (0)