Skip to content

Commit

Permalink
Move getHardwareInfo to libsaimeta
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 21, 2021
1 parent 3951504 commit 638ca82
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 197 deletions.
6 changes: 0 additions & 6 deletions lib/inc/RedisRemoteSaiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextConfig> m_contextConfig;
Expand Down
4 changes: 0 additions & 4 deletions lib/inc/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/ClientSai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "meta/sai_serialize.h"
#include "meta/SaiAttributeList.h"
#include "meta/Globals.h"

#include <inttypes.h>

Expand Down Expand Up @@ -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());
Expand Down
46 changes: 2 additions & 44 deletions lib/src/RedisRemoteSaiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "meta/sai_serialize.h"
#include "meta/SaiAttributeList.h"
#include "meta/Globals.h"

#include <inttypes.h>

Expand Down Expand Up @@ -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,
Expand All @@ -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())
{
Expand Down
47 changes: 3 additions & 44 deletions lib/src/Switch.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Switch.h"

#include "meta/Globals.h"

#include "swss/logger.h"

#include <cstring>
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}

50 changes: 50 additions & 0 deletions meta/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 13 additions & 2 deletions meta/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern "C" {

#include "swss/logger.h"

#include <memory>
#include <string>

namespace saimeta
Expand All @@ -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);
};
}

Expand All @@ -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__)

46 changes: 3 additions & 43 deletions meta/MetaTestSaiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "SwitchConfigContainer.h"

#include "swss/logger.h"

#include "Globals.h"
#include "sai_serialize.h"

using namespace saimeta;
Expand All @@ -25,48 +27,6 @@ MetaTestSaiInterface::MetaTestSaiInterface()
std::make_shared<NumberOidIndexGenerator>());
}

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,
Expand All @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions vslib/inc/VirtualSwitchSaiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,6 @@ namespace saivs
void syncProcessEventNetLinkMsg(
_In_ std::shared_ptr<EventPayloadNetLinkMsg> payload);

public:

std::string getHardwareInfo(
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list) const;

private:

std::weak_ptr<saimeta::Meta> m_meta;
Expand Down
Loading

0 comments on commit 638ca82

Please sign in to comment.