From 638ca825f8b8951943e73322e96594c30487b7e1 Mon Sep 17 00:00:00 2001 From: kcudnik Date: Tue, 10 Aug 2021 17:17:43 +0200 Subject: [PATCH] Move getHardwareInfo to libsaimeta --- lib/inc/RedisRemoteSaiInterface.h | 6 --- lib/inc/Switch.h | 4 -- lib/src/ClientSai.cpp | 3 +- lib/src/RedisRemoteSaiInterface.cpp | 46 +--------------------- lib/src/Switch.cpp | 47 ++--------------------- meta/Globals.cpp | 50 ++++++++++++++++++++++++ meta/Globals.h | 15 +++++++- meta/MetaTestSaiInterface.cpp | 46 ++-------------------- vslib/inc/VirtualSwitchSaiInterface.h | 6 --- vslib/src/VirtualSwitchSaiInterface.cpp | 51 ++----------------------- 10 files changed, 77 insertions(+), 197 deletions(-) diff --git a/lib/inc/RedisRemoteSaiInterface.h b/lib/inc/RedisRemoteSaiInterface.h index 200619867556..1362152be28f 100644 --- a/lib/inc/RedisRemoteSaiInterface.h +++ b/lib/inc/RedisRemoteSaiInterface.h @@ -434,12 +434,6 @@ namespace sairedis void refreshTableDump(); - public: - - static std::string getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList); - private: std::shared_ptr m_contextConfig; diff --git a/lib/inc/Switch.h b/lib/inc/Switch.h index 00a3cb4c01f9..7b5d16a4dde7 100644 --- a/lib/inc/Switch.h +++ b/lib/inc/Switch.h @@ -41,10 +41,6 @@ namespace sairedis const std::string& getHardwareInfo() const; - static std::string getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList); - private: sai_object_id_t m_switchId; diff --git a/lib/src/ClientSai.cpp b/lib/src/ClientSai.cpp index 9260830346a4..dc62654a684c 100644 --- a/lib/src/ClientSai.cpp +++ b/lib/src/ClientSai.cpp @@ -12,6 +12,7 @@ #include "meta/sai_serialize.h" #include "meta/SaiAttributeList.h" +#include "meta/Globals.h" #include @@ -138,7 +139,7 @@ sai_status_t ClientSai::create( // have hwinfo and context and context container information, we // could allocate switch object ID locally - auto hwinfo = RedisRemoteSaiInterface::getHardwareInfo(attr_count, attr_list); + auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list); // TODO support context SWSS_LOG_NOTICE("request to connect switch (context: 0) and hwinfo='%s'", hwinfo.c_str()); diff --git a/lib/src/RedisRemoteSaiInterface.cpp b/lib/src/RedisRemoteSaiInterface.cpp index ea2afba36573..c91e27f127fd 100644 --- a/lib/src/RedisRemoteSaiInterface.cpp +++ b/lib/src/RedisRemoteSaiInterface.cpp @@ -12,6 +12,7 @@ #include "meta/sai_serialize.h" #include "meta/SaiAttributeList.h" +#include "meta/Globals.h" #include @@ -133,49 +134,6 @@ sai_status_t RedisRemoteSaiInterface::uninitialize(void) return SAI_STATUS_SUCCESS; } -// TODO move to common namespace utils class -std::string RedisRemoteSaiInterface::getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList) -{ - SWSS_LOG_ENTER(); - - auto *attr = sai_metadata_get_attr_by_id( - SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO, - attrCount, - attrList); - - if (attr == NULL) - return ""; - - auto& s8list = attr->value.s8list; - - if (s8list.count == 0) - return ""; - - if (s8list.list == NULL) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is NULL! but count is %u", s8list.count); - return ""; - } - - uint32_t count = s8list.count; - - if (count > SAI_MAX_HARDWARE_ID_LEN) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.count (%u) > SAI_MAX_HARDWARE_ID_LEN (%d), LIMITING !!", - count, - SAI_MAX_HARDWARE_ID_LEN); - - count = SAI_MAX_HARDWARE_ID_LEN; - } - - // check actual length, since buffer may contain nulls - auto actualLength = strnlen((const char*)s8list.list, count); - - return std::string((const char*)s8list.list, actualLength); -} - sai_status_t RedisRemoteSaiInterface::create( _In_ sai_object_type_t objectType, _Out_ sai_object_id_t* objectId, @@ -192,7 +150,7 @@ sai_status_t RedisRemoteSaiInterface::create( // for given hardware info we always return same switch id, // this is required since we could be performing warm boot here - auto hwinfo = getHardwareInfo(attr_count, attr_list); + auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list); if (hwinfo.size()) { diff --git a/lib/src/Switch.cpp b/lib/src/Switch.cpp index 4714ba9296c6..f16d6934d710 100644 --- a/lib/src/Switch.cpp +++ b/lib/src/Switch.cpp @@ -1,5 +1,7 @@ #include "Switch.h" +#include "meta/Globals.h" + #include "swss/logger.h" #include @@ -32,7 +34,7 @@ Switch::Switch( // SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO is create only attribute - m_hardwareInfo = getHardwareInfo(attrCount, attrList); + m_hardwareInfo = saimeta::Globals::getHardwareInfo(attrCount, attrList); SWSS_LOG_NOTICE("created switch with hwinfo = '%s'", m_hardwareInfo.c_str()); } @@ -126,46 +128,3 @@ const std::string& Switch::getHardwareInfo() const return m_hardwareInfo; } - -std::string Switch::getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList) -{ - SWSS_LOG_ENTER(); - - auto *attr = sai_metadata_get_attr_by_id( - SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO, - attrCount, - attrList); - - if (attr == NULL) - return ""; - - auto& s8list = attr->value.s8list; - - if (s8list.count == 0) - return ""; - - if (s8list.list == NULL) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is NULL! but count is %u", s8list.count); - return ""; - } - - uint32_t count = s8list.count; - - if (count > SAI_MAX_HARDWARE_ID_LEN) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.count (%u) > SAI_MAX_HARDWARE_ID_LEN (%d), LIMITING !!", - count, - SAI_MAX_HARDWARE_ID_LEN); - - count = SAI_MAX_HARDWARE_ID_LEN; - } - - // check actual length, since buffer may contain nulls - auto actualLength = strnlen((const char*)s8list.list, count); - - return std::string((const char*)s8list.list, actualLength); -} - diff --git a/meta/Globals.cpp b/meta/Globals.cpp index 2a8c0aa30fb9..aa7d2cc9291a 100644 --- a/meta/Globals.cpp +++ b/meta/Globals.cpp @@ -17,4 +17,54 @@ std::string Globals::getAttrInfo( return std::string(md.attridname) + ":" + sai_serialize_attr_value_type(md.attrvaluetype); } +std::string Globals::getHardwareInfo( + _In_ uint32_t attrCount, + _In_ const sai_attribute_t *attrList) +{ + SWSS_LOG_ENTER(); + + auto *attr = sai_metadata_get_attr_by_id( + SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO, + attrCount, + attrList); + + if (attr == NULL) + { + return ""; + } + + auto& s8list = attr->value.s8list; + + if (s8list.count == 0) + { + return ""; + } + + if (s8list.list == NULL) + { + SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is NULL! but count is %u", s8list.count); + return ""; + } + + uint32_t count = s8list.count; + + if (count > SAI_MAX_HARDWARE_ID_LEN) + { + SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.count (%u) > SAI_MAX_HARDWARE_ID_LEN (%d), LIMITING !!", + count, + SAI_MAX_HARDWARE_ID_LEN); + + count = SAI_MAX_HARDWARE_ID_LEN; + } + + // check actual length, since buffer may contain nulls + auto actualLength = strnlen((const char*)s8list.list, count); + + if (actualLength != count) + { + SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is null padded"); + } + + return std::string((const char*)s8list.list, actualLength); +} diff --git a/meta/Globals.h b/meta/Globals.h index d5ada227b7ef..3a9785fce71a 100644 --- a/meta/Globals.h +++ b/meta/Globals.h @@ -6,7 +6,6 @@ extern "C" { #include "swss/logger.h" -#include #include namespace saimeta @@ -22,6 +21,19 @@ namespace saimeta static std::string getAttrInfo( _In_ const sai_attr_metadata_t& md); + + /** + * @brief Get hardware info. + * + * Get hardware info from attribute list, typically passed to + * create_switch api and convert it from s8list to std::string. + * Object type is assumed to be SAI_OBJECT_TYPE_SWITCH. + * + * @return Hardware info converted to string. + */ + static std::string getHardwareInfo( + _In_ uint32_t attrCount, + _In_ const sai_attribute_t *attrList); }; } @@ -31,4 +43,3 @@ namespace saimeta #define META_LOG_NOTICE( md, format, ...) SWSS_LOG_NOTICE ("%s " format, saimeta::Globals::getAttrInfo(md).c_str(), ##__VA_ARGS__) #define META_LOG_INFO( md, format, ...) SWSS_LOG_INFO ("%s " format, saimeta::Globals::getAttrInfo(md).c_str(), ##__VA_ARGS__) #define META_LOG_THROW( md, format, ...) SWSS_LOG_THROW ("%s " format, saimeta::Globals::getAttrInfo(md).c_str(), ##__VA_ARGS__) - diff --git a/meta/MetaTestSaiInterface.cpp b/meta/MetaTestSaiInterface.cpp index bcd3d7363eab..ee79b8fa6c42 100644 --- a/meta/MetaTestSaiInterface.cpp +++ b/meta/MetaTestSaiInterface.cpp @@ -3,6 +3,8 @@ #include "SwitchConfigContainer.h" #include "swss/logger.h" + +#include "Globals.h" #include "sai_serialize.h" using namespace saimeta; @@ -25,48 +27,6 @@ MetaTestSaiInterface::MetaTestSaiInterface() std::make_shared()); } -static std::string getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList) -{ - SWSS_LOG_ENTER(); - - auto *attr = sai_metadata_get_attr_by_id( - SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO, - attrCount, - attrList); - - if (attr == NULL) - return ""; - - auto& s8list = attr->value.s8list; - - if (s8list.count == 0) - return ""; - - if (s8list.list == NULL) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is NULL! but count is %u", s8list.count); - return ""; - } - - uint32_t count = s8list.count; - - if (count > SAI_MAX_HARDWARE_ID_LEN) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.count (%u) > SAI_MAX_HARDWARE_ID_LEN (%d), LIMITING !!", - count, - SAI_MAX_HARDWARE_ID_LEN); - - count = SAI_MAX_HARDWARE_ID_LEN; - } - - // check actual length, since buffer may contain nulls - auto actualLength = strnlen((const char*)s8list.list, count); - - return std::string((const char*)s8list.list, actualLength); -} - sai_status_t MetaTestSaiInterface::create( _In_ sai_object_type_t objectType, _Out_ sai_object_id_t* objectId, @@ -81,7 +41,7 @@ sai_status_t MetaTestSaiInterface::create( // for given hardware info we always return same switch id, // this is required since we could be performing warm boot here - auto hwinfo = getHardwareInfo(attr_count, attr_list); + auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list); switchId = m_virtualObjectIdManager->allocateNewSwitchObjectId(hwinfo); diff --git a/vslib/inc/VirtualSwitchSaiInterface.h b/vslib/inc/VirtualSwitchSaiInterface.h index 9477372a4ab0..40fb9dcb584a 100644 --- a/vslib/inc/VirtualSwitchSaiInterface.h +++ b/vslib/inc/VirtualSwitchSaiInterface.h @@ -380,12 +380,6 @@ namespace saivs void syncProcessEventNetLinkMsg( _In_ std::shared_ptr payload); - public: - - std::string getHardwareInfo( - _In_ uint32_t attr_count, - _In_ const sai_attribute_t *attr_list) const; - private: std::weak_ptr m_meta; diff --git a/vslib/src/VirtualSwitchSaiInterface.cpp b/vslib/src/VirtualSwitchSaiInterface.cpp index a13d8e7d3339..92214efdac09 100644 --- a/vslib/src/VirtualSwitchSaiInterface.cpp +++ b/vslib/src/VirtualSwitchSaiInterface.cpp @@ -8,16 +8,15 @@ #include "meta/sai_serialize.h" #include "meta/SaiAttributeList.h" - -#include - -#include "meta/sai_serialize.h" +#include "meta/Globals.h" #include "SwitchStateBase.h" #include "SwitchBCM81724.h" #include "SwitchBCM56850.h" #include "SwitchMLNX2700.h" +#include + /* * Max number of counters used in 1 api call */ @@ -282,48 +281,6 @@ void VirtualSwitchSaiInterface::update_local_metadata( mmeta->meta_warm_boot_notify(); } -std::string VirtualSwitchSaiInterface::getHardwareInfo( - _In_ uint32_t attrCount, - _In_ const sai_attribute_t *attrList) const -{ - SWSS_LOG_ENTER(); - - auto *attr = sai_metadata_get_attr_by_id( - SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO, - attrCount, - attrList); - - if (attr == NULL) - return ""; - - auto& s8list = attr->value.s8list; - - if (s8list.count == 0) - return ""; - - if (s8list.list == NULL) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.list is NULL! but count is %u", s8list.count); - return ""; - } - - uint32_t count = s8list.count; - - if (count > SAI_MAX_HARDWARE_ID_LEN) - { - SWSS_LOG_WARN("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO s8list.count (%u) > SAI_MAX_HARDWARE_ID_LEN (%d), LIMITING !!", - count, - SAI_MAX_HARDWARE_ID_LEN); - - count = SAI_MAX_HARDWARE_ID_LEN; - } - - // check actual length, since buffer may contain nulls - auto actualLength = strnlen((const char*)s8list.list, count); - - return std::string((const char*)s8list.list, actualLength); -} - sai_status_t VirtualSwitchSaiInterface::create( _In_ sai_object_type_t objectType, _Out_ sai_object_id_t* objectId, @@ -343,7 +300,7 @@ sai_status_t VirtualSwitchSaiInterface::create( // for given hardware info we always return same switch id, // this is required since we could be performing warm boot here - auto hwinfo = getHardwareInfo(attr_count, attr_list); + auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list); switchId = m_realObjectIdManager->allocateNewSwitchObjectId(hwinfo);