Skip to content

Commit

Permalink
[flex counter] handle router interface stats (#410)
Browse files Browse the repository at this point in the history
* [flex counter] handle router interface stats

Signed-off-by: Mykola Faryma <mykolaf@mellanox.com>

* allign with new SAI headers

Signed-off-by: Mykola Faryma <mykolaf@mellanox.com>

* update aspellcheck dictionary

Signed-off-by: Mykola Faryma <mykolaf@mellanox.com>
  • Loading branch information
mykolaf authored and lguohan committed Feb 2, 2019
1 parent c03d639 commit 145ea44
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 0 deletions.
3 changes: 3 additions & 0 deletions meta/sai_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ std::string sai_serialize_port_pool_stat(
std::string sai_serialize_queue_stat(
_In_ const sai_queue_stat_t counter);

std::string sai_serialize_router_interface_stat(
_In_ const sai_router_interface_stat_t counter);

std::string sai_serialize_ingress_priority_group_stat(
_In_ const sai_ingress_priority_group_stat_t counter);

Expand Down
8 changes: 8 additions & 0 deletions meta/saiserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,14 @@ std::string sai_serialize_queue_stat(
return sai_serialize_enum(counter, &sai_metadata_enum_sai_queue_stat_t);
}

std::string sai_serialize_router_interface_stat(
_In_ const sai_router_interface_stat_t counter)
{
SWSS_LOG_ENTER();

return sai_serialize_enum(counter, &sai_metadata_enum_sai_router_interface_stat_t);
}

std::string sai_serialize_ingress_priority_group_stat(
_In_ const sai_ingress_priority_group_stat_t counter)
{
Expand Down
15 changes: 15 additions & 0 deletions syncd/syncd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,10 @@ void processFlexCounterEvent(
{
FlexCounter::removePriorityGroup(vid, groupName);
}
else if (objectType == SAI_OBJECT_TYPE_ROUTER_INTERFACE)
{
FlexCounter::removeRif(vid, groupName);
}
else
{
SWSS_LOG_ERROR("Object type for removal not supported, %s", objectTypeStr.c_str());
Expand Down Expand Up @@ -2957,6 +2961,17 @@ void processFlexCounterEvent(

FlexCounter::setPriorityGroupAttrList(vid, rid, groupName, pgAttrIds);
}
else if (objectType == SAI_OBJECT_TYPE_ROUTER_INTERFACE && field == RIF_COUNTER_ID_LIST)
{
std::vector<sai_router_interface_stat_t> rifCounterIds;
for (const auto &str : idStrings)
{
sai_router_interface_stat_t stat;
sai_deserialize_router_interface_stat(str.c_str(), &stat);
rifCounterIds.push_back(stat);
}
FlexCounter::setRifCounterList(vid, rid, groupName, rifCounterIds);
}
else
{
SWSS_LOG_ERROR("Object type and field combination is not supported, object type %s, field %s", objectTypeStr.c_str(), field.c_str());
Expand Down
163 changes: 163 additions & 0 deletions syncd/syncd_flex_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ static std::map<std::string, std::shared_ptr<FlexCounter>> g_flex_counters_map;
static std::set<sai_port_stat_t> supportedPortCounters;
static std::set<sai_queue_stat_t> supportedQueueCounters;
static std::set<sai_ingress_priority_group_stat_t> supportedPriorityGroupCounters;
static std::set<sai_router_interface_stat_t> supportedRifCounters;

FlexCounter::PortCounterIds::PortCounterIds(
_In_ sai_object_id_t port,
Expand Down Expand Up @@ -49,6 +50,14 @@ FlexCounter::IngressPriorityGroupCounterIds::IngressPriorityGroupCounterIds(
SWSS_LOG_ENTER();
}

FlexCounter::RifCounterIds::RifCounterIds(
_In_ sai_object_id_t rif,
_In_ const std::vector<sai_router_interface_stat_t> &rifIds):
rifId(rif), rifCounterIds(rifIds)
{
SWSS_LOG_ENTER();
}

void FlexCounter::setPollInterval(
_In_ uint32_t pollInterval,
_In_ std::string instanceId)
Expand Down Expand Up @@ -364,6 +373,62 @@ void FlexCounter::setPriorityGroupAttrList(
}
}

void FlexCounter::setRifCounterList(
_In_ sai_object_id_t rifVid,
_In_ sai_object_id_t rifId,
_In_ std::string instanceId,
_In_ const std::vector<sai_router_interface_stat_t> &counterIds)
{
SWSS_LOG_ENTER();

FlexCounter &fc = getInstance(instanceId);

// Initialize the supported counters list before setting
if (supportedRifCounters.empty())
{
fc.saiUpdateSupportedRifCounters(rifId);
}

// Remove unsupported counters
std::vector<sai_router_interface_stat_t> supportedIds;
for (auto &counter : counterIds)
{
if (fc.isRifCounterSupported(counter))
{
supportedIds.push_back(counter);
}
}

if (supportedIds.empty())
{
SWSS_LOG_ERROR("Router interface %s does not have supported counters", sai_serialize_object_id(rifId).c_str());

// Remove flex counter if all counter IDs and plugins are unregistered
if (fc.isEmpty())
{
removeInstance(instanceId);
}

return;
}

auto it = fc.m_rifCounterIdsMap.find(rifVid);
if (it != fc.m_rifCounterIdsMap.end())
{
(*it).second->rifCounterIds = supportedIds;
return;
}

auto rifCounterIds = std::make_shared<RifCounterIds>(rifId, supportedIds);
fc.m_rifCounterIdsMap.emplace(rifVid, rifCounterIds);

// Start flex counter thread in case it was not running due to empty counter IDs map
if (fc.m_pollInterval > 0)
{
fc.startFlexCounterThread();
}
}

void FlexCounter::removePort(
_In_ sai_object_id_t portVid,
_In_ std::string instanceId)
Expand Down Expand Up @@ -465,6 +530,35 @@ void FlexCounter::removePriorityGroup(
}
}

void FlexCounter::removeRif(
_In_ sai_object_id_t rifVid,
_In_ std::string instanceId)
{
SWSS_LOG_ENTER();

FlexCounter &fc = getInstance(instanceId);

auto it = fc.m_rifCounterIdsMap.find(rifVid);
if (it == fc.m_rifCounterIdsMap.end())
{
SWSS_LOG_NOTICE("Trying to remove nonexisting router interface counter from Id 0x%lx", rifVid);
// Remove flex counter if all counter IDs and plugins are unregistered
if (fc.isEmpty())
{
removeInstance(instanceId);
}
return;
}

fc.m_rifCounterIdsMap.erase(it);

// Remove flex counter if all counter IDs and plugins are unregistered
if (fc.isEmpty())
{
removeInstance(instanceId);
}
}

void FlexCounter::addPortCounterPlugin(
_In_ std::string sha,
_In_ std::string instanceId)
Expand Down Expand Up @@ -609,6 +703,13 @@ bool FlexCounter::isPriorityGroupCounterSupported(sai_ingress_priority_group_sta
return supportedPriorityGroupCounters.count(counter) != 0;
}

bool FlexCounter::isRifCounterSupported(sai_router_interface_stat_t counter) const
{
SWSS_LOG_ENTER();

return supportedRifCounters.count(counter) != 0;
}

FlexCounter::FlexCounter(std::string instanceId) : m_instanceId(instanceId)
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -644,6 +745,7 @@ void FlexCounter::collectCounters(
std::map<sai_object_id_t, std::shared_ptr<QueueAttrIds>> queueAttrIdsMap;
std::map<sai_object_id_t, std::shared_ptr<IngressPriorityGroupCounterIds>> priorityGroupCounterIdsMap;
std::map<sai_object_id_t, std::shared_ptr<IngressPriorityGroupAttrIds>> priorityGroupAttrIdsMap;
std::map<sai_object_id_t, std::shared_ptr<RifCounterIds>> rifCounterIdsMap;

{
std::lock_guard<std::mutex> lock(g_mutex);
Expand All @@ -652,6 +754,7 @@ void FlexCounter::collectCounters(
queueAttrIdsMap = m_queueAttrIdsMap;
priorityGroupCounterIdsMap = m_priorityGroupCounterIdsMap;
priorityGroupAttrIdsMap = m_priorityGroupAttrIdsMap;
rifCounterIdsMap = m_rifCounterIdsMap;
}

// Collect stats for every registered port
Expand Down Expand Up @@ -870,6 +973,41 @@ void FlexCounter::collectCounters(

countersTable.set(priorityGroupVidStr, values, "");
}
// Collect stats for every registered router interface
for (const auto &kv: rifCounterIdsMap)
{
const auto &rifVid = kv.first;
const auto &rifId = kv.second->rifId;
const auto &rifCounterIds = kv.second->rifCounterIds;

std::vector<uint64_t> rifStats(rifCounterIds.size());

// Get rif stats
sai_status_t status = sai_metadata_sai_router_interface_api->get_router_interface_stats(
rifId,
static_cast<uint32_t>(rifCounterIds.size()),
(const sai_stat_id_t *)rifCounterIds.data(),
rifStats.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get stats of router interface 0x%lx: %d", rifId, status);
continue;
}

// Push all counter values to a single vector
std::vector<swss::FieldValueTuple> values;

for (size_t i = 0; i != rifCounterIds.size(); i++)
{
const std::string &counterName = sai_serialize_router_interface_stat(rifCounterIds[i]);
values.emplace_back(counterName, std::to_string(rifStats[i]));
}

// Write counters to DB
std::string rifVidStr = sai_serialize_object_id(rifVid);

countersTable.set(rifVidStr, values, "");
}

countersTable.flush();
}
Expand Down Expand Up @@ -1094,3 +1232,28 @@ void FlexCounter::saiUpdateSupportedPriorityGroupCounters(
}
}
}

void FlexCounter::saiUpdateSupportedRifCounters(sai_object_id_t rifId)
{
SWSS_LOG_ENTER();

uint64_t value;
for (int cntr_id = SAI_ROUTER_INTERFACE_STAT_IN_OCTETS; cntr_id <= SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS; ++cntr_id)
{
sai_router_interface_stat_t counter = static_cast<sai_router_interface_stat_t>(cntr_id);

sai_status_t status = sai_metadata_sai_router_interface_api->get_router_interface_stats(rifId, 1, (const sai_stat_id_t *)&counter, &value);

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_INFO("Counter %s is not supported on router interface RID %s: %s",
sai_serialize_router_interface_stat(counter).c_str(),
sai_serialize_object_id(rifId).c_str(),
sai_serialize_status(status).c_str());

continue;
}

supportedRifCounters.insert(counter);
}
}
21 changes: 21 additions & 0 deletions syncd/syncd_flex_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class FlexCounter
_In_ sai_object_id_t priorityGroupId,
_In_ std::string instanceId,
_In_ const std::vector<sai_ingress_priority_group_stat_t> &counterIds);
static void setRifCounterList(
_In_ sai_object_id_t rifVid,
_In_ sai_object_id_t rifId,
_In_ std::string instanceId,
_In_ const std::vector<sai_router_interface_stat_t> &counterIds);
static void setQueueAttrList(
_In_ sai_object_id_t queueVid,
_In_ sai_object_id_t queueId,
Expand All @@ -58,6 +63,9 @@ class FlexCounter
static void removePriorityGroup(
_In_ sai_object_id_t priorityGroupVid,
_In_ std::string instanceId);
static void removeRif(
_In_ sai_object_id_t rifVid,
_In_ std::string instanceId);

static void addPortCounterPlugin(
_In_ std::string sha,
Expand Down Expand Up @@ -130,6 +138,16 @@ class FlexCounter
std::vector<sai_port_stat_t> portCounterIds;
};

struct RifCounterIds
{
RifCounterIds(
_In_ sai_object_id_t rif,
_In_ const std::vector<sai_router_interface_stat_t> &rifIds);

sai_object_id_t rifId;
std::vector<sai_router_interface_stat_t> rifCounterIds;
};

FlexCounter(std::string instanceId);
static FlexCounter& getInstance(std::string instanceId);
static void removeInstance(std::string instanceId);
Expand All @@ -143,9 +161,11 @@ class FlexCounter
void saiUpdateSupportedPortCounters(sai_object_id_t portId);
void saiUpdateSupportedQueueCounters(sai_object_id_t queueId, const std::vector<sai_queue_stat_t> &counterIds);
void saiUpdateSupportedPriorityGroupCounters(sai_object_id_t priorityGroupId, const std::vector<sai_ingress_priority_group_stat_t> &counterIds);
void saiUpdateSupportedRifCounters(sai_object_id_t rifId);
bool isPortCounterSupported(sai_port_stat_t counter) const;
bool isQueueCounterSupported(sai_queue_stat_t counter) const;
bool isPriorityGroupCounterSupported(sai_ingress_priority_group_stat_t counter) const;
bool isRifCounterSupported(sai_router_interface_stat_t counter) const;
bool isEmpty();

// Key is a Virtual ID
Expand All @@ -154,6 +174,7 @@ class FlexCounter
std::map<sai_object_id_t, std::shared_ptr<QueueAttrIds>> m_queueAttrIdsMap;
std::map<sai_object_id_t, std::shared_ptr<IngressPriorityGroupCounterIds>> m_priorityGroupCounterIdsMap;
std::map<sai_object_id_t, std::shared_ptr<IngressPriorityGroupAttrIds>> m_priorityGroupAttrIdsMap;
std::map<sai_object_id_t, std::shared_ptr<RifCounterIds>> m_rifCounterIdsMap;

// Plugins
std::set<std::string> m_queuePlugins;
Expand Down
2 changes: 2 additions & 0 deletions tests/aspell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ removedVidToRid
RID
RIDs
RIDTOVID
rif
RIF
RO
RPC
runtime
Expand Down

0 comments on commit 145ea44

Please sign in to comment.