SocketFastOpen.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/detail/SocketFastOpen.h>
  17. #include <folly/portability/Sockets.h>
  18. #include <cerrno>
  19. #include <cstdio>
  20. namespace folly {
  21. namespace detail {
  22. #if FOLLY_ALLOW_TFO && defined(__linux__)
  23. // Sometimes these flags are not present in the headers,
  24. // so define them if not present.
  25. #if !defined(MSG_FASTOPEN)
  26. #define MSG_FASTOPEN 0x20000000
  27. #endif
  28. #if !defined(TCP_FASTOPEN)
  29. #define TCP_FASTOPEN 23
  30. #endif
  31. #if !defined(TCPI_OPT_SYN_DATA)
  32. #define TCPI_OPT_SYN_DATA 32
  33. #endif
  34. ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
  35. flags |= MSG_FASTOPEN;
  36. return sendmsg(sockfd, msg, flags);
  37. }
  38. int tfo_enable(int sockfd, size_t max_queue_size) {
  39. return setsockopt(
  40. sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size));
  41. }
  42. bool tfo_succeeded(int sockfd) {
  43. // Call getsockopt to check if TFO was used.
  44. struct tcp_info info;
  45. socklen_t info_len = sizeof(info);
  46. errno = 0;
  47. if (getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, &info_len) != 0) {
  48. // errno is set from getsockopt
  49. return false;
  50. }
  51. return info.tcpi_options & TCPI_OPT_SYN_DATA;
  52. }
  53. #elif FOLLY_ALLOW_TFO && defined(__APPLE__)
  54. ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
  55. sa_endpoints_t endpoints;
  56. endpoints.sae_srcif = 0;
  57. endpoints.sae_srcaddr = nullptr;
  58. endpoints.sae_srcaddrlen = 0;
  59. endpoints.sae_dstaddr = (struct sockaddr*)msg->msg_name;
  60. endpoints.sae_dstaddrlen = msg->msg_namelen;
  61. int ret = connectx(
  62. sockfd,
  63. &endpoints,
  64. SAE_ASSOCID_ANY,
  65. CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
  66. nullptr,
  67. 0,
  68. nullptr,
  69. nullptr);
  70. if (ret != 0) {
  71. return ret;
  72. }
  73. ret = sendmsg(sockfd, msg, flags);
  74. return ret;
  75. }
  76. int tfo_enable(int sockfd, size_t max_queue_size) {
  77. return setsockopt(
  78. sockfd,
  79. IPPROTO_TCP,
  80. TCP_FASTOPEN,
  81. &max_queue_size,
  82. sizeof(max_queue_size));
  83. }
  84. bool tfo_succeeded(int /* sockfd */) {
  85. errno = EOPNOTSUPP;
  86. return false;
  87. }
  88. #else
  89. ssize_t
  90. tfo_sendmsg(int /* sockfd */, const struct msghdr* /* msg */, int /* flags */) {
  91. errno = EOPNOTSUPP;
  92. return -1;
  93. }
  94. int tfo_enable(int /* sockfd */, size_t /* max_queue_size */) {
  95. errno = ENOPROTOOPT;
  96. return -1;
  97. }
  98. bool tfo_succeeded(int /* sockfd */) {
  99. errno = EOPNOTSUPP;
  100. return false;
  101. }
  102. #endif
  103. } // namespace detail
  104. } // namespace folly