bind_bhash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <netdb.h>
  12. #include <pthread.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #define MAX_THREADS 600
  16. #define MAX_CONNECTIONS 40
  17. static const char *setup_addr_v6 = "::1";
  18. static const char *setup_addr_v4 = "127.0.0.1";
  19. static const char *setup_addr;
  20. static const char *bind_addr;
  21. static const char *port;
  22. bool use_v6;
  23. int ret;
  24. static int fd_array[MAX_THREADS][MAX_CONNECTIONS];
  25. static int bind_socket(int opt, const char *addr)
  26. {
  27. struct addrinfo *res, hint = {};
  28. int sock_fd, reuse = 1, err;
  29. int domain = use_v6 ? AF_INET6 : AF_INET;
  30. sock_fd = socket(domain, SOCK_STREAM, 0);
  31. if (sock_fd < 0) {
  32. perror("socket fd err");
  33. return sock_fd;
  34. }
  35. hint.ai_family = domain;
  36. hint.ai_socktype = SOCK_STREAM;
  37. err = getaddrinfo(addr, port, &hint, &res);
  38. if (err) {
  39. perror("getaddrinfo failed");
  40. goto cleanup;
  41. }
  42. if (opt) {
  43. err = setsockopt(sock_fd, SOL_SOCKET, opt, &reuse, sizeof(reuse));
  44. if (err) {
  45. perror("setsockopt failed");
  46. goto cleanup;
  47. }
  48. }
  49. err = bind(sock_fd, res->ai_addr, res->ai_addrlen);
  50. if (err) {
  51. perror("failed to bind to port");
  52. goto cleanup;
  53. }
  54. return sock_fd;
  55. cleanup:
  56. close(sock_fd);
  57. return err;
  58. }
  59. static void *setup(void *arg)
  60. {
  61. int sock_fd, i;
  62. int *array = (int *)arg;
  63. for (i = 0; i < MAX_CONNECTIONS; i++) {
  64. sock_fd = bind_socket(SO_REUSEADDR | SO_REUSEPORT, setup_addr);
  65. if (sock_fd < 0) {
  66. ret = sock_fd;
  67. pthread_exit(&ret);
  68. }
  69. array[i] = sock_fd;
  70. }
  71. return NULL;
  72. }
  73. int main(int argc, const char *argv[])
  74. {
  75. int listener_fd, sock_fd, i, j;
  76. pthread_t tid[MAX_THREADS];
  77. clock_t begin, end;
  78. if (argc != 4) {
  79. printf("Usage: listener <port> <ipv6 | ipv4> <bind-addr>\n");
  80. return -1;
  81. }
  82. port = argv[1];
  83. use_v6 = strcmp(argv[2], "ipv6") == 0;
  84. bind_addr = argv[3];
  85. setup_addr = use_v6 ? setup_addr_v6 : setup_addr_v4;
  86. listener_fd = bind_socket(SO_REUSEADDR | SO_REUSEPORT, setup_addr);
  87. if (listen(listener_fd, 100) < 0) {
  88. perror("listen failed");
  89. return -1;
  90. }
  91. /* Set up threads to populate the bhash table entry for the port */
  92. for (i = 0; i < MAX_THREADS; i++)
  93. pthread_create(&tid[i], NULL, setup, fd_array[i]);
  94. for (i = 0; i < MAX_THREADS; i++)
  95. pthread_join(tid[i], NULL);
  96. if (ret)
  97. goto done;
  98. begin = clock();
  99. /* Bind to the same port on a different address */
  100. sock_fd = bind_socket(0, bind_addr);
  101. if (sock_fd < 0)
  102. goto done;
  103. end = clock();
  104. printf("time spent = %f\n", (double)(end - begin) / CLOCKS_PER_SEC);
  105. /* clean up */
  106. close(sock_fd);
  107. done:
  108. close(listener_fd);
  109. for (i = 0; i < MAX_THREADS; i++) {
  110. for (j = 0; i < MAX_THREADS; i++)
  111. close(fd_array[i][j]);
  112. }
  113. return 0;
  114. }