Skip to content

Commit

Permalink
Merge branch 'master' into host_tx_ready_enhancements-sonic-sairedis
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik authored Nov 24, 2023
2 parents fc717d2 + 9804bd7 commit 377c44e
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ libSyncd_a_SOURCES = \
NotificationQueue.cpp \
PortMap.cpp \
PortMapParser.cpp \
PortStateChangeHandler.cpp \
RedisClient.cpp \
RedisNotificationProducer.cpp \
RequestShutdownCommandLineOptions.cpp \
Expand Down
53 changes: 53 additions & 0 deletions syncd/PortStateChangeHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "PortStateChangeHandler.h"

#include <string>

#include "swss/logger.h"

using namespace syncd;

constexpr size_t PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE;

PortStateChangeHandler::PortStateChangeHandler(
_In_ std::shared_ptr<swss::SelectableEvent> portStateChangeEvent)
{
SWSS_LOG_ENTER();

if (portStateChangeEvent == nullptr)
{
SWSS_LOG_THROW("Unexpected error: port state change event is null.");
}

m_portStateChangeEvent = portStateChangeEvent;

m_portStateChangeQueue = std::make_shared<PortOperStatusNotificationQueue>(
PORT_STATE_CHANGE_QUEUE_SIZE);
}

void PortStateChangeHandler::handlePortStateChangeNotification(
_In_ uint32_t count,
_In_ const sai_port_oper_status_notification_t *data)
{
SWSS_LOG_ENTER();

for (uint32_t idx = 0; idx < count; ++idx)
{
if (m_portStateChangeQueue->enqueue(data[idx]) == false)
{
SWSS_LOG_ERROR(
"Unexpected error: failed to enqueue the port state change "
"notification.");

return;
}
}

m_portStateChangeEvent->notify();
}

std::shared_ptr<PortOperStatusNotificationQueue> PortStateChangeHandler::getQueue() const
{
SWSS_LOG_ENTER();

return m_portStateChangeQueue;
}
52 changes: 52 additions & 0 deletions syncd/PortStateChangeHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

extern "C" {
#include "saimetadata.h"
}

#include <memory>

#include "ConcurrentQueue.h"
#include "swss/selectableevent.h"

namespace syncd
{
using PortOperStatusNotificationQueue =
ConcurrentQueue<sai_port_oper_status_notification_t>;

// Class to handle the port state change callback from SAI. This consists a
// selectable event that will be used to send notification from producer thread
// to consumer thread, and a mutex protected concurrent queue to share the port
// state change notification data between producer and consumer threads.
class PortStateChangeHandler
{
public:

PortStateChangeHandler(
_In_ std::shared_ptr<swss::SelectableEvent> portStateChangeEvent);

virtual ~PortStateChangeHandler() = default;

// Adds the port operational status notification data to a queue and generates a
// notification event.
void handlePortStateChangeNotification(
_In_ uint32_t count,
_In_ const sai_port_oper_status_notification_t *data);

// Returns the shared pointer of the queue.
std::shared_ptr<PortOperStatusNotificationQueue> getQueue() const;

private:

// Choosing 4k max event queue size based on if we had 256 ports, it can
// accommodate on average 16 port events per ports in worst case.
static constexpr size_t PORT_STATE_CHANGE_QUEUE_SIZE = 4096;

// SelectableEvent for producer to generate the event and for consumer to
// listen on.
std::shared_ptr<swss::SelectableEvent> m_portStateChangeEvent;

// Mutex protected queue to share the data between producer and consumer.
std::shared_ptr<PortOperStatusNotificationQueue> m_portStateChangeQueue;
};
} // namespace syncd
19 changes: 19 additions & 0 deletions syncd/SelectableEventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace syncd
{
// This class implements handler for Selectable events.
class SelectableEventHandler
{
public:

virtual ~SelectableEventHandler() = default;

virtual void handleSelectableEvent() = 0;

protected:

SelectableEventHandler() = default;
};

} // namespace syncd
5 changes: 5 additions & 0 deletions syncd/scripts/syncd_init_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ config_syncd_mlnx()
if [[ -f /tmp/sai_extra.profile ]]; then
cat /tmp/sai_extra.profile >> /tmp/sai.profile
fi

if [[ -f /$HWSKU_DIR/module_control_support.profile ]]; then
cat /$HWSKU_DIR/module_control_support.profile >> /tmp/sai.profile
fi

}

config_syncd_centec()
Expand Down
1 change: 1 addition & 0 deletions unittest/syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tests_SOURCES = main.cpp \
TestNotificationProcessor.cpp \
TestNotificationHandler.cpp \
TestMdioIpcServer.cpp \
TestPortStateChangeHandler.cpp \
TestVendorSai.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
Expand Down
57 changes: 57 additions & 0 deletions unittest/syncd/TestPortStateChangeHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "PortStateChangeHandler.h"

#include <gtest/gtest.h>

using namespace syncd;

constexpr size_t portStateChangeQueueSize = 4096;

class PortStateChangeHandlerTest : public ::testing::Test
{
protected:
PortStateChangeHandlerTest()
: m_portStateChangeHandler(std::make_shared<swss::SelectableEvent>())
{
SWSS_LOG_ENTER();
}

~PortStateChangeHandlerTest() override = default;

PortStateChangeHandler m_portStateChangeHandler;
};

TEST_F(PortStateChangeHandlerTest, VerifyGetQueue)
{
auto queue = m_portStateChangeHandler.getQueue();
EXPECT_EQ(queue->size(), 0);
}

