Skip to content

Commit

Permalink
Simplify PortsOrch::doPortTask
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Luo <[email protected]>
  • Loading branch information
qiluo-msft committed Aug 8, 2018
1 parent b1cf199 commit a34dae0
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 93 deletions.
186 changes: 93 additions & 93 deletions orchagent/portsorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,96 @@ bool PortsOrch::initPort(const string &alias, const set<int> &lane_set)
return true;
}

void PortsOrch::doPortConfigDoneTask(const vector<FieldValueTuple>& tuples)
{
SWSS_LOG_ENTER();

m_portConfigDone = true;

for (auto i : tuples)
{
if (fvField(i) == "count")
{
m_portCount = to_uint<uint32_t>(fvValue(i));
}
}

if (m_lanesAliasSpeedMap.size() != m_portCount)
{
SWSS_LOG_ERROR("Unexpected number of PORTs received before PortConfigDone: %zu != %u", m_lanesAliasSpeedMap.size(), m_portCount);
return;
}

/* Once all ports received, go through the each port and perform appropriate actions:
* 1. Remove ports which don't exist anymore
* 2. Create new ports
* 3. Initialize all ports
*/
for (auto it = m_portListLaneMap.begin(); it != m_portListLaneMap.end();)
{
if (m_lanesAliasSpeedMap.find(it->first) == m_lanesAliasSpeedMap.end())
{
char *platform = getenv("platform");
if (platform && (strstr(platform, BFN_PLATFORM_SUBSTRING) || strstr(platform, MLNX_PLATFORM_SUBSTRING)))
{
if (!removePort(it->second))
{
throw runtime_error("PortsOrch initialization failure.");
}
}
else
{
SWSS_LOG_NOTICE("Failed to remove Port %lx due to missing SAI remove_port API.", it->second);
}

it = m_portListLaneMap.erase(it);
}
else
{
it++;
}
}

for (auto it = m_lanesAliasSpeedMap.begin(); it != m_lanesAliasSpeedMap.end();)
{
bool port_created = false;

if (m_portListLaneMap.find(it->first) == m_portListLaneMap.end())
{
// work around to avoid syncd termination on SAI error due missing create_port SAI API
// can be removed when SAI redis return NotImplemented error
char *platform = getenv("platform");
if (platform && (strstr(platform, BFN_PLATFORM_SUBSTRING) || strstr(platform, MLNX_PLATFORM_SUBSTRING)))
{
if (!addPort(it->first, get<1>(it->second), get<2>(it->second), get<3>(it->second)))
{
throw runtime_error("PortsOrch initialization failure.");
}

port_created = true;
}
else
{
SWSS_LOG_NOTICE("Failed to create Port %s due to missing SAI create_port API.", get<0>(it->second).c_str());
}
}
else
{
port_created = true;
}

if (port_created)
{
if (!initPort(get<0>(it->second), it->first))
{
throw runtime_error("PortsOrch initialization failure.");
}
}

it = m_lanesAliasSpeedMap.erase(it);
}
}

void PortsOrch::doPortTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
Expand All @@ -1191,15 +1281,10 @@ void PortsOrch::doPortTask(Consumer &consumer)

if (alias == "PortConfigDone")
{
m_portConfigDone = true;
doPortConfigDoneTask(kfvFieldsValues(t));

for (auto i : kfvFieldsValues(t))
{
if (fvField(i) == "count")
{
m_portCount = to_uint<uint32_t>(fvValue(i));
}
}
it = consumer.m_toSync.erase(it);
continue;
}

/* Get notification from application */
Expand Down Expand Up @@ -1279,90 +1364,6 @@ void PortsOrch::doPortTask(Consumer &consumer)
m_lanesAliasSpeedMap[lane_set] = make_tuple(alias, speed, an, fec_mode);
}

