Bump Folly dependency for Android (#463)

Summary:
Pull Request resolved: https://github.com/facebook/flipper/pull/463

Was made aware [here](https://github.com/facebook/flipper/issues/461) that
we currently don't build with NDK 20 because of Folly. The above issue exists only for x86 platform and not for other platform. I have updated the folly and RSocket. Although this diff doesn't solve the issue mentioned above for x86, but it updates the dependencies to the latest version. To mitigate temporarily, I have updated the gradle to not build for x86.

Reviewed By: passy

Differential Revision: D15713205

fbshipit-source-id: 79dd5825b0b876c87a1868fde8f3353a76012267
This commit is contained in:
Pritesh Nandgaonkar
2019-06-12 03:36:07 -07:00
committed by Facebook Github Bot
parent 173ce43192
commit 7d8db465e1
6 changed files with 229 additions and 178 deletions

View File

@@ -20,7 +20,7 @@ android {
buildConfigField "boolean", "IS_INTERNAL_BUILD", 'true' buildConfigField "boolean", "IS_INTERNAL_BUILD", 'true'
ndk { ndk {
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' abiFilters 'x86_64', 'armeabi-v7a', 'arm64-v8a'
} }
externalNativeBuild { externalNativeBuild {

View File

@@ -11,7 +11,7 @@ import org.apache.tools.ant.filters.ReplaceTokens
// Increment this when making changes to any of the native // Increment this when making changes to any of the native
// dependencies. // dependencies.
// !!! // !!!
final def CACHE_REVISION = 3 final def CACHE_REVISION = 27
final def externalDir = new File("$projectDir/external") final def externalDir = new File("$projectDir/external")
final def downloadsDir = new File("$externalDir/downloads") final def downloadsDir = new File("$externalDir/downloads")
@@ -117,7 +117,7 @@ task prepareBoost(dependsOn: [downloadBoost], type: Copy) {
} }
task downloadFolly(dependsOn: createNativeDepsDirectories, type: Download) { task downloadFolly(dependsOn: createNativeDepsDirectories, type: Download) {
src 'https://github.com/facebook/folly/archive/v2018.09.10.00.tar.gz' src 'https://github.com/facebook/folly/archive/v2019.06.10.00.tar.gz'
onlyIfNewer true onlyIfNewer true
overwrite false overwrite false
dest new File(downloadsDir, 'folly-' + getDownloadFileName(src)) dest new File(downloadsDir, 'folly-' + getDownloadFileName(src))
@@ -127,10 +127,10 @@ task prepareFolly(dependsOn: [downloadFolly], type: Copy) {
onlyIf { isCacheOutOfDate(CACHE_REVISION) } onlyIf { isCacheOutOfDate(CACHE_REVISION) }
from tarTree(downloadFolly.dest) from tarTree(downloadFolly.dest)
from './overrides/Folly/' from './overrides/Folly/'
include 'folly-2018.09.10.00/folly/**/*', 'build.gradle', 'CMakeLists.txt', 'ApplicationManifest.xml' include 'folly-2019.06.10.00/folly/**/*', 'build.gradle', 'CMakeLists.txt', 'ApplicationManifest.xml'
eachFile { it.path = it.path - "folly-2018.09.10.00/" } eachFile { it.path = it.path - "folly-2019.06.10.00/" }
includeEmptyDirs = false includeEmptyDirs = false
into "$externalDir/folly" into "$externalDir/folly/"
} }
//TODO: Get rid off this hack. //TODO: Get rid off this hack.
@@ -210,7 +210,7 @@ task finalizeOpenSSL(dependsOn: [configureOpenSSL], type: Exec) {
} }
task downloadRSocket(dependsOn: [], type: Download) { task downloadRSocket(dependsOn: [], type: Download) {
src 'https://github.com/priteshrnandgaonkar/rsocket-cpp/archive/0.10.3.tar.gz' src 'https://github.com/priteshrnandgaonkar/rsocket-cpp/archive/0.10.7.tar.gz'
onlyIfNewer true onlyIfNewer true
overwrite false overwrite false
dest new File(downloadsDir, 'rsocket-' + getDownloadFileName(src)) dest new File(downloadsDir, 'rsocket-' + getDownloadFileName(src))
@@ -219,7 +219,7 @@ task downloadRSocket(dependsOn: [], type: Download) {
task prepareRSocket(dependsOn: [downloadRSocket], type: Copy) { task prepareRSocket(dependsOn: [downloadRSocket], type: Copy) {
from tarTree(downloadRSocket.dest) from tarTree(downloadRSocket.dest)
from './overrides/RSocket/' from './overrides/RSocket/'
include 'rsocket-cpp-0.10.3/**/*', 'build.gradle', 'ApplicationManifest.xml', 'CMakeLists.txt' include 'rsocket-cpp-0.10.7/**/*', 'build.gradle', 'ApplicationManifest.xml', 'CMakeLists.txt'
includeEmptyDirs = false includeEmptyDirs = false
into "$externalDir/RSocket" into "$externalDir/RSocket"
} }

View File

@@ -35,10 +35,16 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
namespace fsp = folly::portability::sockets;
namespace folly { namespace folly {
#ifndef TCP_SAVE_SYN
#define TCP_SAVE_SYN 27
#endif
#ifndef TCP_SAVED_SYN
#define TCP_SAVED_SYN 28
#endif
static constexpr bool msgErrQueueSupported = static constexpr bool msgErrQueueSupported =
#ifdef FOLLY_HAVE_MSG_ERRQUEUE #ifdef FOLLY_HAVE_MSG_ERRQUEUE
true; true;
@@ -50,59 +56,32 @@ const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
const uint32_t AsyncServerSocket::kDefaultCallbackAcceptAtOnce; const uint32_t AsyncServerSocket::kDefaultCallbackAcceptAtOnce;
const uint32_t AsyncServerSocket::kDefaultMaxMessagesInQueue; const uint32_t AsyncServerSocket::kDefaultMaxMessagesInQueue;
int setCloseOnExec(int fd, int value) {
// Read the current flags
int old_flags = fcntl(fd, F_GETFD, 0);
// If reading the flags failed, return error indication now
if (old_flags < 0) {
return -1;
}
// Set just the flag we want to set
int new_flags;
if (value != 0) {
new_flags = old_flags | FD_CLOEXEC;
} else {
new_flags = old_flags & ~FD_CLOEXEC;
}
// Store modified flag word in the descriptor
return fcntl(fd, F_SETFD, new_flags);
}
void AsyncServerSocket::RemoteAcceptor::start( void AsyncServerSocket::RemoteAcceptor::start(
EventBase* eventBase, uint32_t maxAtOnce, uint32_t maxInQueue) { EventBase* eventBase,
uint32_t maxAtOnce,
uint32_t maxInQueue) {
setMaxReadAtOnce(maxAtOnce); setMaxReadAtOnce(maxAtOnce);
queue_.setMaxQueueSize(maxInQueue); queue_.setMaxQueueSize(maxInQueue);
if (!eventBase->runInEventBaseThread([=](){ eventBase->runInEventBaseThread([=]() {
callback_->acceptStarted(); callback_->acceptStarted();
this->startConsuming(eventBase, &queue_); this->startConsuming(eventBase, &queue_);
})) { });
throw std::invalid_argument("unable to start waiting on accept "
"notification queue in the specified "
"EventBase thread");
}
} }
void AsyncServerSocket::RemoteAcceptor::stop( void AsyncServerSocket::RemoteAcceptor::stop(
EventBase* eventBase, AcceptCallback* callback) { EventBase* eventBase,
if (!eventBase->runInEventBaseThread([=](){ AcceptCallback* callback) {
eventBase->runInEventBaseThread([=]() {
callback->acceptStopped(); callback->acceptStopped();
delete this; delete this;
})) { });
throw std::invalid_argument("unable to start waiting on accept "
"notification queue in the specified "
"EventBase thread");
}
} }
void AsyncServerSocket::RemoteAcceptor::messageAvailable( void AsyncServerSocket::RemoteAcceptor::messageAvailable(
QueueMessage&& msg) noexcept { QueueMessage&& msg) noexcept {
switch (msg.type) { switch (msg.type) {
case MessageType::MSG_NEW_CONN: case MessageType::MSG_NEW_CONN: {
{
if (connectionEventCallback_) { if (connectionEventCallback_) {
connectionEventCallback_->onConnectionDequeuedByAcceptorCallback( connectionEventCallback_->onConnectionDequeuedByAcceptorCallback(
msg.fd, msg.address); msg.fd, msg.address);
@@ -110,14 +89,12 @@ void AsyncServerSocket::RemoteAcceptor::messageAvailable(
callback_->connectionAccepted(msg.fd, msg.address); callback_->connectionAccepted(msg.fd, msg.address);
break; break;
} }
case MessageType::MSG_ERROR: case MessageType::MSG_ERROR: {
{
std::runtime_error ex(msg.msg); std::runtime_error ex(msg.msg);
callback_->acceptError(ex); callback_->acceptError(ex);
break; break;
} }
default: default: {
{
LOG(ERROR) << "invalid accept notification message type " LOG(ERROR) << "invalid accept notification message type "
<< int(msg.type); << int(msg.type);
std::runtime_error ex( std::runtime_error ex(
@@ -137,7 +114,9 @@ class AsyncServerSocket::BackoffTimeout : public AsyncTimeout {
explicit BackoffTimeout(AsyncServerSocket* socket) explicit BackoffTimeout(AsyncServerSocket* socket)
: AsyncTimeout(socket->getEventBase()), socket_(socket) {} : AsyncTimeout(socket->getEventBase()), socket_(socket) {}
void timeoutExpired() noexcept override { socket_->backoffTimeoutExpired(); } void timeoutExpired() noexcept override {
socket_->backoffTimeoutExpired();
}
private: private:
AsyncServerSocket* socket_; AsyncServerSocket* socket_;
@@ -160,7 +139,9 @@ AsyncServerSocket::AsyncServerSocket(EventBase* eventBase)
backoffTimeout_(nullptr), backoffTimeout_(nullptr),
callbacks_(), callbacks_(),
keepAliveEnabled_(true), keepAliveEnabled_(true),
closeOnExec_(true) {} closeOnExec_(true) {
disableTransparentTls();
}
void AsyncServerSocket::setShutdownSocketSet( void AsyncServerSocket::setShutdownSocketSet(
const std::weak_ptr<ShutdownSocketSet>& wNewSS) { const std::weak_ptr<ShutdownSocketSet>& wNewSS) {
@@ -193,8 +174,7 @@ AsyncServerSocket::~AsyncServerSocket() {
int AsyncServerSocket::stopAccepting(int shutdownFlags) { int AsyncServerSocket::stopAccepting(int shutdownFlags) {
int result = 0; int result = 0;
for (auto& handler : sockets_) { for (auto& handler : sockets_) {
VLOG(10) << "AsyncServerSocket::stopAccepting " << this << VLOG(10) << "AsyncServerSocket::stopAccepting " << this << handler.socket_;
handler.socket_;
} }
if (eventBase_) { if (eventBase_) {
eventBase_->dcheckIsInEventBaseThread(); eventBase_->dcheckIsInEventBaseThread();
@@ -279,7 +259,8 @@ void AsyncServerSocket::detachEventBase() {
} }
} }
void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) { void AsyncServerSocket::useExistingSockets(
const std::vector<NetworkSocket>& fds) {
if (eventBase_) { if (eventBase_) {
eventBase_->dcheckIsInEventBaseThread(); eventBase_->dcheckIsInEventBaseThread();
} }
@@ -301,7 +282,7 @@ void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) {
#if __linux__ #if __linux__
if (noTransparentTls_) { if (noTransparentTls_) {
// Ignore return value, errors are ok // Ignore return value, errors are ok
setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0); netops::setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
} }
#endif #endif
@@ -311,31 +292,30 @@ void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) {
} }
} }
void AsyncServerSocket::useExistingSocket(int fd) { void AsyncServerSocket::useExistingSocket(NetworkSocket fd) {
useExistingSockets({fd}); useExistingSockets({fd});
} }
void AsyncServerSocket::bindSocket( void AsyncServerSocket::bindSocket(
int fd, NetworkSocket fd,
const SocketAddress& address, const SocketAddress& address,
bool isExistingSocket) { bool isExistingSocket) {
sockaddr_storage addrStorage; sockaddr_storage addrStorage;
address.getAddress(&addrStorage); address.getAddress(&addrStorage);
sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage); sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
if (fsp::bind(fd, saddr, address.getActualSize()) != 0) { if (netops::bind(fd, saddr, address.getActualSize()) != 0) {
if (!isExistingSocket) { if (!isExistingSocket) {
closeNoInt(fd); closeNoInt(fd);
} }
folly::throwSystemError(errno, folly::throwSystemError(
"failed to bind to async server socket: " + errno, "failed to bind to async server socket: " + address.describe());
address.describe());
} }
#if __linux__ #if __linux__
if (noTransparentTls_) { if (noTransparentTls_) {
// Ignore return value, errors are ok // Ignore return value, errors are ok
setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0); netops::setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
} }
#endif #endif
@@ -347,9 +327,9 @@ void AsyncServerSocket::bindSocket(
bool AsyncServerSocket::setZeroCopy(bool enable) { bool AsyncServerSocket::setZeroCopy(bool enable) {
if (msgErrQueueSupported) { if (msgErrQueueSupported) {
int fd = getSocket();
int val = enable ? 1 : 0; int val = enable ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val)); int ret = netops::setsockopt(
getNetworkSocket(), SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val));
return (0 == ret); return (0 == ret);
} }
@@ -366,7 +346,7 @@ void AsyncServerSocket::bind(const SocketAddress& address) {
// However, in the normal case we need to create a new socket now. // However, in the normal case we need to create a new socket now.
// Don't set socket_ yet, so that socket_ will remain uninitialized if an // Don't set socket_ yet, so that socket_ will remain uninitialized if an
// error occurs. // error occurs.
int fd; NetworkSocket fd;
if (sockets_.size() == 0) { if (sockets_.size() == 0) {
fd = createSocket(address.getFamily()); fd = createSocket(address.getFamily());
} else if (sockets_.size() == 1) { } else if (sockets_.size() == 1) {
@@ -377,8 +357,7 @@ void AsyncServerSocket::bind(const SocketAddress& address) {
} }
fd = sockets_[0].socket_; fd = sockets_[0].socket_;
} else { } else {
throw std::invalid_argument( throw std::invalid_argument("Attempted to bind to multiple fds");
"Attempted to bind to multiple fds");
} }
bindSocket(fd, address, !sockets_.empty()); bindSocket(fd, address, !sockets_.empty());
@@ -391,13 +370,14 @@ void AsyncServerSocket::bind(
throw std::invalid_argument("No ip addresses were provided"); throw std::invalid_argument("No ip addresses were provided");
} }
if (!sockets_.empty()) { if (!sockets_.empty()) {
throw std::invalid_argument("Cannot call bind on a AsyncServerSocket " throw std::invalid_argument(
"Cannot call bind on a AsyncServerSocket "
"that already has a socket."); "that already has a socket.");
} }
for (const IPAddress& ipAddress : ipAddresses) { for (const IPAddress& ipAddress : ipAddresses) {
SocketAddress address(ipAddress.toFullyQualified(), port); SocketAddress address(ipAddress.toFullyQualified(), port);
int fd = createSocket(address.getFamily()); auto fd = createSocket(address.getFamily());
bindSocket(fd, address, false); bindSocket(fd, address, false);
} }
@@ -426,15 +406,17 @@ void AsyncServerSocket::bind(uint16_t port) {
"bad getaddrinfo"); "bad getaddrinfo");
} }
SCOPE_EXIT { freeaddrinfo(res0); }; SCOPE_EXIT {
freeaddrinfo(res0);
};
auto setupAddress = [&](struct addrinfo* res) { auto setupAddress = [&](struct addrinfo* res) {
int s = fsp::socket(res->ai_family, res->ai_socktype, res->ai_protocol); auto s = netops::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// IPv6/IPv4 may not be supported by the kernel // IPv6/IPv4 may not be supported by the kernel
if (s < 0 && errno == EAFNOSUPPORT) { if (s == NetworkSocket() && errno == EAFNOSUPPORT) {
return; return;
} }
CHECK_GE(s, 0); CHECK_NE(s, NetworkSocket());
try { try {
setupSocket(s, res->ai_family); setupSocket(s, res->ai_family);
@@ -445,12 +427,14 @@ void AsyncServerSocket::bind(uint16_t port) {
if (res->ai_family == AF_INET6) { if (res->ai_family == AF_INET6) {
int v6only = 1; int v6only = 1;
CHECK(0 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, CHECK(
&v6only, sizeof(v6only))); 0 ==
netops::setsockopt(
s, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)));
} }
// Bind to the socket // Bind to the socket
if (fsp::bind(s, res->ai_addr, socklen_t(res->ai_addrlen)) != 0) { if (netops::bind(s, res->ai_addr, socklen_t(res->ai_addrlen)) != 0) {
folly::throwSystemError( folly::throwSystemError(
errno, errno,
"failed to bind to async server socket for port ", "failed to bind to async server socket for port ",
@@ -462,7 +446,7 @@ void AsyncServerSocket::bind(uint16_t port) {
#if __linux__ #if __linux__
if (noTransparentTls_) { if (noTransparentTls_) {
// Ignore return value, errors are ok // Ignore return value, errors are ok
setsockopt(s, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0); netops::setsockopt(s, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
} }
#endif #endif
@@ -509,7 +493,7 @@ void AsyncServerSocket::bind(uint16_t port) {
// were opened, then restarting from scratch. // were opened, then restarting from scratch.
if (port == 0 && !sockets_.empty() && tries != kNumTries) { if (port == 0 && !sockets_.empty() && tries != kNumTries) {
for (const auto& socket : sockets_) { for (const auto& socket : sockets_) {
if (socket.socket_ <= 0) { if (socket.socket_ == NetworkSocket()) {
continue; continue;
} else if ( } else if (
const auto shutdownSocketSet = wShutdownSocketSet_.lock()) { const auto shutdownSocketSet = wShutdownSocketSet_.lock()) {
@@ -532,8 +516,7 @@ void AsyncServerSocket::bind(uint16_t port) {
} }
if (sockets_.size() == 0) { if (sockets_.size() == 0) {
throw std::runtime_error( throw std::runtime_error("did not bind any async server socket for port");
"did not bind any async server socket for port");
} }
} }
@@ -544,9 +527,8 @@ void AsyncServerSocket::listen(int backlog) {
// Start listening // Start listening
for (auto& handler : sockets_) { for (auto& handler : sockets_) {
if (fsp::listen(handler.socket_, backlog) == -1) { if (netops::listen(handler.socket_, backlog) == -1) {
folly::throwSystemError(errno, folly::throwSystemError(errno, "failed to listen on async server socket");
"failed to listen on async server socket");
} }
} }
} }
@@ -560,8 +542,7 @@ void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
addressReturn->setFromLocalAddress(sockets_[0].socket_); addressReturn->setFromLocalAddress(sockets_[0].socket_);
} }
std::vector<SocketAddress> AsyncServerSocket::getAddresses() std::vector<SocketAddress> AsyncServerSocket::getAddresses() const {
const {
CHECK(sockets_.size() >= 1); CHECK(sockets_.size() >= 1);
auto tsaVec = std::vector<SocketAddress>(sockets_.size()); auto tsaVec = std::vector<SocketAddress>(sockets_.size());
auto tsaIter = tsaVec.begin(); auto tsaIter = tsaVec.begin();
@@ -571,7 +552,8 @@ std::vector<SocketAddress> AsyncServerSocket::getAddresses()
return tsaVec; return tsaVec;
} }
void AsyncServerSocket::addAcceptCallback(AcceptCallback *callback, void AsyncServerSocket::addAcceptCallback(
AcceptCallback* callback,
EventBase* eventBase, EventBase* eventBase,
uint32_t maxAtOnce) { uint32_t maxAtOnce) {
if (eventBase_) { if (eventBase_) {
@@ -620,7 +602,8 @@ void AsyncServerSocket::addAcceptCallback(AcceptCallback *callback,
callbacks_.back().consumer = acceptor; callbacks_.back().consumer = acceptor;
} }
void AsyncServerSocket::removeAcceptCallback(AcceptCallback *callback, void AsyncServerSocket::removeAcceptCallback(
AcceptCallback* callback,
EventBase* eventBase) { EventBase* eventBase) {
if (eventBase_) { if (eventBase_) {
eventBase_->dcheckIsInEventBaseThread(); eventBase_->dcheckIsInEventBaseThread();
@@ -634,7 +617,8 @@ void AsyncServerSocket::removeAcceptCallback(AcceptCallback *callback,
uint32_t n = 0; uint32_t n = 0;
while (true) { while (true) {
if (it == callbacks_.end()) { if (it == callbacks_.end()) {
throw std::runtime_error("AsyncServerSocket::removeAcceptCallback(): " throw std::runtime_error(
"AsyncServerSocket::removeAcceptCallback(): "
"accept callback not found"); "accept callback not found");
} }
if (it->callback == callback && if (it->callback == callback &&
@@ -699,8 +683,7 @@ void AsyncServerSocket::startAccepting() {
} }
for (auto& handler : sockets_) { for (auto& handler : sockets_) {
if (!handler.registerHandler( if (!handler.registerHandler(EventHandler::READ | EventHandler::PERSIST)) {
EventHandler::READ | EventHandler::PERSIST)) {
throw std::runtime_error("failed to register for accept events"); throw std::runtime_error("failed to register for accept events");
} }
} }
@@ -721,9 +704,9 @@ void AsyncServerSocket::pauseAccepting() {
} }
} }
int AsyncServerSocket::createSocket(int family) { NetworkSocket AsyncServerSocket::createSocket(int family) {
int fd = fsp::socket(family, SOCK_STREAM, 0); auto fd = netops::socket(family, SOCK_STREAM, 0);
if (fd == -1) { if (fd == NetworkSocket()) {
folly::throwSystemError(errno, "error creating async server socket"); folly::throwSystemError(errno, "error creating async server socket");
} }
@@ -736,16 +719,45 @@ int AsyncServerSocket::createSocket(int family) {
return fd; return fd;
} }
void AsyncServerSocket::setupSocket(int fd, int family) { /**
* Enable/Disable TOS reflection for the server socket
* If enabled, the 'accepted' connections will reflect the
* TOS derived from the client's connect request
*/
void AsyncServerSocket::setTosReflect(bool enable) {
if (!kIsLinux || enable == false) {
tosReflect_ = false;
return;
}
for (auto& handler : sockets_) {
if (handler.socket_ == NetworkSocket()) {
continue;
}
int val = (enable) ? 1 : 0;
int ret = netops::setsockopt(
handler.socket_, IPPROTO_TCP, TCP_SAVE_SYN, &val, sizeof(val));
if (ret == 0) {
VLOG(10) << "Enabled SYN save for socket " << handler.socket_;
} else {
folly::throwSystemError(errno, "failed to enable TOS reflect");
}
}
tosReflect_ = true;
}
void AsyncServerSocket::setupSocket(NetworkSocket fd, int family) {
// Put the socket in non-blocking mode // Put the socket in non-blocking mode
if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) { if (netops::set_socket_non_blocking(fd) != 0) {
folly::throwSystemError(errno, folly::throwSystemError(errno, "failed to put socket in non-blocking mode");
"failed to put socket in non-blocking mode");
} }
// Set reuseaddr to avoid 2MSL delay on server restart // Set reuseaddr to avoid 2MSL delay on server restart
int one = 1; int one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) { if (netops::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) !=
0) {
// This isn't a fatal error; just log an error message and continue // This isn't a fatal error; just log an error message and continue
LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket " << errno; LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket " << errno;
} }
@@ -753,42 +765,46 @@ void AsyncServerSocket::setupSocket(int fd, int family) {
// Set reuseport to support multiple accept threads // Set reuseport to support multiple accept threads
int zero = 0; int zero = 0;
if (reusePortEnabled_ && if (reusePortEnabled_ &&
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) { netops::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) !=
0) {
LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket " LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket "
<< strerror(errno); << errnoStr(errno);
#ifdef WIN32 #ifdef WIN32
folly::throwSystemError(errno, "failed to bind to the async server socket"); folly::throwSystemError(errno, "failed to bind to the async server socket");
#else #else
SocketAddress address; SocketAddress address;
address.setFromLocalAddress(fd); address.setFromLocalAddress(fd);
folly::throwSystemError(errno, folly::throwSystemError(
"failed to bind to async server socket: " + errno, "failed to bind to async server socket: " + address.describe());
address.describe());
#endif #endif
} }
// Set keepalive as desired // Set keepalive as desired
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, if (netops::setsockopt(
(keepAliveEnabled_) ? &one : &zero, sizeof(int)) != 0) { fd,
LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: " << SOL_SOCKET,
strerror(errno); SO_KEEPALIVE,
(keepAliveEnabled_) ? &one : &zero,
sizeof(int)) != 0) {
LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: "
<< errnoStr(errno);
} }
// Setup FD_CLOEXEC flag // Setup FD_CLOEXEC flag
if (closeOnExec_ && if (closeOnExec_ && (-1 == netops::set_socket_close_on_exec(fd))) {
(-1 == folly::setCloseOnExec(fd, closeOnExec_))) { LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: "
LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: " << << errnoStr(errno);
strerror(errno);
} }
// Set TCP nodelay if available, MAC OS X Hack // Set TCP nodelay if available, MAC OS X Hack
// See http://lists.danga.com/pipermail/memcached/2005-March/001240.html // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html
#ifndef TCP_NOPUSH #ifndef TCP_NOPUSH
if (family != AF_UNIX) { if (family != AF_UNIX) {
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) { if (netops::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) !=
0) {
// This isn't a fatal error; just log an error message and continue // This isn't a fatal error; just log an error message and continue
LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: " << LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: "
strerror(errno); << errnoStr(errno);
} }
} }
#else #else
@@ -808,8 +824,9 @@ void AsyncServerSocket::setupSocket(int fd, int family) {
} }
} }
void AsyncServerSocket::handlerReady(uint16_t /* events */, void AsyncServerSocket::handlerReady(
int fd, uint16_t /* events */,
NetworkSocket fd,
sa_family_t addressFamily) noexcept { sa_family_t addressFamily) noexcept {
assert(!callbacks_.empty()); assert(!callbacks_.empty());
DestructorGuard dg(this); DestructorGuard dg(this);
@@ -830,18 +847,53 @@ void AsyncServerSocket::handlerReady(uint16_t /* events */,
} }
// Accept a new client socket // Accept a new client socket
#ifdef SOCK_NONBLOCK // #ifdef SOCK_NONBLOCK
// int clientSocket = accept4(fd, saddr, &addrLen, SOCK_NONBLOCK); // auto clientSocket = NetworkSocket::fromFd(
// accept4(fd.toFd(), saddr, &addrLen, SOCK_NONBLOCK));
// #else // #else
int clientSocket = accept(fd, saddr, &addrLen); auto clientSocket = netops::accept(fd, saddr, &addrLen);
#endif // #endif
address.setFromSockaddr(saddr, addrLen); address.setFromSockaddr(saddr, addrLen);
if (clientSocket >= 0 && connectionEventCallback_) { if (clientSocket != NetworkSocket() && connectionEventCallback_) {
connectionEventCallback_->onConnectionAccepted(clientSocket, address); connectionEventCallback_->onConnectionAccepted(clientSocket, address);
} }
// Connection accepted, get the SYN packet from the client if
// TOS reflect is enabled
if (kIsLinux && clientSocket != NetworkSocket() && tosReflect_) {
std::array<uint32_t, 64> buffer;
socklen_t len = sizeof(buffer);
int ret = netops::getsockopt(
clientSocket, IPPROTO_TCP, TCP_SAVED_SYN, &buffer, &len);
if (ret == 0) {
uint32_t tosWord = folly::Endian::big(buffer[0]);
if (addressFamily == AF_INET6) {
tosWord = (tosWord & 0x0FC00000) >> 20;
ret = netops::setsockopt(
clientSocket,
IPPROTO_IPV6,
IPV6_TCLASS,
&tosWord,
sizeof(tosWord));
} else if (addressFamily == AF_INET) {
tosWord = (tosWord & 0x00FC0000) >> 16;
ret = netops::setsockopt(
clientSocket, IPPROTO_IP, IP_TOS, &tosWord, sizeof(tosWord));
}
if (ret != 0) {
LOG(ERROR) << "Unable to set TOS for accepted socket "
<< clientSocket;
}
} else {
LOG(ERROR) << "Unable to get SYN packet for accepted socket "
<< clientSocket;
}
}
std::chrono::time_point<std::chrono::steady_clock> nowMs = std::chrono::time_point<std::chrono::steady_clock> nowMs =
std::chrono::steady_clock::now(); std::chrono::steady_clock::now();
auto timeSinceLastAccept = std::max<int64_t>( auto timeSinceLastAccept = std::max<int64_t>(
@@ -855,18 +907,18 @@ void AsyncServerSocket::handlerReady(uint16_t /* events */,
acceptRate_ = 1; acceptRate_ = 1;
} else if (rand() > acceptRate_ * RAND_MAX) { } else if (rand() > acceptRate_ * RAND_MAX) {
++numDroppedConnections_; ++numDroppedConnections_;
if (clientSocket >= 0) { if (clientSocket != NetworkSocket()) {
closeNoInt(clientSocket); closeNoInt(clientSocket);
if (connectionEventCallback_) { if (connectionEventCallback_) {
connectionEventCallback_->onConnectionDropped(clientSocket, connectionEventCallback_->onConnectionDropped(
address); clientSocket, address);
} }
} }
continue; continue;
} }
} }
if (clientSocket < 0) { if (clientSocket == NetworkSocket()) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
// No more sockets to accept right now. // No more sockets to accept right now.
// Check for this code first, since it's the most common. // Check for this code first, since it's the most common.
@@ -892,10 +944,10 @@ void AsyncServerSocket::handlerReady(uint16_t /* events */,
#ifndef SOCK_NONBLOCK #ifndef SOCK_NONBLOCK
// Explicitly set the new connection to non-blocking mode // Explicitly set the new connection to non-blocking mode
if (fcntl(clientSocket, F_SETFL, O_NONBLOCK) != 0) { if (netops::set_socket_non_blocking(clientSocket) != 0) {
closeNoInt(clientSocket); closeNoInt(clientSocket);
dispatchError("failed to set accepted socket to non-blocking mode", dispatchError(
errno); "failed to set accepted socket to non-blocking mode", errno);
if (connectionEventCallback_) { if (connectionEventCallback_) {
connectionEventCallback_->onConnectionDropped(clientSocket, address); connectionEventCallback_->onConnectionDropped(clientSocket, address);
} }
@@ -913,14 +965,15 @@ void AsyncServerSocket::handlerReady(uint16_t /* events */,
} }
} }
void AsyncServerSocket::dispatchSocket(int socket, void AsyncServerSocket::dispatchSocket(
NetworkSocket socket,
SocketAddress&& address) { SocketAddress&& address) {
uint32_t startingIndex = callbackIndex_; uint32_t startingIndex = callbackIndex_;
// Short circuit if the callback is in the primary EventBase thread // Short circuit if the callback is in the primary EventBase thread
CallbackInfo* info = nextCallback(); CallbackInfo* info = nextCallback();
if (info->eventBase == nullptr) { if (info->eventBase == nullptr || info->eventBase == this->eventBase_) {
info->callback->connectionAccepted(socket, address); info->callback->connectionAccepted(socket, address);
return; return;
} }
@@ -937,8 +990,7 @@ void AsyncServerSocket::dispatchSocket(int socket,
if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) { if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
if (connectionEventCallback_) { if (connectionEventCallback_) {
connectionEventCallback_->onConnectionEnqueuedForAcceptorCallback( connectionEventCallback_->onConnectionEnqueuedForAcceptorCallback(
socket, socket, addr);
addr);
} }
// Success! return. // Success! return.
return; return;
@@ -953,7 +1005,6 @@ void AsyncServerSocket::dispatchSocket(int socket,
acceptRate_ *= 1 - kAcceptRateDecreaseSpeed; acceptRate_ *= 1 - kAcceptRateDecreaseSpeed;
} }
if (callbackIndex_ == startingIndex) { if (callbackIndex_ == startingIndex) {
// The notification queue was full // The notification queue was full
// We can't really do anything at this point other than close the socket. // We can't really do anything at this point other than close the socket.
@@ -989,7 +1040,7 @@ void AsyncServerSocket::dispatchError(const char *msgstr, int errnoValue) {
while (true) { while (true) {
// Short circuit if the callback is in the primary EventBase thread // Short circuit if the callback is in the primary EventBase thread
if (info->eventBase == nullptr) { if (info->eventBase == nullptr || info->eventBase == this->eventBase_) {
std::runtime_error ex( std::runtime_error ex(
std::string(msgstr) + folly::to<std::string>(errnoValue)); std::string(msgstr) + folly::to<std::string>(errnoValue));
info->callback->acceptError(ex); info->callback->acceptError(ex);
@@ -1082,8 +1133,7 @@ void AsyncServerSocket::backoffTimeoutExpired() {
// Register the handler. // Register the handler.
for (auto& handler : sockets_) { for (auto& handler : sockets_) {
if (!handler.registerHandler( if (!handler.registerHandler(EventHandler::READ | EventHandler::PERSIST)) {
EventHandler::READ | EventHandler::PERSIST)) {
// We're hosed. We could just re-schedule backoffTimeout_ to // We're hosed. We could just re-schedule backoffTimeout_ to
// re-try again after a little bit. However, we don't want to // re-try again after a little bit. However, we don't want to
// loop retrying forever if we can't re-enable accepts. Just // loop retrying forever if we can't re-enable accepts. Just

View File

@@ -109,6 +109,7 @@ list(APPEND SRC_FILES ${FOLLY_DIR}/io/async/HHWheelTimer.cpp
${FOLLY_DIR}/Format.cpp ${FOLLY_DIR}/Format.cpp
${FOLLY_DIR}/String.cpp ${FOLLY_DIR}/String.cpp
${FOLLY_DIR}/memory/detail/MallocImpl.cpp ${FOLLY_DIR}/memory/detail/MallocImpl.cpp
${FOLLY_DIR}/net/NetOps.cpp
) )
add_library(${PACKAGE_NAME} STATIC ${SRC_FILES}) add_library(${PACKAGE_NAME} STATIC ${SRC_FILES})

View File

@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.6.0)
PROJECT(rsocket CXX) PROJECT(rsocket CXX)
set(PACKAGE_NAME rsocket) set(PACKAGE_NAME rsocket)
set(RSOCKET_VERSION 0.10.3) set(RSOCKET_VERSION 0.10.7)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)

View File

@@ -4,7 +4,7 @@ set(CMAKE_VERBOSE_MAKEFILE on)
set(PACKAGE_NAME flippercpp) set(PACKAGE_NAME flippercpp)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(RSOCKET_VERSION 0.10.3) set(RSOCKET_VERSION 0.10.7)
set(external_DIR ${PROJECT_SOURCE_DIR}/../android/third-party/external) set(external_DIR ${PROJECT_SOURCE_DIR}/../android/third-party/external)
set(libfolly_DIR ${external_DIR}/folly/) set(libfolly_DIR ${external_DIR}/folly/)
set(rsocket_DIR ${external_DIR}/RSocket/) set(rsocket_DIR ${external_DIR}/RSocket/)