Skip to content

Commit d49eaa2

Browse files
authored
[macsecorch]: Fix MACsec SC creating before MACsec port enabling (#2087)
* Update MACsec SC attributes after MACsec SC creating Signed-off-by: Ze Gan <[email protected]> * Add unittest Signed-off-by: Ze Gan <[email protected]> * teardown unittest Signed-off-by: Ze Gan <[email protected]> * polish name Signed-off-by: Ze Gan <[email protected]>
1 parent 53c630b commit d49eaa2

File tree

3 files changed

+154
-23
lines changed

3 files changed

+154
-23
lines changed

orchagent/macsecorch.cpp

Lines changed: 103 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,18 @@ bool MACsecOrch::updateMACsecPort(MACsecPort &macsec_port, const TaskArgs &port_
11831183
if (get_value(port_attr, "enable_encrypt", alpha_boolean))
11841184
{
11851185
macsec_port.m_enable_encrypt = alpha_boolean.operator bool();
1186+
if (!updateMACsecSCs(
1187+
macsec_port,
1188+
[&macsec_port, this](MACsecOrch::MACsecSC &macsec_sc)
1189+
{
1190+
sai_attribute_t attr;
1191+
attr.id = SAI_MACSEC_SC_ATTR_ENCRYPTION_ENABLE;
1192+
attr.value.booldata = macsec_port.m_enable_encrypt;
1193+
return this->updateMACsecAttr(SAI_OBJECT_TYPE_MACSEC_SC, macsec_sc.m_sc_id, attr);
1194+
}))
1195+
{
1196+
return false;
1197+
}
11861198
}
11871199
if (get_value(port_attr, "send_sci", alpha_boolean))
11881200
{
@@ -1212,42 +1224,74 @@ bool MACsecOrch::updateMACsecPort(MACsecPort &macsec_port, const TaskArgs &port_
12121224
SWSS_LOG_WARN("Unknown Cipher Suite %s", cipher_suite.c_str());
12131225
return false;
12141226
}
1227+
if (!updateMACsecSCs(
1228+
macsec_port,
1229+
[&macsec_port, this](MACsecOrch::MACsecSC &macsec_sc)
1230+
{
1231+
sai_attribute_t attr;
1232+
attr.id = SAI_MACSEC_SC_ATTR_MACSEC_CIPHER_SUITE;
1233+
attr.value.s32 = macsec_port.m_cipher_suite;
1234+
return this->updateMACsecAttr(SAI_OBJECT_TYPE_MACSEC_SC, macsec_sc.m_sc_id, attr);
1235+
}))
1236+
{
1237+
return false;
1238+
}
12151239
}
12161240
swss::AlphaBoolean enable = false;
12171241
if (get_value(port_attr, "enable", enable) && enable.operator bool() != macsec_port.m_enable)
12181242
{
1219-
std::vector<MACsecOrch::MACsecSC *> macsec_scs;
12201243
macsec_port.m_enable = enable.operator bool();
1221-
for (auto &sc : macsec_port.m_egress_scs)
1244+
if (!updateMACsecSCs(
1245+
macsec_port,
1246+
[&macsec_port, &recover, this](MACsecOrch::MACsecSC &macsec_sc)
1247+
{
1248+
// Change the ACL entry action from packet action to MACsec flow
1249+
if (macsec_port.m_enable)
1250+
{
1251+
if (!this->setMACsecFlowActive(macsec_sc.m_entry_id, macsec_sc.m_flow_id, true))
1252+
{
1253+
SWSS_LOG_WARN("Cannot change the ACL entry action from packet action to MACsec flow");
1254+
return false;
1255+
}
1256+
auto entry_id = macsec_sc.m_entry_id;
1257+
auto flow_id = macsec_sc.m_flow_id;
1258+
recover.add_action([this, entry_id, flow_id]()
1259+
{ this->setMACsecFlowActive(entry_id, flow_id, false); });
1260+
}
1261+
else
1262+
{
1263+
this->setMACsecFlowActive(macsec_sc.m_entry_id, macsec_sc.m_flow_id, false);
1264+
}
1265+
return true;
1266+
}))
12221267
{
1223-
macsec_scs.push_back(&sc.second);
1268+
return false;
12241269
}
1225-
for (auto &sc : macsec_port.m_ingress_scs)
1270+
}
1271+
1272+
recover.clear();
1273+
return true;
1274+
}
1275+
1276+
bool MACsecOrch::updateMACsecSCs(MACsecPort &macsec_port, std::function<bool(MACsecOrch::MACsecSC &)> action)
1277+
{
1278+
SWSS_LOG_ENTER();
1279+
1280+
for (auto &sc : macsec_port.m_egress_scs)
1281+
{
1282+
if (!action(sc.second))
12261283
{
1227-
macsec_scs.push_back(&sc.second);
1284+
return false;
12281285
}
1229-
for (auto &macsec_sc : macsec_scs)
1286+
}
1287+
for (auto &sc : macsec_port.m_ingress_scs)
1288+
{
1289+
if (!action(sc.second))
12301290
{
1231-
// Change the ACL entry action from packet action to MACsec flow
1232-
if (macsec_port.m_enable)
1233-
{
1234-
if (!setMACsecFlowActive(macsec_sc->m_entry_id, macsec_sc->m_flow_id, true))
1235-
{
1236-
SWSS_LOG_WARN("Cannot change the ACL entry action from packet action to MACsec flow");
1237-
return false;
1238-
}
1239-
auto entry_id = macsec_sc->m_entry_id;
1240-
auto flow_id = macsec_sc->m_flow_id;
1241-
recover.add_action([this, entry_id, flow_id]() { this->setMACsecFlowActive(entry_id, flow_id, false); });
1242-
}
1243-
else
1244-
{
1245-
setMACsecFlowActive(macsec_sc->m_entry_id, macsec_sc->m_flow_id, false);
1246-
}
1291+
return false;
12471292
}
12481293
}
12491294

1250-
recover.clear();
12511295
return true;
12521296
}
12531297

@@ -1721,6 +1765,42 @@ bool MACsecOrch::deleteMACsecSC(sai_object_id_t sc_id)
17211765
return true;
17221766
}
17231767

