From a9526d91f9e1d69d5e6104231577e94b6f2931af Mon Sep 17 00:00:00 2001 From: Stepan Blyschak Date: Wed, 15 Sep 2021 14:24:19 +0000 Subject: [PATCH 1/3] [FlexCounter] Add support for ACL counters Signed-off-by: Stepan Blyschak --- meta/SaiSerialize.cpp | 17 ++++++ meta/sai_serialize.h | 9 ++- syncd/FlexCounter.cpp | 124 +++++++++++++++++++++++++++++++++++++++++- syncd/FlexCounter.h | 24 +++++++- 4 files changed, 170 insertions(+), 4 deletions(-) diff --git a/meta/SaiSerialize.cpp b/meta/SaiSerialize.cpp index aab641c78..1fa52762b 100644 --- a/meta/SaiSerialize.cpp +++ b/meta/SaiSerialize.cpp @@ -965,6 +965,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) @@ -3786,6 +3794,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( diff --git a/meta/sai_serialize.h b/meta/sai_serialize.h index 969b85843..2fefd161c 100644 --- a/meta/sai_serialize.h +++ b/meta/sai_serialize.h @@ -141,6 +141,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); @@ -372,7 +375,7 @@ void sai_deserialize_free_queue_deadlock_ntf( void sai_deserialize_free_bfd_session_state_ntf( _In_ uint32_t count, - _In_ sai_bfd_session_state_notification_t* bfdsessionstate); + _In_ sai_bfd_session_state_notification_t* bfdsessionstate); void sai_deserialize_ingress_priority_group_attr( _In_ const std::string& s, @@ -386,6 +389,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( diff --git a/syncd/FlexCounter.cpp b/syncd/FlexCounter.cpp index 36d76c003..02a3c88d3 100644 --- a/syncd/FlexCounter.cpp +++ b/syncd/FlexCounter.cpp @@ -113,6 +113,15 @@ FlexCounter::MACsecSAAttrIds::MACsecSAAttrIds( // empty intentionally } +FlexCounter::AclCounterAttrIds::AclCounterAttrIds( + _In_ sai_object_id_t aclCounter, + _In_ const std::vector &aclCounterIds): + m_aclCounterId(aclCounter), + m_aclCounterAttrIds(aclCounterIds) +{ + SWSS_LOG_ENTER(); +} + void FlexCounter::setPollInterval( _In_ uint32_t pollInterval) { @@ -468,6 +477,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 &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(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, @@ -718,6 +749,30 @@ 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) { @@ -980,7 +1035,8 @@ bool FlexCounter::allIdsEmpty() const m_rifCounterIdsMap.empty() && m_bufferPoolCounterIdsMap.empty() && m_switchDebugCounterIdsMap.empty() && - m_macsecSAAttrIdsMap.empty(); + m_macsecSAAttrIdsMap.empty() && + m_aclCounterAttrIdsMap.empty(); } bool FlexCounter::allPluginsEmpty() const @@ -1475,6 +1531,55 @@ 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 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(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 values; + + for (const auto& aclCounterAttr : aclCounterAttrs) + { + auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_ACL_COUNTER, aclCounterAttr.id); + values.emplace_back(meta->attridname, sai_serialize_attr_value(*meta, aclCounterAttr)); + } + + // Write counters to DB + std::string aclCounterVidStr = sai_serialize_object_id(aclCounterVid); + + countersTable.set(aclCounterVidStr, values, ""); + } +} + + void FlexCounter::collectRifCounters( _In_ swss::Table &countersTable) { @@ -2100,6 +2205,10 @@ void FlexCounter::removeCounter( { removeMACsecSA(vid); } + else if (objectType == SAI_OBJECT_TYPE_ACL_COUNTER) + { + removeAclCounter(vid); + } else { SWSS_LOG_ERROR("Object type for removal not supported, %s", @@ -2247,6 +2356,19 @@ void FlexCounter::addCounter( setMACsecSAAttrList(vid, rid, macsecSAIds); } + else if (objectType == SAI_OBJECT_TYPE_ACL_COUNTER && field == ACL_COUNTER_ATTR_ID_LIST) + { + std::vector 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; diff --git a/syncd/FlexCounter.h b/syncd/FlexCounter.h index 4a30cf6c9..7efbd4186 100644 --- a/syncd/FlexCounter.h +++ b/syncd/FlexCounter.h @@ -113,6 +113,9 @@ namespace syncd void removeMACsecSA( _In_ sai_object_id_t macsecSAVid); + void removeAclCounter( + _In_ sai_object_id_t aclCounterVid); + private: // set counter list void setPortCounterList( @@ -168,6 +171,11 @@ namespace syncd _In_ sai_object_id_t macsecSARid, _In_ const std::vector &attrIds); + void setAclCounterAttrList( + _In_ sai_object_id_t aclCounterVid, + _In_ sai_object_id_t aclCounterRid, + _In_ const std::vector &attrIds); + private: // is counter supported bool isPortCounterSupported( @@ -308,6 +316,16 @@ namespace syncd std::vector m_macsecSAAttrIds; }; + struct AclCounterAttrIds + { + AclCounterAttrIds( + _In_ sai_object_id_t aclCounter, + _In_ const std::vector &aclCounterIds); + + sai_object_id_t m_aclCounterId; + std::vector m_aclCounterAttrIds; + }; + private: void collectCounters( @@ -363,6 +381,9 @@ namespace syncd void collectMACsecSAAttrs( _In_ swss::Table &countersTable); + void collectAclCounterAttrs( + _In_ swss::Table &countersTable); + private: void addCollectCountersHandler( @@ -397,11 +418,10 @@ namespace syncd std::map> m_rifCounterIdsMap; std::map> m_bufferPoolCounterIdsMap; std::map> m_switchDebugCounterIdsMap; - std::map> m_queueAttrIdsMap; std::map> m_priorityGroupAttrIdsMap; - std::map> m_macsecSAAttrIdsMap; + std::map> m_aclCounterAttrIdsMap; private: From de1ddc933c30b5b3285c852c97d5905bc4826ba7 Mon Sep 17 00:00:00 2001 From: Stepan Blyshchak Date: Tue, 28 Sep 2021 17:42:24 +0300 Subject: [PATCH 2/3] internal review comments handling Signed-off-by: Stepan Blyshchak --- syncd/FlexCounter.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/syncd/FlexCounter.cpp b/syncd/FlexCounter.cpp index 02a3c88d3..9b850e69c 100644 --- a/syncd/FlexCounter.cpp +++ b/syncd/FlexCounter.cpp @@ -1569,13 +1569,17 @@ void FlexCounter::collectAclCounterAttrs( 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 - std::string aclCounterVidStr = sai_serialize_object_id(aclCounterVid); + auto aclCounterVidStr = sai_serialize_object_id(aclCounterVid); - countersTable.set(aclCounterVidStr, values, ""); + countersTable.set(aclCounterVidStr, values); } } From 0152f603246c3fb442920e68cbeeec7a2fa6c823 Mon Sep 17 00:00:00 2001 From: Stepan Blyschak Date: Mon, 8 Nov 2021 13:09:43 +0200 Subject: [PATCH 3/3] remove newlines Signed-off-by: Stepan Blyschak --- syncd/FlexCounter.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/syncd/FlexCounter.cpp b/syncd/FlexCounter.cpp index 3b09caf20..30852a6e0 100644 --- a/syncd/FlexCounter.cpp +++ b/syncd/FlexCounter.cpp @@ -824,7 +824,6 @@ void FlexCounter::removeAclCounter( } } - void FlexCounter::removeRif( _In_ sai_object_id_t rifVid) { @@ -1696,7 +1695,6 @@ void FlexCounter::collectAclCounterAttrs( } } - void FlexCounter::collectRifCounters( _In_ swss::Table &countersTable) {