13
13
14
14
#include " linkcache.h"
15
15
#include " portsyncd/linksync.h"
16
+ #include " shellcmd.h"
16
17
17
18
#include < iostream>
18
19
#include < set>
@@ -23,6 +24,7 @@ using namespace swss;
23
24
#define VLAN_DRV_NAME " bridge"
24
25
#define TEAM_DRV_NAME " team"
25
26
27
+ const string MGMT_PREFIX = " eth" ;
26
28
const string INTFS_PREFIX = " Ethernet" ;
27
29
const string LAG_PREFIX = " PortChannel" ;
28
30
@@ -39,8 +41,56 @@ extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }
39
41
LinkSync::LinkSync (DBConnector *appl_db, DBConnector *state_db) :
40
42
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
41
43
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)
43
46
{
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
+
44
94
/* See the comments for g_portSet in portsyncd.cpp */
45
95
for (string port : g_portSet)
46
96
{
@@ -58,25 +108,22 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
58
108
}
59
109
}
60
110
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++)
69
114
{
70
115
string key = idx_p->if_name ;
116
+
117
+ /* Skip all non-frontpanel ports */
71
118
if (key.compare (0 , INTFS_PREFIX.length (), INTFS_PREFIX))
72
119
{
73
120
continue ;
74
121
}
75
122
76
123
m_ifindexOldNameMap[idx_p->if_index ] = key;
77
124
78
- /* Bring down the existing kernel interfaces */
79
125
string cmd, res;
126
+ /* Bring down the existing kernel interfaces */
80
127
SWSS_LOG_INFO (" Bring down old interface %s(%d)" , key.c_str (), idx_p->if_index );
81
128
cmd = " ip link set " + key + " down" ;
82
129
try
@@ -102,7 +149,8 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
102
149
string key = rtnl_link_get_name (link );
103
150
104
151
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))
106
154
{
107
155
return ;
108
156
}
@@ -130,6 +178,17 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
130
178
nlmsg_type, key.c_str (), admin, oper, addrStr, ifindex, master);
131
179
}
132
180
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
+
133
192
/* teamd instances are dealt in teamsyncd */
134
193
if (type && !strcmp (type, TEAM_DRV_NAME))
135
194
{
0 commit comments