Commit d5b2c698 authored by Fred Qiu's avatar Fred Qiu Committed by Facebook GitHub Bot

Skip some TFO tests according to kernel tcp_fastopen setting

Summary:
Added code to skip some TFO unit tests if the kernel tcp_fastopen setting does
not support the test.

Reviewed By: mingtaoy

Differential Revision: D30937011

fbshipit-source-id: 6973897445a87744686ed568cb0c0c755faa46c3
parent 96e1a8b6
......@@ -18,6 +18,7 @@
#include <cerrno>
#include <cstdio>
#include <fstream>
#include <folly/portability/Sockets.h>
......@@ -63,6 +64,40 @@ bool tfo_succeeded(NetworkSocket sockfd) {
return info.tcpi_options & TCPI_OPT_SYN_DATA;
}
PlatformTFOSettings tfo_platform_availability() {
static PlatformTFOSettings TFOSettings = [] {
size_t fastOpen = 0;
try {
std::ifstream ifs("/proc/sys/net/ipv4/tcp_fastopen");
if (ifs.is_open()) {
ifs >> fastOpen;
}
} catch (std::exception&) {
}
PlatformTFOSettings settings{};
if ((fastOpen & 1) == 0) {
settings.client = TFOAvailability::None;
} else if ((fastOpen & 4) == 0) {
settings.client = TFOAvailability::WithCookies;
} else {
settings.client = TFOAvailability::Unconditional;
}
if ((fastOpen & 2) == 0) {
settings.server = TFOAvailability::None;
} else if ((fastOpen & 0x200) == 0) {
settings.server = TFOAvailability::WithCookies;
} else {
settings.server = TFOAvailability::Unconditional;
}
return settings;
}();
return TFOSettings;
}
#elif FOLLY_ALLOW_TFO && defined(__APPLE__)
ssize_t tfo_sendmsg(NetworkSocket sockfd, const struct msghdr* msg, int flags) {
......@@ -103,6 +138,12 @@ bool tfo_succeeded(NetworkSocket /* sockfd */) {
return false;
}
PlatformTFOSettings tfo_platform_availability() {
static PlatformTFOSettings TFOSettings{
TFOAvailability::None, TFOAvailability::None};
return TFOSettings;
}
#else
ssize_t tfo_sendmsg(
......@@ -123,6 +164,12 @@ bool tfo_succeeded(NetworkSocket /* sockfd */) {
return false;
}
PlatformTFOSettings tfo_platform_availability() {
static PlatformTFOSettings TFOSettings{
TFOAvailability::None, TFOAvailability::None};
return TFOSettings;
}
#endif
} // namespace detail
} // namespace folly
......@@ -30,6 +30,40 @@
namespace folly {
namespace detail {
/**
* TFOAvailability describes support for TCP Fast Open (TFO).
*/
enum class TFOAvailability {
/**
* TFOAvailability::None indicates that the current environment
* does not support TFO (or we could not detect support via probing).
*/
None,
/**
* TFOAvailability::WithCookies indicates that TFO is supported by the
* platform. However, in order for TFO data to be used or for TFO to
* be attempted, a prior connection to the same peer must have successfully
* taken place so that the peer could issue a cookie.
*/
WithCookies,
/**
* TFOAvailability::Unconditional indicates that TFO will always be
* unconditionally sent or accepted, regardless of whether or not prior
* connections have taken place.
*/
Unconditional
};
/**
* PlatformTFOSettings describes TFO support for client connections and
* server connections.
*/
struct PlatformTFOSettings {
TFOAvailability client{TFOAvailability::None};
TFOAvailability server{TFOAvailability::None};
};
/**
* tfo_sendto has the same semantics as sendmsg, but is used to
* send with TFO data.
......@@ -45,5 +79,11 @@ int tfo_enable(NetworkSocket sockfd, size_t max_queue_size);
* Check if TFO succeeded in being used.
*/
bool tfo_succeeded(NetworkSocket sockfd);
/**
* Check if TFO is supported by the kernel.
*/
PlatformTFOSettings tfo_platform_availability();
} // namespace detail
} // namespace folly
......@@ -37,6 +37,7 @@
#include <folly/io/async/ssl/OpenSSLTransportCertificate.h>
#include <folly/io/async/test/BlockingSocket.h>
#include <folly/io/async/test/MockAsyncTransportObserver.h>
#include <folly/io/async/test/TFOTest.h>
#include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h>
#include <folly/net/test/MockNetOpsDispatcher.h>
......@@ -2850,6 +2851,10 @@ MockAsyncTFOSSLSocket::UniquePtr setupSocketWithFallback(
}
TEST(AsyncSSLSocketTest, ConnectWriteReadCloseTFOFallback) {
if (!folly::test::isTFOAvailable()) {
GTEST_SKIP() << "TFO not supported.";
}
// Start listening on a local port
WriteCallbackBase writeCallback;
ReadCallback readCallback(&writeCallback);
......
......@@ -36,6 +36,7 @@
#include <folly/io/async/test/AsyncSocketTest.h>
#include <folly/io/async/test/MockAsyncSocketObserver.h>
#include <folly/io/async/test/MockAsyncTransportObserver.h>
#include <folly/io/async/test/TFOTest.h>
#include <folly/io/async/test/Util.h>
#include <folly/net/test/MockNetOpsDispatcher.h>
#include <folly/portability/GMock.h>
......@@ -2599,6 +2600,10 @@ TEST(AsyncSocketTest, BufferCallbackKill) {
#if FOLLY_ALLOW_TFO
TEST(AsyncSocketTest, ConnectTFO) {
if (!folly::test::isTFOAvailable()) {
GTEST_SKIP() << "TFO not supported.";
}
// Start listening on a local port
TestServer server(true);
......@@ -2649,6 +2654,10 @@ TEST(AsyncSocketTest, ConnectTFO) {
}
TEST(AsyncSocketTest, ConnectTFOSupplyEarlyReadCB) {
if (!folly::test::isTFOAvailable()) {
GTEST_SKIP() << "TFO not supported.";
}
// Start listening on a local port
TestServer server(true);
......@@ -3053,6 +3062,10 @@ TEST(AsyncSocketTest, TestTFOEagain) {
// Sending a large amount of data in the first write which will
// definitely not fit into MSS.
TEST(AsyncSocketTest, ConnectTFOWithBigData) {
if (!folly::test::isTFOAvailable()) {
GTEST_SKIP() << "TFO not supported.";
}
// Start listening on a local port
TestServer server(true);
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 test {
bool isTFOAvailable() {
auto settings = detail::tfo_platform_availability();
return settings.client != folly::detail::TFOAvailability::None &&
settings.client == settings.server;
}
} // namespace test
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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
namespace folly {
namespace test {
bool isTFOAvailable();
} // namespace test
} // 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