Skip to content

Commit

Permalink
Add FlexCounter for MACsec SA
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <ganze718@gmail.com>
  • Loading branch information
Pterosaur committed Oct 29, 2020
1 parent 0bf336a commit 7ca38ba
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
117 changes: 116 additions & 1 deletion syncd/FlexCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ FlexCounter::BufferPoolCounterIds::BufferPoolCounterIds(
SWSS_LOG_ENTER();
}

FlexCounter::MACsecSAAttrIds::MACsecSAAttrIds(
_In_ sai_object_id_t macsecSA,
_In_ const std::vector<sai_macsec_sa_attr_t> &macsecSAIds):
m_macsecSAId(macsecSA), m_macsecSAAttrIds(macsecSAIds)
{
SWSS_LOG_ENTER();
}

void FlexCounter::setPollInterval(
_In_ uint32_t pollInterval)
{
Expand Down Expand Up @@ -437,6 +445,28 @@ void FlexCounter::setPriorityGroupAttrList(
addCollectCountersHandler(PG_ATTR_ID_LIST, &FlexCounter::collectPriorityGroupAttrs);
}

void FlexCounter::setMACsecSAAttrList(
_In_ sai_object_id_t macsecSAVid,
_In_ sai_object_id_t macsecSARid,
_In_ const std::vector<sai_macsec_sa_attr_t> &attrIds)
{
SWSS_LOG_ENTER();

auto it = m_macsecSAAttrIdsMap.find(macsecSAVid);

if (it != m_macsecSAAttrIdsMap.end())
{
it->second->m_macsecSAAttrIds = attrIds;
return;
}

auto macsecSAAttrIds = std::make_shared<MACsecSAAttrIds>(macsecSARid, attrIds);

m_macsecSAAttrIdsMap.emplace(macsecSAVid, macsecSAAttrIds);

addCollectCountersHandler(MACSEC_SA_ATTR_ID_LIST, &FlexCounter::collectMACsecSAAttrs);
}

void FlexCounter::setRifCounterList(
_In_ sai_object_id_t rifVid,
_In_ sai_object_id_t rifRid,
Expand Down Expand Up @@ -664,6 +694,25 @@ void FlexCounter::removePriorityGroup(
}
}

void FlexCounter::removeMACsecSA(
_In_ sai_object_id_t macsecSAVid)
{
auto itr = m_macsecSAAttrIdsMap.find(macsecSAVid);
if (itr != m_macsecSAAttrIdsMap.end())
{
m_macsecSAAttrIdsMap.erase(itr);
if (m_macsecSAAttrIdsMap.empty())
{
removeCollectCountersHandler(MACSEC_SA_ATTR_ID_LIST);
}
}
else
{
SWSS_LOG_WARN("Trying to remove nonexisting MACsec SA %s",
sai_serialize_object_id(macsecSAVid).c_str());
}
}

void FlexCounter::removeRif(
_In_ sai_object_id_t rifVid)
{
Expand Down Expand Up @@ -923,7 +972,8 @@ bool FlexCounter::allIdsEmpty() const
m_portDebugCounterIdsMap.empty() &&
m_rifCounterIdsMap.empty() &&
m_bufferPoolCounterIdsMap.empty() &&
m_switchDebugCounterIdsMap.empty();
m_switchDebugCounterIdsMap.empty() &&
m_macsecSAAttrIdsMap.empty();
}

bool FlexCounter::allPluginsEmpty() const
Expand Down Expand Up @@ -1367,6 +1417,54 @@ void FlexCounter::collectPriorityGroupAttrs(
}
}

void FlexCounter::collectMACsecSAAttrs(
_In_ swss::Table &countersTable)
{
SWSS_LOG_ENTER();

// Collect attrs for every registered MACsec SA
for (const auto &kv: m_macsecSAAttrIdsMap)
{
const auto &macsecSAVid = kv.first;
const auto &macsecSARid = kv.second->m_macsecSAId;
const auto &macsecSAAttrIds = kv.second->m_macsecSAAttrIds;

std::vector<sai_attribute_t> macsecSAAttr(macsecSAAttrIds.size());

for (size_t i = 0; i < macsecSAAttrIds.size(); i++)
{
macsecSAAttr[i].id = macsecSAAttrIds[i];
}

// Get MACsec SA attr
sai_status_t status = m_vendorSai->get(
SAI_OBJECT_TYPE_MACSEC_SA,
macsecSARid,
static_cast<uint32_t>(macsecSAAttr.size()),
macsecSAAttr.data());

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_WARN("Failed to get attr of MACsec SA 0x%" PRIx64 ": %d", macsecSAVid, status);
continue;
}

// Push all counter values to a single vector
std::vector<swss::FieldValueTuple> values;

for (size_t i = 0; i != macsecSAAttrIds.size(); i++)
{
const std::string &attrName = sai_serialize_enum(macsecSAAttrIds[i], &sai_metadata_enum_sai_macsec_sa_attr_t);
auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_MACSEC_SA, macsecSAAttr[i].id);
values.emplace_back(attrName, sai_serialize_attr_value(*meta, macsecSAAttr[i]));
}
// Write counters to DB
std::string macsecSAVidStr = sai_serialize_object_id(macsecSAVid);

