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 {
#define TCPI_OPT_SYN_DATA 32
#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;
return sendmsg(sockfd, msg, flags);
return netops::sendmsg(sockfd, msg, flags);
}
int tfo_enable(int sockfd, size_t max_queue_size) {
return setsockopt(
int tfo_enable(NetworkSocket sockfd, size_t max_queue_size) {
return netops::setsockopt(
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.
struct tcp_info info;
socklen_t info_len = sizeof(info);
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
return false;
}
......@@ -64,7 +65,7 @@ bool tfo_succeeded(int sockfd) {
#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;
endpoints.sae_srcif = 0;
endpoints.sae_srcaddr = nullptr;
......@@ -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_dstaddrlen = msg->msg_namelen;
int ret = connectx(
sockfd,
sockfd.toFd(),
&endpoints,
SAE_ASSOCID_ANY,
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
......@@ -84,12 +85,12 @@ ssize_t tfo_sendmsg(int sockfd, const struct msghdr* msg, int flags) {
if (ret != 0) {
return ret;
}
ret = sendmsg(sockfd, msg, flags);
ret = netops::sendmsg(sockfd, msg, flags);
return ret;
}
int tfo_enable(int sockfd, size_t max_queue_size) {
return setsockopt(
int tfo_enable(NetworkSocket sockfd, size_t max_queue_size) {
return netops::setsockopt(
sockfd,
IPPROTO_TCP,
TCP_FASTOPEN,
......@@ -97,25 +98,27 @@ int tfo_enable(int sockfd, size_t max_queue_size) {
sizeof(max_queue_size));
}
bool tfo_succeeded(int /* sockfd */) {
bool tfo_succeeded(NetworkSocket /* sockfd */) {
errno = EOPNOTSUPP;
return false;
}
#else
ssize_t
tfo_sendmsg(int /* sockfd */, const struct msghdr* /* msg */, int /* flags */) {
ssize_t tfo_sendmsg(
NetworkSocket /* sockfd */,
const struct msghdr* /* msg */,
int /* flags */) {
errno = EOPNOTSUPP;
return -1;
}
int tfo_enable(int /* sockfd */, size_t /* max_queue_size */) {
int tfo_enable(NetworkSocket /* sockfd */, size_t /* max_queue_size */) {
errno = ENOPROTOOPT;
return -1;
}
bool tfo_succeeded(int /* sockfd */) {
bool tfo_succeeded(NetworkSocket /* sockfd */) {
errno = EOPNOTSUPP;
return false;
}
......
......@@ -16,9 +16,11 @@
#pragma once
#include <folly/portability/Sockets.h>
#include <sys/types.h>
#include <folly/net/NetworkSocket.h>
#include <folly/portability/Sockets.h>
#if !defined(FOLLY_ALLOW_TFO)
#if defined(__linux__) || defined(__APPLE__)
// only allow for linux right now
......@@ -28,21 +30,29 @@
namespace folly {
namespace detail {
/**
* tfo_sendto has the same semantics as sendmsg, but is used to
* 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.
*/
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.
*/
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 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