Skip to content

Commit 269f0b7

Browse files
AndriiSkcudnik
AndriiS
authored andcommitted
Added SAI dump support to syncd (sonic-net#199)
* Added SAI dump support to syncd * Refactored sai_sdk_dump tool to use SAI directly
1 parent bfdd383 commit 269f0b7

File tree

6 files changed

+182
-1
lines changed

6 files changed

+182
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ syncd/syncd
6767
syncd/syncd_request_shutdown
6868
saiplayer/saiplayer
6969
saidump/saidump
70+
saisdkdump/saisdkdump
7071
meta/tests
7172

7273
# Temporary files #

Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SUBDIRS = meta lib vslib syncd saiplayer saidump saidiscovery tests
1+
SUBDIRS = meta lib vslib syncd saiplayer saidump saidiscovery tests saisdkdump

configure.ac

+1
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ AC_OUTPUT(Makefile
9696
syncd/Makefile
9797
saiplayer/Makefile
9898
saidump/Makefile
99+
saisdkdump/Makefile
99100
saidiscovery/Makefile
100101
tests/Makefile)

debian/syncd.install

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
usr/bin/saidump
22
usr/bin/saiplayer
3+
usr/bin/saisdkdump
34
usr/bin/syncd*
45
syncd/scripts/* usr/bin

saisdkdump/Makefile.am

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
AM_CPPFLAGS = -I$(top_srcdir)/vslib/inc -I$(top_srcdir)/lib/inc -I$(top_srcdir)/SAI/inc -I$(top_srcdir)/SAI/meta
2+
3+
bin_PROGRAMS = saisdkdump
4+
5+
if DEBUG
6+
DBGFLAGS = -ggdb -DDEBUG
7+
else
8+
DBGFLAGS = -g
9+
endif
10+
11+
if SAIVS
12+
SAILIB=-L$(top_srcdir)/vslib/src/.libs -lsaivs
13+
else
14+
SAILIB=-lsai
15+
endif
16+
17+
saisdkdump_SOURCES = saisdkdump.cpp
18+
saisdkdump_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS)
19+
saisdkdump_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta

saisdkdump/saisdkdump.cpp

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <ctime>
4+
#include <sstream>
5+
6+
#include <unistd.h>
7+
#include <getopt.h>
8+
9+
#include "swss/logger.h"
10+
11+
extern "C" {
12+
#include <sai.h>
13+
}
14+
15+
std::string sai_profile = "/tmp/sai.profile";
16+
17+
void print_usage()
18+
{
19+
std::cerr << "Following SAI dump options can be specified:" << std::endl;
20+
std::cerr << "-------------------------------------------" << std::endl;
21+
std::cerr << "--dump_file -f Full path for dump file" << std::endl;
22+
std::cerr << "--profile -p Full path to SAI profile file [ default is " << sai_profile << " ]" << std::endl;
23+
std::cerr << "--help -h usage" << std::endl;
24+
}
25+
26+
__attribute__((__noreturn__)) void exit_with_sai_failure(const char *msg, sai_status_t status)
27+
{
28+
if (msg)
29+
{
30+
std::cerr << msg << " rc=" << status << std::endl;
31+
}
32+
SWSS_LOG_ERROR("saisdkdump exited with SAI rc: 0x%x, msg: %s .", status, (msg != NULL ? msg : ""));
33+
exit(EXIT_FAILURE);
34+
}
35+
36+
const char* profile_get_value(
37+
_In_ sai_switch_profile_id_t profile_id,
38+
_In_ const char* variable)
39+
{
40+
return sai_profile.c_str();
41+
}
42+
43+
int profile_get_next_value(
44+
_In_ sai_switch_profile_id_t profile_id,
45+
_Out_ const char** variable,
46+
_Out_ const char** value)
47+
{
48+
return -1;
49+
}
50+
51+
service_method_table_t test_services = {
52+
profile_get_value,
53+
profile_get_next_value
54+
};
55+
56+
int main(int argc, char **argv)
57+
{
58+
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE);
59+
60+
SWSS_LOG_ENTER();
61+
62+
static struct option longOptions[] =
63+
{
64+
{ "help", no_argument, 0, 'h' },
65+
{ "dump_file", required_argument, 0, 'f' },
66+
{ "profile", required_argument, 0, 'p' }
67+
};
68+
69+
bool fileSpecified = false;
70+
std::string fileName;
71+
int option_index = 0;
72+
int c = 0;
73+
74+
while((c = getopt_long(argc, argv, "hf:p:", longOptions, &option_index)) != -1)
75+
{
76+
switch (c)
77+
{
78+
case 'f':
79+
if (optarg != NULL)
80+
{
81+
fileName = std::string(optarg);
82+
fileSpecified = true;
83+
}
84+
break;
85+
case 'p':
86+
if (optarg != NULL)
87+
{
88+
sai_profile = std::string(optarg);
89+
}
90+
break;
91+
92+
case 'h':
93+
print_usage();
94+
break;
95+
96+
default:
97+
SWSS_LOG_ERROR("getopt failure");
98+
exit(EXIT_FAILURE);
99+
}
100+
}
101+
102+
if (!fileSpecified)
103+
{
104+
std::ostringstream strStream;
105+
time_t t = time(NULL);
106+
struct tm *now = localtime(&t);
107+
strStream << "/tmp/saisdkdump_" << now->tm_mday << "_" << now->tm_mon + 1 << "_" << now->tm_year + 1900 << "_" << now->tm_hour << "_" << now->tm_min << "_" << now->tm_sec;
108+
fileName = strStream.str();
109+
SWSS_LOG_INFO("The dump file is not specified, generated \"%s\" file name", fileName.c_str());
110+
}
111+
112+
sai_status_t status = sai_api_initialize(0, (service_method_table_t*)&test_services);
113+
if (status != SAI_STATUS_SUCCESS)
114+
{
115+
exit_with_sai_failure("Failed to initialize SAI api", status);
116+
}
117+
118+
sai_switch_api_t* switch_api;
119+
status = sai_api_query(SAI_API_SWITCH, (void**) &switch_api);
120+
if (status != SAI_STATUS_SUCCESS)
121+
{
122+
exit_with_sai_failure("Failed to query switch api", status);
123+
}
124+
125+
sai_object_id_t switch_id;
126+
const uint32_t AttributesCount = 1;
127+
sai_attribute_t attrs[AttributesCount];
128+
attrs[0].id = SAI_SWITCH_ATTR_INIT_SWITCH;
129+
attrs[0].value.booldata = false;
130+
131+
status = switch_api->create_switch(&switch_id, AttributesCount, attrs);
132+
if (status != SAI_STATUS_SUCCESS)
133+
{
134+
exit_with_sai_failure("Failed to create a switch", status);
135+
}
136+
137+
status = sai_dbg_generate_dump(fileName.c_str());
138+
if (status != SAI_STATUS_SUCCESS)
139+
{
140+
exit_with_sai_failure("Failed to generate SAI dump", status);
141+
}
142+
143+
SWSS_LOG_NOTICE("The SAI dump is generated to %s .", fileName.c_str());
144+
std::cout << "The SAI dump is generated to " << fileName << std::endl;
145+
146+
status = switch_api->remove_switch(switch_id);
147+
if (status != SAI_STATUS_SUCCESS)
148+
{
149+
SWSS_LOG_ERROR("remove switch 0x%x failed: 0x%x", switch_id, status);
150+
}
151+
152+
status = sai_api_uninitialize();
153+
if (status != SAI_STATUS_SUCCESS)
154+
{
155+
exit_with_sai_failure("SAI api uninitialize failed", status);
156+
}
157+
158+
return EXIT_SUCCESS;
159+
}

0 commit comments

Comments
 (0)