Skip to content

Commit

Permalink
[FlexCounter] Add support for ACL counters (sonic-net#953)
Browse files Browse the repository at this point in the history
Added support for ACL counters to Flex Counter Infrastructure.
  • Loading branch information
stepanblyschak committed Nov 8, 2021
1 parent 67b3136 commit 6f1a0ea
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 2 deletions.
17 changes: 17 additions & 0 deletions meta/SaiSerialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,14 @@ std::string sai_serialize_macsec_sa_attr(
return sai_serialize_enum(attr, &sai_metadata_enum_sai_macsec_sa_attr_t);
}

std::string sai_serialize_acl_counter_attr(
_In_ const sai_acl_counter_attr_t &attr)
{
SWSS_LOG_ENTER();

return sai_serialize_enum(attr, &sai_metadata_enum_sai_acl_counter_attr_t);
}

std::string sai_serialize_switch_oper_status(
_In_ sai_object_id_t switch_id,
_In_ sai_switch_oper_status_t status)
Expand Down Expand Up @@ -4192,6 +4200,15 @@ void sai_deserialize_macsec_sa_attr(
sai_deserialize_enum(s, &sai_metadata_enum_sai_macsec_sa_attr_t, (int32_t&)attr);
}

void sai_deserialize_acl_counter_attr(
_In_ const std::string& s,
_Out_ sai_acl_counter_attr_t& attr)
{
SWSS_LOG_ENTER();

sai_deserialize_enum(s, &sai_metadata_enum_sai_acl_counter_attr_t, (int32_t&)attr);
}

// sairedis

void sai_deserialize(
Expand Down
7 changes: 7 additions & 0 deletions meta/sai_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ std::string sai_serialize_hex_binary(
std::string sai_serialize_macsec_sa_attr(
_In_ const sai_macsec_sa_attr_t &attr);

std::string sai_serialize_acl_counter_attr(
_In_ const sai_acl_counter_attr_t &attr);

std::string sai_serialize_switch_oper_status(
_In_ sai_object_id_t switch_id,
_In_ sai_switch_oper_status_t status);
Expand Down Expand Up @@ -433,6 +436,10 @@ void sai_deserialize_macsec_sa_attr(
_In_ const std::string& s,
_Out_ sai_macsec_sa_attr_t& attr);

void sai_deserialize_acl_counter_attr(
_In_ const std::string& s,
_Out_ sai_acl_counter_attr_t& attr);

// sairedis

void sai_deserialize(
Expand Down
124 changes: 124 additions & 0 deletions syncd/FlexCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ FlexCounter::MACsecSAAttrIds::MACsecSAAttrIds(
// empty intentionally
}

FlexCounter::AclCounterAttrIds::AclCounterAttrIds(
_In_ sai_object_id_t aclCounter,
_In_ const std::vector<sai_acl_counter_attr_t> &aclCounterIds):
m_aclCounterId(aclCounter),
m_aclCounterAttrIds(aclCounterIds)
{
SWSS_LOG_ENTER();
}

FlexCounter::TunnelCounterIds::TunnelCounterIds(
_In_ sai_object_id_t tunnelRid,
_In_ const std::vector<sai_tunnel_stat_t> &tunnelIds):
Expand Down Expand Up @@ -479,6 +488,28 @@ void FlexCounter::setMACsecSAAttrList(
addCollectCountersHandler(MACSEC_SA_ATTR_ID_LIST, &FlexCounter::collectMACsecSAAttrs);
}

void FlexCounter::setAclCounterAttrList(
_In_ sai_object_id_t aclCounterVid,
_In_ sai_object_id_t aclCounterRid,
_In_ const std::vector<sai_acl_counter_attr_t> &attrIds)
{
SWSS_LOG_ENTER();

auto it = m_aclCounterAttrIdsMap.find(aclCounterVid);

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

auto aclCounterAttrIds = std::make_shared<AclCounterAttrIds>(aclCounterRid, attrIds);

m_aclCounterAttrIdsMap.emplace(aclCounterVid, aclCounterAttrIds);

addCollectCountersHandler(ACL_COUNTER_ATTR_ID_LIST, &FlexCounter::collectAclCounterAttrs);
}

void FlexCounter::setRifCounterList(
_In_ sai_object_id_t rifVid,
_In_ sai_object_id_t rifRid,
Expand Down Expand Up @@ -770,6 +801,29 @@ void FlexCounter::removeMACsecSA(
}
}

void FlexCounter::removeAclCounter(
_In_ sai_object_id_t aclCounterVid)
{
SWSS_LOG_ENTER();

auto itr = m_aclCounterAttrIdsMap.find(aclCounterVid);

if (itr != m_aclCounterAttrIdsMap.end())
{
m_aclCounterAttrIdsMap.erase(itr);

if (m_aclCounterAttrIdsMap.empty())
{
removeCollectCountersHandler(ACL_COUNTER_ATTR_ID_LIST);
}
}
else
{
SWSS_LOG_WARN("Trying to remove nonexisting ACL counter %s",
sai_serialize_object_id(aclCounterVid).c_str());
}
}

void FlexCounter::removeRif(
_In_ sai_object_id_t rifVid)
{
Expand Down Expand Up @@ -1075,6 +1129,7 @@ bool FlexCounter::allIdsEmpty() const
m_bufferPoolCounterIdsMap.empty() &&
m_switchDebugCounterIdsMap.empty() &&
m_macsecSAAttrIdsMap.empty() &&
m_aclCounterAttrIdsMap.empty() &&
m_tunnelCounterIdsMap.empty();
}

Expand Down Expand Up @@ -1588,6 +1643,58 @@ void FlexCounter::collectMACsecSAAttrs(
}
}

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

for (const auto &kv: m_aclCounterAttrIdsMap)
{
const auto &aclCounterVid = kv.first;
const auto &aclCounterRid = kv.second->m_aclCounterId;
const auto &aclCounterAttrIds = kv.second->m_aclCounterAttrIds;

std::vector<sai_attribute_t> aclCounterAttrs(aclCounterAttrIds.size());

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

sai_status_t status = m_vendorSai->get(
SAI_OBJECT_TYPE_ACL_COUNTER,
aclCounterRid,
static_cast<uint32_t>(aclCounterAttrs.size()),
aclCounterAttrs.data());

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_WARN(
"Failed to get attr of ACL counter %s: %s",
sai_serialize_object_id(aclCounterVid).c_str(),
sai_serialize_status(status).c_str());
continue;
}

std::vector<swss::FieldValueTuple> values;

for (const auto& aclCounterAttr : aclCounterAttrs)
{
auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_ACL_COUNTER, aclCounterAttr.id);
if (!meta)
{
SWSS_LOG_THROW("Failed to get metadata for SAI_OBJECT_TYPE_ACL_COUNTER");
}
values.emplace_back(meta->attridname, sai_serialize_attr_value(*meta, aclCounterAttr));
}

// Write counters to DB
auto aclCounterVidStr = sai_serialize_object_id(aclCounterVid);

countersTable.set(aclCounterVidStr, values);
}
}

