Skip to content

Commit bcfb876

Browse files
Merge remote-tracking branch 'origin/syslog_telemetry_shared' into remanava_events
2 parents ee8e79e + 302aba7 commit bcfb876

32 files changed

+1661
-273
lines changed

.azure-pipelines/build-sairedis-template.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646

4747
container:
4848
image: sonicdev-microsoft.azurecr.io:443/${{ parameters.sonic_slave }}:latest
49+
options: "--privileged"
4950

5051
steps:
5152
- checkout: sonic-sairedis
@@ -103,7 +104,10 @@ jobs:
103104
set -ex
104105
rm ../*.deb || true
105106
./autogen.sh
106-
fakeroot dpkg-buildpackage -b -us -uc -Tbinary-syncd-vs -j$(nproc)
107+
DEB_BUILD_OPTIONS=nocheck fakeroot dpkg-buildpackage -b -us -uc -Tbinary-syncd-vs -j$(nproc)
108+
# Add SYS_TIME capability for settimeofday ok in syncd test
109+
sudo setcap "cap_sys_time=eip" syncd/.libs/tests
110+
make check
107111
mv ../*.deb .
108112
displayName: "Compile sonic sairedis"
109113
- script: |

.azure-pipelines/build-template.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
displayName: ${{ parameters.arch }}
3939
timeoutInMinutes: ${{ parameters.timeout }}
4040
variables:
41-
DIFF_COVER_CHECK_THRESHOLD: 50
41+
DIFF_COVER_CHECK_THRESHOLD: 80
4242
${{ if eq(parameters.run_unit_test, true) }}:
4343
DIFF_COVER_ENABLE: 'true'
4444

.azure-pipelines/docker-sonic-vs/Dockerfile

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ ARG docker_container_name
44

55
ADD ["debs", "/debs"]
66

7-
RUN dpkg --purge python-swsscommon
8-
RUN dpkg --purge python3-swsscommon
9-
RUN dpkg --purge swss
10-
RUN dpkg --purge libsairedis
11-
RUN dpkg --purge libswsscommon
12-
RUN dpkg --purge libsaimetadata
13-
RUN dpkg --purge libsaivs
14-
RUN dpkg --purge syncd-vs
7+
RUN dpkg --purge python-swsscommon python3-swsscommon swss libsairedis sonic-db-cli libswsscommon libsaimetadata libsaivs syncd-vs
158

169
RUN dpkg -i /debs/libswsscommon_1.0.0_amd64.deb
1710
RUN dpkg -i /debs/python-swsscommon_1.0.0_amd64.deb
1811
RUN dpkg -i /debs/python3-swsscommon_1.0.0_amd64.deb
12+
RUN dpkg -i /debs/sonic-db-cli_1.0.0_amd64.deb
1913

2014
RUN dpkg -i /debs/libsaimetadata_1.0.0_amd64.deb
2115
RUN dpkg -i /debs/libsairedis_1.0.0_amd64.deb

common/Makefile.am

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ EXTRA_DIST = \
88
producer_state_table_apply_view.lua \
99
table_dump.lua \
1010
redis_multi.lua \
11+
portcounter.lua \
1112
fdb_flush.lua \
1213
fdb_flush.v2.lua
1314

@@ -47,7 +48,6 @@ libswsscommon_la_SOURCES = \
4748
select.cpp \
4849
selectableevent.cpp \
4950
selectabletimer.cpp \
50-
signalhandlerhelper.cpp \
5151
consumertable.cpp \
5252
consumertablebase.cpp \
5353
consumerstatetable.cpp \
@@ -69,6 +69,8 @@ libswsscommon_la_SOURCES = \
6969
subscriberstatetable.cpp \
7070
timestamp.cpp \
7171
warm_restart.cpp \
72+
luatable.cpp \
73+
countertable.cpp \
7274
redisutility.cpp
7375

7476
libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)

common/configdb.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
7676
## Start listen Redis keyspace event. Pass a callback function to `init` to handle initial table data.
7777
self.pubsub = self.get_redis_client(self.db_name).pubsub()
7878
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.get_dbid(self.db_name)))
79-
79+
8080
# Build a cache of data for all subscribed tables that will recieve the initial table data so we dont send duplicate event notifications
8181
init_data = {tbl: self.get_table(tbl) for tbl in self.handlers if init_data_handler or self.fire_init_data[tbl]}
8282

@@ -88,16 +88,16 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
8888
return False
8989
return True
9090

91-
init_callback_data = {tbl: data for tbl, data in init_data.items() if load_data(tbl, data)}
91+
init_callback_data = {tbl: data for tbl, data in init_data.items() if load_data(tbl, data)}
9292

9393
# Pass all initial data that we DID NOT send as updates to handlers through the init callback if provided by caller
9494
if init_data_handler:
9595
init_data_handler(init_callback_data)
9696

97-
while not SignalHandlerHelper.checkSignal(SIGNAL_INT):
98-
item = self.pubsub.listen_message()
97+
while True:
98+
item = self.pubsub.listen_message(interrupt_on_signal=True)
9999
if 'type' not in item:
100-
# When timeout or cancelled, item will not contains 'type'
100+
# When timeout or interrupted, item will not contains 'type'
101101
continue
102102

103103
if item['type'] == 'pmessage':

common/countertable.cpp

+258
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
#include <hiredis/hiredis.h>
2+
#include <system_error>
3+
4+
#include "common/logger.h"
5+
#include "common/redisreply.h"
6+
#include "common/rediscommand.h"
7+
#include "common/redisapi.h"
8+
#include "common/json.hpp"
9+
#include "common/schema.h"
10+
#include "common/countertable.h"
11+
12+
using namespace std;
13+
using namespace swss;
14+
15+
const std::string Counter::defaultLuaScript = "return nil";
16+
17+
/*
18+
* Port counter type
19+
*/
20+
unique_ptr<KeyCache<string>> PortCounter::keyCachePtr = nullptr;
21+
22+
PortCounter::PortCounter(PortCounter::Mode mode)
23+
: m_mode(mode)
24+
{
25+
m_luaScript = loadLuaScript("portcounter.lua");
26+
}
27+
28+
const std::string&
29+
PortCounter::getLuaScript() const
30+
{
31+
return m_luaScript;
32+
}
33+
34+
bool
35+
PortCounter::usingLuaTable(const CounterTable& t, const std::string &name) const
36+
{
37+
// Use LuaTable if and only if it is in UNION mode and has gearbox part.
38+
// That name map exists means the port has gearbox port part in PHY chip.
39+
return m_mode == Mode::UNION &&
40+
t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_line");
41+
}
42+
43+
std::vector<std::string>
44+
PortCounter::getLuaKeys(const CounterTable& t, const std::string &name) const
45+
{
46+
if (m_mode != Mode::UNION)
47+
return {};
48+
49+
KeyCache<string> &cache = keyCacheInstance();
50+
if (cache.enabled())
51+
{
52+
try {
53+
return {cache.at(name), cache.at(name + "_system"), cache.at(name + "_line")};
54+
}
55+
catch (const std::out_of_range&) {
56+
return {};
57+
}
58+
}
59+
60+
auto oidLinesidePtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_line");
61+
auto oidSystemsidePtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_system");
62+
auto oidAsicPtr = t.getCountersDB()->hget(COUNTERS_PORT_NAME_MAP, name);
63+
64+
if (oidAsicPtr && oidSystemsidePtr && oidLinesidePtr)
65+
{
66+
return {*oidAsicPtr, *oidSystemsidePtr, *oidLinesidePtr};
67+
}
68+
69+
return {};
70+
}
71+
72+
Counter::KeyPair
73+
PortCounter::getKey(const CounterTable& t, const std::string &name) const
74+
{
75+
int dbId = COUNTERS_DB;
76+
string portName = name;
77+
if (m_mode != Mode::ASIC && m_mode != Mode::UNION)
78+
{
79+
dbId = GB_COUNTERS_DB;
80+
if (m_mode == Mode::SYSTEMSIDE)
81+
{
82+
portName = name + "_system";
83+
}
84+
else
85+
{
86+
portName = name + "_line";
87+
}
88+
}
89+
90+
KeyCache<string> &cache = keyCacheInstance();
91+
if (cache.enabled())
92+
{
93+
try {
94+
return {dbId, cache.at(portName)};
95+
}
96+
catch (const std::out_of_range&) {
97+
return {-1, ""};
98+
}
99+
}
100+
101+
shared_ptr<std::string> oidPtr = nullptr;
102+
if (m_mode == Mode::ASIC || m_mode == Mode::UNION)
103+
{
104+
oidPtr = t.getCountersDB()->hget(COUNTERS_PORT_NAME_MAP, portName);
105+
}
106+
else
107+
{
108+
oidPtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, portName);
109+
}
110+
111+
if (oidPtr == nullptr)
112+
return {-1, ""};
113+
return {dbId, *oidPtr};
114+
}
115+
116+
KeyCache<string>&
117+
PortCounter::keyCacheInstance(void)
118+
{
119+
if (keyCachePtr == nullptr)
120+
{
121+
keyCachePtr.reset(new KeyCache<string>(PortCounter::cachingKey));
122+
}
123+
return *keyCachePtr;
124+
}
125+
126+
void PortCounter::cachingKey(const CounterTable& t)
127+
{
128+
auto fvs = t.getCountersDB()->hgetall(COUNTERS_PORT_NAME_MAP);
129+
keyCachePtr->insert(fvs.begin(), fvs.end());
130+
131+
fvs = t.getGbcountersDB()->hgetall(COUNTERS_PORT_NAME_MAP);
132+
keyCachePtr->insert(fvs.begin(), fvs.end());
133+
}
134+
135+
/*
136+
* MACSEC counter type
137+
*/
138+
unique_ptr<KeyCache<Counter::KeyPair>> MacsecCounter::keyCachePtr = nullptr;
139+
140+
Counter::KeyPair
141+
MacsecCounter::getKey(const CounterTable& t, const std::string &name) const
142+
{
143+
KeyCache<Counter::KeyPair> &cache = keyCacheInstance();
144+
if (cache.enabled())
145+
{
146+
try {
147+
return cache.at(name);
148+
}
149+
catch (const std::out_of_range&) {
150+
return {-1, ""};
151+
}
152+
}
153+
154+
int dbId = COUNTERS_DB;
155+
auto oidPtr = t.getCountersDB()->hget(COUNTERS_MACSEC_NAME_MAP, name);
156+
157+
if (oidPtr == nullptr)
158+
{
159+
dbId = GB_COUNTERS_DB;
160+
oidPtr = t.getGbcountersDB()->hget(COUNTERS_MACSEC_NAME_MAP, name);
161+
}
162+
163+
if (oidPtr == nullptr)
164+
return {-1, ""};
165+
return {dbId, *oidPtr};
166+
}
167+
168+
KeyCache<Counter::KeyPair>&
169+
MacsecCounter::keyCacheInstance(void)
170+
{
171+
if (keyCachePtr == nullptr)
172+
{
173+
keyCachePtr.reset(new KeyCache<Counter::KeyPair>(MacsecCounter::cachingKey));
174+
}
175+
return *keyCachePtr;
176+
}
177+
178+
void MacsecCounter::cachingKey(const CounterTable& t)
179+
{
180+
auto fvs = t.getCountersDB()->hgetall(COUNTERS_MACSEC_NAME_MAP);
181+
for (auto fv: fvs)
182+
{
183+
keyCachePtr->insert(fv.first, Counter::KeyPair(COUNTERS_DB, fv.second));
184+
}
185+
186+
fvs = t.getGbcountersDB()->hgetall(COUNTERS_MACSEC_NAME_MAP);
187+
for (auto fv: fvs)
188+
{
189+
keyCachePtr->insert(fv.first, Counter::KeyPair(GB_COUNTERS_DB, fv.second));
190+
}
191+
}
192+
193+
CounterTable::CounterTable(const DBConnector *db, const string &tableName)
194+
: TableBase(tableName, SonicDBConfig::getSeparator(db))
195+
, m_countersDB(db->newConnector(0))
196+
{
197+
unique_ptr<DBConnector> ptr(new DBConnector(GB_COUNTERS_DB, *m_countersDB));
198+
m_gbcountersDB = std::move(ptr);
199+
}
200+
201+
bool CounterTable::get(const Counter &counter, const std::string &name, std::vector<FieldValueTuple> &values)
202+
{
203+
if (counter.usingLuaTable(*this, name))
204+
{
205+
LuaTable luaTable(m_countersDB.get(), getTableName(), counter.getLuaScript());
206+
return luaTable.get(counter.getLuaKeys(*this, name), values);
207+
}
208+
else
209+
{
210+
auto keyPair = counter.getKey(*this, name);
211+
if (keyPair.second.empty())
212+
{
213+
values.clear();
214+
return false;
215+
}
216+
217+
if (keyPair.first == GB_COUNTERS_DB)
218+
{
219+
Table table(m_gbcountersDB.get(), getTableName());
220+
return table.get(keyPair.second, values);
221+
}
222+
else
223+
{
224+
Table table(m_countersDB.get(), getTableName());
225+
return table.get(keyPair.second, values);
226+
}
227+
}
228+
}
229+
230+
231+
bool CounterTable::hget(const Counter &counter, const std::string &name, const std::string &field, std::string &value)
232+
{
233+
if (counter.usingLuaTable(*this, name))
234+
{
235+
LuaTable luaTable(m_countersDB.get(), getTableName(), counter.getLuaScript());
236+
return luaTable.hget(counter.getLuaKeys(*this, name), field, value);
237+
}
238+
else
239+
{
240+
auto keyPair = counter.getKey(*this, name);
241+
if (keyPair.second.empty())
242+
{
243+
value.clear();
244+
return false;
245+
}
246+
247+
if (keyPair.first == GB_COUNTERS_DB)
248+
{
249+
Table table(getGbcountersDB().get(), getTableName());
250+
return table.hget(keyPair.second, field, value);
251+
}
252+
else
253+
{
254+
Table table(m_countersDB.get(), getTableName());
255+
return table.hget(keyPair.second, field, value);
256+
}
257+
}
258+
}

0 commit comments

Comments
 (0)