Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ unittest/vslib/testslibsaivs
unittest/proxylib/tests
unittest/proxylib/testslibsaiproxy
unittest/saidump/tests
unittest/saisdkdump/tests
vslib/tests
stub/tests
unittest/stub/stub/testslibsaistub
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ AC_OUTPUT(Makefile
unittest/syncd/Makefile
unittest/proxylib/Makefile
unittest/saidump/Makefile
unittest/saisdkdump/Makefile
pyext/Makefile
pyext/py2/Makefile
pyext/py3/Makefile)
10 changes: 8 additions & 2 deletions saisdkdump/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AM_CXXFLAGS = $(SAIINC)
AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/saisdkdump

bin_PROGRAMS = saisdkdump

Expand All @@ -8,7 +8,13 @@ else
SAILIB=-lsai
endif

noinst_LIBRARIES = libsaisdkdump_profile.a

libsaisdkdump_profile_a_SOURCES = ProfileMap.cpp
libsaisdkdump_profile_a_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
libsaisdkdump_profile_a_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)

saisdkdump_SOURCES = saisdkdump.cpp
saisdkdump_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
saisdkdump_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)
saisdkdump_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS) $(EXTRA_LIBSAI_LDFLAGS)
saisdkdump_LDADD = libsaisdkdump_profile.a -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS) $(EXTRA_LIBSAI_LDFLAGS)
132 changes: 132 additions & 0 deletions saisdkdump/ProfileMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "ProfileMap.h"

#include <cerrno>
#include <cstring>
#include <fstream>

#include "swss/logger.h"

ProfileMap::ProfileMap()
: m_iter(m_map.begin())
{
}

bool ProfileMap::loadFromFile(const std::string& profileMapFile)
{
SWSS_LOG_ENTER();

m_map.clear();
m_iter = m_map.begin();

if (profileMapFile.empty())
{
return true;
}

std::ifstream profile(profileMapFile);

if (!profile.is_open())
{
SWSS_LOG_ERROR("failed to open profile map file: '%s' : %s",
profileMapFile.c_str(), strerror(errno));

return false;
}

std::string line;

while (getline(profile, line))
{
if (!line.empty() && line.back() == '\r')
{
line.pop_back();
}

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_map[key] = value;

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

return true;
}

void ProfileMap::clear()
{
SWSS_LOG_ENTER();

m_map.clear();
m_iter = m_map.begin();
}

const char* ProfileMap::getValue(const char* variable) const
{
SWSS_LOG_ENTER();

if (variable == NULL)
{
SWSS_LOG_WARN("variable is null");
return NULL;
}

auto it = m_map.find(variable);

if (it == m_map.end())
{
SWSS_LOG_NOTICE("%s: NULL", variable);
return NULL;
}

SWSS_LOG_NOTICE("%s: %s", variable, it->second.c_str());

return it->second.c_str();
}

int ProfileMap::getNextValue(const char** variable, const char** value)
{
SWSS_LOG_ENTER();

if (value == NULL)
{
SWSS_LOG_INFO("resetting profile map iterator");

m_iter = m_map.begin();
return 0;
}

if (variable == NULL)
{
SWSS_LOG_WARN("variable is null");
return -1;
}

if (m_iter == m_map.end())
{
SWSS_LOG_INFO("iterator reached end");
return -1;
}

*variable = m_iter->first.c_str();
*value = m_iter->second.c_str();

SWSS_LOG_INFO("key: %s:%s", *variable, *value);

m_iter++;

return 0;
}
22 changes: 22 additions & 0 deletions saisdkdump/ProfileMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <map>
#include <string>

class ProfileMap
{
public:
ProfileMap();

bool loadFromFile(const std::string& profileMapFile);

void clear();

const char* getValue(const char* variable) const;

int getNextValue(const char** variable, const char** value);

private:
std::map<std::string, std::string> m_map;
mutable std::map<std::string, std::string>::iterator m_iter;
};
15 changes: 12 additions & 3 deletions saisdkdump/saisdkdump.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <iostream>
#include <sstream>
#include <ctime>
#include <sstream>

#include <unistd.h>
#include <getopt.h>
#include <cstring>

#include "ProfileMap.h"
#include "swss/logger.h"

extern "C" {
Expand All @@ -16,6 +17,8 @@ extern "C" {

std::string sai_profile = "/tmp/sai.profile";

static ProfileMap g_profileMap;

void print_usage()
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -46,7 +49,7 @@ const char* profile_get_value(
{
SWSS_LOG_ENTER();

return sai_profile.c_str();
return g_profileMap.getValue(variable);
}

int profile_get_next_value(
Expand All @@ -55,7 +58,8 @@ int profile_get_next_value(
_Out_ const char** value)
{
SWSS_LOG_ENTER();
return -1;

return g_profileMap.getNextValue(variable, value);
}

sai_service_method_table_t test_services = {
Expand Down Expand Up @@ -120,6 +124,11 @@ int main(int argc, char **argv)
SWSS_LOG_INFO("The dump file is not specified, generated \"%s\" file name", fileName.c_str());
}

if (!g_profileMap.loadFromFile(sai_profile))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Possible regression for the default (no--p) case. sai_profile defaults to /tmp/sai.profile (not empty), and -p is optional. loadFromFile returns false when the file can't be opened, and here that maps to exit(EXIT_FAILURE). So if a user runs saisdkdump without -p and /tmp/sai.profile doesn't exist, the tool now hard-fails — whereas before this PR it ran fine (the callbacks just returned the path / -1 and SAI init proceeded). For a general techsupport/diagnostic tool, failing hard when no profile is present at the default path is a meaningful change.

Note the asymmetry that suggests this is unintended: loadFromFile("") returns true and loads nothing (a deliberate "no profile is fine" path), but the non-empty default defeats it, so that graceful branch is never taken unless the user explicitly passes -p "".

Suggest mirroring the existing -f handling: track whether -p was explicitly given (like fileSpecified) and only hard-fail on a missing file when the user explicitly requested one; tolerate absence of the default path (warn + empty map, continue). That keeps prior behavior for the common no--p case while still catching a genuine "pointed at a profile that doesn't exist" mistake.

If the bluefield techsupport flow always passes -p and the default-path fail-hard is acceptable/intended, feel free to confirm and disregard — just flagging since saisdkdump is invoked as a general diagnostic tool.

{
exit(EXIT_FAILURE);
}

sai_status_t status = sai_api_initialize(0, (sai_service_method_table_t*)&test_services);
if (status != SAI_STATUS_SUCCESS)
{
Expand Down
2 changes: 1 addition & 1 deletion unittest/Makefile.am
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUBDIRS = meta lib vslib syncd proxylib saidump
SUBDIRS = meta lib vslib syncd proxylib saidump saisdkdump
13 changes: 13 additions & 0 deletions unittest/saisdkdump/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/saisdkdump

bin_PROGRAMS = tests

LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main

tests_SOURCES = main.cpp TestProfileMap.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/saisdkdump/libsaisdkdump_profile.a \
-lswsscommon -lpthread $(CODE_COVERAGE_LIBS)

TESTS = tests
Loading
Loading