Skip to content

Commit 4f5436f

Browse files
Shuotian Chengqiluo-msft
Shuotian Cheng
authored andcommitted
[portsyncd]: Store eth0 oper status into state DB (#630)
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.
1 parent 6ea4ea3 commit 4f5436f

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)
1+
INCLUDES = -I $(top_srcdir) -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
11+
portsyncd_SOURCES = portsyncd.cpp linksync.cpp $(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
@@ -13,6 +13,7 @@
1313

1414
#include "linkcache.h"
1515
#include "portsyncd/linksync.h"
16+
#include "shellcmd.h"
1617

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

27+
const string MGMT_PREFIX = "eth";
2628
const string INTFS_PREFIX = "Ethernet";
2729
const string LAG_PREFIX = "PortChannel";
2830

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

61-
struct if_nameindex *if_ni, *idx_p;
62-
if_ni = if_nameindex();
63-
if (if_ni == NULL)
64-
{
65-
return;
66-
}
67-
68-
for (idx_p = if_ni; ! (idx_p->if_index == 0 && idx_p->if_name == NULL); idx_p++)
111+
for (idx_p = if_ni;
112+
idx_p != NULL && idx_p->if_index != 0 && idx_p->if_name != NULL;
113+
idx_p++)
69114
{
70115
string key = idx_p->if_name;
116+
117+
/* Skip all non-frontpanel ports */
71118
if (key.compare(0, INTFS_PREFIX.length(), INTFS_PREFIX))
72119
{
73120
continue;
74121
}
75122

76123
m_ifindexOldNameMap[idx_p->if_index] = key;
77124

78-
/* Bring down the existing kernel interfaces */
79125
string cmd, res;
126+
/* Bring down the existing kernel interfaces */
80127
SWSS_LOG_INFO("Bring down old interface %s(%d)", key.c_str(), idx_p->if_index);
81128
cmd = "ip link set " + key + " down";
82129
try
@@ -102,7 +149,8 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
102149
string key = rtnl_link_get_name(link);
103150

104151
if (key.compare(0, INTFS_PREFIX.length(), INTFS_PREFIX) &&
105-
key.compare(0, LAG_PREFIX.length(), LAG_PREFIX))
152+
key.compare(0, LAG_PREFIX.length(), LAG_PREFIX) &&
153+
key.compare(0, MGMT_PREFIX.length(), MGMT_PREFIX))
106154
{
107155
return;
108156
}
@@ -130,6 +178,17 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
130178
nlmsg_type, key.c_str(), admin, oper, addrStr, ifindex, master);
131179
}
132180

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

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)