Skip to content

Commit dc63a80

Browse files
committed
Add SAI_PORT_ATTR_OPER_SPEED support (sonic-net#1107)
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]>
1 parent b3bda89 commit dc63a80

5 files changed

Lines changed: 117 additions & 0 deletions

File tree

unittest/vslib/TestSwitchBCM81724.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,38 @@ TEST(SwitchBCM81724, refresh_read_only)
117117

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

120+
char fakeinfo_buffer[sizeof(HostInterfaceInfo)] = { 0 };
121+
HostInterfaceInfo *fakeinfo = reinterpret_cast<HostInterfaceInfo *>(reinterpret_cast<void *>(fakeinfo_buffer));
122+
fakeinfo->m_portId = portId;
123+
sw.m_hostif_info_map[""] = std::shared_ptr<HostInterfaceInfo>(fakeinfo, [](HostInterfaceInfo *){});
124+
sw.m_switchConfig->m_laneMap->m_lane_to_ifname[1] = "eth0";
125+
126+
attr.id = SAI_PORT_ATTR_OPER_STATUS;
127+
attr.value.s32 = SAI_PORT_OPER_STATUS_DOWN;
128+
129+
EXPECT_EQ(sw.set(SAI_OBJECT_TYPE_PORT, strPortId, &attr), SAI_STATUS_SUCCESS);
130+
131+
attr.id = SAI_PORT_ATTR_OPER_SPEED;
132+
133+
EXPECT_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);
134+
EXPECT_EQ(attr.value.u32, 0);
135+
136+
attr.id = SAI_PORT_ATTR_OPER_STATUS;
137+
attr.value.s32 = SAI_PORT_OPER_STATUS_UP;
138+
139+
EXPECT_EQ(sw.set(SAI_OBJECT_TYPE_PORT, strPortId, &attr), SAI_STATUS_SUCCESS);
140+
141+
attr.id = SAI_PORT_ATTR_OPER_SPEED;
142+
143+
EXPECT_EQ(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);
144+
EXPECT_GE(attr.value.u32, 0);
145+
146+
sw.m_hostif_info_map.clear();
147+
148+
attr.id = SAI_PORT_ATTR_OPER_SPEED;
149+
150+
EXPECT_NE(sw.get(SAI_OBJECT_TYPE_PORT, strPortId, 1, &attr), SAI_STATUS_SUCCESS);
151+
120152
//std::cout << sw.dump_switch_database_for_warm_restart();
121153
}
122154

vslib/SwitchBCM81724.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ sai_status_t SwitchBCM81724::refresh_read_only(
274274

275275
case SAI_PORT_ATTR_OPER_STATUS:
276276
return SAI_STATUS_SUCCESS;
277+
278+
case SAI_PORT_ATTR_OPER_SPEED:
279+
return refresh_port_oper_speed(object_id);
277280
}
278281
}
279282

vslib/SwitchStateBase.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,36 @@ sai_status_t SwitchStateBase::refresh_port_serdes_id(
21822182
return SAI_STATUS_SUCCESS;
21832183
}
21842184

2185+
sai_status_t SwitchStateBase::refresh_port_oper_speed(
2186+
_In_ sai_object_id_t port_id)
2187+
{
2188+
SWSS_LOG_ENTER();
2189+
2190+
sai_attribute_t attr;
2191+
2192+
attr.id = SAI_PORT_ATTR_OPER_STATUS;
2193+
2194+
CHECK_STATUS(get(SAI_OBJECT_TYPE_PORT, port_id, 1, &attr));
2195+
2196+
if (attr.value.s32 == SAI_PORT_OPER_STATUS_DOWN)
2197+
{
2198+
attr.value.u32 = 0;
2199+
}
2200+
else
2201+
{
2202+
if (!vs_get_oper_speed(port_id, attr.value.u32))
2203+
{
2204+
return SAI_STATUS_FAILURE;
2205+
}
2206+
}
2207+
2208+
attr.id = SAI_PORT_ATTR_OPER_SPEED;
2209+
2210+
CHECK_STATUS(set(SAI_OBJECT_TYPE_PORT, port_id, &attr));
2211+
2212+
return SAI_STATUS_SUCCESS;
2213+
}
2214+
21852215
// XXX extra work may be needed on GET api if N on list will be > then actual
21862216

21872217
/*
@@ -2291,6 +2321,12 @@ sai_status_t SwitchStateBase::refresh_read_only(
22912321

22922322
case SAI_PORT_ATTR_PORT_SERDES_ID:
22932323
return refresh_port_serdes_id(object_id);
2324+
2325+
case SAI_PORT_ATTR_SUPPORTED_AUTO_NEG_MODE:
2326+
return SAI_STATUS_SUCCESS;
2327+
2328+
case SAI_PORT_ATTR_OPER_SPEED:
2329+
return refresh_port_oper_speed(object_id);
22942330
}
22952331
}
22962332

vslib/SwitchStateBase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ namespace saivs
163163
virtual sai_status_t refresh_port_serdes_id(
164164
_In_ sai_object_id_t bridge_id);
165165

166+
virtual sai_status_t refresh_port_oper_speed(
167+
_In_ sai_object_id_t port_id);
168+
166169
public:
167170

168171
virtual sai_status_t warm_boot_initialize_objects();
@@ -484,6 +487,10 @@ namespace saivs
484487
bool hasIfIndex(
485488
_In_ int ifIndex) const;
486489

490+
bool vs_get_oper_speed(
491+
_In_ sai_object_id_t port_id,
492+
_Out_ uint32_t& speed);
493+
487494
public: // TODO move inside warm boot load state
488495

489496
sai_status_t vs_recreate_hostif_tap_interfaces();

vslib/SwitchStateBaseHostif.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/if.h>
2626

2727
#include <algorithm>
28+
#include <fstream>
2829

2930
using namespace saivs;
3031

@@ -871,6 +872,44 @@ bool SwitchStateBase::hasIfIndex(
871872
return false;
872873
}
873874

875+
bool SwitchStateBase::vs_get_oper_speed(
876+
_In_ sai_object_id_t port_id,
877+
_Out_ uint32_t& speed)
878+
879+
{
880+
SWSS_LOG_ENTER();
881+
882+
auto info = findHostInterfaceInfoByPort(port_id);
883+
884+
if (!info)
885+
{
886+
SWSS_LOG_ERROR("Port %s don't exists",
887+
sai_serialize_object_id(port_id).c_str());
888+
889+
return false;
890+
}
891+
892+
auto veth_name = vs_get_veth_name(info->m_name, port_id);
893+
std::string veth_speed_filename = "/sys/class/net/";
894+
895+
veth_speed_filename += veth_name;
896+
veth_speed_filename += "/speed";
897+
898+
std::ifstream ifs(veth_speed_filename);
899+
900+
if (!ifs.is_open())
901+
{
902+
SWSS_LOG_ERROR("Failed to open %s", veth_speed_filename.c_str());
903+
904+
return false;
905+
}
906+
907+
ifs >> speed;
908+
ifs.close();
909+
910+
return true;
911+
}
912+
874913
void SwitchStateBase::syncOnLinkMsg(
875914
_In_ std::shared_ptr<EventPayloadNetLinkMsg> payload)
876915
{

0 commit comments

Comments
 (0)