Skip to content

Commit

Permalink
Added SAI dump support to syncd (sonic-net#199)
Browse files Browse the repository at this point in the history
* Added SAI dump support to syncd

* Refactored sai_sdk_dump tool to use SAI directly
  • Loading branch information
AndriiS authored and kcudnik committed Jul 31, 2017
1 parent bfdd383 commit 269f0b7
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ syncd/syncd
syncd/syncd_request_shutdown
saiplayer/saiplayer
saidump/saidump
saisdkdump/saisdkdump
meta/tests

# Temporary files #
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUBDIRS = meta lib vslib syncd saiplayer saidump saidiscovery tests
SUBDIRS = meta lib vslib syncd saiplayer saidump saidiscovery tests saisdkdump
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@ AC_OUTPUT(Makefile
syncd/Makefile
saiplayer/Makefile
saidump/Makefile
saisdkdump/Makefile
saidiscovery/Makefile
tests/Makefile)
1 change: 1 addition & 0 deletions debian/syncd.install
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usr/bin/saidump
usr/bin/saiplayer
usr/bin/saisdkdump
usr/bin/syncd*
syncd/scripts/* usr/bin
19 changes: 19 additions & 0 deletions saisdkdump/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
AM_CPPFLAGS = -I$(top_srcdir)/vslib/inc -I$(top_srcdir)/lib/inc -I$(top_srcdir)/SAI/inc -I$(top_srcdir)/SAI/meta

bin_PROGRAMS = saisdkdump

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
else
DBGFLAGS = -g
endif

if SAIVS
SAILIB=-L$(top_srcdir)/vslib/src/.libs -lsaivs
else
SAILIB=-lsai
endif

saisdkdump_SOURCES = saisdkdump.cpp
saisdkdump_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS)
saisdkdump_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta
159 changes: 159 additions & 0 deletions saisdkdump/saisdkdump.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include <iostream>
#include <sstream>
#include <ctime>
#include <sstream>

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

#include "swss/logger.h"

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

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

void print_usage()
{
std::cerr << "Following SAI dump options can be specified:" << std::endl;
std::cerr << "-------------------------------------------" << std::endl;
std::cerr << "--dump_file -f Full path for dump file" << std::endl;
std::cerr << "--profile -p Full path to SAI profile file [ default is " << sai_profile << " ]" << std::endl;
std::cerr << "--help -h usage" << std::endl;
}

__attribute__((__noreturn__)) void exit_with_sai_failure(const char *msg, sai_status_t status)
{
if (msg)
{
std::cerr << msg << " rc=" << status << std::endl;
}
SWSS_LOG_ERROR("saisdkdump exited with SAI rc: 0x%x, msg: %s .", status, (msg != NULL ? msg : ""));
exit(EXIT_FAILURE);
}

const char* profile_get_value(
_In_ sai_switch_profile_id_t profile_id,
_In_ const char* variable)
{
return sai_profile.c_str();
}

int profile_get_next_value(
_In_ sai_switch_profile_id_t profile_id,
_Out_ const char** variable,
_Out_ const char** value)
{
return -1;
}

service_method_table_t test_services = {
profile_get_value,
profile_get_next_value
};

int main(int argc, char **argv)
{
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE);

SWSS_LOG_ENTER();

static struct option longOptions[] =
{
{ "help", no_argument, 0, 'h' },
{ "dump_file", required_argument, 0, 'f' },
{ "profile", required_argument, 0, 'p' }
};

bool fileSpecified = false;
std::string fileName;
int option_index = 0;
int c = 0;

while((c = getopt_long(argc, argv, "hf:p:", longOptions, &option_index)) != -1)
{
switch (c)
{
case 'f':
if (optarg != NULL)
{
fileName = std::string(optarg);
fileSpecified = true;
}
break;
case 'p':
if (optarg != NULL)
{
sai_profile = std::string(optarg);
}
break;

case 'h':
print_usage();
break;

default:
SWSS_LOG_ERROR("getopt failure");
exit(EXIT_FAILURE);
}
}

if (!fileSpecified)
{
std::ostringstream strStream;
time_t t = time(NULL);
struct tm *now = localtime(&t);
strStream << "/tmp/saisdkdump_" << now->tm_mday << "_" << now->tm_mon + 1 << "_" << now->tm_year + 1900 << "_" << now->tm_hour << "_" << now->tm_min << "_" << now->tm_sec;
fileName = strStream.str();
SWSS_LOG_INFO("The dump file is not specified, generated \"%s\" file name", fileName.c_str());
}

sai_status_t status = sai_api_initialize(0, (service_method_table_t*)&test_services);
if (status != SAI_STATUS_SUCCESS)
{
exit_with_sai_failure("Failed to initialize SAI api", status);
}

sai_switch_api_t* switch_api;
status = sai_api_query(SAI_API_SWITCH, (void**) &switch_api);
if (status != SAI_STATUS_SUCCESS)
{
exit_with_sai_failure("Failed to query switch api", status);
}

sai_object_id_t switch_id;
const uint32_t AttributesCount = 1;
sai_attribute_t attrs[AttributesCount];
attrs[0].id = SAI_SWITCH_ATTR_INIT_SWITCH;
attrs[0].value.booldata = false;

status = switch_api->create_switch(&switch_id, AttributesCount, attrs);
if (status != SAI_STATUS_SUCCESS)
{
exit_with_sai_failure("Failed to create a switch", status);
}

status = sai_dbg_generate_dump(fileName.c_str());
if (status != SAI_STATUS_SUCCESS)
{
exit_with_sai_failure("Failed to generate SAI dump", status);
}

SWSS_LOG_NOTICE("The SAI dump is generated to %s .", fileName.c_str());
std::cout << "The SAI dump is generated to " << fileName << std::endl;

status = switch_api->remove_switch(switch_id);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("remove switch 0x%x failed: 0x%x", switch_id, status);
}

status = sai_api_uninitialize();
if (status != SAI_STATUS_SUCCESS)
{
exit_with_sai_failure("SAI api uninitialize failed", status);
}

return EXIT_SUCCESS;
}

0 comments on commit 269f0b7

Please sign in to comment.