Skip to content

Commit 118b3f0

Browse files
prsunnylguohan
authored andcommitted
Populate existing interface cache, bring down before configDone (sonic-net#491)
* Populate existing interface cache, bring down before configDone * simplify the logic and add logging Signed-off-by: Guohan Lu <[email protected]> * add logging for publish event to state db Signed-off-by: Guohan Lu <[email protected]>
1 parent e34104e commit 118b3f0

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

portsyncd/linksync.cpp

+47-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "dbconnector.h"
1010
#include "producerstatetable.h"
1111
#include "tokenize.h"
12+
#include "exec.h"
1213

1314
#include "linkcache.h"
1415
#include "portsyncd/linksync.h"
@@ -28,6 +29,13 @@ const string LAG_PREFIX = "PortChannel";
2829
extern set<string> g_portSet;
2930
extern bool g_init;
3031

32+
struct if_nameindex
33+
{
34+
unsigned int if_index;
35+
char *if_name;
36+
};
37+
extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }
38+
3139
LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
3240
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
3341
m_portTable(appl_db, APP_PORT_TABLE_NAME),
@@ -49,6 +57,38 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
4957
}
5058
}
5159
}
60+
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++)
69+
{
70+
string key = idx_p->if_name;
71+
if (key.compare(0, INTFS_PREFIX.length(), INTFS_PREFIX))
72+
{
73+
continue;
74+
}
75+
76+
m_ifindexOldNameMap[idx_p->if_index] = key;
77+
78+
/* Bring down the existing kernel interfaces */
79+
string cmd, res;
80+
SWSS_LOG_INFO("Bring down old interface %s(%d)", key.c_str(), idx_p->if_index);
81+
cmd = "ip link set " + key + " down";
82+
try
83+
{
84+
swss::exec(cmd, res);
85+
}
86+
catch (...)
87+
{
88+
/* Ignore error in this flow ; */
89+
SWSS_LOG_WARN("Failed to bring down old interface %s(%d)", key.c_str(), idx_p->if_index);
90+
}
91+
}
5292
}
5393

5494
void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
@@ -99,29 +139,15 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
99139
/* In the event of swss restart, it is possible to get netlink messages during bridge
100140
* delete, interface delete etc which are part of cleanup. These netlink messages for
101141
* the front-panel interface must not be published or it will update the statedb with
102-
* old interface info and result in subsequent failures. A new interface creation shall
103-
* not have master or admin status iff_up. So if the first netlink message comes with these
104-
* values set, it is considered to be happening during a cleanup process.
105-
* Fix to ignore this and any further messages for this ifindex
142+
* old interface info and result in subsequent failures. Ingore all netlink messages
143+
* coming from old interfaces.
106144
*/
107145

108-
static std::map<unsigned int, std::string> m_ifindexOldNameMap;
109-
if (m_ifindexNameMap.find(ifindex) == m_ifindexNameMap.end())
146+
if (m_ifindexOldNameMap.find(ifindex) != m_ifindexOldNameMap.end())
110147
{
111-
if (master)
112-
{
113-
m_ifindexOldNameMap[ifindex] = key;
114-
SWSS_LOG_INFO("nlmsg type:%d Ignoring for %d, master %d", nlmsg_type, ifindex, master);
115-
return;
116-
}
117-
else if (m_ifindexOldNameMap.find(ifindex) != m_ifindexOldNameMap.end())
118-
{
119-
if (m_ifindexOldNameMap[ifindex] == key)
120-
{
121-
SWSS_LOG_INFO("nlmsg type:%d Ignoring message for old interface %d", nlmsg_type, ifindex);
122-
return;
123-
}
124-
}
148+
SWSS_LOG_INFO("nlmsg type:%d Ignoring message for old interface %s(%d)",
149+
nlmsg_type, key.c_str(), ifindex);
150+
return;
125151
}
126152

127153
/* Insert or update the ifindex to key map */
@@ -153,6 +179,7 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
153179
vector<FieldValueTuple> vector;
154180
vector.push_back(tuple);
155181
m_statePortTable.set(key, vector);
182+
SWSS_LOG_INFO("Publish %s(ok) to state db", key.c_str());
156183
}
157184

158185
m_portTableProducer.set(key, fvVector);

portsyncd/linksync.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class LinkSync : public NetMsg
2323
Table m_portTable, m_statePortTable;
2424

2525
std::map<unsigned int, std::string> m_ifindexNameMap;
26+
std::map<unsigned int, std::string> m_ifindexOldNameMap;
2627
};
2728

2829
}

0 commit comments

Comments
 (0)