From 08233d44a6cfde878d7e10bca38ae935ed1c8fd5 Mon Sep 17 00:00:00 2001 From: Mauro Date: Wed, 30 Jun 2021 07:57:22 +0000 Subject: [HICN-713] Transport Library Major Refactoring 2 Co-authored-by: Luca Muscariello Co-authored-by: Michele Papalini Co-authored-by: Olivier Roques Co-authored-by: Giulio Grassi Signed-off-by: Mauro Sardara Change-Id: I5b2c667bad66feb45abdb5effe22ed0f6c85d1c2 --- .../src/io_modules/forwarder/CMakeLists.txt | 5 +-- libtransport/src/io_modules/forwarder/forwarder.cc | 31 +++++++------- .../src/io_modules/forwarder/forwarder_module.cc | 6 +-- .../src/io_modules/forwarder/global_id_counter.h | 23 ++-------- .../src/io_modules/forwarder/udp_tunnel.cc | 50 +++++++++++----------- libtransport/src/io_modules/forwarder/udp_tunnel.h | 3 +- .../io_modules/forwarder/udp_tunnel_listener.cc | 16 +++---- .../src/io_modules/forwarder/udp_tunnel_listener.h | 3 +- 8 files changed, 59 insertions(+), 78 deletions(-) (limited to 'libtransport/src/io_modules/forwarder') diff --git a/libtransport/src/io_modules/forwarder/CMakeLists.txt b/libtransport/src/io_modules/forwarder/CMakeLists.txt index 92662bc4c..a1d0c5db5 100644 --- a/libtransport/src/io_modules/forwarder/CMakeLists.txt +++ b/libtransport/src/io_modules/forwarder/CMakeLists.txt @@ -11,9 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.5 FATAL_ERROR) - - list(APPEND MODULE_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/connector.h ${CMAKE_CURRENT_SOURCE_DIR}/endpoint.h @@ -37,7 +34,7 @@ build_module(forwarder_module SHARED SOURCES ${MODULE_SOURCE_FILES} DEPENDS ${DEPENDENCIES} - COMPONENT lib${LIBTRANSPORT} + COMPONENT ${LIBTRANSPORT_COMPONENT}-io-modules INCLUDE_DIRS ${LIBTRANSPORT_INCLUDE_DIRS} ${LIBTRANSPORT_INTERNAL_INCLUDE_DIRS} DEFINITIONS ${COMPILER_DEFINITIONS} COMPILE_OPTIONS ${COMPILE_FLAGS} diff --git a/libtransport/src/io_modules/forwarder/forwarder.cc b/libtransport/src/io_modules/forwarder/forwarder.cc index 7e89e2f9f..0546cb8b3 100644 --- a/libtransport/src/io_modules/forwarder/forwarder.cc +++ b/libtransport/src/io_modules/forwarder/forwarder.cc @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -127,7 +128,7 @@ void Forwarder::onPacketFromListener(Connector *connector, std::bind(&Forwarder::onPacketReceived, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); - TRANSPORT_LOGD("Packet received from listener."); + DLOG_IF(INFO, VLOG_IS_ON(3)) << "Packet received from listener."; { utils::SpinLock::Acquire locked(connector_lock_); @@ -157,10 +158,8 @@ void Forwarder::onPacketReceived(Connector *connector, if ((is_producer && is_interest) || (!is_producer && !is_interest)) { c.second->send(*packet); } else { - TRANSPORT_LOGD( - "Error sending packet to local connector. is_interest = %d - " - "is_producer = %d", - (int)is_interest, (int)is_producer); + LOG(ERROR) << "Error sending packet to local connector. is_interest = " + << is_interest << " - is_producer = " << is_producer; } } @@ -178,9 +177,9 @@ void Forwarder::send(Packet &packet) { auto remote_endpoint = remote_connectors_.begin()->second->getRemoteEndpoint(); - TRANSPORT_LOGD("Sending packet to: %s:%u", - remote_endpoint.getAddress().to_string().c_str(), - remote_endpoint.getPort()); + DLOG_IF(INFO, VLOG_IS_ON(3)) + << "Sending packet to: " << remote_endpoint.getAddress() << ":" + << remote_endpoint.getPort(); remote_connectors_.begin()->second->send(packet); } @@ -199,7 +198,7 @@ void Forwarder::parseForwarderConfiguration( // Get number of threads int n_threads = 1; forwarder_config.lookupValue("n_threads", n_threads); - TRANSPORT_LOGD("Forwarder threads from config file: %u", n_threads); + VLOG(1) << "Forwarder threads from config file: " << n_threads; config_.setThreadNumber(n_threads); } @@ -219,8 +218,8 @@ void Forwarder::parseForwarderConfiguration( listener.lookupValue("local_port", port); list.port = (uint16_t)(port); - TRANSPORT_LOGD("Adding listener %s, (%s:%u)", list.name.c_str(), - list.address.c_str(), list.port); + VLOG(1) << "Adding listener " << list.name << ", ( " << list.address + << ":" << list.port << ")"; config_.addListener(std::move(list)); } } @@ -262,9 +261,9 @@ void Forwarder::parseForwarderConfiguration( conn.remote_port = (uint16_t)(port); - TRANSPORT_LOGD("Adding connector %s, (%s:%u %s:%u)", conn.name.c_str(), - conn.local_address.c_str(), conn.local_port, - conn.remote_address.c_str(), conn.remote_port); + VLOG(1) << "Adding connector " << conn.name << ", (" << conn.local_address + << ":" << conn.local_port << " " << conn.remote_address << ":" + << conn.remote_port << ")"; config_.addConnector(std::move(conn)); } } @@ -285,8 +284,8 @@ void Forwarder::parseForwarderConfiguration( route.lookupValue("connector", r.connector); r.weight = (uint16_t)(weight); - TRANSPORT_LOGD("Adding route %s %s (%s %u)", r.name.c_str(), - r.prefix.c_str(), r.connector.c_str(), r.weight); + VLOG(1) << "Adding route " << r.name << " " << r.prefix << " (" + << r.connector << " " << r.weight << ")"; config_.addRoute(std::move(r)); } } diff --git a/libtransport/src/io_modules/forwarder/forwarder_module.cc b/libtransport/src/io_modules/forwarder/forwarder_module.cc index 356b42d3b..4f95b9ca0 100644 --- a/libtransport/src/io_modules/forwarder/forwarder_module.cc +++ b/libtransport/src/io_modules/forwarder/forwarder_module.cc @@ -13,8 +13,8 @@ * limitations under the License. */ +#include #include -#include #include namespace transport { @@ -36,8 +36,8 @@ bool ForwarderModule::isConnected() { return true; } void ForwarderModule::send(Packet &packet) { IoModule::send(packet); forwarder_.send(packet); - // TRANSPORT_LOGD("ForwarderModule: sending from %u to %d", local_id_, - // 1 - local_id_); + DLOG_IF(INFO, VLOG_IS_ON(3)) + << "Sending from " << connector_id_ << " to " << 1 - connector_id_; // local_faces_.at(1 - local_id_).onPacket(packet); } diff --git a/libtransport/src/io_modules/forwarder/global_id_counter.h b/libtransport/src/io_modules/forwarder/global_id_counter.h index fe8d76730..0a67b76d5 100644 --- a/libtransport/src/io_modules/forwarder/global_id_counter.h +++ b/libtransport/src/io_modules/forwarder/global_id_counter.h @@ -15,6 +15,8 @@ #pragma once +#include + #include #include @@ -23,32 +25,15 @@ namespace transport { namespace core { template -class GlobalCounter { +class GlobalCounter : public utils::Singleton> { public: - static GlobalCounter& getInstance() { - std::lock_guard lock(global_mutex_); - - if (!instance_) { - instance_.reset(new GlobalCounter()); - } - - return *instance_; - } - + friend class utils::Singleton; T getNext() { return counter_++; } private: GlobalCounter() : counter_(0) {} - static std::unique_ptr> instance_; - static std::mutex global_mutex_; std::atomic counter_; }; -template -std::unique_ptr> GlobalCounter::instance_ = nullptr; - -template -std::mutex GlobalCounter::global_mutex_; - } // namespace core } // namespace transport \ No newline at end of file diff --git a/libtransport/src/io_modules/forwarder/udp_tunnel.cc b/libtransport/src/io_modules/forwarder/udp_tunnel.cc index dc725fc4e..bf6a69b92 100644 --- a/libtransport/src/io_modules/forwarder/udp_tunnel.cc +++ b/libtransport/src/io_modules/forwarder/udp_tunnel.cc @@ -2,6 +2,7 @@ * Copyright (c) 2017-2019 Cisco and/or its affiliates. */ +#include #include #include #include @@ -62,7 +63,7 @@ void UdpTunnelConnector::send(Packet &packet) { void UdpTunnelConnector::send(const uint8_t *packet, std::size_t len) {} void UdpTunnelConnector::close() { - TRANSPORT_LOGD("UDPTunnelConnector::close"); + DLOG_IF(INFO, VLOG_IS_ON(2)) << "UDPTunnelConnector::close"; state_ = State::CLOSED; bool is_socket_owned = socket_.use_count() == 1; if (is_socket_owned) { @@ -150,8 +151,8 @@ void UdpTunnelConnector::writeHandler(std::error_code ec) { output_buffer_.pop_front(); } } else if (retval != EWOULDBLOCK && retval != EAGAIN) { - TRANSPORT_LOGE("Error sending messages! %s %d\n", strerror(errno), - retval); + LOG(ERROR) << "Error sending messages! " << strerror(errno) + << " << retval"; return; } } @@ -164,9 +165,8 @@ void UdpTunnelConnector::writeHandler(std::error_code ec) { } void UdpTunnelConnector::readHandler(std::error_code ec) { - TRANSPORT_LOGD("UdpTunnelConnector receive packet"); + DLOG_IF(INFO, VLOG_IS_ON(3)) << "UdpTunnelConnector receive packet"; - // TRANSPORT_LOGD("UdpTunnelConnector received packet length=%lu", length); if (TRANSPORT_EXPECT_TRUE(!ec)) { if (TRANSPORT_EXPECT_TRUE(state_ == State::CONNECTED)) { if (current_position_ == 0) { @@ -182,8 +182,8 @@ void UdpTunnelConnector::readHandler(std::error_code ec) { int res = recvmmsg(socket_->native_handle(), rx_msgs_ + current_position_, max_burst - current_position_, MSG_DONTWAIT, nullptr); if (res < 0) { - TRANSPORT_LOGE("Error receiving messages! %s %d\n", strerror(errno), - res); + LOG(ERROR) << "Error receiving messages! " << strerror(errno) << " " + << res; return; } @@ -200,19 +200,20 @@ void UdpTunnelConnector::readHandler(std::error_code ec) { doRecvPacket(); } else { - TRANSPORT_LOGE( - "Error in UDP: Receiving packets from a not connected socket."); + LOG(ERROR) + << "Error in UDP: Receiving packets from a not connected socket."; } } else if (ec.value() == static_cast(std::errc::operation_canceled)) { - TRANSPORT_LOGE("The connection has been closed by the application."); + LOG(ERROR) << "The connection has been closed by the application."; return; } else { if (TRANSPORT_EXPECT_TRUE(state_ == State::CONNECTED)) { // receive_callback_(this, *read_msg_, ec); - TRANSPORT_LOGE("Error in UDP connector: %d %s", ec.value(), - ec.message().c_str()); + LOG(ERROR) << "Error in UDP connector: " << ec.value() << " " + << ec.message(); } else { - TRANSPORT_LOGE("Error while not connector"); + LOG(ERROR) << "Error in connector while not connected. " << ec.value() + << " " << ec.message(); } } } @@ -226,16 +227,17 @@ void UdpTunnelConnector::doRecvPacket() { #else socket_->async_wait(asio::ip::tcp::socket::wait_read, #endif - std::bind(&UdpTunnelConnector::readHandler, this, - std::placeholders::_1)); + std::bind(&UdpTunnelConnector::readHandler, this, + std::placeholders::_1)); } #else - TRANSPORT_LOGD("UdpTunnelConnector receive packet"); + DLOG_IF(INFO, VLOG_IS_ON(3)) << "UdpTunnelConnector receive packet"; read_msg_ = getRawBuffer(); socket_->async_receive_from( asio::buffer(read_msg_.first, read_msg_.second), remote_endpoint_recv_, [this](std::error_code ec, std::size_t length) { - TRANSPORT_LOGD("UdpTunnelConnector received packet length=%lu", length); + DLOG_IF(INFO, VLOG_IS_ON(3)) + << "UdpTunnelConnector received packet length=" << length; if (TRANSPORT_EXPECT_TRUE(!ec)) { if (TRANSPORT_EXPECT_TRUE(state_ == State::CONNECTED)) { auto packet = getPacketFromBuffer(read_msg_.first, length); @@ -244,19 +246,19 @@ void UdpTunnelConnector::doRecvPacket() { make_error_code(forwarder_error::success)); doRecvPacket(); } else { - TRANSPORT_LOGE( - "Error in UDP: Receiving packets from a not connected socket."); + LOG(ERROR) << "Error in UDP: Receiving packets from a not " + "connected socket."; } } else if (ec.value() == static_cast(std::errc::operation_canceled)) { - TRANSPORT_LOGE("The connection has been closed by the application."); + LOG(ERROR) << "The connection has been closed by the application."; return; } else { if (TRANSPORT_EXPECT_TRUE(state_ == State::CONNECTED)) { - TRANSPORT_LOGE("Error in UDP connector: %d %s", ec.value(), - ec.message().c_str()); + LOG(ERROR) << "Error in UDP connector: " << ec.value() + << ec.message(); } else { - TRANSPORT_LOGE("Error while not connector"); + LOG(ERROR) << "Error while not connected"; } } }); @@ -276,7 +278,7 @@ void UdpTunnelConnector::doConnect() { doSendPacket(); } } else { - TRANSPORT_LOGE("[Hproxy] - UDP Connection failed!!!"); + LOG(ERROR) << "UDP Connection failed!!!"; timer_.expires_from_now(std::chrono::milliseconds(500)); timer_.async_wait(std::bind(&UdpTunnelConnector::doConnect, this)); } diff --git a/libtransport/src/io_modules/forwarder/udp_tunnel.h b/libtransport/src/io_modules/forwarder/udp_tunnel.h index df472af91..4f044f93f 100644 --- a/libtransport/src/io_modules/forwarder/udp_tunnel.h +++ b/libtransport/src/io_modules/forwarder/udp_tunnel.h @@ -4,12 +4,11 @@ #pragma once +#include #include #include #include -#include -#include #include #include diff --git a/libtransport/src/io_modules/forwarder/udp_tunnel_listener.cc b/libtransport/src/io_modules/forwarder/udp_tunnel_listener.cc index 12246c3cf..d047cc568 100644 --- a/libtransport/src/io_modules/forwarder/udp_tunnel_listener.cc +++ b/libtransport/src/io_modules/forwarder/udp_tunnel_listener.cc @@ -2,8 +2,8 @@ * Copyright (c) 2017-2019 Cisco and/or its affiliates. */ +#include #include -#include #include #include @@ -36,9 +36,8 @@ void UdpTunnelListener::close() { #ifdef LINUX void UdpTunnelListener::readHandler(std::error_code ec) { - TRANSPORT_LOGD("UdpTunnelConnector receive packet"); + DLOG_IF(INFO, VLOG_IS_ON(3)) << "UdpTunnelConnector receive packet"; - // TRANSPORT_LOGD("UdpTunnelConnector received packet length=%lu", length); if (TRANSPORT_EXPECT_TRUE(!ec)) { if (current_position_ == 0) { for (int i = 0; i < Connector::max_burst; i++) { @@ -56,7 +55,8 @@ void UdpTunnelListener::readHandler(std::error_code ec) { Connector::max_burst - current_position_, MSG_DONTWAIT, nullptr); if (res < 0) { - TRANSPORT_LOGE("Error in recvmmsg."); + LOG(ERROR) << "Error in recvmmsg."; + return; } for (int i = 0; i < res; i++) { @@ -119,10 +119,10 @@ void UdpTunnelListener::readHandler(std::error_code ec) { doRecvPacket(); } else if (ec.value() == static_cast(std::errc::operation_canceled)) { - TRANSPORT_LOGE("The connection has been closed by the application."); + LOG(ERROR) << "The connection has been closed by the application."; return; } else { - TRANSPORT_LOGE("%d %s", ec.value(), ec.message().c_str()); + LOG(ERROR) << ec.value() << " " << ec.message(); } } #endif @@ -165,10 +165,10 @@ void UdpTunnelListener::doRecvPacket() { doRecvPacket(); } else if (ec.value() == static_cast(std::errc::operation_canceled)) { - TRANSPORT_LOGE("The connection has been closed by the application."); + LOG(ERROR) << "The connection has been closed by the application."; return; } else { - TRANSPORT_LOGE("%d %s", ec.value(), ec.message().c_str()); + LOG(ERROR) << ec.value() << " " << ec.message(); } }); #endif diff --git a/libtransport/src/io_modules/forwarder/udp_tunnel_listener.h b/libtransport/src/io_modules/forwarder/udp_tunnel_listener.h index 0ee40a400..5d197dcb0 100644 --- a/libtransport/src/io_modules/forwarder/udp_tunnel_listener.h +++ b/libtransport/src/io_modules/forwarder/udp_tunnel_listener.h @@ -4,11 +4,10 @@ #pragma once +#include #include #include -#include -#include #include namespace std { -- cgit 1.2.3-korg