Skip to content

Commit

Permalink
Add SAI_PORT_ATTR_OPER_SPEED support (sonic-net#1107)
Browse files Browse the repository at this point in the history
Add SAI_PORT_ATTR_OPER_SPEED get in vslib.
If the SAI_PORT_ATTR_OPER_STATUS is DOWN, SAI_PORT_ATTR_OPER_SPEED should be 0, otherwise its value comes from /sys/class/net/eth{X}/speed

Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Sep 9, 2022
1 parent b3bda89 commit dc63a80
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
32 changes: 32 additions & 0 deletions unittest/vslib/TestSwitchBCM81724.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ TEST(SwitchBCM81724, refresh_read_only)

EXPECT_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);

char fakeinfo_buffer[sizeof(HostInterfaceInfo)] = { 0 };
HostInterfaceInfo *fakeinfo = reinterpret_cast<HostInterfaceInfo *>(reinterpret_cast<void *>(fakeinfo_buffer));
fakeinfo->m_portId = portId;
sw.m_hostif_info_map[""] = std::shared_ptr<HostInterfaceInfo>(fakeinfo, [](HostInterfaceInfo *){});
sw.m_switchConfig->m_laneMap->m_lane_to_ifname[1] = "eth0";

attr.id = SAI_PORT_ATTR_OPER_STATUS;
attr.value.s32 = SAI_PORT_OPER_STATUS_DOWN;

EXPECT_EQ(sw.set(SAI_OBJECT_TYPE_PORT, strPortId, &attr), SAI_STATUS_SUCCESS);

attr.id = SAI_PORT_ATTR_OPER_SPEED;

EXPECT_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);
EXPECT_EQ(attr.value.u32, 0);

attr.id = SAI_PORT_ATTR_OPER_STATUS;
attr.value.s32 = SAI_PORT_OPER_STATUS_UP;

EXPECT_EQ(sw.set(SAI_OBJECT_TYPE_PORT, strPortId, &attr), SAI_STATUS_SUCCESS);

attr.id = SAI_PORT_ATTR_OPER_SPEED;

EXPECT_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);
EXPECT_GE(attr.value.u32, 0);

sw.m_hostif_info_map.clear();

attr.id = SAI_PORT_ATTR_OPER_SPEED;

EXPECT_NE(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);

//std::cout << sw.dump_switch_database_for_warm_restart();
}

Expand Down
3 changes: 3 additions & 0 deletions vslib/SwitchBCM81724.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ sai_status_t SwitchBCM81724::refresh_read_only(

case SAI_PORT_ATTR_OPER_STATUS:
return SAI_STATUS_SUCCESS;

case SAI_PORT_ATTR_OPER_SPEED:
return refresh_port_oper_speed(object_id);
}
}

Expand Down
36 changes: 36 additions & 0 deletions vslib/SwitchStateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,36 @@ sai_status_t SwitchStateBase::refresh_port_serdes_id(
return SAI_STATUS_SUCCESS;
}

sai_status_t SwitchStateBase::refresh_port_oper_speed(
_In_ sai_object_id_t port_id)
{
SWSS_LOG_ENTER();

sai_attribute_t attr;

attr.id = SAI_PORT_ATTR_OPER_STATUS;

CHECK_STATUS(get(SAI_OBJECT_TYPE_PORT, port_id, 1, &attr));

if (attr.value.s32 == SAI_PORT_OPER_STATUS_DOWN)
{
attr.value.u32 = 0;
}
else
{
if (!vs_get_oper_speed(port_id, attr.value.u32))
{
return SAI_STATUS_FAILURE;
}
}

attr.id = SAI_PORT_ATTR_OPER_SPEED;

CHECK_STATUS(set(SAI_OBJECT_TYPE_PORT, port_id, &attr));

return SAI_STATUS_SUCCESS;
}

// XXX extra work may be needed on GET api if N on list will be > then actual

/*
Expand Down Expand Up @@ -2291,6 +2321,12 @@ sai_status_t SwitchStateBase::refresh_read_only(

case SAI_PORT_ATTR_PORT_SERDES_ID:
return refresh_port_serdes_id(object_id);

case SAI_PORT_ATTR_SUPPORTED_AUTO_NEG_MODE:
return SAI_STATUS_SUCCESS;

case SAI_PORT_ATTR_OPER_SPEED:
return refresh_port_oper_speed(object_id);
}
}

Expand Down
7 changes: 7 additions & 0 deletions vslib/SwitchStateBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ namespace saivs
virtual sai_status_t refresh_port_serdes_id(
_In_ sai_object_id_t bridge_id);

virtual sai_status_t refresh_port_oper_speed(
_In_ sai_object_id_t port_id);

public:

virtual sai_status_t warm_boot_initialize_objects();
Expand Down Expand Up @@ -484,6 +487,10 @@ namespace saivs
bool hasIfIndex(
_In_ int ifIndex) const;

bool vs_get_oper_speed(
_In_ sai_object_id_t port_id,
_Out_ uint32_t& speed);

public: // TODO move inside warm boot load state

sai_status_t vs_recreate_hostif_tap_interfaces();
Expand Down
39 changes: 39 additions & 0 deletions vslib/SwitchStateBaseHostif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <linux/if.h>

#include <algorithm>
#include <fstream>

using namespace saivs;

Expand Down Expand Up @@ -871,6 +872,44 @@ bool SwitchStateBase::hasIfIndex(
return false;
}

bool SwitchStateBase::vs_get_oper_speed(
_In_ sai_object_id_t port_id,
_Out_ uint32_t& speed)

{
SWSS_LOG_ENTER();

auto info = findHostInterfaceInfoByPort(port_id);

if (!info)
{
SWSS_LOG_ERROR("Port %s don't exists",
sai_serialize_object_id(port_id).c_str());

return false;
}

auto veth_name = vs_get_veth_name(info->m_name, port_id);
std::string veth_speed_filename = "/sys/class/net/";

veth_speed_filename += veth_name;
veth_speed_filename += "/speed";

std::ifstream ifs(veth_speed_filename);

if (!ifs.is_open())
{
SWSS_LOG_ERROR("Failed to open %s", veth_speed_filename.c_str());

return false;
}

ifs >> speed;
ifs.close();

return true;
}

void SwitchStateBase::syncOnLinkMsg(
_In_ std::shared_ptr<EventPayloadNetLinkMsg> payload)
{
Expand Down

0 comments on commit dc63a80

Please sign in to comment.