forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[syncd] Move port map and port map parser to proper class (sonic-net#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
- Loading branch information
Showing
8 changed files
with
187 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters