Skip to content

Commit 8a3a897

Browse files
author
Shu0T1an ChenG
committed
[portsyncd]: Store eth0 oper status into state DB
portsyncd listens to the netlink messages. In order for SNMP to get the current oper status of the managemnt interface, portsyncd will store eth0 oper status explicitly into the state database. Signed-off-by: Shu0T1an ChenG <[email protected]>
1 parent 65b015b commit 8a3a897

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

portsyncd/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart
1+
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr
22

33
bin_PROGRAMS = portsyncd
44

@@ -8,7 +8,7 @@ else
88
DBGFLAGS = -g
99
endif
1010

11-
portsyncd_SOURCES = portsyncd.cpp linksync.cpp $(top_srcdir)/warmrestart/warm_restart.cpp $(top_srcdir)/warmrestart/warm_restart.h
11+
portsyncd_SOURCES = portsyncd.cpp linksync.cpp $(top_srcdir)/warmrestart/warm_restart.cpp $(top_srcdir)/warmrestart/warm_restart.h $(top_srcdir)/cfgmgr/shellcmd.h
1212

1313
portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
1414
portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)

portsyncd/linksync.cpp

+70-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "linkcache.h"
1515
#include "portsyncd/linksync.h"
1616
#include "warm_restart.h"
17+
#include "shellcmd.h"
1718

1819
#include <iostream>
1920
#include <set>
@@ -24,6 +25,7 @@ using namespace swss;
2425
#define VLAN_DRV_NAME "bridge"
2526
#define TEAM_DRV_NAME "team"
2627

28+
const string MGMT_PREFIX = "eth";
2729
const string INTFS_PREFIX = "Ethernet";
2830
const string LAG_PREFIX = "PortChannel";
2931

@@ -40,8 +42,56 @@ extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }
4042
LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
4143
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
4244
m_portTable(appl_db, APP_PORT_TABLE_NAME),
43-
m_statePortTable(state_db, STATE_PORT_TABLE_NAME)
45+
m_statePortTable(state_db, STATE_PORT_TABLE_NAME),
46+
m_stateMgmtPortTable(state_db, STATE_MGMT_PORT_TABLE_NAME)
4447
{
48+
struct if_nameindex *if_ni, *idx_p;
49+
if_ni = if_nameindex();
50+
51+
for (idx_p = if_ni;
52+
idx_p != NULL && idx_p->if_index != 0 && idx_p->if_name != NULL;
53+
idx_p++)
54+
{
55+
string key = idx_p->if_name;
56+
57+
/* Explicitly store management ports oper status into the state database.
58+
* This piece of information is used by SNMP. */
59+
if (!key.compare(0, MGMT_PREFIX.length(), MGMT_PREFIX))
60+
{
61+
string cmd, res;
62+
cmd = "cat /sys/class/net/" + key + "/operstate";
63+
try
64+
{
65+
EXEC_WITH_ERROR_THROW(cmd, res);
66+
}
67+
catch (...)
68+
{
69+
SWSS_LOG_WARN("Failed to get %s oper status", key.c_str());
70+
continue;
71+
}
72+
73+
/* Remove the trailing newline */
74+
if (res.length() >= 1 && res.at(res.length() - 1) == '\n')
75+
{
76+
res.erase(res.length() - 1);
77+
/* The value of operstate will be either up or down */
78+
if (res != "up" && res != "down")
79+
{
80+
SWSS_LOG_WARN("Unknown %s oper status %s",
81+
key.c_str(), res.c_str());
82+
}
83+
FieldValueTuple fv("oper_status", res);
84+
vector<FieldValueTuple> fvs;
85+
fvs.push_back(fv);
86+
87+
m_stateMgmtPortTable.set(key, fvs);
88+
SWSS_LOG_INFO("Store %s oper status %s to state DB",
89+
key.c_str(), res.c_str());
90+
}
91+
continue;
92+
}
93+
}
94+
4595
if (!WarmStart::isWarmStart())
4696
{
4797
/* See the comments for g_portSet in portsyncd.cpp */
@@ -61,25 +111,22 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
61111
}
62112
}
63113

64-
struct if_nameindex *if_ni, *idx_p;
65-
if_ni = if_nameindex();
66-
if (if_ni == NULL)
67-
{
68-
return;
69-
}
70-
71-
for (idx_p = if_ni; ! (idx_p->if_index == 0 && idx_p->if_name == NULL); idx_p++)
114+
for (idx_p = if_ni;
115+
idx_p != NULL && idx_p->if_index != 0 && idx_p->if_name != NULL;
116+
idx_p++)
72117
{
73118
string key = idx_p->if_name;
119+
120+
/* Skip all non-frontpanel ports */
74121
if (key.compare(0, INTFS_PREFIX.length(), INTFS_PREFIX))
75122
{
76123
continue;
77124
}
78125

79126
m_ifindexOldNameMap[idx_p->if_index] = key;
80127

81-
/* Bring down the existing kernel interfaces */
82128
string cmd, res;
129+
/* Bring down the existing kernel interfaces */
83130
SWSS_LOG_INFO("Bring down old interface %s(%d)", key.c_str(), idx_p->if_index);
84131
cmd = "ip link set " + key + " down";
85132
try
@@ -106,7 +153,8 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
106153
string key = rtnl_link_get_name(link);
107154

108155
if (key.compare(0, INTFS_PREFIX.length(), INTFS_PREFIX) &&
109-
key.compare(0, LAG_PREFIX.length(), LAG_PREFIX))
156+
key.compare(0, LAG_PREFIX.length(), LAG_PREFIX) &&
157+
key.compare(0, MGMT_PREFIX.length(), MGMT_PREFIX))
110158
{
111159
return;
112160
}
@@ -133,6 +181,17 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
133181
nlmsg_type, key.c_str(), admin, oper, addrStr, ifindex, master);
134182
}
135183

184+
if (!key.compare(0, MGMT_PREFIX.length(), MGMT_PREFIX))
185+
{
186+
FieldValueTuple fv("oper_status", oper ? "up" : "down");
187+
vector<FieldValueTuple> fvs;
188+
fvs.push_back(fv);
189+
m_stateMgmtPortTable.set(key, fvs);
190+
SWSS_LOG_INFO("Store %s oper status %s to state DB",
191+
key.c_str(), oper ? "up" : "down");
192+
return;
193+
}
194+
136195
/* teamd instances are dealt in teamsyncd */
137196
if (type && !strcmp(type, TEAM_DRV_NAME))
138197
{

portsyncd/linksync.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LinkSync : public NetMsg
2020

2121
private:
2222
ProducerStateTable m_portTableProducer;
23-
Table m_portTable, m_statePortTable;
23+
Table m_portTable, m_statePortTable, m_stateMgmtPortTable;
2424

2525
std::map<unsigned int, std::string> m_ifindexNameMap;
2626
std::map<unsigned int, std::string> m_ifindexOldNameMap;

0 commit comments

Comments
 (0)