Commit 083c73bd authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

NetworkSocket support for SocketFastOpen

Summary: More support infrastructure.

Reviewed By: yfeldblum

Differential Revision: D12818441

fbshipit-source-id: b6d3a276ea332eccf8c6f43484fd25ec543bcc08
parent f4c9e7d2
...@@ -40,22 +40,23 @@ namespace detail { ...@@ -40,22 +40,23 @@ namespace detail {
#define TCPI_OPT_SYN_DATA 32 #define TCPI_OPT_SYN_DATA 32
#endif #endif
ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) { ssize_t tfo_sendmsg(NetworkSocket sockfd, const struct msghdr* msg, int flags) {
flags |= MSG_FASTOPEN; flags |= MSG_FASTOPEN;
return sendmsg(sockfd, msg, flags); return netops::sendmsg(sockfd, msg, flags);
} }
int tfo_enable(int sockfd, size_t max_queue_size) { int tfo_enable(NetworkSocket sockfd, size_t max_queue_size) {
return setsockopt( return netops::setsockopt(
sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size)); sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size));
} }
bool tfo_succeeded(int sockfd) { bool tfo_succeeded(NetworkSocket sockfd) {
// Call getsockopt to check if TFO was used. // Call getsockopt to check if TFO was used.
struct tcp_info info; struct tcp_info info;
socklen_t info_len = sizeof(info); socklen_t info_len = sizeof(info);
errno = 0; errno = 0;
if (getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, &info_len) != 0) { if (netops::getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, &info_len) !=
0) {
// errno is set from getsockopt // errno is set from getsockopt
return false; return false;
} }
...@@ -64,7 +65,7 @@ bool tfo_succeeded(int sockfd) { ...@@ -64,7 +65,7 @@ bool tfo_succeeded(int sockfd) {
#elif FOLLY_ALLOW_TFO && defined(__APPLE__) #elif FOLLY_ALLOW_TFO && defined(__APPLE__)
ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) { ssize_t tfo_sendmsg(NetworkSocket sockfd, const struct msghdr* msg, int flags) {
sa_endpoints_t endpoints; sa_endpoints_t endpoints;
endpoints.sae_srcif = 0; endpoints.sae_srcif = 0;
endpoints.sae_srcaddr = nullptr; endpoints.sae_srcaddr = nullptr;
...@@ -72,7 +73,7 @@ ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) { ...@@ -72,7 +73,7 @@ ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
endpoints.sae_dstaddr = (struct sockaddr*)msg->msg_name; endpoints.sae_dstaddr = (struct sockaddr*)msg->msg_name;
endpoints.sae_dstaddrlen = msg->msg_namelen; endpoints.sae_dstaddrlen = msg->msg_namelen;
int ret = connectx( int ret = connectx(
sockfd, sockfd.toFd(),
&endpoints, &endpoints,
SAE_ASSOCID_ANY, SAE_ASSOCID_ANY,
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT, CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
...@@ -84,12 +85,12 @@ ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) { ...@@ -84,12 +85,12 @@ ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }
ret = sendmsg(sockfd, msg, flags); ret = netops::sendmsg(sockfd, msg, flags);
return ret; return ret;
} }
int tfo_enable(int sockfd, size_t max_queue_size) { int tfo_enable(NetworkSocket sockfd, size_t max_queue_size) {
return setsockopt( return netops::setsockopt(
sockfd, sockfd,
IPPROTO_TCP, IPPROTO_TCP,
TCP_FASTOPEN, TCP_FASTOPEN,
...@@ -97,25 +98,27 @@ int tfo_enable(int sockfd, size_t max_queue_size) { ...@@ -97,25 +98,27 @@ int tfo_enable(int sockfd, size_t max_queue_size) {
sizeof(max_queue_size)); sizeof(max_queue_size));
} }
bool tfo_succeeded(int /* sockfd */) { bool tfo_succeeded(NetworkSocket /* sockfd */) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return false; return false;
} }
#else #else
ssize_t ssize_t tfo_sendmsg(
tfo_sendmsg(int /* sockfd */, const struct msghdr* /* msg */, int /* flags */) { NetworkSocket /* sockfd */,
const struct msghdr* /* msg */,
int /* flags */) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return -1; return -1;
} }
int tfo_enable(int /* sockfd */, size_t /* max_queue_size */) { int tfo_enable(NetworkSocket /* sockfd */, size_t /* max_queue_size */) {
errno = ENOPROTOOPT; errno = ENOPROTOOPT;
return -1; return -1;
} }
bool tfo_succeeded(int /* sockfd */) { bool tfo_succeeded(NetworkSocket /* sockfd */) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return false; return false;
} }
......
...@@ -16,9 +16,11 @@ ...@@ -16,9 +16,11 @@
#pragma once #pragma once
#include <folly/portability/Sockets.h>
#include <sys/types.h> #include <sys/types.h>
#include <folly/net/NetworkSocket.h>
#include <folly/portability/Sockets.h>
#if !defined(FOLLY_ALLOW_TFO) #if !defined(FOLLY_ALLOW_TFO)
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
// only allow for linux right now // only allow for linux right now
...@@ -28,21 +30,29 @@ ...@@ -28,21 +30,29 @@
namespace folly { namespace folly {
namespace detail { namespace detail {
/** /**
* tfo_sendto has the same semantics as sendmsg, but is used to * tfo_sendto has the same semantics as sendmsg, but is used to
* send with TFO data. * send with TFO data.
*/ */
ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags); ssize_t tfo_sendmsg(NetworkSocket sockfd, const struct msghdr* msg, int flags);
inline ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
return tfo_sendmsg(NetworkSocket::fromFd(sockfd), msg, flags);
}
/** /**
* Enable TFO on a listening socket. * Enable TFO on a listening socket.
*/ */
int tfo_enable(int sockfd, size_t max_queue_size); int tfo_enable(NetworkSocket sockfd, size_t max_queue_size);
inline int tfo_enable(int sockfd, size_t max_queue_size) {
return tfo_enable(NetworkSocket::fromFd(sockfd), max_queue_size);
}
/** /**
* Check if TFO succeeded in being used. * Check if TFO succeeded in being used.
*/ */
bool tfo_succeeded(int sockfd); bool tfo_succeeded(NetworkSocket sockfd);
inline bool tfo_succeeded(int sockfd) {
return tfo_succeeded(NetworkSocket::fromFd(sockfd));
}
} // namespace detail } // namespace detail
} // namespace folly } // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment