Skip to content

Commit 055832d

Browse files
Pterosaurjimmyzhai
authored andcommitted
[macsec]: Set MTU for MACsec (#2398)
What I did Set extra MTU for MACsec enabled port. Why I did it MACsec frame will expend the packet with MACsec SecTAG, Otherwise if a packet length equals the MTU which will be dropped by SAI port. Signed-off-by: Ze Gan <[email protected]> Co-authored-by: Junhua Zhai <[email protected]>
1 parent 3f69944 commit 055832d

File tree

4 files changed

+110
-16
lines changed

4 files changed

+110
-16
lines changed

orchagent/macsecorch.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,8 @@ bool MACsecOrch::createMACsecPort(
12751275
phy);
12761276
});
12771277

1278+
m_port_orch->setMACsecEnabledState(port_id, true);
1279+
12781280
if (phy)
12791281
{
12801282
if (!setPFCForward(port_id, true))
@@ -1542,6 +1544,8 @@ bool MACsecOrch::deleteMACsecPort(
15421544
result &= false;
15431545
}
15441546

1547+
m_port_orch->setMACsecEnabledState(port_id, false);
1548+
15451549
if (phy)
15461550
{
15471551
if (!setPFCForward(port_id, false))

orchagent/p4orch/tests/fake_portorch.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ bool PortsOrch::getPortAdminStatus(sai_object_id_t id, bool &up)
497497
return true;
498498
}
499499

500-
bool PortsOrch::setPortMtu(sai_object_id_t id, sai_uint32_t mtu)
500+
bool PortsOrch::setPortMtu(const Port &port, sai_uint32_t mtu)
501501
{
502502
return true;
503503
}
@@ -561,12 +561,12 @@ bool PortsOrch::getPortSpeed(sai_object_id_t port_id, sai_uint32_t &speed)
561561
return true;
562562
}
563563

564-
bool PortsOrch::setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value)
564+
bool PortsOrch::setGearboxPortsAttr(const Port &port, sai_port_attr_t id, void *value)
565565
{
566566
return true;
567567
}
568568

569-
bool PortsOrch::setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value)
569+
bool PortsOrch::setGearboxPortAttr(const Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value)
570570
{
571571
return true;
572572
}

orchagent/portsorch.cpp

+94-9
Original file line numberDiff line numberDiff line change
@@ -1168,27 +1168,62 @@ bool PortsOrch::getPortAdminStatus(sai_object_id_t id, bool &up)
11681168
return true;
11691169
}
11701170

1171-
bool PortsOrch::setPortMtu(sai_object_id_t id, sai_uint32_t mtu)
1171+
bool PortsOrch::getPortMtu(const Port& port, sai_uint32_t &mtu)
1172+
{
1173+
SWSS_LOG_ENTER();
1174+
1175+
sai_attribute_t attr;
1176+
attr.id = SAI_PORT_ATTR_MTU;
1177+
1178+
sai_status_t status = sai_port_api->get_port_attribute(port.m_port_id, 1, &attr);
1179+
1180+
if (status != SAI_STATUS_SUCCESS)
1181+
{
1182+
return false;
1183+
}
1184+
1185+
mtu = attr.value.u32 - (uint32_t)(sizeof(struct ether_header) + FCS_LEN + VLAN_TAG_LEN);
1186+
1187+
if (isMACsecPort(port.m_port_id))
1188+
{
1189+
mtu -= MAX_MACSEC_SECTAG_SIZE;
1190+
}
1191+
1192+
return true;
1193+
}
1194+
1195+
bool PortsOrch::setPortMtu(const Port& port, sai_uint32_t mtu)
11721196
{
11731197
SWSS_LOG_ENTER();
11741198

11751199
sai_attribute_t attr;
11761200
attr.id = SAI_PORT_ATTR_MTU;
11771201
/* mtu + 14 + 4 + 4 = 22 bytes */
1178-
attr.value.u32 = (uint32_t)(mtu + sizeof(struct ether_header) + FCS_LEN + VLAN_TAG_LEN);
1202+
mtu += (uint32_t)(sizeof(struct ether_header) + FCS_LEN + VLAN_TAG_LEN);
1203+
attr.value.u32 = mtu;
11791204

1180-
sai_status_t status = sai_port_api->set_port_attribute(id, &attr);
1205+
if (isMACsecPort(port.m_port_id))
1206+
{
1207+
attr.value.u32 += MAX_MACSEC_SECTAG_SIZE;
1208+
}
1209+
1210+
sai_status_t status = sai_port_api->set_port_attribute(port.m_port_id, &attr);
11811211
if (status != SAI_STATUS_SUCCESS)
11821212
{
11831213
SWSS_LOG_ERROR("Failed to set MTU %u to port pid:%" PRIx64 ", rv:%d",
1184-
attr.value.u32, id, status);
1214+
attr.value.u32, port.m_port_id, status);
11851215
task_process_status handle_status = handleSaiSetStatus(SAI_API_PORT, status);
11861216
if (handle_status != task_success)
11871217
{
11881218
return parseHandleSaiStatusFailure(handle_status);
11891219
}
11901220
}
1191-
SWSS_LOG_INFO("Set MTU %u to port pid:%" PRIx64, attr.value.u32, id);
1221+
1222+
if (m_gearboxEnabled)
1223+
{
1224+
setGearboxPortsAttr(port, SAI_PORT_ATTR_MTU, &mtu);
1225+
}
1226+
SWSS_LOG_INFO("Set MTU %u to port pid:%" PRIx64, attr.value.u32, port.m_port_id);
11921227
return true;
11931228
}
11941229

@@ -2144,7 +2179,7 @@ void PortsOrch::initPortSupportedFecModes(const std::string& alias, sai_object_i
21442179
/*
21452180
* If Gearbox is enabled and this is a Gearbox port then set the attributes accordingly.
21462181
*/
2147-
bool PortsOrch::setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value)
2182+
bool PortsOrch::setGearboxPortsAttr(const Port &port, sai_port_attr_t id, void *value)
21482183
{
21492184
bool status = false;
21502185

@@ -2162,7 +2197,7 @@ bool PortsOrch::setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value)
21622197
* If Gearbox is enabled and this is a Gearbox port then set the specific lane attribute.
21632198
* Note: the appl_db is also updated (Gearbox config_db tables are TBA).
21642199
*/
2165-
bool PortsOrch::setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value)
2200+
bool PortsOrch::setGearboxPortAttr(const Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value)
21662201
{
21672202
sai_status_t status = SAI_STATUS_SUCCESS;
21682203
sai_object_id_t dest_port_id;
@@ -2216,6 +2251,15 @@ bool PortsOrch::setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_p
22162251
}
22172252
SWSS_LOG_NOTICE("BOX: Set %s lane %s %d", port.m_alias.c_str(), speed_attr.c_str(), speed);
22182253
break;
2254+
case SAI_PORT_ATTR_MTU:
2255+
attr.id = id;
2256+
attr.value.u32 = *static_cast<sai_uint32_t*>(value);
2257+
if (LINE_PORT_TYPE == port_type && isMACsecPort(dest_port_id))
2258+
{
2259+
attr.value.u32 += MAX_MACSEC_SECTAG_SIZE;
2260+
}
2261+
SWSS_LOG_NOTICE("BOX: Set %s MTU %d", port.m_alias.c_str(), attr.value.u32);
2262+
break;
22192263
default:
22202264
return false;
22212265
}
@@ -3565,7 +3609,7 @@ void PortsOrch::doPortTask(Consumer &consumer)
35653609

35663610
if (mtu != 0 && mtu != p.m_mtu)
35673611
{
3568-
if (setPortMtu(p.m_port_id, mtu))
3612+
if (setPortMtu(p, mtu))
35693613
{
35703614
p.m_mtu = mtu;
35713615
m_portList[alias] = p;
@@ -4632,6 +4676,12 @@ bool PortsOrch::initializePort(Port &port)
46324676
return false;
46334677
}
46344678

4679+
/* initialize port mtu */
4680+
if (!getPortMtu(port, port.m_mtu))
4681+
{
4682+
SWSS_LOG_ERROR("Failed to get initial port mtu %d", port.m_mtu);
4683+
}
4684+
46354685
/*
46364686
* always initialize Port SAI_HOSTIF_ATTR_OPER_STATUS based on oper_status value in appDB.
46374687
*/
@@ -7008,6 +7058,8 @@ bool PortsOrch::initGearboxPort(Port &port)
70087058
SWSS_LOG_NOTICE("BOX: Connected Gearbox ports; system-side:0x%" PRIx64 " to line-side:0x%" PRIx64, systemPort, linePort);
70097059
m_gearboxPortListLaneMap[port.m_port_id] = make_tuple(systemPort, linePort);
70107060
port.m_line_side_id = linePort;
7061+
saiOidToAlias[systemPort] = port.m_alias;
7062+
saiOidToAlias[linePort] = port.m_alias;
70117063

70127064
/* Add gearbox system/line port name map to counter table */
70137065
FieldValueTuple tuple(port.m_alias + "_system", sai_serialize_object_id(systemPort));
@@ -7510,6 +7562,39 @@ bool PortsOrch::decrFdbCount(const std::string& alias, int count)
75107562
return true;
75117563
}
75127564

7565+
void PortsOrch::setMACsecEnabledState(sai_object_id_t port_id, bool enabled)
7566+
{
7567+
SWSS_LOG_ENTER();
7568+
7569+
Port p;
7570+
if (!getPort(port_id, p))
7571+
{
7572+
SWSS_LOG_ERROR("Failed to get port object for port id 0x%" PRIx64, port_id);
7573+
return;
7574+
}
7575+
7576+
if (enabled)
7577+
{
7578+
m_macsecEnabledPorts.insert(port_id);
7579+
}
7580+
else
7581+
{
7582+
m_macsecEnabledPorts.erase(port_id);
7583+
}
7584+
7585+
if (p.m_mtu)
7586+
{
7587+
setPortMtu(p, p.m_mtu);
7588+
}
7589+
}
7590+
7591+
bool PortsOrch::isMACsecPort(sai_object_id_t port_id) const
7592+
{
7593+
SWSS_LOG_ENTER();
7594+
7595+
return m_macsecEnabledPorts.find(port_id) != m_macsecEnabledPorts.end();
7596+
}
7597+
75137598
/* Refresh the per-port Auto-Negotiation operational states */
75147599
void PortsOrch::refreshPortStateAutoNeg(const Port &port)
75157600
{
@@ -7624,4 +7709,4 @@ void PortsOrch::doTask(swss::SelectableTimer &timer)
76247709
{
76257710
m_port_state_poller->stop();
76267711
}
7627-
}
7712+
}

orchagent/portsorch.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#define FCS_LEN 4
2020
#define VLAN_TAG_LEN 4
21+
#define MAX_MACSEC_SECTAG_SIZE 32
2122
#define PORT_STAT_COUNTER_FLEX_COUNTER_GROUP "PORT_STAT_COUNTER"
2223
#define PORT_RATE_COUNTER_FLEX_COUNTER_GROUP "PORT_RATE_COUNTER"
2324
#define PORT_BUFFER_DROP_STAT_FLEX_COUNTER_GROUP "PORT_BUFFER_DROP_STAT"
@@ -175,6 +176,9 @@ class PortsOrch : public Orch, public Subject
175176

176177
bool decrFdbCount(const string& alias, int count);
177178

179+
void setMACsecEnabledState(sai_object_id_t port_id, bool enabled);
180+
bool isMACsecPort(sai_object_id_t port_id) const;
181+
178182
private:
179183
unique_ptr<Table> m_counterTable;
180184
unique_ptr<Table> m_counterLagTable;
@@ -310,7 +314,8 @@ class PortsOrch : public Orch, public Subject
310314

311315
bool setPortAdminStatus(Port &port, bool up);
312316
bool getPortAdminStatus(sai_object_id_t id, bool& up);
313-
bool setPortMtu(sai_object_id_t id, sai_uint32_t mtu);
317+
bool getPortMtu(const Port& port, sai_uint32_t &mtu);
318+
bool setPortMtu(const Port& port, sai_uint32_t mtu);
314319
bool setPortTpid(sai_object_id_t id, sai_uint16_t tpid);
315320
bool setPortPvid (Port &port, sai_uint32_t pvid);
316321
bool getPortPvid(Port &port, sai_uint32_t &pvid);
@@ -328,8 +333,8 @@ class PortsOrch : public Orch, public Subject
328333
void initPortSupportedFecModes(const std::string& alias, sai_object_id_t port_id);
329334
task_process_status setPortSpeed(Port &port, sai_uint32_t speed);
330335
bool getPortSpeed(sai_object_id_t id, sai_uint32_t &speed);
331-
bool setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value);
332-
bool setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value);
336+
bool setGearboxPortsAttr(const Port &port, sai_port_attr_t id, void *value);
337+
bool setGearboxPortAttr(const Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value);
333338

334339
bool getPortAdvSpeeds(const Port& port, bool remote, std::vector<sai_uint32_t>& speed_list);
335340
bool getPortAdvSpeeds(const Port& port, bool remote, string& adv_speeds);
@@ -403,8 +408,8 @@ class PortsOrch : public Orch, public Subject
403408
void voqSyncAddLagMember(Port &lag, Port &port);
404409
void voqSyncDelLagMember(Port &lag, Port &port);
405410
unique_ptr<LagIdAllocator> m_lagIdAllocator;
411+
set<sai_object_id_t> m_macsecEnabledPorts;
406412

407413
std::unordered_set<std::string> generateCounterStats(const string& type, bool gearbox = false);
408-
409414
};
410415
#endif /* SWSS_PORTSORCH_H */

0 commit comments

Comments
 (0)