xfs: preserve DIFLAG2_NREXT64 when setting other inode attributes
[platform/kernel/linux-starfive.git] / tools / testing / selftests / net / bind_bhash_test.c
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 }