Skip to content

Commit 1210c45

Browse files
andriymoroz-mlnxShuotian Cheng
authored andcommitted
[portsorch]: Add set port MTU function (sonic-net#218)
* Add set port MTU function * Add L2 header length to the MTU on hardware
1 parent 722649b commit 1210c45

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

orchagent/portsorch.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sstream>
66
#include <set>
77

8+
#include <netinet/if_ether.h>
89
#include "net/if.h"
910

1011
#include "logger.h"
@@ -223,6 +224,25 @@ bool PortsOrch::setPortAdminStatus(sai_object_id_t id, bool up)
223224
return true;
224225
}
225226

227+
bool PortsOrch::setPortMtu(sai_object_id_t id, sai_uint32_t mtu)
228+
{
229+
SWSS_LOG_ENTER();
230+
231+
sai_attribute_t attr;
232+
attr.id = SAI_PORT_ATTR_MTU;
233+
/* mtu + 14 + 4 + 4 = 22 bytes */
234+
attr.value.u32 = mtu + sizeof(struct ether_header) + FCS_LEN + VLAN_TAG_LEN;
235+
236+
sai_status_t status = sai_port_api->set_port_attribute(id, &attr);
237+
if (status != SAI_STATUS_SUCCESS)
238+
{
239+
SWSS_LOG_ERROR("Failed to set MTU %u to port pid:%lx", attr.value.u32, id);
240+
return false;
241+
}
242+
SWSS_LOG_INFO("Set MTU %u to port pid:%lx", attr.value.u32, id);
243+
return true;
244+
}
245+
226246
bool PortsOrch::setHostIntfsOperStatus(sai_object_id_t port_id, bool up)
227247
{
228248
SWSS_LOG_ENTER();
@@ -303,6 +323,8 @@ void PortsOrch::doPortTask(Consumer &consumer)
303323
{
304324
set<int> lane_set;
305325
string admin_status;
326+
uint32_t mtu = 0;
327+
306328
for (auto i : kfvFieldsValues(t))
307329
{
308330
/* Get lane information of a physical port and initialize the port */
@@ -322,6 +344,10 @@ void PortsOrch::doPortTask(Consumer &consumer)
322344
/* Set port admin status */
323345
if (fvField(i) == "admin_status")
324346
admin_status = fvValue(i);
347+
348+
/* Set port mtu */
349+
if (fvField(i) == "mtu")
350+
mtu = stoul(fvValue(i));
325351
}
326352

327353
if (lane_set.size())
@@ -385,6 +411,24 @@ void PortsOrch::doPortTask(Consumer &consumer)
385411
else
386412
SWSS_LOG_ERROR("Failed to get port id by alias:%s", alias.c_str());
387413
}
414+
415+
if (mtu != 0)
416+
{
417+
Port p;
418+
if (getPort(alias, p))
419+
{
420+
if (setPortMtu(p.m_port_id, mtu))
421+
SWSS_LOG_NOTICE("Set port %s MTU to %u", alias.c_str(), mtu);
422+
else
423+
{
424+
SWSS_LOG_ERROR("Failed to set port %s MTU to %u", alias.c_str(), mtu);
425+
it++;
426+
continue;
427+
}
428+
}
429+
else
430+
SWSS_LOG_ERROR("Failed to get port id by alias:%s", alias.c_str());
431+
}
388432
}
389433
else
390434
SWSS_LOG_ERROR("Unknown operation type %s", op.c_str());

orchagent/portsorch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <map>
1111

12+
#define FCS_LEN 4
13+
#define VLAN_TAG_LEN 4
14+
1215
static const map<sai_port_oper_status_t, string> oper_status_strings =
1316
{
1417
{ SAI_PORT_OPER_STATUS_UNKNOWN, "unknown" },
@@ -80,6 +83,7 @@ class PortsOrch : public Orch, public Subject
8083
bool removeLagMember(Port lag, Port port);
8184

8285
bool setPortAdminStatus(sai_object_id_t id, bool up);
86+
bool setPortMtu(sai_object_id_t id, sai_uint32_t mtu);
8387
};
8488
#endif /* SWSS_PORTSORCH_H */
8589

0 commit comments

Comments
 (0)