Skip to content

Commit

Permalink
Add SAI_PORT_ATTR_OPER_SPEED support
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <ganze718@gmail.com>
  • Loading branch information
Pterosaur committed Aug 20, 2022
1 parent 9652ea4 commit ecf607d
Show file tree
Hide file tree
Showing 5 changed files with 114 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_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_FAILURE);

//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
33 changes: 33 additions & 0 deletions vslib/SwitchStateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,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 @@ -2312,6 +2342,9 @@ sai_status_t SwitchStateBase::refresh_read_only(

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 @@ -165,6 +165,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 @@ -486,6 +489,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 ecf607d

Please sign in to comment.