14
14
#include " linkcache.h"
15
15
#include " portsyncd/linksync.h"
16
16
#include " warm_restart.h"
17
+ #include " shellcmd.h"
17
18
18
19
#include < iostream>
19
20
#include < set>
@@ -24,6 +25,7 @@ using namespace swss;
24
25
#define VLAN_DRV_NAME " bridge"
25
26
#define TEAM_DRV_NAME " team"
26
27
28
+ const string MGMT_PREFIX = " eth" ;
27
29
const string INTFS_PREFIX = " Ethernet" ;
28
30
const string LAG_PREFIX = " PortChannel" ;
29
31
@@ -40,8 +42,56 @@ extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }
40
42
LinkSync::LinkSync (DBConnector *appl_db, DBConnector *state_db) :
41
43
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
42
44
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)
44
47
{
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
+
45
95
if (!WarmStart::isWarmStart ())
46
96
{
47
97
/* See the comments for g_portSet in portsyncd.cpp */
@@ -61,25 +111,22 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
61
111
}
62
112
}
63
113
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++)
72
117
{
73
118
string key = idx_p->if_name ;
119
+
120
+ /* Skip all non-frontpanel ports */
74
121
if (key.compare (0 , INTFS_PREFIX.length (), INTFS_PREFIX))
75
122
{
76
123
continue ;
77
124
}
78
125
79
126
m_ifindexOldNameMap[idx_p->if_index ] = key;
80
127
81
- /* Bring down the existing kernel interfaces */
82
128
string cmd, res;
129
+ /* Bring down the existing kernel interfaces */
83
130
SWSS_LOG_INFO (" Bring down old interface %s(%d)" , key.c_str (), idx_p->if_index );
84
131
cmd = " ip link set " + key + " down" ;
85
132
try
@@ -106,7 +153,8 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
106
153
string key = rtnl_link_get_name (link );
107
154
108
155
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))
110
158
{
111
159
return ;
112
160
}
@@ -133,6 +181,17 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
133
181
nlmsg_type, key.c_str (), admin, oper, addrStr, ifindex, master);
134
182
}
135
183
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
+
136
195
/* teamd instances are dealt in teamsyncd */
137
196
if (type && !strcmp (type, TEAM_DRV_NAME))
138
197
{
0 commit comments