From 3663e30b3b27cb7a873d7e97e05b563a92e40dfc Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Sat, 6 Feb 2021 14:07:10 +0100 Subject: [PATCH] [syncd] Create base class SaiSwitchInterface (#786) This class will be needed for future asic compare. --- syncd/BestCandidateFinder.cpp | 4 +- syncd/BestCandidateFinder.h | 8 +-- syncd/ComparisonLogic.cpp | 2 +- syncd/ComparisonLogic.h | 6 +- syncd/Makefile.am | 3 +- syncd/SaiSwitch.cpp | 37 +--------- syncd/SaiSwitch.h | 103 +++++++++------------------- syncd/SaiSwitchInterface.cpp | 49 ++++++++++++++ syncd/SaiSwitchInterface.h | 123 ++++++++++++++++++++++++++++++++++ 9 files changed, 217 insertions(+), 118 deletions(-) create mode 100644 syncd/SaiSwitchInterface.cpp create mode 100644 syncd/SaiSwitchInterface.h diff --git a/syncd/BestCandidateFinder.cpp b/syncd/BestCandidateFinder.cpp index 71541a738758..d82ae8b856ad 100644 --- a/syncd/BestCandidateFinder.cpp +++ b/syncd/BestCandidateFinder.cpp @@ -14,7 +14,7 @@ using namespace syncd; BestCandidateFinder::BestCandidateFinder( _In_ const AsicView& currentView, _In_ const AsicView& temporaryView, - _In_ std::shared_ptr sw): + _In_ std::shared_ptr sw): m_currentView(currentView), m_temporaryView(temporaryView), m_switch(sw) @@ -2792,7 +2792,7 @@ bool BestCandidateFinder::hasEqualAttribute( std::shared_ptr BestCandidateFinder::getSaiAttrFromDefaultValue( _In_ const AsicView ¤tView, - _In_ std::shared_ptr sw, + _In_ std::shared_ptr sw, _In_ const sai_attr_metadata_t &meta) { SWSS_LOG_ENTER(); diff --git a/syncd/BestCandidateFinder.h b/syncd/BestCandidateFinder.h index c08188e92cac..2114749b79b8 100644 --- a/syncd/BestCandidateFinder.h +++ b/syncd/BestCandidateFinder.h @@ -2,7 +2,7 @@ #include "SaiObj.h" #include "AsicView.h" -#include "SaiSwitch.h" +#include "SaiSwitchInterface.h" #include @@ -25,7 +25,7 @@ namespace syncd BestCandidateFinder( _In_ const AsicView ¤tView, _In_ const AsicView &temporaryView, - _In_ std::shared_ptr sw); + _In_ std::shared_ptr sw); virtual ~BestCandidateFinder() = default; @@ -176,7 +176,7 @@ namespace syncd static std::shared_ptr getSaiAttrFromDefaultValue( _In_ const AsicView ¤tView, - _In_ std::shared_ptr sw, + _In_ std::shared_ptr sw, _In_ const sai_attr_metadata_t &meta); static bool hasEqualQosMapList( @@ -188,7 +188,7 @@ namespace syncd const AsicView& m_temporaryView; - std::shared_ptr m_switch; + std::shared_ptr m_switch; std::shared_ptr m_temporaryObj; diff --git a/syncd/ComparisonLogic.cpp b/syncd/ComparisonLogic.cpp index 842f0b375b43..5c6ec2224c2d 100644 --- a/syncd/ComparisonLogic.cpp +++ b/syncd/ComparisonLogic.cpp @@ -24,7 +24,7 @@ using namespace saimeta; ComparisonLogic::ComparisonLogic( _In_ std::shared_ptr vendorSai, - _In_ std::shared_ptr sw, + _In_ std::shared_ptr sw, _In_ std::shared_ptr handler, _In_ std::set initViewRemovedVids, _In_ std::shared_ptr current, diff --git a/syncd/ComparisonLogic.h b/syncd/ComparisonLogic.h index 869e12912af8..2255f0062784 100644 --- a/syncd/ComparisonLogic.h +++ b/syncd/ComparisonLogic.h @@ -6,7 +6,7 @@ extern "C"{ #include "AsicView.h" #include "VendorSai.h" -#include "SaiSwitch.h" +#include "SaiSwitchInterface.h" #include "VirtualOidTranslator.h" #include "NotificationHandler.h" #include "BreakConfig.h" @@ -21,7 +21,7 @@ namespace syncd ComparisonLogic( _In_ std::shared_ptr vendorSai, - _In_ std::shared_ptr sw, + _In_ std::shared_ptr sw, _In_ std::shared_ptr handler, _In_ std::set initViewRemovedVids, _In_ std::shared_ptr current, @@ -230,7 +230,7 @@ namespace syncd std::shared_ptr m_vendorSai; - std::shared_ptr m_switch; + std::shared_ptr m_switch; std::set m_initViewRemovedVids; diff --git a/syncd/Makefile.am b/syncd/Makefile.am index 59dd5f21a137..fcf71039a5f1 100644 --- a/syncd/Makefile.am +++ b/syncd/Makefile.am @@ -16,11 +16,13 @@ endif noinst_LIBRARIES = libSyncd.a libSyncdRequestShutdown.a libSyncd_a_SOURCES = \ + SaiSwitchInterface.cpp \ ZeroMQSelectableChannel.cpp \ RedisSelectableChannel.cpp \ SelectableChannel.cpp \ ZeroMQNotificationProducer.cpp \ RedisNotificationProducer.cpp \ + ComparisonLogic.cpp \ Syncd.cpp \ BreakConfig.cpp \ BreakConfigParser.cpp \ @@ -32,7 +34,6 @@ libSyncd_a_SOURCES = \ Workaround.cpp \ ServiceMethodTable.cpp \ SwitchNotifications.cpp \ - ComparisonLogic.cpp \ VirtualOidTranslator.cpp \ NotificationProcessor.cpp \ NotificationHandler.cpp \ diff --git a/syncd/SaiSwitch.cpp b/syncd/SaiSwitch.cpp index 7878f4d85785..dfd2df3d77f5 100644 --- a/syncd/SaiSwitch.cpp +++ b/syncd/SaiSwitch.cpp @@ -27,6 +27,7 @@ SaiSwitch::SaiSwitch( _In_ std::shared_ptr translator, _In_ std::shared_ptr vendorSai, _In_ bool warmBoot): + SaiSwitchInterface(switch_vid, switch_rid), m_vendorSai(vendorSai), m_warmBoot(warmBoot), m_translator(translator), @@ -36,9 +37,6 @@ SaiSwitch::SaiSwitch( SWSS_LOG_TIMER("constructor"); - m_switch_rid = switch_rid; - m_switch_vid = switch_vid; - GlobalSwitchId::setSwitchId(m_switch_rid); m_hardware_info = saiGetHardwareInfo(); @@ -341,20 +339,6 @@ void SaiSwitch::redisSetDummyAsicStateForRealObjectId( m_client->setDummyAsicStateObject(vid); } -sai_object_id_t SaiSwitch::getVid() const -{ - SWSS_LOG_ENTER(); - - return m_switch_vid; -} - -sai_object_id_t SaiSwitch::getRid() const -{ - SWSS_LOG_ENTER(); - - return m_switch_rid; -} - std::string SaiSwitch::getHardwareInfo() const { SWSS_LOG_ENTER(); @@ -550,25 +534,6 @@ sai_object_id_t SaiSwitch::helperGetSwitchAttrOid( return rid; } -sai_object_id_t SaiSwitch::getSwitchDefaultAttrOid( - _In_ sai_attr_id_t attr_id) const -{ - SWSS_LOG_ENTER(); - - auto it = m_default_rid_map.find(attr_id); - - if (it == m_default_rid_map.end()) - { - auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_SWITCH, attr_id); - - const char* name = (meta) ? meta->attridname : "UNKNOWN"; - - SWSS_LOG_THROW("attribute %s (%d) not found in default RID map", name, attr_id); - } - - return it->second; -} - bool SaiSwitch::isColdBootDiscoveredRid( _In_ sai_object_id_t rid) const { diff --git a/syncd/SaiSwitch.h b/syncd/SaiSwitch.h index 521739d3aa51..da13b5851812 100644 --- a/syncd/SaiSwitch.h +++ b/syncd/SaiSwitch.h @@ -7,6 +7,7 @@ extern "C" { #include "SaiInterface.h" #include "VirtualOidTranslator.h" #include "RedisClient.h" +#include "SaiSwitchInterface.h" #include #include @@ -17,7 +18,8 @@ extern "C" { namespace syncd { - class SaiSwitch + class SaiSwitch: + public SaiSwitchInterface { private: @@ -38,15 +40,11 @@ namespace syncd public: - sai_object_id_t getVid() const; - sai_object_id_t getRid() const; - - sai_switch_type_t getSwitchType() const; std::string getHardwareInfo() const; - std::unordered_map getVidToRidMap() const; + virtual std::unordered_map getVidToRidMap() const override; - std::unordered_map getRidToVidMap() const; + virtual std::unordered_map getRidToVidMap() const override; /** * @brief Indicates whether RID was discovered on switch init. @@ -62,8 +60,8 @@ namespace syncd * * @return True if RID was discovered during init. */ - bool isDiscoveredRid( - _In_ sai_object_id_t rid) const; + virtual bool isDiscoveredRid( + _In_ sai_object_id_t rid) const override; /** * @brief Indicates whether RID was discovered on switch init at cold boot. @@ -76,8 +74,8 @@ namespace syncd * * @return True if RID was discovered during cold boot init. */ - bool isColdBootDiscoveredRid( - _In_ sai_object_id_t rid) const; + virtual bool isColdBootDiscoveredRid( + _In_ sai_object_id_t rid) const override; /** * @brief Indicates whether RID is one of default switch objects @@ -87,8 +85,8 @@ namespace syncd * * @return True if object is default switch object. */ - bool isSwitchObjectDefaultRid( - _In_ sai_object_id_t rid) const; + virtual bool isSwitchObjectDefaultRid( + _In_ sai_object_id_t rid) const override; /** * @brief Indicates whether object can't be removed. @@ -104,8 +102,8 @@ namespace syncd * * @return True if object can't be removed from switch. */ - bool isNonRemovableRid( - _In_ sai_object_id_t rid) const; + virtual bool isNonRemovableRid( + _In_ sai_object_id_t rid) const override; /* * Redis Static Methods. @@ -123,23 +121,7 @@ namespace syncd * * @returns Discovered objects during switch init. */ - std::set getDiscoveredRids() const; - - /** - * @brief Gets default object based on switch attribute. - * - * NOTE: This method will throw exception if invalid attribute is - * specified, since attribute queried by this method are explicitly - * declared in SaiSwitch constructor. - * - * @param attr_id Switch attribute to query. - * - * @return Valid RID or specified switch attribute received from - * switch. This value can be also SAI_NULL_OBJECT_ID if switch don't - * support this attribute. - */ - sai_object_id_t getSwitchDefaultAttrOid( - _In_ sai_attr_id_t attr_id) const; + virtual std::set getDiscoveredRids() const override; /** * @brief Remove existing object from the switch. @@ -149,8 +131,8 @@ namespace syncd * * @param rid Real object ID. */ - void removeExistingObject( - _In_ sai_object_id_t rid); + virtual void removeExistingObject( + _In_ sai_object_id_t rid) override; /** * @brief Remove existing object reference only from discovery map. @@ -160,16 +142,16 @@ namespace syncd * * @param rid Real object ID. */ - void removeExistingObjectReference( - _In_ sai_object_id_t rid); + virtual void removeExistingObjectReference( + _In_ sai_object_id_t rid) override; /** * @brief Gets switch default MAC address. * * @param[out] mac MAC address to be obtained. */ - void getDefaultMacAddress( - _Out_ sai_mac_t& mac) const; + virtual void getDefaultMacAddress( + _Out_ sai_mac_t& mac) const override; /** * @brief Gets default value of attribute for given object. @@ -179,23 +161,23 @@ namespace syncd * * If object or attribute is not found, SAI_NULL_OBJECT_ID is returned. */ - sai_object_id_t getDefaultValueForOidAttr( + virtual sai_object_id_t getDefaultValueForOidAttr( _In_ sai_object_id_t rid, - _In_ sai_attr_id_t attr_id); + _In_ sai_attr_id_t attr_id) override; /** * @brief Get cold boot discovered VIDs. * * @return Set of cold boot discovered VIDs after cold boot. */ - std::set getColdBootDiscoveredVids() const; + virtual std::set getColdBootDiscoveredVids() const override; /** * @brief Get warm boot discovered VIDs. * * @return Set of warm boot discovered VIDs after warm boot. */ - std::set getWarmBootDiscoveredVids() const; + virtual std::set getWarmBootDiscoveredVids() const override; /** * @brief On post port create. @@ -204,9 +186,9 @@ namespace syncd * queues, ipgs and scheduler groups that belong to new created port, * and updated ASIC DB accordingly. */ - void onPostPortCreate( + virtual void onPostPortCreate( _In_ sai_object_id_t port_rid, - _In_ sai_object_id_t port_vid); + _In_ sai_object_id_t port_vid) override; /** * @brief Post port remove. @@ -214,11 +196,11 @@ namespace syncd * Performs actions after port remove. Will remove lanes associated * with port from redis lane map. */ - void postPortRemove( - _In_ sai_object_id_t portRid); + virtual void postPortRemove( + _In_ sai_object_id_t portRid) override; - void collectPortRelatedObjects( - _In_ sai_object_id_t portRid); + virtual void collectPortRelatedObjects( + _In_ sai_object_id_t portRid) override; private: @@ -314,17 +296,9 @@ namespace syncd void checkWarmBootDiscoveredRids(); - private: - - /** - * @brief Switch virtual ID assigned by syncd. - */ - sai_object_id_t m_switch_vid; + sai_switch_type_t getSwitchType() const; - /** - * @brief Switch real ID assigned by SAI SDK. - */ - sai_object_id_t m_switch_rid; + private: std::string m_hardware_info; @@ -340,15 +314,6 @@ namespace syncd * later, when this scenario will happen. */ - /** - * @brief Map of default RIDs retrieved from Switch object. - * - * It will contain RIDs like CPU port, default virtual router, default - * trap group. etc. Those objects here should be considered non - * removable. - */ - std::map m_default_rid_map; - /** * @brief Discovered objects. * @@ -381,10 +346,6 @@ namespace syncd */ std::unordered_map> m_defaultOidMap; - std::set m_coldBootDiscoveredVids; - - std::set m_warmBootDiscoveredVids; - std::shared_ptr m_vendorSai; bool m_warmBoot; diff --git a/syncd/SaiSwitchInterface.cpp b/syncd/SaiSwitchInterface.cpp new file mode 100644 index 000000000000..a7fd09274ba2 --- /dev/null +++ b/syncd/SaiSwitchInterface.cpp @@ -0,0 +1,49 @@ +#include "SaiSwitchInterface.h" + +#include "swss/logger.h" + +using namespace syncd; + +SaiSwitchInterface::SaiSwitchInterface( + _In_ sai_object_id_t switchVid, + _In_ sai_object_id_t switchRid): + m_switch_vid(switchVid), + m_switch_rid(switchRid) +{ + SWSS_LOG_ENTER(); + + // empty +} + +sai_object_id_t SaiSwitchInterface::getVid() const +{ + SWSS_LOG_ENTER(); + + return m_switch_vid; +} + +sai_object_id_t SaiSwitchInterface::getRid() const +{ + SWSS_LOG_ENTER(); + + return m_switch_rid; +} + +sai_object_id_t SaiSwitchInterface::getSwitchDefaultAttrOid( + _In_ sai_attr_id_t attr_id) const +{ + SWSS_LOG_ENTER(); + + auto it = m_default_rid_map.find(attr_id); + + if (it == m_default_rid_map.end()) + { + auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_SWITCH, attr_id); + + const char* name = (meta) ? meta->attridname : "UNKNOWN"; + + SWSS_LOG_THROW("attribute %s (%d) not found in default RID map", name, attr_id); + } + + return it->second; +} diff --git a/syncd/SaiSwitchInterface.h b/syncd/SaiSwitchInterface.h new file mode 100644 index 000000000000..2166f89790c8 --- /dev/null +++ b/syncd/SaiSwitchInterface.h @@ -0,0 +1,123 @@ +#pragma once + +extern "C" { +#include "sai.h" +#include "saimetadata.h" +} + +#include +#include +#include + +namespace syncd +{ + class SaiSwitchInterface + { + private: + + SaiSwitchInterface(const SaiSwitchInterface&); + SaiSwitchInterface& operator=(const SaiSwitchInterface&); + + public: + + SaiSwitchInterface( + _In_ sai_object_id_t switchVid, + _In_ sai_object_id_t switchRid); + + virtual ~SaiSwitchInterface() = default; + + public: + + sai_object_id_t getVid() const; + sai_object_id_t getRid() const; + + public: + + virtual std::unordered_map getVidToRidMap() const = 0; + + virtual std::unordered_map getRidToVidMap() const = 0; + + virtual bool isDiscoveredRid( + _In_ sai_object_id_t rid) const = 0; + + virtual bool isColdBootDiscoveredRid( + _In_ sai_object_id_t rid) const = 0; + + virtual bool isSwitchObjectDefaultRid( + _In_ sai_object_id_t rid) const = 0; + + virtual bool isNonRemovableRid( + _In_ sai_object_id_t rid) const = 0; + + virtual std::set getDiscoveredRids() const = 0; + + /** + * @brief Gets default object based on switch attribute. + * + * NOTE: This method will throw exception if invalid attribute is + * specified, since attribute queried by this method are explicitly + * declared in SaiSwitch constructor. + * + * @param attr_id Switch attribute to query. + * + * @return Valid RID or specified switch attribute received from + * switch. This value can be also SAI_NULL_OBJECT_ID if switch don't + * support this attribute. + */ + virtual sai_object_id_t getSwitchDefaultAttrOid( + _In_ sai_attr_id_t attr_id) const; + + + virtual void removeExistingObject( + _In_ sai_object_id_t rid) = 0; + + virtual void removeExistingObjectReference( + _In_ sai_object_id_t rid) = 0; + + virtual void getDefaultMacAddress( + _Out_ sai_mac_t& mac) const = 0; + + virtual sai_object_id_t getDefaultValueForOidAttr( + _In_ sai_object_id_t rid, + _In_ sai_attr_id_t attr_id) = 0; + + virtual std::set getColdBootDiscoveredVids() const = 0; + + virtual std::set getWarmBootDiscoveredVids() const = 0; + + virtual void onPostPortCreate( + _In_ sai_object_id_t port_rid, + _In_ sai_object_id_t port_vid) = 0; + + virtual void postPortRemove( + _In_ sai_object_id_t portRid) = 0; + + virtual void collectPortRelatedObjects( + _In_ sai_object_id_t portRid) = 0; + + protected: + + /** + * @brief Switch virtual ID assigned by syncd. + */ + sai_object_id_t m_switch_vid; + + /** + * @brief Switch real ID assigned by SAI SDK. + */ + sai_object_id_t m_switch_rid; + + /** + * @brief Map of default RIDs retrieved from Switch object. + * + * It will contain RIDs like CPU port, default virtual router, default + * trap group. etc. Those objects here should be considered non + * removable. + */ + std::map m_default_rid_map; + + std::set m_coldBootDiscoveredVids; + + std::set m_warmBootDiscoveredVids; + }; +}