void FlexCounter::collectRifCounters(
_In_ swss::Table &countersTable)
{
Expand Down Expand Up @@ -2607,6 +2714,10 @@ void FlexCounter::removeCounter(
{
removeMACsecSA(vid);
}
else if (objectType == SAI_OBJECT_TYPE_ACL_COUNTER)
{
removeAclCounter(vid);
}
else if (objectType == SAI_OBJECT_TYPE_TUNNEL)
{
removeTunnel(vid);
Expand Down Expand Up @@ -2758,6 +2869,19 @@ void FlexCounter::addCounter(

setMACsecSAAttrList(vid, rid, macsecSAIds);
}
else if (objectType == SAI_OBJECT_TYPE_ACL_COUNTER && field == ACL_COUNTER_ATTR_ID_LIST)
{
std::vector<sai_acl_counter_attr_t> aclCounterIds;

for (const auto &str : idStrings)
{
sai_acl_counter_attr_t attr{};
sai_deserialize_acl_counter_attr(str, attr);
aclCounterIds.push_back(attr);
}

setAclCounterAttrList(vid, rid, aclCounterIds);
}
else if (objectType == SAI_OBJECT_TYPE_BUFFER_POOL && field == BUFFER_POOL_COUNTER_ID_LIST)
{
counterIds = idStrings;
Expand Down
25 changes: 23 additions & 2 deletions syncd/FlexCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ namespace syncd
void removeMACsecSA(
_In_ sai_object_id_t macsecSAVid);

void removeAclCounter(
_In_ sai_object_id_t aclCounterVid);

void removeTunnel(
_In_ sai_object_id_t tunnelVid);

Expand Down Expand Up @@ -178,6 +181,11 @@ namespace syncd
_In_ sai_object_id_t macsecSARid,
_In_ const std::vector<sai_macsec_sa_attr_t> &attrIds);

void setAclCounterAttrList(
_In_ sai_object_id_t aclCounterVid,
_In_ sai_object_id_t aclCounterRid,
_In_ const std::vector<sai_acl_counter_attr_t> &attrIds);

private: // is counter supported

bool isPortCounterSupported(
Expand Down Expand Up @@ -362,6 +370,16 @@ namespace syncd
std::vector<sai_macsec_sa_attr_t> m_macsecSAAttrIds;
};

struct AclCounterAttrIds
{
AclCounterAttrIds(
_In_ sai_object_id_t aclCounter,
_In_ const std::vector<sai_acl_counter_attr_t> &aclCounterIds);

sai_object_id_t m_aclCounterId;
std::vector<sai_acl_counter_attr_t> m_aclCounterAttrIds;
};

struct TunnelCounterIds
{
TunnelCounterIds(
Expand All @@ -371,6 +389,7 @@ namespace syncd
sai_object_id_t m_tunnelId;
std::vector<sai_tunnel_stat_t> m_tunnelCounterIds;
};

private:

void collectCounters(
Expand Down Expand Up @@ -429,6 +448,9 @@ namespace syncd
void collectMACsecSAAttrs(
_In_ swss::Table &countersTable);

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

private:

void addCollectCountersHandler(
Expand Down Expand Up @@ -466,11 +488,10 @@ namespace syncd
std::map<sai_object_id_t, std::shared_ptr<BufferPoolCounterIds>> m_bufferPoolCounterIdsMap;
std::map<sai_object_id_t, std::shared_ptr<SwitchCounterIds>> m_switchDebugCounterIdsMap;
std::map<sai_object_id_t, std::shared_ptr<TunnelCounterIds>> m_tunnelCounterIdsMap;

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;
std::map<sai_object_id_t, std::shared_ptr<AclCounterAttrIds>> m_aclCounterAttrIdsMap;

private:

Expand Down

0 comments on commit 6f1a0ea

Please sign in to comment.