From: Subodh Iyengar Date: Wed, 11 May 2016 19:34:00 +0000 (-0700) Subject: wangle support for TFO X-Git-Tag: 2016.07.26~247 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=bd52f50227674b1f25eaa091610a61643fc354cb;p=folly.git wangle support for TFO Summary: Add server support for TFO to wangle. Reviewed By: Orvid Differential Revision: D3275427 fbshipit-source-id: 466baa342e2e0c4751e9c96d1e660cffb90e0634 --- diff --git a/folly/Makefile.am b/folly/Makefile.am index 3b248629..b3b355f2 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -70,6 +70,7 @@ nobase_follyinclude_HEADERS = \ detail/RangeSse42.h \ detail/Sleeper.h \ detail/SlowFingerprint.h \ + detail/SocketFastOpen.h \ detail/SpinLockImpl.h \ detail/StaticSingletonManager.h \ detail/Stats.h \ @@ -427,6 +428,7 @@ libfolly_la_SOURCES = \ io/async/ssl/SSLErrors.cpp \ json.cpp \ detail/MemoryIdler.cpp \ + detail/SocketFastOpen.cpp \ MacAddress.cpp \ MemoryMapping.cpp \ portability/Dirent.cpp \ diff --git a/folly/detail/SocketFastOpen.cpp b/folly/detail/SocketFastOpen.cpp new file mode 100644 index 00000000..90938a9a --- /dev/null +++ b/folly/detail/SocketFastOpen.cpp @@ -0,0 +1,40 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace folly { +namespace detail { + +#if FOLLY_ALLOW_TFO +ssize_t tfo_sendto( + int sockfd, + const void* buf, + size_t len, + int flags, + const struct sockaddr* dest_addr, + socklen_t addrlen) { + flags |= MSG_FASTOPEN; + return sendto(sockfd, buf, len, flags, dest_addr, addrlen); +} + +int tfo_enable(int sockfd, size_t max_queue_size) { + return setsockopt( + sockfd, SOL_TCP, TCP_FASTOPEN, &max_queue_size, sizeof(max_queue_size)); +} +#endif +} +} diff --git a/folly/detail/SocketFastOpen.h b/folly/detail/SocketFastOpen.h new file mode 100644 index 00000000..a372a902 --- /dev/null +++ b/folly/detail/SocketFastOpen.h @@ -0,0 +1,49 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#if !defined(FOLLY_ALLOW_TFO) && defined(TCP_FASTOPEN) && defined(MSG_FASTOPEN) +#define FOLLY_ALLOW_TFO 1 +#endif + +namespace folly { +namespace detail { + +#if FOLLY_ALLOW_TFO + +/** + * tfo_sendto has the same semantics as sendto, but is used to + * send with TFO data. + */ +ssize_t tfo_sendto( + int sockfd, + const void* buf, + size_t len, + int flags, + const struct sockaddr* dest_addr, + socklen_t addlen); + +/** + * Enable TFO on a listening socket. + */ +int tfo_enable(int sockfd, size_t max_queue_size); +#endif +} +} diff --git a/folly/io/async/AsyncServerSocket.cpp b/folly/io/async/AsyncServerSocket.cpp index fccf662d..2693cc8a 100644 --- a/folly/io/async/AsyncServerSocket.cpp +++ b/folly/io/async/AsyncServerSocket.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -701,6 +702,14 @@ void AsyncServerSocket::setupSocket(int fd, int family) { } #endif +#if FOLLY_ALLOW_TFO + if (tfo_ && detail::tfo_enable(fd, tfoMaxQueueSize_) != 0) { + // This isn't a fatal error; just log an error message and continue + LOG(WARNING) << "failed to set TCP_FASTOPEN on async server socket: " + << folly::errnoStr(errno); + } +#endif + if (shutdownSocketSet_) { shutdownSocketSet_->add(fd); } diff --git a/folly/io/async/AsyncServerSocket.h b/folly/io/async/AsyncServerSocket.h index dcebbc59..9214d231 100644 --- a/folly/io/async/AsyncServerSocket.h +++ b/folly/io/async/AsyncServerSocket.h @@ -16,21 +16,20 @@ #pragma once +#include +#include +#include +#include #include -#include #include +#include #include -#include -#include -#include -#include -#include -#include -#include #include #include #include - +#include +#include +#include // Due to the way kernel headers are included, this may or may not be defined. // Number pulled from 3.10 kernel headers. @@ -659,6 +658,14 @@ class AsyncServerSocket : public DelayedDestruction return closeOnExec_; } + /** + * Tries to enable TFO if the machine supports it. + */ + void setTFOEnabled(bool enabled, uint32_t maxTFOQueueSize) { + tfo_ = enabled; + tfoMaxQueueSize_ = maxTFOQueueSize; + } + /** * Get whether or not the socket is accepting new connections */ @@ -837,6 +844,8 @@ class AsyncServerSocket : public DelayedDestruction bool keepAliveEnabled_; bool reusePortEnabled_{false}; bool closeOnExec_; + bool tfo_{false}; + uint32_t tfoMaxQueueSize_{0}; ShutdownSocketSet* shutdownSocketSet_; ConnectionEventCallback* connectionEventCallback_{nullptr}; };