wangle support for TFO
authorSubodh Iyengar <subodh@fb.com>
Wed, 11 May 2016 19:34:00 +0000 (12:34 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Wed, 11 May 2016 19:35:28 +0000 (12:35 -0700)
Summary: Add server support for TFO to wangle.

Reviewed By: Orvid

Differential Revision: D3275427

fbshipit-source-id: 466baa342e2e0c4751e9c96d1e660cffb90e0634

folly/Makefile.am
folly/detail/SocketFastOpen.cpp [new file with mode: 0644]
folly/detail/SocketFastOpen.h [new file with mode: 0644]
folly/io/async/AsyncServerSocket.cpp
folly/io/async/AsyncServerSocket.h

index 3b248629b3f00c01699f63674a56fe28c4e445fc..b3b355f25b3596a51abefb0e9d56179d0d14f354 100644 (file)
@@ -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 (file)
index 0000000..90938a9
--- /dev/null
@@ -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 <folly/detail/SocketFastOpen.h>
+
+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 (file)
index 0000000..a372a90
--- /dev/null
@@ -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 <sys/socket.h>
+#include <sys/types.h>
+
+#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
+}
+}
index fccf662d745c4c52d462788de1faa6a7a1aa7f43..2693cc8a7199e3956edc88e966416d685d09414c 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <folly/FileUtil.h>
 #include <folly/SocketAddress.h>
+#include <folly/detail/SocketFastOpen.h>
 #include <folly/io/async/EventBase.h>
 #include <folly/io/async/NotificationQueue.h>
 
@@ -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);
   }
index dcebbc59fb7ed15c5bf92fdb9ebda3062e8497bf..9214d231fc7fa18c0d3246a2e5f2950b810fc7d9 100644 (file)
 
 #pragma once
 
+#include <folly/SocketAddress.h>
+#include <folly/io/ShutdownSocketSet.h>
+#include <folly/io/async/AsyncSocketBase.h>
+#include <folly/io/async/AsyncTimeout.h>
 #include <folly/io/async/DelayedDestruction.h>
-#include <folly/io/async/EventHandler.h>
 #include <folly/io/async/EventBase.h>
+#include <folly/io/async/EventHandler.h>
 #include <folly/io/async/NotificationQueue.h>
-#include <folly/io/async/AsyncTimeout.h>
-#include <folly/io/async/AsyncSocketBase.h>
-#include <folly/io/ShutdownSocketSet.h>
-#include <folly/SocketAddress.h>
-#include <memory>
-#include <exception>
-#include <vector>
 #include <limits.h>
 #include <stddef.h>
 #include <sys/socket.h>
-
+#include <exception>
+#include <memory>
+#include <vector>
 
 // 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};
 };