countersTable.set(macsecSAVidStr, values, "");
}
}

void FlexCounter::collectRifCounters(
_In_ swss::Table &countersTable)
{
Expand Down Expand Up @@ -1988,6 +2086,10 @@ void FlexCounter::removeCounter(
{
removeSwitchDebugCounters(vid);
}
else if (objectType == SAI_OBJECT_TYPE_MACSEC_SA)
{
removeMACsecSA(vid);
}
else
{
SWSS_LOG_ERROR("Object type for removal not supported, %s",
Expand Down Expand Up @@ -2122,6 +2224,19 @@ void FlexCounter::addCounter(

setSwitchDebugCounterList(vid, rid, switchCounterIds);
}
else if (objectType == SAI_OBJECT_TYPE_MACSEC_SA && field == MACSEC_SA_ATTR_ID_LIST)
{
std::vector<sai_macsec_sa_attr_t> MACsecSAIds;

for (const auto &str : idStrings)
{
int32_t attr;
sai_deserialize_enum(str, &sai_metadata_enum_sai_macsec_sa_attr_t, attr);
MACsecSAIds.push_back(static_cast<sai_macsec_sa_attr_t>(attr));
}

setMACsecSAAttrList(vid, rid, MACsecSAIds);
}
else if (objectType == SAI_OBJECT_TYPE_BUFFER_POOL && field == BUFFER_POOL_COUNTER_ID_LIST)
{
counterIds = idStrings;
Expand Down
23 changes: 23 additions & 0 deletions syncd/FlexCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ namespace syncd
void removeSwitchDebugCounters(
_In_ sai_object_id_t switchVid);

void removeMACsecSA(
_In_ sai_object_id_t macsecSAVid);

private: // set counter list

void setPortCounterList(
Expand Down Expand Up @@ -160,6 +163,11 @@ namespace syncd
_In_ sai_object_id_t priorityGroupRid,
_In_ const std::vector<sai_ingress_priority_group_attr_t> &attrIds);

void setMACsecSAAttrList(
_In_ sai_object_id_t macsecSAVid,
_In_ sai_object_id_t macsecSARid,
_In_ const std::vector<sai_macsec_sa_attr_t> &attrIds);

private: // is counter supported

bool isPortCounterSupported(
Expand Down Expand Up @@ -290,6 +298,16 @@ namespace syncd
std::vector<sai_router_interface_stat_t> rifCounterIds;
};

struct MACsecSAAttrIds
{
MACsecSAAttrIds(
_In_ sai_object_id_t macsecSA,
_In_ const std::vector<sai_macsec_sa_attr_t> &macsecSAIds);

sai_object_id_t m_macsecSAId;
std::vector<sai_macsec_sa_attr_t> m_macsecSAAttrIds;
};

private:

void collectCounters(
Expand Down Expand Up @@ -342,6 +360,9 @@ namespace syncd
void collectPriorityGroupAttrs(
_In_ swss::Table &countersTable);

void collectMACsecSAAttrs(
_In_ swss::Table &countersTable);

private:

void addCollectCountersHandler(
Expand Down Expand Up @@ -380,6 +401,8 @@ namespace syncd
std::map<sai_object_id_t, std::shared_ptr<QueueAttrIds>> m_queueAttrIdsMap;
std::map<sai_object_id_t, std::shared_ptr<IngressPriorityGroupAttrIds>> m_priorityGroupAttrIdsMap;

std::map<sai_object_id_t, std::shared_ptr<MACsecSAAttrIds>> m_macsecSAAttrIdsMap;

private:

bool m_runFlexCounterThread;
Expand Down

0 comments on commit 7ca38ba

Please sign in to comment.