From 3e28a07db4bad58d1721a3ed77126c692ff1276c Mon Sep 17 00:00:00 2001
From: Carsten Patzke <carsten.patzke@desy.de>
Date: Fri, 11 Sep 2020 13:47:46 +0200
Subject: [PATCH] Only enable LibFabric localhost emulation if compiler flag is
 set

---
 CMakeLists.txt                                           | 7 ++++++-
 .../cpp/src/asapo_fabric/common/fabric_context_impl.cpp  | 9 ++++++++-
 tests/automatic/CMakeLists.txt                           | 6 +++++-
 tests/manual/asapo_fabric/fabric_client.cpp              | 2 ++
 tests/manual/asapo_fabric/fabric_server.cpp              | 2 ++
 5 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f444089d0..c5603cd83 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -43,6 +43,7 @@ option(BUILD_CONSUMER_TOOLS "Build consumer tools" OFF)
 option(BUILD_EXAMPLES "Build examples" OFF)
 
 option(ENABLE_LIBFABRIC "Enables LibFabric support for RDMA transfers" OFF)
+option(ENABLE_LIBFABRIC_LOCALHOST "Emulates LibFabric stack over TCP. Only for localhost and testing purposes." OFF)
 
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules/)
 
@@ -75,6 +76,10 @@ IF(ENABLE_LIBFABRIC)
     message(STATUS "LIB_FABRIC: Path: ${LIBFABRIC_LIBRARY} Include: ${LIBFABRIC_INCLUDE_DIR}")
     add_definitions(-DLIBFABRIC_ENABLED)
     SET(ASAPO_COMMON_FABRIC_LIBRARIES ${ASAPO_COMMON_FABRIC_LIBRARIES} fabric)
+    IF(ENABLE_LIBFABRIC_LOCALHOST)
+        message(STATUS "LIB_FABRIC: Enabled emulated RDMA when localhost is used. Should only be used for tests.")
+        add_definitions(-DLIBFARBIC_ALLOW_LOCALHOST)
+    ENDIF()
 ENDIF()
 SET_PROPERTY(GLOBAL PROPERTY ASAPO_COMMON_FABRIC_LIBRARIES ${ASAPO_COMMON_FABRIC_LIBRARIES})
 
@@ -123,4 +128,4 @@ endif()
 
 include(prepare_version)
 
-add_subdirectory(docs)
\ No newline at end of file
+add_subdirectory(docs)
diff --git a/common/cpp/src/asapo_fabric/common/fabric_context_impl.cpp b/common/cpp/src/asapo_fabric/common/fabric_context_impl.cpp
index 19477ac5c..2614c32bf 100644
--- a/common/cpp/src/asapo_fabric/common/fabric_context_impl.cpp
+++ b/common/cpp/src/asapo_fabric/common/fabric_context_impl.cpp
@@ -66,8 +66,15 @@ void FabricContextImpl::InitCommon(const std::string& networkIpHint, uint16_t se
     uint64_t additionalFlags = isServer ? FI_SOURCE : 0;
 
     fi_info* hints = fi_allocinfo();
-    if (networkIpHint == "127.0.0.1") {
+
+#ifdef LIBFARBIC_ALLOW_LOCALHOST
+    constexpr bool allowLocalhost = true;
+#else
+    constexpr bool allowLocalhost = false;
+#endif
+    if (networkIpHint == "127.0.0.1" && allowLocalhost) {
         // sockets mode
+        printf("WARN: Using sockets to emulate RDMA, this should only used for tests.\n");
         hints->fabric_attr->prov_name = strdup("sockets");
         hotfix_using_sockets_ = true;
     } else {
diff --git a/tests/automatic/CMakeLists.txt b/tests/automatic/CMakeLists.txt
index 9df59ca92..9c8a63194 100644
--- a/tests/automatic/CMakeLists.txt
+++ b/tests/automatic/CMakeLists.txt
@@ -39,5 +39,9 @@ endif()
 add_subdirectory(bug_fixes)
 
 if (ENABLE_LIBFABRIC)
-    add_subdirectory(asapo_fabric)
+    if (ENABLE_LIBFABRIC_LOCALHOST)
+        add_subdirectory(asapo_fabric)
+    else ()
+        message(WARNING "Disabled automated LibFabric test because 'ENABLE_LIBFABRIC_LOCALHOST' is not enabled.")
+    endif()
 endif()
diff --git a/tests/manual/asapo_fabric/fabric_client.cpp b/tests/manual/asapo_fabric/fabric_client.cpp
index ea48c16d5..68b416d72 100644
--- a/tests/manual/asapo_fabric/fabric_client.cpp
+++ b/tests/manual/asapo_fabric/fabric_client.cpp
@@ -10,7 +10,9 @@ int main(int argc, char* argv[]) {
     if (argc < 3 || argc > 5) {
         std::cout
                 << "Usage: " << argv[0] << " <serverAddress> <serverPort> [kiByte=1024*400/*400MiByte*/ /*MUST BE SYNC WITH SERVER*/] [count=10]" << std::endl
+                #ifdef LIBFARBIC_ALLOW_LOCALHOST
                 << "If the address is localhost or 127.0.0.1 the verbs connection will be emulated" << std::endl
+                #endif
                 ;
         return 1;
     }
diff --git a/tests/manual/asapo_fabric/fabric_server.cpp b/tests/manual/asapo_fabric/fabric_server.cpp
index ebfb258ee..53376b7b3 100644
--- a/tests/manual/asapo_fabric/fabric_server.cpp
+++ b/tests/manual/asapo_fabric/fabric_server.cpp
@@ -40,7 +40,9 @@ int main(int argc, char* argv[]) {
     if (argc < 3 || argc > 4) {
         std::cout
                 << "Usage: " << argv[0] << " <listenAddress> <listenPort> [kiByte=1024*400/*400MiByte*/ /*MUST BE SYNC WITH CLIENT*/]" << std::endl
+                #ifdef LIBFARBIC_ALLOW_LOCALHOST
                 << "If the address is localhost or 127.0.0.1 the verbs connection will be emulated" << std::endl
+                #endif
                 ;
         return 1;
     }
-- 
GitLab