Skip to content

Commit 42af975

Browse files
authored
[vslib]: Update packet number of MACsec SA at runtime (#1007)
1 parent f67bcdd commit 42af975

7 files changed

+103
-0
lines changed

unittest/vslib/TestMACsecManager.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ TEST(MACsecManager, create_macsec_ingress_sa)
2727
attr.m_sak = "";
2828
manager.create_macsec_ingress_sa(attr);
2929
}
30+
31+
TEST(MACsecManager, update_macsec_sa_pn)
32+
{
33+
// This is a system call that may not be valid in the test environment,
34+
// So, this case is just for the testing coverage checking.
35+
36+
MACsecManager manager;
37+
38+
MACsecAttr attr;
39+
attr.m_vethName = "eth0";
40+
attr.m_macsecName = "macsec_eth0";
41+
attr.m_sci = "02:42:ac:11:00:03";
42+
attr.m_an = 0;
43+
attr.m_pn = 1;
44+
attr.m_cipher = MACsecAttr::CIPHER_NAME_GCM_AES_XPN_128;
45+
attr.m_ssci = 0x1;
46+
attr.m_salt = "";
47+
attr.m_authKey = "";
48+
attr.m_sak = "";
49+
manager.update_macsec_sa_pn(attr, 2);
50+
}

unittest/vslib/TestSwitchStateBaseMACsec.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,19 @@ TEST(SwitchStateBase, removeMACsecPort)
110110
EXPECT_EQ(1, ss.m_macsecFlowPortMap.size());
111111
EXPECT_EQ(1, ss.m_uncreatedIngressMACsecSAs.size());
112112
}
113+
114+
TEST(SwitchStateBase, setMACsecSA)
115+
{
116+
// Due to this function highly depends on system environment which cannot be tested directly,
117+
// Just create this Test block for passing coverage
118+
auto sc = std::make_shared<SwitchConfig>(0, "");
119+
auto scc = std::make_shared<SwitchConfigContainer>();
120+
121+
SwitchStateBase ss(
122+
0x2100000000,
123+
std::make_shared<RealObjectIdManager>(0, scc),
124+
sc);
125+
126+
ss.setMACsecSA(0, nullptr);
127+
}
128+

vslib/MACsecManager.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,33 @@ bool MACsecManager::enable_macsec_filter(
292292
return true;
293293
}
294294

295+
bool MACsecManager::update_macsec_sa_pn(
296+
_In_ const MACsecAttr &attr,
297+
_In_ sai_uint64_t pn)
298+
{
299+
SWSS_LOG_ENTER();
300+
301+
std::ostringstream ostream;
302+
ostream
303+
<< "/sbin/ip macsec set "
304+
<< shellquote(attr.m_macsecName);
305+
306+
if (attr.m_direction == SAI_MACSEC_DIRECTION_EGRESS)
307+
{
308+
ostream << " tx";
309+
}
310+
else
311+
{
312+
ostream << " rx sci " << attr.m_sci;
313+
}
314+
315+
ostream << " sa " << attr.m_an << " pn " << pn;
316+
317+
SWSS_LOG_NOTICE("%s", ostream.str().c_str());
318+
319+
return exec(ostream.str());
320+
}
321+
295322
bool MACsecManager::get_macsec_sa_pn(
296323
_In_ const MACsecAttr &attr,
297324
_Out_ sai_uint64_t &pn) const

vslib/MACsecManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ namespace saivs
3636
_In_ const std::string &macsecInterface,
3737
_In_ bool enable);
3838

39+
bool update_macsec_sa_pn(
40+
_In_ const MACsecAttr &attr,
41+
_In_ sai_uint64_t pn);
42+
3943
bool get_macsec_sa_pn(
4044
_In_ const MACsecAttr &attr,
4145
_Out_ sai_uint64_t &pn) const;

vslib/SwitchStateBase.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ sai_status_t SwitchStateBase::set(
520520
return setAclEntry(objectId, attr);
521521
}
522522

523+
if (objectType == SAI_OBJECT_TYPE_MACSEC_SA)
524+
{
525+
sai_object_id_t objectId;
526+
sai_deserialize_object_id(serializedObjectId, objectId);
527+
return setMACsecSA(objectId, attr);
528+
}
529+
523530
return set_internal(objectType, serializedObjectId, attr);
524531
}
525532

vslib/SwitchStateBase.h

+4
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ namespace saivs
521521
_In_ sai_object_id_t entry_id,
522522
_In_ const sai_attribute_t* attr);
523523

524+
sai_status_t setMACsecSA(
525+
_In_ sai_object_id_t macsec_sa_id,
526+
_In_ const sai_attribute_t* attr);
527+
524528
sai_status_t createMACsecPort(
525529
_In_ sai_object_id_t macsec_sa_id,
526530
_In_ sai_object_id_t switch_id,

vslib/SwitchStateBaseMACsec.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ sai_status_t SwitchStateBase::setAclEntryMACsecFlowActive(
138138
return SAI_STATUS_SUCCESS;
139139
}
140140

141+
sai_status_t SwitchStateBase::setMACsecSA(
142+
_In_ sai_object_id_t macsec_sa_id,
143+
_In_ const sai_attribute_t* attr)
144+
{
145+
SWSS_LOG_ENTER();
146+
147+
MACsecAttr macsecAttr;
148+
149+
CHECK_STATUS(loadMACsecAttr(SAI_OBJECT_TYPE_MACSEC_SA, macsec_sa_id, macsecAttr));
150+
151+
if (attr->id == SAI_MACSEC_SA_ATTR_MINIMUM_INGRESS_XPN || attr->id == SAI_MACSEC_SA_ATTR_CONFIGURED_EGRESS_XPN)
152+
{
153+
if (!m_macsecManager.update_macsec_sa_pn(macsecAttr, attr->value.u64))
154+
{
155+
SWSS_LOG_WARN("Fail to update PN (%" PRIu64 ") of MACsec SA %s", attr->value.u64, sai_serialize_object_id(macsec_sa_id).c_str());
156+
157+
return SAI_STATUS_FAILURE;
158+
}
159+
}
160+
161+
auto sid = sai_serialize_object_id(macsec_sa_id);
162+
return set_internal(SAI_OBJECT_TYPE_MACSEC_SA, sid, attr);
163+
}
164+
141165
sai_status_t SwitchStateBase::createMACsecPort(
142166
_In_ sai_object_id_t macsecSaId,
143167
_In_ sai_object_id_t switchId,

0 commit comments

Comments
 (0)