/* Once all ports received, go through the each port and perform appropriate actions:
* 1. Remove ports which don't exist anymore
* 2. Create new ports
* 3. Initialize all ports
*/
if (m_portConfigDone && (m_lanesAliasSpeedMap.size() == m_portCount))
{
for (auto it = m_portListLaneMap.begin(); it != m_portListLaneMap.end();)
{
if (m_lanesAliasSpeedMap.find(it->first) == m_lanesAliasSpeedMap.end())
{
char *platform = getenv("platform");
if (platform && (strstr(platform, BFN_PLATFORM_SUBSTRING) || strstr(platform, MLNX_PLATFORM_SUBSTRING)))
{
if (!removePort(it->second))
{
throw runtime_error("PortsOrch initialization failure.");
}
}
else
{
SWSS_LOG_NOTICE("Failed to remove Port %lx due to missing SAI remove_port API.", it->second);
}

it = m_portListLaneMap.erase(it);
}
else
{
it++;
}
}

for (auto it = m_lanesAliasSpeedMap.begin(); it != m_lanesAliasSpeedMap.end();)
{
bool port_created = false;

if (m_portListLaneMap.find(it->first) == m_portListLaneMap.end())
{
// work around to avoid syncd termination on SAI error due missing create_port SAI API
// can be removed when SAI redis return NotImplemented error
char *platform = getenv("platform");
if (platform && (strstr(platform, BFN_PLATFORM_SUBSTRING) || strstr(platform, MLNX_PLATFORM_SUBSTRING)))
{
if (!addPort(it->first, get<1>(it->second), get<2>(it->second), get<3>(it->second)))
{
throw runtime_error("PortsOrch initialization failure.");
}

port_created = true;
}
else
{
SWSS_LOG_NOTICE("Failed to create Port %s due to missing SAI create_port API.", get<0>(it->second).c_str());
}
}
else
{
port_created = true;
}

if (port_created)
{
if (!initPort(get<0>(it->second), it->first))
{
throw runtime_error("PortsOrch initialization failure.");
}
}

it = m_lanesAliasSpeedMap.erase(it);
}
}

if (!m_portConfigDone)
{
it = consumer.m_toSync.erase(it);
continue;
}

if (alias == "PortConfigDone")
{
it = consumer.m_toSync.erase(it);
continue;
}

if (!gBufferOrch->isPortReady(alias))
{
// buffer configuration hasn't been applied yet. save it for future retry
Expand Down Expand Up @@ -1496,7 +1497,6 @@ void PortsOrch::doPortTask(Consumer &consumer)
}
}


if (!fec_mode.empty())
{
if (fec_mode_map.find(fec_mode) != fec_mode_map.end())
Expand Down
1 change: 1 addition & 0 deletions orchagent/portsorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PortsOrch : public Orch, public Subject
bool isInitDone();

map<string, Port>& getAllPorts();
void doPortConfigDoneTask(const vector<FieldValueTuple>& tuples);
bool getBridgePort(sai_object_id_t id, Port &port);
bool getPort(string alias, Port &port);
bool getPort(sai_object_id_t id, Port &port);
Expand Down
60 changes: 60 additions & 0 deletions tests/test_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,63 @@ def test_PortNotification(dvs):
break

assert oper_status == "up"

def test_PortFec(dvs):

dvs.runcmd("ifconfig Ethernet0 10.0.0.0/31 up") == 0
dvs.runcmd("ifconfig Ethernet4 10.0.0.2/31 up") == 0

dvs.servers[0].runcmd("ip link set down dev eth0") == 0

time.sleep(1)

db = swsscommon.DBConnector(0, dvs.redis_sock, 0)
adb = swsscommon.DBConnector(1, dvs.redis_sock, 0)

tbl = swsscommon.Table(db, "PORT_TABLE")
ptbl = swsscommon.ProducerTable(db, "PORT_TABLE")
atbl = swsscommon.Table(adb, "ASIC_STATE:SAI_OBJECT_TYPE_PORT")

(status, fvs) = tbl.get("Ethernet0")

assert status == True

oper_status = "unknown"

for v in fvs:
if v[0] == "oper_status":
oper_status = v[1]
break

assert oper_status == "down"

dvs.servers[0].runcmd("ip link set up dev eth0") == 0

time.sleep(1)

(status, fvs) = tbl.get("Ethernet0")

assert status == True

oper_status = "unknown"

for v in fvs:
if v[0] == "oper_status":
oper_status = v[1]
break

assert oper_status == "up"

# set fec
fvs = swsscommon.FieldValuePairs([("fec","rs"), ("speed", "1000")])
ptbl.set("Ethernet0", fvs)

time.sleep(1)

# get fec
(status, fvs) = atbl.get(dvs.asicdb.portnamemap["Ethernet0"])
assert status == True

for fv in fvs:
if fv[0] == "SAI_PORT_ATTR_FEC_MODE":
assert fv[1] == "SAI_PORT_FEC_MODE_RS"

0 comments on commit a34dae0

Please sign in to comment.