Skip to content

Commit

Permalink
[saiplayer] Add support for profile.ini (sonic-net#622)
Browse files Browse the repository at this point in the history
* [saiplayer] Add support for profile.ini

Will allow to read context_config.ini file

* Fix getopt string

* Support context config from command line

* Add support for multiple contexts in saiplayer

* Add notice message when using context config from cmd
  • Loading branch information
kcudnik authored Jun 16, 2020
1 parent db0b35a commit ea432b3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
5 changes: 5 additions & 0 deletions saiplayer/CommandLineOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ CommandLineOptions::CommandLineOptions()
m_enableDebug = false;
m_sleep = false;
m_syncMode = false;

m_profileMapFile = "";
m_contextConfig = "";
}

std::string CommandLineOptions::getCommandLineString() const
Expand All @@ -32,6 +35,8 @@ std::string CommandLineOptions::getCommandLineString() const
ss << " EnableDebug=" << (m_enableDebug ? "YES" : "NO");
ss << " Sleep=" << (m_sleep ? "YES" : "NO");
ss << " SyncMode=" << (m_syncMode ? "YES" : "NO");
ss << " ProfileMapFile=" << m_profileMapFile;
ss << " ContextConfig=" << m_contextConfig;

return ss.str();
}
4 changes: 4 additions & 0 deletions saiplayer/CommandLineOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ namespace saiplayer

bool m_syncMode;

std::string m_profileMapFile;

std::string m_contextConfig;

std::vector<std::string> m_files;
};
}
18 changes: 16 additions & 2 deletions saiplayer/CommandLineOptionsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(

auto options = std::make_shared<CommandLineOptions>();

const char* const optstring = "uiCdsmh";
const char* const optstring = "uiCdsmp:x:h";

while(true)
{
Expand All @@ -28,6 +28,8 @@ std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(
{ "enableDebug", no_argument, 0, 'd' },
{ "sleep", no_argument, 0, 's' },
{ "syncMode", no_argument, 0, 'm' },
{ "profile", required_argument, 0, 'p' },
{ "contextContig", required_argument, 0, 'x' },
{ "help", no_argument, 0, 'h' },
};

Expand Down Expand Up @@ -66,10 +68,18 @@ std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(
options->m_syncMode = true;
break;

case 'x':
options->m_contextConfig = std::string(optarg);
break;

case 'h':
printUsage();
exit(EXIT_SUCCESS);

case 'p':
options->m_profileMapFile = std::string(optarg);
break;

case '?':
SWSS_LOG_WARN("unknown option %c", optopt);
printUsage();
Expand Down Expand Up @@ -100,7 +110,7 @@ void CommandLineOptionsParser::printUsage()
{
SWSS_LOG_ENTER();

std::cout << "Usage: saiplayer [-u] [-i] [-C] [-d] [-s] [-m] [-h] recordfile" << std::endl << std::endl;
std::cout << "Usage: saiplayer [-u] [-i] [-C] [-d] [-s] [-m] [-p profile] [-x contextConfig] [-h] recordfile" << std::endl << std::endl;

std::cout << " -u --useTempView:" << std::endl;
std::cout << " Enable temporary view between init and apply" << std::endl << std::endl;
Expand All @@ -114,6 +124,10 @@ void CommandLineOptionsParser::printUsage()
std::cout << " Sleep after success reply, to notice any switch notifications" << std::endl << std::endl;
std::cout << " -m --syncMode:" << std::endl;
std::cout << " Enable synchronous mode" << std::endl << std::endl;
std::cout << " -p --profile profile" << std::endl;
std::cout << " Provide profile map file" << std::endl;
std::cout << " -x --contextConfig" << std::endl;
std::cout << " Context configuration file" << std::endl;

std::cout << " -h --help:" << std::endl;
std::cout << " Print out this message" << std::endl << std::endl;
Expand Down
76 changes: 76 additions & 0 deletions saiplayer/SaiPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "sairedis.h"
#include "sairediscommon.h"
#include "VirtualObjectIdManager.h"

#include "meta/sai_serialize.h"
#include "meta/SaiAttributeList.h"
Expand Down Expand Up @@ -40,6 +41,15 @@ SaiPlayer::SaiPlayer(

SWSS_LOG_NOTICE("cmd: %s", cmd->getCommandLineString().c_str());

loadProfileMap();

if (cmd->m_contextConfig.size())
{
SWSS_LOG_NOTICE("using command line contextConfig instead SAI_REDIS_KEY_CONTEXT_CONFIG in profile.ini");

m_profileMap[SAI_REDIS_KEY_CONTEXT_CONFIG] = cmd->m_contextConfig;
}

m_profileIter = m_profileMap.begin();

m_smt.profileGetValue = std::bind(&SaiPlayer::profileGetValue, this, _1, _2);
Expand All @@ -61,6 +71,53 @@ SaiPlayer::~SaiPlayer()
// empty
}

void SaiPlayer::loadProfileMap()
{
SWSS_LOG_ENTER();

if (m_commandLineOptions->m_profileMapFile.size() == 0)
{
SWSS_LOG_NOTICE("profile map file not specified");
return;
}

std::ifstream profile(m_commandLineOptions->m_profileMapFile);

if (!profile.is_open())
{
SWSS_LOG_ERROR("failed to open profile map file: %s: %s",
m_commandLineOptions->m_profileMapFile.c_str(),
strerror(errno));

exit(EXIT_FAILURE);
}

std::string line;

while (getline(profile, line))
{
if (line.size() > 0 && (line[0] == '#' || line[0] == ';'))
{
continue;
}

size_t pos = line.find("=");

if (pos == std::string::npos)
{
SWSS_LOG_WARN("not found '=' in line %s", line.c_str());
continue;
}

std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);

m_profileMap[key] = value;

SWSS_LOG_INFO("insert: %s:%s", key.c_str(), value.c_str());
}
}

void SaiPlayer::onFdbEvent(
_In_ uint32_t count,
_In_ const sai_fdb_event_notification_data_t *data)
Expand Down Expand Up @@ -629,6 +686,8 @@ sai_status_t SaiPlayer::handle_generic(

meta_key.objecttype = object_type;

std::vector<sai_attribute_t> swattr;

switch (api)
{
case SAI_COMMON_API_CREATE:
Expand All @@ -650,6 +709,23 @@ sai_status_t SaiPlayer::handle_generic(
* We are creating switch, in both cases local and redis
* switch id is deterministic and should be the same.
*/

/*
* Since now recording could contain switches from multiple
* contexts, we need to pass extra attribute to point to
* the right context when creating switch.
*/

swattr.resize(attr_count + 1);

memcpy(swattr.data(), attr_list, attr_count * sizeof(sai_attribute_t));

swattr[attr_count].id = SAI_REDIS_SWITCH_ATTR_CONTEXT;
swattr[attr_count].value.u32 = sairedis::VirtualObjectIdManager::getGlobalContext(switch_id);

attr_count++;

attr_list = swattr.data();
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions saiplayer/SaiPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ namespace saiplayer
_Out_ const char** variable,
_Out_ const char** value);

void loadProfileMap();

private: // notification handlers

void onFdbEvent(
Expand Down

0 comments on commit ea432b3

Please sign in to comment.