Device: Write MACsec configs by network ID

This commit is contained in:
Thomas Stoddard
2026-09-24 19:49:23 -04:00
committed by Kyle Schwarz
parent b54bf80c40
commit f0b1341cdf
14 changed files with 154 additions and 14 deletions
+1
View File
@@ -570,6 +570,7 @@ if(LIBICSNEO_BUILD_UNIT_TESTS)
test/unit/a2bencoderdecodertest.cpp
test/unit/mdioencoderdecodertest.cpp
test/unit/livedataencoderdecodertest.cpp
test/unit/macsectest.cpp
test/unit/ringbuffertest.cpp
test/unit/apperrordecodertest.cpp
test/unit/icsneoc2.cpp
@@ -154,7 +154,6 @@ void init_macsecconfig(pybind11::module_ & m)
.def("set_storage", &MACsecConfig::setStorage, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("clear", &MACsecConfig::clear, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("serialize", &MACsecConfig::serialize, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_bin_index", &MACsecConfig::getBinIndex, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_type", &MACsecConfig::getType, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_max_num_rule", &MACsecConfig::getMaxNumRule, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_max_num_secy", &MACsecConfig::getMaxNumSecY, pybind11::call_guard<pybind11::gil_scoped_release>())
+2 -1
View File
@@ -34,6 +34,7 @@ void init_device(pybind11::module_& m) {
.def("get_serial", &Device::getSerial)
.def("get_pcb_serial", &Device::getPCBSerial, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_mac_addresses", &Device::getMACAddresses, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_macsec_networks", &Device::getMACsecNetworks)
.def("get_supported_rx_networks", &Device::getSupportedRXNetworks, pybind11::return_value_policy::reference)
.def("get_supported_tx_networks", &Device::getSupportedTXNetworks, pybind11::return_value_policy::reference)
.def("get_tc10_status", &Device::getTC10Status, pybind11::call_guard<pybind11::gil_scoped_release>())
@@ -64,7 +65,7 @@ void init_device(pybind11::module_& m) {
.def("set_value_live_data", &Device::setValueLiveData, pybind11::arg("message"), pybind11::call_guard<pybind11::gil_scoped_release>())
.def("transmit", pybind11::overload_cast<std::shared_ptr<Frame>>(&Device::transmit), pybind11::call_guard<pybind11::gil_scoped_release>())
.def("upload_coremini", [](Device& device, std::string& path, Disk::MemoryType memType) { std::ifstream ifs(path, std::ios::binary); return device.uploadCoremini(ifs, memType); }, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("write_macsec_config", &Device::writeMACsecConfig, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("write_macsec_config", &Device::writeMACsecConfig, pybind11::arg("config"), pybind11::arg("network"), pybind11::call_guard<pybind11::gil_scoped_release>())
.def("send_eth_phy_msg", &Device::sendEthPhyMsg, pybind11::arg("message"), pybind11::arg("timeout") = std::chrono::milliseconds(50), pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_chip_versions", &Device::getChipVersions, pybind11::arg("refreshComponents") = true, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("supports_disk_formatting", &Device::supportsDiskFormatting, pybind11::call_guard<pybind11::gil_scoped_release>())
+2 -6
View File
@@ -231,16 +231,16 @@ MACsecConfig::MACsecConfig(const DeviceType& deviceType) : type(deviceType) {
case icsneo::DeviceType::Enum::RADMoon3:
case icsneo::DeviceType::Enum::RADEpsilon:
case icsneo::DeviceType::Enum::RADGigastar2:
case icsneo::DeviceType::Enum::RADComet2:
case icsneo::DeviceType::Enum::RADComet3:
maxSecY = 2;
maxRule = 2;
maxSa = 4;
binIndex = 0;
break;
default:
maxSecY = 0;
maxSa = 0;
maxRule = 0;
binIndex = 0;
ReportEvent(APIEvent::Type::MACsecNotSupported, APIEvent::Severity::Error);
return;
}
@@ -515,10 +515,6 @@ MACsecConfig::operator bool() const {
return (maxSa != 0) || (maxSecY != 0) || (maxRule != 0);
}
uint16_t MACsecConfig::getBinIndex() const {
return binIndex;
}
DeviceType MACsecConfig::getType() const {
return type;
}
+17 -2
View File
@@ -3761,7 +3761,15 @@ std::optional<GPTPStatus> Device::getGPTPStatus(std::chrono::milliseconds timeou
return *retMsg;
}
bool Device::writeMACsecConfig(const MACsecConfig& cfg) {
std::vector<Network> Device::getMACsecNetworks() const {
std::vector<Network> networks;
for(const auto& network : getMACsecNetworkMap()) {
networks.emplace_back(network.first);
}
return networks;
}
bool Device::writeMACsecConfig(const MACsecConfig& cfg, Network::NetID network) {
if(!cfg) {
report(APIEvent::Type::MACsecNotSupported, APIEvent::Severity::Error);
return false;
@@ -3772,8 +3780,15 @@ bool Device::writeMACsecConfig(const MACsecConfig& cfg) {
return false;
}
const auto& networks = getMACsecNetworkMap();
const auto binaryIndex = networks.find(network);
if(binaryIndex == networks.end()) {
report(APIEvent::Type::MACsecNotSupported, APIEvent::Severity::Error);
return false;
}
std::vector<uint8_t> raw = cfg.serialize();
return writeBinaryFile(raw, cfg.getBinIndex());
return writeBinaryFile(raw, binaryIndex->second);
}
bool Device::enableNetworkCommunication(bool enable, uint32_t timeout) {
+1 -1
View File
@@ -77,7 +77,7 @@ int main(int, char**) {
cfg.setTxEnable(true);
// Write config to the device
if(!device->writeMACsecConfig(cfg)) {
if(!device->writeMACsecConfig(cfg, icsneo::Network::NetID::AE_01)) {
std::cout << "Failed to write MACsec config" << std::endl;
std::cout << icsneo::GetLastError() << std::endl;
return -1;
-2
View File
@@ -150,7 +150,6 @@ private:
uint8_t maxSecY;
uint8_t maxSa;
uint8_t maxRule;
uint16_t binIndex;
DeviceType type;
std::vector<MACsecRxRule> rxRule;
@@ -205,7 +204,6 @@ public:
void clear();
std::vector<uint8_t> serialize() const;
operator bool() const;
uint16_t getBinIndex() const;
DeviceType getType() const;
uint8_t getMaxNumRule() const;
uint8_t getMaxNumSecY() const;
+10 -1
View File
@@ -12,6 +12,7 @@
#include <type_traits>
#include <optional>
#include <unordered_map>
#include <map>
#include <set>
#include <unordered_set>
#include <chrono>
@@ -897,7 +898,8 @@ public:
std::optional<GPTPStatus> getGPTPStatus(std::chrono::milliseconds timeout = std::chrono::milliseconds(100));
/* MACsec support */
virtual bool writeMACsecConfig(const MACsecConfig& cfg);
std::vector<Network> getMACsecNetworks() const;
bool writeMACsecConfig(const MACsecConfig& cfg, Network::NetID network);
std::shared_ptr<DeviceExtension> getExtension(const std::string& name) const;
@@ -917,6 +919,13 @@ public:
bool iso15765SetupRxFlowControl(const Network& network, const Iso15765MessageArgs& msg);
protected:
using MACsecNetworkMap = std::map<Network::NetID, uint16_t>;
virtual const MACsecNetworkMap& getMACsecNetworkMap() const {
static const MACsecNetworkMap networks;
return networks;
}
bool online = false;
int messagePollingCallbackID = 0;
int internalHandlerCallbackID = 0;
@@ -70,6 +70,13 @@ public:
}
protected:
const MACsecNetworkMap& getMACsecNetworkMap() const override {
static const MACsecNetworkMap networks = {
{Network::NetID::AE_01, 0},
};
return networks;
}
RADComet2(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADComet2Settings, Disk::NeoMemoryDiskDriver, Disk::NeoMemoryDiskDriver>(makeDriver);
}
@@ -77,6 +77,13 @@ public:
}
protected:
const MACsecNetworkMap& getMACsecNetworkMap() const override {
static const MACsecNetworkMap networks = {
{Network::NetID::AE_01, 0},
};
return networks;
}
RADComet3(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADComet3Settings, Disk::NeoMemoryDiskDriver, Disk::NeoMemoryDiskDriver>(makeDriver);
}
@@ -217,6 +217,14 @@ public:
return chipVersions;
}
protected:
const MACsecNetworkMap& getMACsecNetworkMap() const override {
static const MACsecNetworkMap networks = {
{Network::NetID::AE_01, 0},
{Network::NetID::AE_02, 1},
};
return networks;
}
RADGigastar2(neodevice_t neodevice, const driver_factory_t &makeDriver) : Device(neodevice)
{
initialize<RADGigastar2Settings, Disk::ExtExtractorDiskReadDriver, Disk::NeoMemoryDiskDriver>(makeDriver);
@@ -38,6 +38,13 @@ public:
.add<ReconnectPhase>();
}
protected:
const MACsecNetworkMap& getMACsecNetworkMap() const override {
static const MACsecNetworkMap networks = {
{Network::NetID::AE_01, 0},
};
return networks;
}
RADMoon2ZL(neodevice_t neodevice, const driver_factory_t& makeDriver) : RADMoon2Base(neodevice) {
initialize<RADMoon2Settings>(makeDriver);
}
@@ -52,6 +52,14 @@ public:
.add<ReconnectPhase>();
}
protected:
const MACsecNetworkMap& getMACsecNetworkMap() const override {
static const MACsecNetworkMap networks = {
{Network::NetID::AE_01, 0},
{Network::NetID::ETHERNET_01, 1},
};
return networks;
}
RADMoon3(neodevice_t neodevice, const driver_factory_t& makeDriver) : Device(neodevice) {
initialize<RADMoon3Settings>(makeDriver);
}
+84
View File
@@ -0,0 +1,84 @@
#include <gtest/gtest.h>
#include <cstring>
#include "icsneo/communication/driver.h"
#include "icsneo/core/macseccfg.h"
#include "icsneo/disk/neomemorydiskdriver.h"
#include "icsneo/device/tree/radcomet2/radcomet2.h"
#include "icsneo/device/tree/radcomet3/radcomet3.h"
#include "icsneo/device/tree/radgigastar2/radgigastar2.h"
#include "icsneo/device/tree/radmoon2/radmoon2.h"
#include "icsneo/device/tree/radmoon2/radmoon2zl.h"
#include "icsneo/device/tree/radmoon3/radmoon3.h"
using namespace icsneo;
namespace {
class MACsecTestDriver : public Driver {
public:
MACsecTestDriver(const device_eventhandler_t& handler) : Driver(handler) {}
bool open() override { return true; }
bool isOpen() override { return true; }
bool close() override { return true; }
driver_finder_t getFinder() override {
return [](std::vector<FoundDevice>&) {};
}
};
FoundDevice makeFoundDevice(const char* serial) {
FoundDevice found;
std::memcpy(found.serial, serial, sizeof(found.serial) - 1);
found.serial[sizeof(found.serial) - 1] = '\0';
found.makeDriver = [](device_eventhandler_t handler, neodevice_t&) {
return std::make_unique<MACsecTestDriver>(handler);
};
return found;
}
std::vector<Network::NetID> getMACsecNetIDs(const Device& device) {
std::vector<Network::NetID> netIDs;
for(const auto& network : device.getMACsecNetworks())
netIDs.push_back(network.getNetID());
return netIDs;
}
TEST(MACsec, RADMoon2ZLOnlyMapsAutomotiveEthernet) {
RADMoon2 moon2(makeFoundDevice("RM0001"));
RADMoon2ZL moon2zl(makeFoundDevice("RN0001"));
EXPECT_TRUE(getMACsecNetIDs(moon2).empty());
EXPECT_EQ(getMACsecNetIDs(moon2zl), std::vector<Network::NetID>({Network::NetID::AE_01}));
}
TEST(MACsec, RADMoon3MapsIndependentPHYs) {
RADMoon3 moon3(makeFoundDevice("R30001"));
EXPECT_EQ(
getMACsecNetIDs(moon3),
std::vector<Network::NetID>({Network::NetID::AE_01, Network::NetID::ETHERNET_01})
);
}
TEST(MACsec, RADGigastar2MapsBothMACsecPorts) {
RADGigastar2 gigastar2(makeFoundDevice("GT0001"));
EXPECT_EQ(
getMACsecNetIDs(gigastar2),
std::vector<Network::NetID>({Network::NetID::AE_01, Network::NetID::AE_02})
);
}
TEST(MACsec, RADCometsMapOneMACsecPort) {
RADComet2 comet2(makeFoundDevice("RC0300"));
RADComet3 comet3(makeFoundDevice("C30001"));
EXPECT_EQ(getMACsecNetIDs(comet2), std::vector<Network::NetID>({Network::NetID::AE_01}));
EXPECT_EQ(getMACsecNetIDs(comet3), std::vector<Network::NetID>({Network::NetID::AE_01}));
EXPECT_TRUE(MACsecConfig(comet2.getType()));
EXPECT_TRUE(MACsecConfig(comet3.getType()));
}
} // namespace