Skip to content

Commit

Permalink
[syncd] Move port map and port map parser to proper class (sonic-net#542
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
kcudnik authored Dec 10, 2019
1 parent 69f8a70 commit 6747eb5
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 60 deletions.
8 changes: 6 additions & 2 deletions syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions syncd/PortMap.cpp
Original file line number Diff line number Diff line change
@@ -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::set<int>, std::string> gPortMap;

void PortMap::insert(
_In_ const std::set<int>& 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::set<int>, std::string>& PortMap::getRawPortMap() const
{
SWSS_LOG_ENTER();

return m_portMap;
};

void PortMap::setGlobalPortMap(
_In_ std::shared_ptr<PortMap> portMap)
{
SWSS_LOG_ENTER();

SWSS_LOG_NOTICE("setting global gPortMap for rpc server");

gPortMap = portMap->getRawPortMap();
}

39 changes: 39 additions & 0 deletions syncd/PortMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "swss/sal.h"

#include <map>
#include <set>
#include <string>
#include <memory>

class PortMap
{
public:

PortMap() = default;

virtual ~PortMap() = default;

public:

void insert(
_In_ const std::set<int>& laneSet,
_In_ const std::string& name);

void clear();

size_t size() const;

const std::map<std::set<int>, std::string>& getRawPortMap() const;

/**
* @brief Set global object for RPC server binding.
*/
static void setGlobalPortMap(
_In_ std::shared_ptr<PortMap> portMap);

private:

std::map<std::set<int>, std::string> m_portMap;
};
64 changes: 64 additions & 0 deletions syncd/PortMapParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "PortMapParser.h"

#include "swss/logger.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>

// FIXME: introduce common config format for SONiC
std::shared_ptr<PortMap> PortMapParser::parsePortMap(
_In_ const std::string& portMapFile)
{
SWSS_LOG_ENTER();

auto portMap = std::make_shared<PortMap>();

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<int> 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;
}
18 changes: 18 additions & 0 deletions syncd/PortMapParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "PortMap.h"

#include <memory>

class PortMapParser
{
private:

PortMapParser() = delete;
~PortMapParser() = delete;

public:

static std::shared_ptr<PortMap> parsePortMap(
_In_ const std::string& portMapFile);
};
61 changes: 5 additions & 56 deletions syncd/syncd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@

#include "TimerWatchdog.h"
#include "CommandLineOptionsParser.h"

extern "C" {
#include <sai.h>
}
#include "PortMapParser.h"

#include <iostream>
#include <map>
Expand Down Expand Up @@ -3591,56 +3588,6 @@ void handleProfileMap(const std::string& profileMapFile)
}
}

#ifdef SAITHRIFT
std::map<std::set<int>, 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<int> lane_set;

while (getline(iss, lane_str, ','))
{
int lane = stoi(lane_str);
lane_set.insert(lane);
}

gPortMap.insert(std::pair<std::set<int>,std::string>(lane_set, name));
}
}
#endif // SAITHRIFT

typedef enum _syncd_restart_type_t
{
SYNCD_RESTART_TYPE_COLD,
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion syncd/syncd_applyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <algorithm>
#include <list>

extern std::shared_ptr<CommandLineOptions> g_commandLineOptions;
extern std::shared_ptr<CommandLineOptions> g_commandLineOptions; // TODO move to syncd object

/*
* NOTE: All methods taking current and temporary view could be moved to
Expand Down
4 changes: 3 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6747eb5

Please sign in to comment.