TEST_F(PortStateChangeHandlerTest,
HandlePortStateChangeNotificationFailsOnEnqueuingData)
{
auto queue = m_portStateChangeHandler.getQueue();
EXPECT_EQ(queue->size(), 0);

// Insert enough data in the queue so it reaches its capacity.
sai_port_oper_status_notification_t operStatus[portStateChangeQueueSize];
m_portStateChangeHandler.handlePortStateChangeNotification(
portStateChangeQueueSize, &operStatus[0]);
EXPECT_EQ(queue->size(), portStateChangeQueueSize);

// Since queue is at its maximum capacity, adding a new element should cause
// insert failure and new element should not get added.
m_portStateChangeHandler.handlePortStateChangeNotification(/*count=*/1,
&operStatus[0]);
EXPECT_EQ(queue->size(), portStateChangeQueueSize);
}

TEST_F(PortStateChangeHandlerTest, HandlePortStateChangeNotificationSucceeds)
{
auto queue = m_portStateChangeHandler.getQueue();
EXPECT_EQ(queue->size(), 0);

sai_port_oper_status_notification_t operStatus;
m_portStateChangeHandler.handlePortStateChangeNotification(/*count=*/1,
&operStatus);
EXPECT_EQ(queue->size(), 1);
}
36 changes: 36 additions & 0 deletions unittest/vslib/TestSwitchStateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ TEST_F(SwitchStateBaseTest, switchHashCapabilitiesGet)
ASSERT_EQ(hfSet1, hfSet2);
}

TEST_F(SwitchStateBaseTest, switchHashAlgorithmCapabilitiesGet)
{
sai_s32_list_t data = { .count = 0, .list = nullptr };

auto status = m_ss->queryAttrEnumValuesCapability(
m_swid, SAI_OBJECT_TYPE_SWITCH, SAI_SWITCH_ATTR_ECMP_DEFAULT_HASH_ALGORITHM, &data
);
ASSERT_EQ(status, SAI_STATUS_BUFFER_OVERFLOW);

std::vector<sai_int32_t> haList(data.count);
data.list = haList.data();

status = m_ss->queryAttrEnumValuesCapability(
m_swid, SAI_OBJECT_TYPE_SWITCH, SAI_SWITCH_ATTR_ECMP_DEFAULT_HASH_ALGORITHM, &data
);
ASSERT_EQ(status, SAI_STATUS_SUCCESS);

const std::set<sai_hash_algorithm_t> haSet1 = {
SAI_HASH_ALGORITHM_CRC,
SAI_HASH_ALGORITHM_XOR,
SAI_HASH_ALGORITHM_RANDOM,
SAI_HASH_ALGORITHM_CRC_32LO,
SAI_HASH_ALGORITHM_CRC_32HI,
SAI_HASH_ALGORITHM_CRC_CCITT,
SAI_HASH_ALGORITHM_CRC_XOR
};

std::set<sai_hash_algorithm_t> haSet2;

std::transform(
haList.cbegin(), haList.cend(), std::inserter(haSet2, haSet2.begin()),
[](sai_int32_t value) { return static_cast<sai_hash_algorithm_t>(value); }
);
ASSERT_EQ(haSet1, haSet2);
}

//Test the following function:
//sai_status_t initialize_voq_switch_objects(
// _In_ uint32_t attr_count,
Expand Down
28 changes: 28 additions & 0 deletions vslib/SwitchStateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3736,6 +3736,29 @@ sai_status_t SwitchStateBase::queryHashNativeHashFieldListCapability(
return SAI_STATUS_SUCCESS;
}

sai_status_t SwitchStateBase::querySwitchHashAlgorithmCapability(
_Inout_ sai_s32_list_t *enum_values_capability)
{
SWSS_LOG_ENTER();

if (enum_values_capability->count < 7)
{
enum_values_capability->count = 7;
return SAI_STATUS_BUFFER_OVERFLOW;
}

enum_values_capability->count = 7;
enum_values_capability->list[0] = SAI_HASH_ALGORITHM_CRC;
enum_values_capability->list[1] = SAI_HASH_ALGORITHM_XOR;
enum_values_capability->list[2] = SAI_HASH_ALGORITHM_RANDOM;
enum_values_capability->list[3] = SAI_HASH_ALGORITHM_CRC_32LO;
enum_values_capability->list[4] = SAI_HASH_ALGORITHM_CRC_32HI;
enum_values_capability->list[5] = SAI_HASH_ALGORITHM_CRC_CCITT;
enum_values_capability->list[6] = SAI_HASH_ALGORITHM_CRC_XOR;

return SAI_STATUS_SUCCESS;
}

sai_status_t SwitchStateBase::queryAttrEnumValuesCapability(
_In_ sai_object_id_t switch_id,
_In_ sai_object_type_t object_type,
Expand All @@ -3762,6 +3785,11 @@ sai_status_t SwitchStateBase::queryAttrEnumValuesCapability(
{
return queryHashNativeHashFieldListCapability(enum_values_capability);
}
else if (object_type == SAI_OBJECT_TYPE_SWITCH && (attr_id == SAI_SWITCH_ATTR_ECMP_DEFAULT_HASH_ALGORITHM ||
attr_id == SAI_SWITCH_ATTR_LAG_DEFAULT_HASH_ALGORITHM))
{
return querySwitchHashAlgorithmCapability(enum_values_capability);
}

return SAI_STATUS_NOT_SUPPORTED;
}
Expand Down
3 changes: 3 additions & 0 deletions vslib/SwitchStateBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,9 @@ namespace saivs
virtual sai_status_t queryHashNativeHashFieldListCapability(
_Inout_ sai_s32_list_t *enum_values_capability);

virtual sai_status_t querySwitchHashAlgorithmCapability(
_Inout_ sai_s32_list_t *enum_values_capability);

virtual sai_status_t queryPortAutonegFecOverrideSupportCapability(
_Out_ sai_attr_capability_t *attr_capability);

Expand Down

0 comments on commit 377c44e

Please sign in to comment.