Skip to content

Commit 5e7e42c

Browse files
[portsorch] Fix HOSTIF oper status configuration on boot (sonic-net#3783)
* [portsorch] Fix using uninitialized m_oper_status Fixes sonic-net/sonic-buildimage#23347 What I did Fix an issue where m_oper_status is used uninitialized. Why I did it To fix hostif set down after warm-boot. How I verified it
1 parent 71219b0 commit 5e7e42c

2 files changed

Lines changed: 69 additions & 12 deletions

File tree

orchagent/portsorch.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6183,18 +6183,6 @@ bool PortsOrch::initializePort(Port &port)
61836183
initializeSchedulerGroups(port);
61846184
}
61856185

6186-
/*
6187-
* always initialize Port SAI_HOSTIF_ATTR_OPER_STATUS based on oper_status value in appDB.
6188-
*/
6189-
bool isUp = port.m_oper_status == SAI_PORT_OPER_STATUS_UP;
6190-
6191-
/* Create host interface */
6192-
if (!addHostIntfs(port, port.m_alias, port.m_hif_id, isUp))
6193-
{
6194-
SWSS_LOG_ERROR("Failed to create host interface for port %s", port.m_alias.c_str());
6195-
return false;
6196-
}
6197-
61986186
/* Check warm start states */
61996187
vector<FieldValueTuple> tuples;
62006188
bool exist = m_portTable->get(port.m_alias, tuples);
@@ -6249,6 +6237,18 @@ bool PortsOrch::initializePort(Port &port)
62496237
}
62506238
}
62516239

6240+
/*
6241+
* always initialize Port SAI_HOSTIF_ATTR_OPER_STATUS based on oper_status value in appDB.
6242+
*/
6243+
bool isUp = port.m_oper_status == SAI_PORT_OPER_STATUS_UP;
6244+
6245+
/* Create host interface */
6246+
if (!addHostIntfs(port, port.m_alias, port.m_hif_id, isUp))
6247+
{
6248+
SWSS_LOG_ERROR("Failed to create host interface for port %s", port.m_alias.c_str());
6249+
return false;
6250+
}
6251+
62526252
/* initialize port admin status */
62536253
if (!getPortAdminStatus(port.m_port_id, port.m_admin_state_up))
62546254
{

tests/mock_tests/portsorch_ut.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,7 @@ namespace portsorch_test
27212721
for (const auto &it : ports)
27222722
{
27232723
portTable.set(it.first, it.second);
2724+
portTable.set(it.first, {{"oper_status", "up"}});
27242725
transceieverInfoTable.set(it.first, {});
27252726
}
27262727

@@ -2788,6 +2789,62 @@ namespace portsorch_test
27882789
ASSERT_EQ(status, SAI_STATUS_SUCCESS);
27892790
ASSERT_TRUE(attr.value.booldata);
27902791
}
2792+
2793+
// Verify host if configuration
2794+
for (const auto& iter: ports)
2795+
{
2796+
const auto& portName = iter.first;
2797+
2798+
Port port;
2799+
gPortsOrch->getPort(portName, port);
2800+
2801+
ASSERT_TRUE(port.m_oper_status == SAI_PORT_OPER_STATUS_UP);
2802+
2803+
attr.id = SAI_HOSTIF_ATTR_OPER_STATUS;
2804+
status = sai_hostif_api->get_hostif_attribute(port.m_hif_id, 1, &attr);
2805+
2806+
ASSERT_EQ(status, SAI_STATUS_SUCCESS);
2807+
ASSERT_TRUE(attr.value.booldata);
2808+
}
2809+
}
2810+
2811+
TEST_F(PortsOrchTest, PortHostIfCreateFailed)
2812+
{
2813+
Table portTable = Table(m_app_db.get(), APP_PORT_TABLE_NAME);
2814+
2815+
auto original_api = sai_hostif_api->create_hostif;
2816+
auto hostIfSpy = SpyOn<SAI_API_HOSTIF, SAI_OBJECT_TYPE_HOSTIF>(&sai_hostif_api->create_hostif);
2817+
hostIfSpy->callFake([&](sai_object_id_t*, sai_object_id_t, uint32_t, const sai_attribute_t*) -> sai_status_t {
2818+
return SAI_STATUS_INSUFFICIENT_RESOURCES;
2819+
}
2820+
);
2821+
2822+
// Get SAI default ports to populate DB
2823+
2824+
auto ports = ut_helper::getInitialSaiPorts();
2825+
2826+
// Populate pot table with SAI ports
2827+
for (const auto &it : ports)
2828+
{
2829+
portTable.set(it.first, it.second);
2830+
}
2831+
2832+
// Set PortConfigDone
2833+
portTable.set("PortConfigDone", { { "count", to_string(ports.size()) } });
2834+
2835+
gPortsOrch->addExistingData(&portTable);
2836+
2837+
// Apply configuration :
2838+
// create ports
2839+
2840+
static_cast<Orch *>(gPortsOrch)->doTask();
2841+
2842+
sai_hostif_api->create_hostif = original_api;
2843+
2844+
Port port;
2845+
gPortsOrch->getPort("Ethernet0", port);
2846+
2847+
ASSERT_FALSE(port.m_init);
27912848
}
27922849

27932850
TEST_F(PortsOrchTest, PfcDlrHandlerCallingDlrInitAttribute)

0 commit comments

Comments
 (0)