1768+
bool MACsecOrch::updateMACsecAttr(sai_object_type_t object_type, sai_object_id_t object_id, const sai_attribute_t &attr)
1769+
{
1770+
SWSS_LOG_ENTER();
1771+
1772+
sai_status_t status = SAI_STATUS_SUCCESS;
1773+
1774+
if (object_type == SAI_OBJECT_TYPE_MACSEC_PORT)
1775+
{
1776+
status = sai_macsec_api->set_macsec_port_attribute(object_id, &attr);
1777+
}
1778+
else if (object_type == SAI_OBJECT_TYPE_MACSEC_SC)
1779+
{
1780+
status = sai_macsec_api->set_macsec_sc_attribute(object_id, &attr);
1781+
}
1782+
else if (object_type == SAI_OBJECT_TYPE_MACSEC_SA)
1783+
{
1784+
status = sai_macsec_api->set_macsec_sa_attribute(object_id, &attr);
1785+
}
1786+
else
1787+
{
1788+
SWSS_LOG_ERROR("Wrong type %s", sai_serialize_object_type(object_type).c_str());
1789+
return false;
1790+
}
1791+
1792+
if (status != SAI_STATUS_SUCCESS)
1793+
{
1794+
task_process_status handle_status = handleSaiSetStatus(SAI_API_MACSEC, status);
1795+
if (handle_status != task_success)
1796+
{
1797+
return parseHandleSaiStatusFailure(handle_status);
1798+
}
1799+
}
1800+
1801+
return true;
1802+
}
1803+
17241804
task_process_status MACsecOrch::createMACsecSA(
17251805
const std::string &port_sci_an,
17261806
const TaskArgs &sa_attr,

orchagent/macsecorch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class MACsecOrch : public Orch
132132
sai_object_id_t switch_id,
133133
sai_macsec_direction_t direction);
134134
bool updateMACsecPort(MACsecPort &macsec_port, const TaskArgs & port_attr);
135+
bool updateMACsecSCs(MACsecPort &macsec_port, std::function<bool(MACsecOrch::MACsecSC &)> action);
135136
bool deleteMACsecPort(
136137
const MACsecPort &macsec_port,
137138
const std::string &port_name,
@@ -179,6 +180,8 @@ class MACsecOrch : public Orch
179180
sai_macsec_direction_t direction);
180181
bool deleteMACsecSC(sai_object_id_t sc_id);
181182

183+
bool updateMACsecAttr(sai_object_type_t object_type, sai_object_id_t object_id, const sai_attribute_t &attr);
184+
182185
/* MACsec SA */
183186
task_process_status createMACsecSA(
184187
const std::string &port_sci_an,

tests/test_macsec.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,54 @@ def test_macsec_term_orch(self, dvs: conftest.DockerVirtualSwitch, testlog):
699699
1)
700700
assert(not inspector.get_macsec_port(macsec_port))
701701

702+
def test_macsec_attribute_change(self, dvs: conftest.DockerVirtualSwitch, testlog):
703+
port_name = "Ethernet0"
704+
local_mac_address = "00-15-5D-78-FF-C1"
705+
peer_mac_address = "00-15-5D-78-FF-C2"
706+
macsec_port_identifier = 1
707+
macsec_port = "macsec_eth1"
708+
sak = "0" * 32
709+
auth_key = "0" * 32
710+
packet_number = 1
711+
ssci = 1
712+
salt = "0" * 24
713+
714+
wpa = WPASupplicantMock(dvs)
715+
inspector = MACsecInspector(dvs)
716+
717+
self.init_macsec(
718+
wpa,
719+
port_name,
720+
local_mac_address,
721+
macsec_port_identifier)
722+
wpa.set_macsec_control(port_name, True)
723+
wpa.config_macsec_port(port_name, {"enable_encrypt": False})
724+
wpa.config_macsec_port(port_name, {"cipher_suite": "GCM-AES-256"})
725+
self.establish_macsec(
726+
wpa,
727+
port_name,
728+
local_mac_address,
729+
peer_mac_address,
730+
macsec_port_identifier,
731+
0,
732+
sak,
733+
packet_number,
734+
auth_key,
735+
ssci,
736+
salt)
737+
macsec_info = inspector.get_macsec_port(macsec_port)
738+
assert("encrypt off" in macsec_info)
739+
assert("GCM-AES-256" in macsec_info)
740+
self.deinit_macsec(
741+
wpa,
742+
inspector,
743+
port_name,
744+
macsec_port,
745+
local_mac_address,
746+
peer_mac_address,
747+
macsec_port_identifier,
748+
0)
749+
702750

703751
# Add Dummy always-pass test at end as workaroud
704752
# for issue when Flaky fail on final test it invokes module tear-down

0 commit comments

Comments
 (0)