From 6747eb576da47fde2ee54502eb2568862cbce6e8 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Tue, 10 Dec 2019 23:15:09 +0100 Subject: [PATCH] [syncd] Move port map and port map parser to proper class (#542) * Move ntf_queue to proper NotificationQueue class * Move command line options and parser to separate classes * Move port map and port map parser to proper class * Remove syncd.h dependency --- syncd/Makefile.am | 8 +++-- syncd/PortMap.cpp | 51 +++++++++++++++++++++++++++++++ syncd/PortMap.h | 39 ++++++++++++++++++++++++ syncd/PortMapParser.cpp | 64 +++++++++++++++++++++++++++++++++++++++ syncd/PortMapParser.h | 18 +++++++++++ syncd/syncd.cpp | 61 +++---------------------------------- syncd/syncd_applyview.cpp | 2 +- tests/Makefile.am | 4 ++- 8 files changed, 187 insertions(+), 60 deletions(-) create mode 100644 syncd/PortMap.cpp create mode 100644 syncd/PortMap.h create mode 100644 syncd/PortMapParser.cpp create mode 100644 syncd/PortMapParser.h diff --git a/syncd/Makefile.am b/syncd/Makefile.am index eac7ae2aca4b..99bf7e8c3f59 100644 --- a/syncd/Makefile.am +++ b/syncd/Makefile.am @@ -25,7 +25,9 @@ syncd_SOURCES = \ TimerWatchdog.cpp \ NotificationQueue.cpp \ CommandLineOptions.cpp \ - CommandLineOptionsParser.cpp + CommandLineOptionsParser.cpp \ + PortMap.cpp \ + PortMapParser.cpp syncd_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS) syncd_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -ldl @@ -54,7 +56,9 @@ tests_SOURCES = \ TimerWatchdog.cpp \ NotificationQueue.cpp \ CommandLineOptions.cpp \ - CommandLineOptionsParser.cpp + CommandLineOptionsParser.cpp \ + PortMap.cpp \ + PortMapParser.cpp tests_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) tests_LDADD = -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/lib/src/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta diff --git a/syncd/PortMap.cpp b/syncd/PortMap.cpp new file mode 100644 index 000000000000..77210955ccf7 --- /dev/null +++ b/syncd/PortMap.cpp @@ -0,0 +1,51 @@ +#include "swss/logger.h" +#include "PortMap.h" + +/** + * @brief Port map global map. + * + * WARNING: This object must have global declaration and this exact name since + * external RPC server is linking against this object when in use. + */ +std::map, std::string> gPortMap; + +void PortMap::insert( + _In_ const std::set& laneSet, + _In_ const std::string& name) +{ + SWSS_LOG_ENTER(); + + m_portMap.insert(std::make_pair(laneSet, name)); +} + +void PortMap::clear() +{ + SWSS_LOG_ENTER(); + + m_portMap.clear(); +} + +size_t PortMap::size() const +{ + SWSS_LOG_ENTER(); + + return m_portMap.size(); +} + +const std::map, std::string>& PortMap::getRawPortMap() const +{ + SWSS_LOG_ENTER(); + + return m_portMap; +}; + +void PortMap::setGlobalPortMap( + _In_ std::shared_ptr portMap) +{ + SWSS_LOG_ENTER(); + + SWSS_LOG_NOTICE("setting global gPortMap for rpc server"); + + gPortMap = portMap->getRawPortMap(); +} + diff --git a/syncd/PortMap.h b/syncd/PortMap.h new file mode 100644 index 000000000000..9333fa4885f4 --- /dev/null +++ b/syncd/PortMap.h @@ -0,0 +1,39 @@ +#pragma once + +#include "swss/sal.h" + +#include +#include +#include +#include + +class PortMap +{ + public: + + PortMap() = default; + + virtual ~PortMap() = default; + + public: + + void insert( + _In_ const std::set& laneSet, + _In_ const std::string& name); + + void clear(); + + size_t size() const; + + const std::map, std::string>& getRawPortMap() const; + + /** + * @brief Set global object for RPC server binding. + */ + static void setGlobalPortMap( + _In_ std::shared_ptr portMap); + + private: + + std::map, std::string> m_portMap; +}; diff --git a/syncd/PortMapParser.cpp b/syncd/PortMapParser.cpp new file mode 100644 index 000000000000..d5412fb79180 --- /dev/null +++ b/syncd/PortMapParser.cpp @@ -0,0 +1,64 @@ +#include "PortMapParser.h" + +#include "swss/logger.h" + +#include +#include +#include +#include + +// FIXME: introduce common config format for SONiC +std::shared_ptr PortMapParser::parsePortMap( + _In_ const std::string& portMapFile) +{ + SWSS_LOG_ENTER(); + + auto portMap = std::make_shared(); + + if (portMapFile.size() == 0) + { + SWSS_LOG_NOTICE("no port map file, returning empty port map"); + + return portMap; + } + + std::ifstream portmap(portMapFile); + + if (!portmap.is_open()) + { + std::cerr << "failed to open port map file:" << portMapFile.c_str() << " : "<< strerror(errno) << std::endl; + SWSS_LOG_ERROR("failed to open port map file: %s: errno: %s", portMapFile.c_str(), strerror(errno)); + exit(EXIT_FAILURE); + } + + std::string line; + + while (getline(portmap, line)) + { + if (line.size() > 0 && (line[0] == '#' || line[0] == ';')) + { + continue; + } + + std::istringstream iss(line); + std::string name, lanes, alias; + iss >> name >> lanes >> alias; + + iss.clear(); + iss.str(lanes); + std::string lane_str; + std::set lane_set; + + while (getline(iss, lane_str, ',')) + { + int lane = stoi(lane_str); + lane_set.insert(lane); + } + + portMap->insert(lane_set, name); + } + + SWSS_LOG_NOTICE("returning port map with %zu entries", portMap->size()); + + return portMap; +} diff --git a/syncd/PortMapParser.h b/syncd/PortMapParser.h new file mode 100644 index 000000000000..f5cd8ab088b0 --- /dev/null +++ b/syncd/PortMapParser.h @@ -0,0 +1,18 @@ +#pragma once + +#include "PortMap.h" + +#include + +class PortMapParser +{ + private: + + PortMapParser() = delete; + ~PortMapParser() = delete; + + public: + + static std::shared_ptr parsePortMap( + _In_ const std::string& portMapFile); +}; diff --git a/syncd/syncd.cpp b/syncd/syncd.cpp index 947fe178a3f2..09efb08e20c1 100644 --- a/syncd/syncd.cpp +++ b/syncd/syncd.cpp @@ -12,10 +12,7 @@ #include "TimerWatchdog.h" #include "CommandLineOptionsParser.h" - -extern "C" { -#include -} +#include "PortMapParser.h" #include #include @@ -3591,56 +3588,6 @@ void handleProfileMap(const std::string& profileMapFile) } } -#ifdef SAITHRIFT -std::map, std::string> gPortMap; - -// FIXME: introduce common config format for SONiC -void handlePortMap(const std::string& portMapFile) -{ - SWSS_LOG_ENTER(); - - if (portMapFile.size() == 0) - { - return; - } - - std::ifstream portmap(portMapFile); - - if (!portmap.is_open()) - { - std::cerr << "failed to open port map file:" << portMapFile.c_str() << " : "<< strerror(errno) << std::endl; - exit(EXIT_FAILURE); - } - - std::string line; - - while(getline(portmap, line)) - { - if (line.size() > 0 && (line[0] == '#' || line[0] == ';')) - { - continue; - } - - std::istringstream iss(line); - std::string name, lanes, alias; - iss >> name >> lanes >> alias; - - iss.clear(); - iss.str(lanes); - std::string lane_str; - std::set lane_set; - - while (getline(iss, lane_str, ',')) - { - int lane = stoi(lane_str); - lane_set.insert(lane); - } - - gPortMap.insert(std::pair,std::string>(lane_set, name)); - } -} -#endif // SAITHRIFT - typedef enum _syncd_restart_type_t { SYNCD_RESTART_TYPE_COLD, @@ -3941,9 +3888,11 @@ int syncd_main(int argc, char **argv) handleProfileMap(g_commandLineOptions->m_profileMapFile); #ifdef SAITHRIFT - if (g_commandLineOption.portMapFile.size() > 0) + if (g_commandLineOptions->m_portMapFile.size() > 0) { - handlePortMap(g_commandLineOptions->m_portMapFile); + auto map = PortMapParser::parsePortMap(g_commandLineOptions->m_portMapFile); + + PortMap::setGlobalPortMap(map); } #endif // SAITHRIFT diff --git a/syncd/syncd_applyview.cpp b/syncd/syncd_applyview.cpp index c7c4096a1b0a..e2acbc16ce1e 100644 --- a/syncd/syncd_applyview.cpp +++ b/syncd/syncd_applyview.cpp @@ -10,7 +10,7 @@ #include #include -extern std::shared_ptr g_commandLineOptions; +extern std::shared_ptr g_commandLineOptions; // TODO move to syncd object /* * NOTE: All methods taking current and temporary view could be moved to diff --git a/tests/Makefile.am b/tests/Makefile.am index 5c90cba2cda0..e521ed1dc174 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -21,7 +21,9 @@ vssyncd_SOURCES = \ ../syncd/TimerWatchdog.cpp \ ../syncd/NotificationQueue.cpp \ ../syncd/CommandLineOptions.cpp \ - ../syncd/CommandLineOptionsParser.cpp + ../syncd/CommandLineOptionsParser.cpp \ + ../syncd/PortMap.cpp \ + ../syncd/PortMapParser.cpp vssyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS) vssyncd_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -ldl