From b645e7b9286c67b76701db8df6b63f56144624fa Mon Sep 17 00:00:00 2001 From: shiraez Date: Wed, 10 Jul 2024 09:24:10 +0300 Subject: [PATCH 1/6] pc: support policer counter + debug prints --- .../flex_counter/flex_counter_manager.cpp | 8 +- orchagent/flex_counter/flex_counter_manager.h | 1 + orchagent/flexcounterorch.cpp | 7 + orchagent/policerorch.cpp | 243 +++++++++++++++++- orchagent/policerorch.h | 29 +++ 5 files changed, 284 insertions(+), 4 deletions(-) diff --git a/orchagent/flex_counter/flex_counter_manager.cpp b/orchagent/flex_counter/flex_counter_manager.cpp index 635c757601..9cb9d721a2 100644 --- a/orchagent/flex_counter/flex_counter_manager.cpp +++ b/orchagent/flex_counter/flex_counter_manager.cpp @@ -50,6 +50,7 @@ const unordered_map FlexCounterManager::counter_id_field_lo { CounterType::HOSTIF_TRAP, FLOW_COUNTER_ID_LIST }, { CounterType::ROUTE, FLOW_COUNTER_ID_LIST }, { CounterType::ENI, ENI_COUNTER_ID_LIST }, + { CounterType::POLICER, POLICER_COUNTER_ID_LIST }, }; FlexManagerDirectory g_FlexManagerDirectory; @@ -60,6 +61,7 @@ FlexCounterManager *FlexManagerDirectory::createFlexCounterManager(const string& const bool enabled, FieldValueTuple fv_plugin) { + SWSS_LOG_DEBUG("create Flex Counter Manager %s", group_name.c_str()); if (m_managers.find(group_name) != m_managers.end()) { if (stats_mode != m_managers[group_name]->getStatsMode()) @@ -215,12 +217,14 @@ void FlexCounterManager::setCounterIdList( auto key = getFlexCounterTableKey(group_name, object_id); auto counter_ids = serializeCounterStats(counter_stats); auto effective_switch_id = switch_id == SAI_NULL_OBJECT_ID ? gSwitchId : switch_id; + SWSS_LOG_DEBUG("counter_ids: %s", counter_ids.c_str()); + // SWSS_LOG_DEBUG("counter_type_it: %s", counter_type_it.second); startFlexCounterPolling(effective_switch_id, key, counter_ids, counter_type_it->second); installed_counters[object_id] = effective_switch_id; - SWSS_LOG_DEBUG("Updated flex counter id list for object '%" PRIu64 "' in group '%s'.", - object_id, + SWSS_LOG_DEBUG("Updated flex counter id list for object '%s' in group '%s'.", + sai_serialize_object_id(object_id).c_str(), group_name.c_str()); } diff --git a/orchagent/flex_counter/flex_counter_manager.h b/orchagent/flex_counter/flex_counter_manager.h index b9e6e4c487..05d0c64ea5 100644 --- a/orchagent/flex_counter/flex_counter_manager.h +++ b/orchagent/flex_counter/flex_counter_manager.h @@ -33,6 +33,7 @@ enum class CounterType HOSTIF_TRAP, ROUTE, ENI + POLICER, }; // FlexCounterManager allows users to manage a group of flex counters. diff --git a/orchagent/flexcounterorch.cpp b/orchagent/flexcounterorch.cpp index b8918d0ac9..0567848e14 100644 --- a/orchagent/flexcounterorch.cpp +++ b/orchagent/flexcounterorch.cpp @@ -25,6 +25,7 @@ extern IntfsOrch *gIntfsOrch; extern BufferOrch *gBufferOrch; extern Directory gDirectory; extern CoppOrch *gCoppOrch; +extern PolicerOrch *gPolicerOrch; extern FlowCounterRouteOrch *gFlowCounterRouteOrch; extern sai_object_id_t gSwitchId; @@ -41,6 +42,7 @@ extern sai_object_id_t gSwitchId; #define FLOW_CNT_TRAP_KEY "FLOW_CNT_TRAP" #define FLOW_CNT_ROUTE_KEY "FLOW_CNT_ROUTE" #define ENI_KEY "ENI" +#define POLICER_KEY "POLICER" unordered_map flexCounterGroupMap = { @@ -63,6 +65,7 @@ unordered_map flexCounterGroupMap = {"MACSEC_SA", COUNTERS_MACSEC_SA_GROUP}, {"MACSEC_SA_ATTR", COUNTERS_MACSEC_SA_ATTR_GROUP}, {"MACSEC_FLOW", COUNTERS_MACSEC_FLOW_GROUP}, + {POLICER_KEY, POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP}, {"ENI", ENI_STAT_COUNTER_FLEX_COUNTER_GROUP} }; @@ -208,6 +211,10 @@ void FlexCounterOrch::doTask(Consumer &consumer) { dash_orch->handleFCStatusUpdate((value == "enable")); } + if (gPolicerOrch && (key== POLICER_KEY) && (value == "enable")) + { + gPolicerOrch->generatePolicerCounterMap(); + } if (gCoppOrch && (key == FLOW_CNT_TRAP_KEY)) { if (value == "enable") diff --git a/orchagent/policerorch.cpp b/orchagent/policerorch.cpp index 68dfffe898..769b9630de 100644 --- a/orchagent/policerorch.cpp +++ b/orchagent/policerorch.cpp @@ -1,6 +1,8 @@ #include "sai.h" #include "policerorch.h" - +#include "sai_serialize.h" +#include "flow_counter_handler.h" +#include "aclorch.h" #include "converter.h" #include @@ -11,6 +13,7 @@ extern sai_policer_api_t* sai_policer_api; extern sai_port_api_t *sai_port_api; extern sai_object_id_t gSwitchId; +extern sai_object_id_t m_counterOid; extern PortsOrch* gPortsOrch; #define ETHERNET_PREFIX "Ethernet" @@ -31,6 +34,8 @@ static const string storm_broadcast = "broadcast"; static const string storm_unknown_unicast = "unknown-unicast"; static const string storm_unknown_mcast = "unknown-multicast"; +extern bool gTraditionalFlexCounter; + static const map meter_type_map = { {"PACKETS", SAI_METER_TYPE_PACKETS}, {"BYTES", SAI_METER_TYPE_BYTES} @@ -58,6 +63,19 @@ static const map packet_action_map = { {"TRANSIT", SAI_PACKET_ACTION_TRANSIT} }; +const vector policer_stat_ids = { + + SAI_POLICER_STAT_PACKETS, + SAI_POLICER_STAT_ATTR_BYTES, + SAI_POLICER_STAT_GREEN_PACKETS, + SAI_POLICER_STAT_GREEN_BYTES, + SAI_POLICER_STAT_YELLOW_PACKETS, + SAI_POLICER_STAT_YELLOW_BYTES, + SAI_POLICER_STAT_RED_PACKETS, + SAI_POLICER_STAT_RED_BYTES + +}; + bool PolicerOrch::policerExists(const string &name) { SWSS_LOG_ENTER(); @@ -113,11 +131,39 @@ bool PolicerOrch::decreaseRefCount(const string &name) return true; } -PolicerOrch::PolicerOrch(vector &tableNames, PortsOrch *portOrch) : Orch(tableNames), m_portsOrch(portOrch) +// PolicerOrch::PolicerOrch(vector &tableNames, PortsOrch *portOrch) : Orch(tableNames), m_portsOrch(portOrch) +// { +// SWSS_LOG_ENTER(); +// } + +PolicerOrch::PolicerOrch( vector &tableNames, PortsOrch *portOrch) : + Orch(tableNames), + m_portsOrch(portOrch), + m_counter_db(std::shared_ptr(new DBConnector("COUNTERS_DB", 0))), + m_asic_db(std::shared_ptr(new DBConnector("ASIC_DB", 0))), + m_counter_table(std::unique_ptr(new Table(m_counter_db.get(), COUNTERS_POLICER_NAME_MAP))), + m_vidToRidTable(std::unique_ptr
(new Table(m_asic_db.get(), "VIDTORID"))), + m_policer_counter_manager(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ,10000,false) { SWSS_LOG_ENTER(); + auto intervT = timespec { .tv_sec = 1 , .tv_nsec = 0 }; + m_FlexCounterUpdTimer = new SelectableTimer(intervT); + auto executorT = new ExecutableTimer(m_FlexCounterUpdTimer, this, "FLEX_COUNTER_UPD_TIMER"); + Orch::addExecutor(executorT); + SWSS_LOG_DEBUG("PolicerOrch created"); + initPolicerCounterPlugin(); + + + // initDefaultHostIntfTable(); + // initDefaultTrapGroup(); + // initDefaultTrapIds(); } +// void PolicerOrch::generatePolicerCounterMap() +// { +// m_FlexCounterUpdTimer->start(); +// } + task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsValuesTuple tuple) { auto key = kfvKey(tuple); @@ -238,6 +284,22 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa SWSS_LOG_DEBUG("Created storm-control policer %s", storm_policer_name.c_str()); m_syncdPolicers[storm_policer_name] = policer_id; m_policerRefCounts[storm_policer_name] = 0; + + // sai_object_id_t counter_id; + // if (!FlowCounterHandler::createGenericCounter(counter_id)) + // { + // SWSS_LOG_WARN("Failed to create counter id"); + // } + // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; + // attr.value.oid = counter_id; + // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); + // if (sai_status != SAI_STATUS_SUCCESS) + // { + // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); + // } + + addPolicerToFlexCounter(policer_id, key.c_str()); + } // Update an existing policer else @@ -267,6 +329,19 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa } } + // sai_object_id_t counter_id; + // if (!FlowCounterHandler::createGenericCounter(counter_id)) + // { + // SWSS_LOG_WARN("Failed to create counter id"); + // } + // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; + // attr.value.oid = counter_id; + // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); + // if (sai_status != SAI_STATUS_SUCCESS) + // { + // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); + // } + // addPolicerToFlexCounter(policer_id, key.c_str()); } policer_id = m_syncdPolicers[storm_policer_name]; @@ -371,6 +446,153 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa return task_process_status::task_success; } +// sai_object_id_t PolicerOrch::getCounterOid() const +// { +// return m_counterOid; +// } + +void PolicerOrch::generatePolicerCounterStats(std::unordered_set counter_stats) +{ + for (const auto& it: policer_stat_ids) + { + counter_stats.emplace(sai_serialize_policer_stat(it)); + } +} + +string PolicerOrch::getPolicerFlexCounterTableKey(string key) +{ + return string(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP) + ":" + key; +} + +void PolicerOrch::removePCFromFlexCounter(const string &id, const string &name) +{ + SWSS_LOG_ENTER(); + /* remove it from COUNTERS_DB maps */ + m_counter_table->hdel("", name); + m_vidToRidTable->hdel("", id); + + /* remove it from FLEX_COUNTER_DB */ + string key = getPolicerFlexCounterTableKey(id); + + stopFlexCounterPolling(gSwitchId, key); + + SWSS_LOG_DEBUG("Unregistered interface %s from Flex counter", name.c_str()); +} + +// void PolicerOrch::getstats(sai_object_id_t policer_id) + +// { +// uint64_t stats[8]; +// sai_status_t status = sai_policer_api->get_policer_stats( +// &policer_id, 4, (uint32_t)attrs.size(), stats); +// if (status != SAI_STATUS_SUCCESS) +// { +// SWSS_LOG_ERROR("Failed to create policer %s, rv:%d", +// storm_policer_name.c_str(), status); +// if (handleSaiCreateStatus(SAI_API_POLICER, status) == task_need_retry) +// { +// return task_process_status::task_need_retry; +// } +// } +// } + +void PolicerOrch::doTask(SelectableTimer &timer) +{ + // SWSS_LOG_ENTER(); + // SWSS_LOG_INFO("policer"); + // SWSS_LOG_DEBUG("Registering %" PRId64 " new policer", m_pendingPcAddToFlexCntr.size()); + string value; + if(m_pendingPcAddToFlexCntr.size() > 0) + { + SWSS_LOG_INFO("policer for if"); + } + for (auto it = m_pendingPcAddToFlexCntr.begin(); it != m_pendingPcAddToFlexCntr.end(); ) + { + SWSS_LOG_INFO("policer for loop"); + + const auto id = sai_serialize_object_id(it->first); + if (!gTraditionalFlexCounter || m_vidToRidTable->hget("", id, value)) + { + SWSS_LOG_INFO("Registering second %s, id %s", it->second.c_str(), id.c_str()); + // SWSS_LOG_INFO("Registering first %s, id %s", it->first, id.c_str()); + vector policerNameVector; + + policerNameVector.emplace_back(it->second.c_str(), id); + m_counter_table->set("", policerNameVector); + m_counter_db->hset(COUNTERS_POLICER_NAME_MAP, it->second.c_str(), id.c_str()); + + std::unordered_set counter_stats; + // FlowCounterHandler::getGenericCounterStatIdList(counter_stats); + // string key = getPolicerFlexCounterTableKey(id); + + // std::ostringstream counters_stream; + generatePolicerCounterStats(counter_stats); + SWSS_LOG_DEBUG("policer1"); + // std::ostringstream counters_stream; + // for (const auto& it: policer_stat_ids) + // { + // counters_stream << sai_serialize_policer_stat(it) << comma; + // } + // auto &&counters_str = counters_stream.str(); + + SWSS_LOG_DEBUG("policer2"); + // auto &&counters_str = counter_stats.str(); + m_policer_counter_manager.setCounterIdList(it->first, CounterType::POLICER, counter_stats); + // startFlexCounterPolling(gSwitchId, it->second.c_str(), serializeCounterStats(counter_stats), POLICER_COUNTER_ID_LIST); + // startFlexCounterPolling(gSwitchId, it->second.c_str(), counters_str.c_str(), POLICER_COUNTER_ID_LIST); + SWSS_LOG_DEBUG("inserted %s to flex counter", it->second.c_str()); + it = m_pendingPcAddToFlexCntr.erase(it); + } + else + { + ++it; + } + } +} + + +void PolicerOrch::initPolicerCounterPlugin() +{ + // std::string pcRatePluginName = "pcounter.lua"; + // std::string pcSha; + // try + // { + // std::string pcLuaScript = swss::loadLuaScript(pcRatePluginName); + // SWSS_LOG_DEBUG("pcLuaScript"); + // pcSha = swss::loadRedisScript(m_counter_db.get(), pcLuaScript); + // } + // catch (const runtime_error &e) + // { + // SWSS_LOG_ERROR("Policer flex counter groups were not set successfully: %s", e.what()); + // } + + // setFlexCounterGroupParameter(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, + // "", + // STATS_MODE_READ, + // POLICER_PLUGIN_FIELD, + // pcSha); + // m_pc_plugin_loaded = true; +} + + +void PolicerOrch::addPolicerToFlexCounter(sai_object_id_t oid, const string &name) +{ + SWSS_LOG_NOTICE("policer oid:%" PRIx64" value: %s", oid, name.c_str()); + m_pendingPcAddToFlexCntr[oid] = name; +} + +void PolicerOrch::generatePolicerCounterMap() +{ + if (m_isPolicerCounterMapGenerated) + { + return; + } + + m_FlexCounterUpdTimer->start(); + + m_isPolicerCounterMapGenerated = true; +} + void PolicerOrch::doTask(Consumer &consumer) { SWSS_LOG_ENTER(); @@ -511,6 +733,23 @@ void PolicerOrch::doTask(Consumer &consumer) SWSS_LOG_NOTICE("Created policer %s", key.c_str()); m_syncdPolicers[key] = policer_id; m_policerRefCounts[key] = 0; + + // std::unordered_set counter_stats; + // FlowCounterHandler::getGenericCounterStatIdList(counter_stats); + // m_policer_counter_manager.setCounterIdList(policer_id, CounterType::POLICER, counter_stats); + // sai_object_id_t counter_id; + // if (!FlowCounterHandler::createGenericCounter(counter_id)) + // { + // SWSS_LOG_WARN("Failed to create counter id"); + // } + // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; + // attr.value.oid = counter_id; + // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); + // if (sai_status != SAI_STATUS_SUCCESS) + // { + // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); + // } + addPolicerToFlexCounter(policer_id, key.c_str()); } // Update an existing policer else diff --git a/orchagent/policerorch.h b/orchagent/policerorch.h index 9814179958..4bfe7b6305 100644 --- a/orchagent/policerorch.h +++ b/orchagent/policerorch.h @@ -3,11 +3,16 @@ #include #include +#include "flex_counter_manager.h" +#include "dbconnector.h" #include "orch.h" #include "portsorch.h" + using namespace std; +#define POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP "POLICER_STAT_COUNTER" + typedef map PolicerTable; typedef map PolicerRefCountTable; @@ -22,12 +27,36 @@ class PolicerOrch : public Orch bool increaseRefCount(const string &name); bool decreaseRefCount(const string &name); task_process_status handlePortStormControlTable(swss::KeyOpFieldsValuesTuple tuple); + void generatePolicerCounterMap(); + void addPolicerToFlexCounter(sai_object_id_t oid, const string &name); + sai_object_id_t getCounterOid() const; private: PortsOrch *m_portsOrch; + // void PolicerOrch::getstats(sai_object_id_t policer_id); virtual void doTask(Consumer& consumer); + void doTask(swss::SelectableTimer&) override; PolicerTable m_syncdPolicers; PolicerRefCountTable m_policerRefCounts; + FlexCounterManager m_policer_counter_manager; + bool m_isPolicerCounterMapGenerated = false; + + void getPolicerCounter(void); + void initPolicerCounterPlugin(void); + void removePCFromFlexCounter(const string &id, const string &name); + void generatePolicerCounterStats(std::unordered_set counter_stats); + string getPolicerFlexCounterTableKey(string key); + +protected: + std::shared_ptr m_counter_db; + std::shared_ptr m_flex_db; + std::shared_ptr m_asic_db; + std::unique_ptr
m_counter_table; + std::unique_ptr
m_vidToRidTable; + std::unique_ptr m_flex_counter_group_table; + std::map m_pendingPcAddToFlexCntr; + + SelectableTimer* m_FlexCounterUpdTimer = nullptr; }; From 7f6d444efb8e16800253d549a4c94d4b9a99d615 Mon Sep 17 00:00:00 2001 From: shiraez Date: Tue, 16 Jul 2024 17:36:09 +0300 Subject: [PATCH 2/6] fix generatePolicerCounterStats --- orchagent/policerorch.cpp | 17 ++++++++++++++--- orchagent/policerorch.h | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/orchagent/policerorch.cpp b/orchagent/policerorch.cpp index 769b9630de..ecf4d65b1b 100644 --- a/orchagent/policerorch.cpp +++ b/orchagent/policerorch.cpp @@ -298,7 +298,7 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); // } - addPolicerToFlexCounter(policer_id, key.c_str()); + // addPolicerToFlexCounter(policer_id, key.c_str()); } // Update an existing policer @@ -451,7 +451,7 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa // return m_counterOid; // } -void PolicerOrch::generatePolicerCounterStats(std::unordered_set counter_stats) +void PolicerOrch::generatePolicerCounterStats(std::unordered_set& counter_stats) { for (const auto& it: policer_stat_ids) { @@ -537,7 +537,7 @@ void PolicerOrch::doTask(SelectableTimer &timer) SWSS_LOG_DEBUG("policer2"); // auto &&counters_str = counter_stats.str(); - m_policer_counter_manager.setCounterIdList(it->first, CounterType::POLICER, counter_stats); + m_policer_counter_manager.setCounterIdList(it->first, CounterType::POLICER, pcounter_stats, gSwitchId); // startFlexCounterPolling(gSwitchId, it->second.c_str(), serializeCounterStats(counter_stats), POLICER_COUNTER_ID_LIST); // startFlexCounterPolling(gSwitchId, it->second.c_str(), counters_str.c_str(), POLICER_COUNTER_ID_LIST); SWSS_LOG_DEBUG("inserted %s to flex counter", it->second.c_str()); @@ -551,6 +551,17 @@ void PolicerOrch::doTask(SelectableTimer &timer) } +std::unordered_set PolicerOrch::generatePCounterStats() +{ + std::unordered_set counter_stats; + for (const auto& it: policer_stat_ids) + { + counter_stats.emplace(sai_serialize_policer_stat(it)); + } + return counter_stats; +} + + void PolicerOrch::initPolicerCounterPlugin() { // std::string pcRatePluginName = "pcounter.lua"; diff --git a/orchagent/policerorch.h b/orchagent/policerorch.h index 4bfe7b6305..6a79d46f11 100644 --- a/orchagent/policerorch.h +++ b/orchagent/policerorch.h @@ -44,8 +44,9 @@ class PolicerOrch : public Orch void getPolicerCounter(void); void initPolicerCounterPlugin(void); void removePCFromFlexCounter(const string &id, const string &name); - void generatePolicerCounterStats(std::unordered_set counter_stats); + void generatePolicerCounterStats(std::unordered_set& counter_stats); string getPolicerFlexCounterTableKey(string key); + std::unordered_set generatePCounterStats(); protected: std::shared_ptr m_counter_db; From a91318a62772cd1173f10b413a2dd51910b23f7d Mon Sep 17 00:00:00 2001 From: shiraez Date: Tue, 30 Jul 2024 12:12:52 +0300 Subject: [PATCH 3/6] policerorch: support remove counter --- orchagent/policerorch.cpp | 148 ++++++-------------------------------- orchagent/policerorch.h | 2 +- 2 files changed, 23 insertions(+), 127 deletions(-) diff --git a/orchagent/policerorch.cpp b/orchagent/policerorch.cpp index ecf4d65b1b..b947779b9f 100644 --- a/orchagent/policerorch.cpp +++ b/orchagent/policerorch.cpp @@ -151,18 +151,8 @@ PolicerOrch::PolicerOrch( vector &tableNames, PortsOrch *portOrc auto executorT = new ExecutableTimer(m_FlexCounterUpdTimer, this, "FLEX_COUNTER_UPD_TIMER"); Orch::addExecutor(executorT); SWSS_LOG_DEBUG("PolicerOrch created"); - initPolicerCounterPlugin(); - - - // initDefaultHostIntfTable(); - // initDefaultTrapGroup(); - // initDefaultTrapIds(); } -// void PolicerOrch::generatePolicerCounterMap() -// { -// m_FlexCounterUpdTimer->start(); -// } task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsValuesTuple tuple) { @@ -285,18 +275,6 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa m_syncdPolicers[storm_policer_name] = policer_id; m_policerRefCounts[storm_policer_name] = 0; - // sai_object_id_t counter_id; - // if (!FlowCounterHandler::createGenericCounter(counter_id)) - // { - // SWSS_LOG_WARN("Failed to create counter id"); - // } - // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; - // attr.value.oid = counter_id; - // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); - // if (sai_status != SAI_STATUS_SUCCESS) - // { - // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); - // } // addPolicerToFlexCounter(policer_id, key.c_str()); @@ -329,18 +307,7 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa } } - // sai_object_id_t counter_id; - // if (!FlowCounterHandler::createGenericCounter(counter_id)) - // { - // SWSS_LOG_WARN("Failed to create counter id"); - // } - // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; - // attr.value.oid = counter_id; - // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); - // if (sai_status != SAI_STATUS_SUCCESS) - // { - // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); - // } + // addPolicerToFlexCounter(policer_id, key.c_str()); } policer_id = m_syncdPolicers[storm_policer_name]; @@ -464,82 +431,50 @@ string PolicerOrch::getPolicerFlexCounterTableKey(string key) return string(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP) + ":" + key; } -void PolicerOrch::removePCFromFlexCounter(const string &id, const string &name) +void PolicerOrch::removePCFromFlexCounter(sai_object_id_t id, const string &name) { SWSS_LOG_ENTER(); + + std::string counter_oid_str; + counter_oid_str = sai_serialize_object_id(id); + + auto update_iter = m_pendingPcAddToFlexCntr.find(id); + if (update_iter == m_pendingPcAddToFlexCntr.end()) + { + m_policer_counter_manager.clearCounterIdList(id); + } + else + { + m_pendingPcAddToFlexCntr.erase(update_iter); + } + /* remove it from COUNTERS_DB maps */ m_counter_table->hdel("", name); - m_vidToRidTable->hdel("", id); - - /* remove it from FLEX_COUNTER_DB */ - string key = getPolicerFlexCounterTableKey(id); + m_vidToRidTable->hdel("", counter_oid_str); - stopFlexCounterPolling(gSwitchId, key); + // /* remove it from FLEX_COUNTER_DB */ + // string key = getPolicerFlexCounterTableKey(counter_oid_str); SWSS_LOG_DEBUG("Unregistered interface %s from Flex counter", name.c_str()); } -// void PolicerOrch::getstats(sai_object_id_t policer_id) - -// { -// uint64_t stats[8]; -// sai_status_t status = sai_policer_api->get_policer_stats( -// &policer_id, 4, (uint32_t)attrs.size(), stats); -// if (status != SAI_STATUS_SUCCESS) -// { -// SWSS_LOG_ERROR("Failed to create policer %s, rv:%d", -// storm_policer_name.c_str(), status); -// if (handleSaiCreateStatus(SAI_API_POLICER, status) == task_need_retry) -// { -// return task_process_status::task_need_retry; -// } -// } -// } - void PolicerOrch::doTask(SelectableTimer &timer) { - // SWSS_LOG_ENTER(); - // SWSS_LOG_INFO("policer"); - // SWSS_LOG_DEBUG("Registering %" PRId64 " new policer", m_pendingPcAddToFlexCntr.size()); string value; - if(m_pendingPcAddToFlexCntr.size() > 0) - { - SWSS_LOG_INFO("policer for if"); - } for (auto it = m_pendingPcAddToFlexCntr.begin(); it != m_pendingPcAddToFlexCntr.end(); ) { - SWSS_LOG_INFO("policer for loop"); - const auto id = sai_serialize_object_id(it->first); if (!gTraditionalFlexCounter || m_vidToRidTable->hget("", id, value)) { SWSS_LOG_INFO("Registering second %s, id %s", it->second.c_str(), id.c_str()); - // SWSS_LOG_INFO("Registering first %s, id %s", it->first, id.c_str()); vector policerNameVector; policerNameVector.emplace_back(it->second.c_str(), id); m_counter_table->set("", policerNameVector); m_counter_db->hset(COUNTERS_POLICER_NAME_MAP, it->second.c_str(), id.c_str()); - std::unordered_set counter_stats; - // FlowCounterHandler::getGenericCounterStatIdList(counter_stats); - // string key = getPolicerFlexCounterTableKey(id); - - // std::ostringstream counters_stream; - generatePolicerCounterStats(counter_stats); - SWSS_LOG_DEBUG("policer1"); - // std::ostringstream counters_stream; - // for (const auto& it: policer_stat_ids) - // { - // counters_stream << sai_serialize_policer_stat(it) << comma; - // } - // auto &&counters_str = counters_stream.str(); - - SWSS_LOG_DEBUG("policer2"); - // auto &&counters_str = counter_stats.str(); + auto pcounter_stats = generatePCounterStats(); m_policer_counter_manager.setCounterIdList(it->first, CounterType::POLICER, pcounter_stats, gSwitchId); - // startFlexCounterPolling(gSwitchId, it->second.c_str(), serializeCounterStats(counter_stats), POLICER_COUNTER_ID_LIST); - // startFlexCounterPolling(gSwitchId, it->second.c_str(), counters_str.c_str(), POLICER_COUNTER_ID_LIST); SWSS_LOG_DEBUG("inserted %s to flex counter", it->second.c_str()); it = m_pendingPcAddToFlexCntr.erase(it); } @@ -561,34 +496,8 @@ std::unordered_set PolicerOrch::generatePCounterStats() return counter_stats; } - -void PolicerOrch::initPolicerCounterPlugin() -{ - // std::string pcRatePluginName = "pcounter.lua"; - // std::string pcSha; - // try - // { - // std::string pcLuaScript = swss::loadLuaScript(pcRatePluginName); - // SWSS_LOG_DEBUG("pcLuaScript"); - // pcSha = swss::loadRedisScript(m_counter_db.get(), pcLuaScript); - // } - // catch (const runtime_error &e) - // { - // SWSS_LOG_ERROR("Policer flex counter groups were not set successfully: %s", e.what()); - // } - - // setFlexCounterGroupParameter(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, - // "", - // STATS_MODE_READ, - // POLICER_PLUGIN_FIELD, - // pcSha); - // m_pc_plugin_loaded = true; -} - - void PolicerOrch::addPolicerToFlexCounter(sai_object_id_t oid, const string &name) { - SWSS_LOG_NOTICE("policer oid:%" PRIx64" value: %s", oid, name.c_str()); m_pendingPcAddToFlexCntr[oid] = name; } @@ -745,21 +654,6 @@ void PolicerOrch::doTask(Consumer &consumer) m_syncdPolicers[key] = policer_id; m_policerRefCounts[key] = 0; - // std::unordered_set counter_stats; - // FlowCounterHandler::getGenericCounterStatIdList(counter_stats); - // m_policer_counter_manager.setCounterIdList(policer_id, CounterType::POLICER, counter_stats); - // sai_object_id_t counter_id; - // if (!FlowCounterHandler::createGenericCounter(counter_id)) - // { - // SWSS_LOG_WARN("Failed to create counter id"); - // } - // attr.id = SAI_POLICER_ATTR_ENABLE_COUNTER_PACKET_ACTION_LIST; - // attr.value.oid = counter_id; - // sai_status_t sai_status = sai_policer_api->set_policer_attribute(policer_id, &attr); - // if (sai_status != SAI_STATUS_SUCCESS) - // { - // SWSS_LOG_WARN("Failed to bind policer %" PRId64 " to counter %" PRId64 "", policer_id, counter_id); - // } addPolicerToFlexCounter(policer_id, key.c_str()); } // Update an existing policer @@ -817,6 +711,8 @@ void PolicerOrch::doTask(Consumer &consumer) continue; } + removePCFromFlexCounter(m_syncdPolicers[key], key); + sai_status_t status = sai_policer_api->remove_policer( m_syncdPolicers.at(key)); if (status != SAI_STATUS_SUCCESS) diff --git a/orchagent/policerorch.h b/orchagent/policerorch.h index 6a79d46f11..c989d4ec50 100644 --- a/orchagent/policerorch.h +++ b/orchagent/policerorch.h @@ -43,7 +43,7 @@ class PolicerOrch : public Orch void getPolicerCounter(void); void initPolicerCounterPlugin(void); - void removePCFromFlexCounter(const string &id, const string &name); + void removePCFromFlexCounter(sai_object_id_t id, const string &name); void generatePolicerCounterStats(std::unordered_set& counter_stats); string getPolicerFlexCounterTableKey(string key); std::unordered_set generatePCounterStats(); From 0d626112eb2183ee7a14f4b999ad057db0dcf235 Mon Sep 17 00:00:00 2001 From: shiraez Date: Tue, 3 Dec 2024 12:40:56 +0200 Subject: [PATCH 4/6] Policer counter test --- tests/test_flex_counters.py | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/test_flex_counters.py b/tests/test_flex_counters.py index 1963248c2f..a81f045bad 100644 --- a/tests/test_flex_counters.py +++ b/tests/test_flex_counters.py @@ -78,6 +78,13 @@ 'name_map': 'COUNTERS_ROUTE_NAME_MAP', 'pre_test': 'pre_route_flow_counter_test', 'post_test': 'post_route_flow_counter_test', + }, + 'policer_counter': { + 'key': 'POLICER', + 'group_name': 'POLICER_STAT_COUNTER', + 'name_map': 'COUNTERS_POLICER_NAME_MAP', + 'pre_test': 'pre_policer_counter_test', + 'post_test': 'post_policer_counter_test', } } @@ -184,6 +191,16 @@ def pre_route_flow_counter_test(self, meta_data): dvs.servers[1].runcmd("ping -6 -c 1 2001::1") dvs.runcmd("vtysh -c \"configure terminal\" -c \"ipv6 route 2000::/64 2001::2\"") + def pre_policer_counter_test(self, meta_data): + self.config_db.db_connection.hset("POLICER|policer1", "meter_type", "packets") + self.config_db.db_connection.hset("POLICER|policer1", "mode", "sr_tcm") + self.config_db.db_connection.hset("POLICER|policer1", "cir", "600") + self.config_db.db_connection.hset("POLICER|policer1", "cbs", "600") + self.config_db.db_connection.hset("POLICER|policer1", "red_packet_action", "drop") + + def post_policer_counter_test(self, meta_data): + self.config_db.db_connection.hdel('POLICER|policer1', "NULL") + def post_rif_counter_test(self, meta_data): self.config_db.db_connection.hdel('INTERFACE|Ethernet0|192.168.0.1/24', "NULL") @@ -740,3 +757,55 @@ def test_create_remove_buffer_watermark_queue_pg_counter(self, dvs): index = '8' if 'queue' in counterpoll_type else '7' self.wait_for_buffer_pg_queue_counter(meta_data['name_map'], 'Ethernet0', index, False) self.wait_for_id_list_remove(meta_data['group_name'], "Ethernet0", counter_oid) + + + def test_create_remove_policer_counter(self, dvs): + """Test creation and removal of policer counters + + Args: + dvs (object): virtual switch object + + Test steps: + 1. Enable policer flex counters + 2. Configure new policer + 3. Verify counter is automatically created + 4. Remove the policer + 5. Verify counter is automatically removed + """ + self.setup_dbs(dvs) + meta_data = counter_group_meta['policer_counter'] + + # Enable flex counter group for policer + self.set_flex_counter_group_status(meta_data['key'], meta_data['name_map']) + + # Create a test policer + self.config_db.create_entry('POLICER', 'policer1', { + 'meter_type': 'bytes', + 'mode': 'sr_tcm', + 'cir': '600000', + 'cbs': '600000', + 'red_packet_action': 'drop' + }) + + # Wait for counter to be created and verify + for _ in range(NUMBER_OF_RETRIES): + counter_oid = self.counters_db.db_connection.hget(meta_data['name_map'], 'policer1') + if counter_oid: + break + time.sleep(1) + + assert counter_oid is not None, "Policer counter was not created" + self.wait_for_id_list(meta_data['group_name'], 'policer1', counter_oid) + + # Remove policer + self.config_db.delete_entry('POLICER', 'policer1') + + # Wait for counter to be removed and verify + for _ in range(NUMBER_OF_RETRIES): + counter_oid = self.counters_db.db_connection.hget(meta_data['name_map'], 'policer1') + if not counter_oid: + break + time.sleep(1) + + assert counter_oid is None, "Policer counter was not removed" + self.wait_for_id_list_remove(meta_data['group_name'], 'policer1', counter_oid) \ No newline at end of file From 6409451e4ceefc3272933dbb67c95ff428958591 Mon Sep 17 00:00:00 2001 From: shiraez Date: Tue, 3 Dec 2024 13:24:07 +0200 Subject: [PATCH 5/6] policer counter - fix typo --- orchagent/flex_counter/flex_counter_manager.h | 2 +- orchagent/policerorch.cpp | 25 ------------------- orchagent/policerorch.h | 3 --- 3 files changed, 1 insertion(+), 29 deletions(-) diff --git a/orchagent/flex_counter/flex_counter_manager.h b/orchagent/flex_counter/flex_counter_manager.h index 05d0c64ea5..08a23af3dd 100644 --- a/orchagent/flex_counter/flex_counter_manager.h +++ b/orchagent/flex_counter/flex_counter_manager.h @@ -32,7 +32,7 @@ enum class CounterType TUNNEL, HOSTIF_TRAP, ROUTE, - ENI + ENI, POLICER, }; diff --git a/orchagent/policerorch.cpp b/orchagent/policerorch.cpp index b947779b9f..c94241a07e 100644 --- a/orchagent/policerorch.cpp +++ b/orchagent/policerorch.cpp @@ -131,10 +131,6 @@ bool PolicerOrch::decreaseRefCount(const string &name) return true; } -// PolicerOrch::PolicerOrch(vector &tableNames, PortsOrch *portOrch) : Orch(tableNames), m_portsOrch(portOrch) -// { -// SWSS_LOG_ENTER(); -// } PolicerOrch::PolicerOrch( vector &tableNames, PortsOrch *portOrch) : Orch(tableNames), @@ -274,9 +270,6 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa SWSS_LOG_DEBUG("Created storm-control policer %s", storm_policer_name.c_str()); m_syncdPolicers[storm_policer_name] = policer_id; m_policerRefCounts[storm_policer_name] = 0; - - - // addPolicerToFlexCounter(policer_id, key.c_str()); } // Update an existing policer @@ -307,8 +300,6 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa } } - - // addPolicerToFlexCounter(policer_id, key.c_str()); } policer_id = m_syncdPolicers[storm_policer_name]; @@ -413,19 +404,6 @@ task_process_status PolicerOrch::handlePortStormControlTable(swss::KeyOpFieldsVa return task_process_status::task_success; } -// sai_object_id_t PolicerOrch::getCounterOid() const -// { -// return m_counterOid; -// } - -void PolicerOrch::generatePolicerCounterStats(std::unordered_set& counter_stats) -{ - for (const auto& it: policer_stat_ids) - { - counter_stats.emplace(sai_serialize_policer_stat(it)); - } -} - string PolicerOrch::getPolicerFlexCounterTableKey(string key) { return string(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP) + ":" + key; @@ -452,9 +430,6 @@ void PolicerOrch::removePCFromFlexCounter(sai_object_id_t id, const string &name m_counter_table->hdel("", name); m_vidToRidTable->hdel("", counter_oid_str); - // /* remove it from FLEX_COUNTER_DB */ - // string key = getPolicerFlexCounterTableKey(counter_oid_str); - SWSS_LOG_DEBUG("Unregistered interface %s from Flex counter", name.c_str()); } diff --git a/orchagent/policerorch.h b/orchagent/policerorch.h index c989d4ec50..af3b5eba8c 100644 --- a/orchagent/policerorch.h +++ b/orchagent/policerorch.h @@ -29,10 +29,8 @@ class PolicerOrch : public Orch task_process_status handlePortStormControlTable(swss::KeyOpFieldsValuesTuple tuple); void generatePolicerCounterMap(); void addPolicerToFlexCounter(sai_object_id_t oid, const string &name); - sai_object_id_t getCounterOid() const; private: PortsOrch *m_portsOrch; - // void PolicerOrch::getstats(sai_object_id_t policer_id); virtual void doTask(Consumer& consumer); void doTask(swss::SelectableTimer&) override; @@ -44,7 +42,6 @@ class PolicerOrch : public Orch void getPolicerCounter(void); void initPolicerCounterPlugin(void); void removePCFromFlexCounter(sai_object_id_t id, const string &name); - void generatePolicerCounterStats(std::unordered_set& counter_stats); string getPolicerFlexCounterTableKey(string key); std::unordered_set generatePCounterStats(); From b3556f856deadd0b2d6fb6b3c725ba27c48fbe12 Mon Sep 17 00:00:00 2001 From: shiraez Date: Wed, 11 Dec 2024 17:33:44 +0200 Subject: [PATCH 6/6] clean code + update flex counter test --- .../flex_counter/flex_counter_manager.cpp | 7 ++-- orchagent/policerorch.cpp | 14 ++++---- orchagent/policerorch.h | 3 +- tests/mock_tests/flexcounter_ut.cpp | 33 +++++++++++++++++++ tests/test_flex_counters.py | 25 +++----------- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/orchagent/flex_counter/flex_counter_manager.cpp b/orchagent/flex_counter/flex_counter_manager.cpp index 9cb9d721a2..c56d623e7b 100644 --- a/orchagent/flex_counter/flex_counter_manager.cpp +++ b/orchagent/flex_counter/flex_counter_manager.cpp @@ -61,7 +61,6 @@ FlexCounterManager *FlexManagerDirectory::createFlexCounterManager(const string& const bool enabled, FieldValueTuple fv_plugin) { - SWSS_LOG_DEBUG("create Flex Counter Manager %s", group_name.c_str()); if (m_managers.find(group_name) != m_managers.end()) { if (stats_mode != m_managers[group_name]->getStatsMode()) @@ -217,14 +216,12 @@ void FlexCounterManager::setCounterIdList( auto key = getFlexCounterTableKey(group_name, object_id); auto counter_ids = serializeCounterStats(counter_stats); auto effective_switch_id = switch_id == SAI_NULL_OBJECT_ID ? gSwitchId : switch_id; - SWSS_LOG_DEBUG("counter_ids: %s", counter_ids.c_str()); - // SWSS_LOG_DEBUG("counter_type_it: %s", counter_type_it.second); startFlexCounterPolling(effective_switch_id, key, counter_ids, counter_type_it->second); installed_counters[object_id] = effective_switch_id; - SWSS_LOG_DEBUG("Updated flex counter id list for object '%s' in group '%s'.", - sai_serialize_object_id(object_id).c_str(), + SWSS_LOG_DEBUG("Updated flex counter id list for object '%" PRIu64 "' in group '%s'.", + object_id, group_name.c_str()); } diff --git a/orchagent/policerorch.cpp b/orchagent/policerorch.cpp index c94241a07e..23224cf513 100644 --- a/orchagent/policerorch.cpp +++ b/orchagent/policerorch.cpp @@ -13,7 +13,6 @@ extern sai_policer_api_t* sai_policer_api; extern sai_port_api_t *sai_port_api; extern sai_object_id_t gSwitchId; -extern sai_object_id_t m_counterOid; extern PortsOrch* gPortsOrch; #define ETHERNET_PREFIX "Ethernet" @@ -146,7 +145,6 @@ PolicerOrch::PolicerOrch( vector &tableNames, PortsOrch *portOrc m_FlexCounterUpdTimer = new SelectableTimer(intervT); auto executorT = new ExecutableTimer(m_FlexCounterUpdTimer, this, "FLEX_COUNTER_UPD_TIMER"); Orch::addExecutor(executorT); - SWSS_LOG_DEBUG("PolicerOrch created"); } @@ -441,7 +439,6 @@ void PolicerOrch::doTask(SelectableTimer &timer) const auto id = sai_serialize_object_id(it->first); if (!gTraditionalFlexCounter || m_vidToRidTable->hget("", id, value)) { - SWSS_LOG_INFO("Registering second %s, id %s", it->second.c_str(), id.c_str()); vector policerNameVector; policerNameVector.emplace_back(it->second.c_str(), id); @@ -471,11 +468,6 @@ std::unordered_set PolicerOrch::generatePCounterStats() return counter_stats; } -void PolicerOrch::addPolicerToFlexCounter(sai_object_id_t oid, const string &name) -{ - m_pendingPcAddToFlexCntr[oid] = name; -} - void PolicerOrch::generatePolicerCounterMap() { if (m_isPolicerCounterMapGenerated) @@ -488,6 +480,12 @@ void PolicerOrch::generatePolicerCounterMap() m_isPolicerCounterMapGenerated = true; } +void PolicerOrch::addPolicerToFlexCounter(sai_object_id_t oid, const string &name) +{ + m_pendingPcAddToFlexCntr[oid] = name; +} + + void PolicerOrch::doTask(Consumer &consumer) { SWSS_LOG_ENTER(); diff --git a/orchagent/policerorch.h b/orchagent/policerorch.h index af3b5eba8c..2b802f98a2 100644 --- a/orchagent/policerorch.h +++ b/orchagent/policerorch.h @@ -27,8 +27,9 @@ class PolicerOrch : public Orch bool increaseRefCount(const string &name); bool decreaseRefCount(const string &name); task_process_status handlePortStormControlTable(swss::KeyOpFieldsValuesTuple tuple); - void generatePolicerCounterMap(); void addPolicerToFlexCounter(sai_object_id_t oid, const string &name); + void generatePolicerCounterMap(); + private: PortsOrch *m_portsOrch; virtual void doTask(Consumer& consumer); diff --git a/tests/mock_tests/flexcounter_ut.cpp b/tests/mock_tests/flexcounter_ut.cpp index fa3b62e795..8f34fd05d7 100644 --- a/tests/mock_tests/flexcounter_ut.cpp +++ b/tests/mock_tests/flexcounter_ut.cpp @@ -331,6 +331,12 @@ namespace flexcounter_test ASSERT_EQ(gIntfsOrch, nullptr); gIntfsOrch = new IntfsOrch(m_app_db.get(), APP_INTF_TABLE_NAME, gVrfOrch, m_chassis_app_db.get()); + + vector policer_tables = { + TableConnector(m_config_db.get(), CFG_POLICER_TABLE_NAME), + TableConnector(m_config_db.get(), CFG_PORT_STORM_CONTROL_TABLE_NAME) + }; + gPolicerOrch = new PolicerOrch(policer_tables, gPortsOrch); } virtual void TearDown() override @@ -357,6 +363,8 @@ namespace flexcounter_test gQosOrch = nullptr; delete gSwitchOrch; gSwitchOrch = nullptr; + delete gPolicerOrch; + gPolicerOrch = nullptr; // clear orchs saved in directory gDirectory.m_values.clear(); @@ -450,6 +458,14 @@ namespace flexcounter_test {RIF_PLUGIN_FIELD, ""}, })); + ASSERT_TRUE(checkFlexCounterGroup(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, + { + {POLL_INTERVAL_FIELD, "10000"}, + {STATS_MODE_FIELD, STATS_MODE_READ}, + {FLEX_COUNTER_STATUS_FIELD, "disable"} + } + )); + Table portTable = Table(m_app_db.get(), APP_PORT_TABLE_NAME); Table sendToIngressPortTable = Table(m_app_db.get(), APP_SEND_TO_INGRESS_PORT_TABLE_NAME); Table pgTable = Table(m_app_db.get(), APP_BUFFER_PG_TABLE_NAME); @@ -548,6 +564,7 @@ namespace flexcounter_test flexCounterCfg.set("PORT", values); flexCounterCfg.set("BUFFER_POOL_WATERMARK", values); flexCounterCfg.set("PFCWD", values); + flexCounterCfg.set("POLICER", values); auto flexCounterOrch = gDirectory.get(); flexCounterOrch->addExistingData(&flexCounterCfg); @@ -600,6 +617,14 @@ namespace flexcounter_test {FLEX_COUNTER_STATUS_FIELD, "enable"}, })); + ASSERT_TRUE(checkFlexCounterGroup(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, + { + {POLL_INTERVAL_FIELD, "10000"}, + {STATS_MODE_FIELD, STATS_MODE_READ}, + {FLEX_COUNTER_STATUS_FIELD, "enable"} + } + )); + sai_object_id_t pool_oid; pool_oid = (*BufferOrch::m_buffer_type_maps[APP_BUFFER_POOL_TABLE_NAME])["ingress_lossless_pool"].m_saiObjectId; ASSERT_TRUE(checkFlexCounter(BUFFER_POOL_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP, pool_oid, BUFFER_POOL_COUNTER_ID_LIST)); @@ -833,6 +858,14 @@ namespace flexcounter_test entries.clear(); static_cast(gBufferOrch)->doTask(); ASSERT_TRUE(checkFlexCounter(BUFFER_POOL_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP, pool_oid)); + + string policer_name = "policer_1"; + auto consumer_policer = dynamic_cast(gPolicerOrch->getExecutor(CFG_POLICER_TABLE_NAME)); + consumer_policer->addToSync({{policer_name, "SET", {{"meter_type", "packets"}, {"mode", "sr_tcm"}, {"cir", "600"}, {"cbs", "600"}}}}); + static_cast(gPolicerOrch)->doTask(); + sai_object_id_t policer_oid; + gPolicerOrch->getPolicerOid(policer_name, policer_oid); + ASSERT_TRUE(checkFlexCounter(POLICER_STAT_COUNTER_FLEX_COUNTER_GROUP, policer_oid)); } INSTANTIATE_TEST_CASE_P( diff --git a/tests/test_flex_counters.py b/tests/test_flex_counters.py index a81f045bad..3031bdec5c 100644 --- a/tests/test_flex_counters.py +++ b/tests/test_flex_counters.py @@ -82,9 +82,7 @@ 'policer_counter': { 'key': 'POLICER', 'group_name': 'POLICER_STAT_COUNTER', - 'name_map': 'COUNTERS_POLICER_NAME_MAP', - 'pre_test': 'pre_policer_counter_test', - 'post_test': 'post_policer_counter_test', + 'name_map': 'COUNTERS_POLICER_NAME_MAP' } } @@ -191,16 +189,6 @@ def pre_route_flow_counter_test(self, meta_data): dvs.servers[1].runcmd("ping -6 -c 1 2001::1") dvs.runcmd("vtysh -c \"configure terminal\" -c \"ipv6 route 2000::/64 2001::2\"") - def pre_policer_counter_test(self, meta_data): - self.config_db.db_connection.hset("POLICER|policer1", "meter_type", "packets") - self.config_db.db_connection.hset("POLICER|policer1", "mode", "sr_tcm") - self.config_db.db_connection.hset("POLICER|policer1", "cir", "600") - self.config_db.db_connection.hset("POLICER|policer1", "cbs", "600") - self.config_db.db_connection.hset("POLICER|policer1", "red_packet_action", "drop") - - def post_policer_counter_test(self, meta_data): - self.config_db.db_connection.hdel('POLICER|policer1', "NULL") - def post_rif_counter_test(self, meta_data): self.config_db.db_connection.hdel('INTERFACE|Ethernet0|192.168.0.1/24', "NULL") @@ -773,11 +761,6 @@ def test_create_remove_policer_counter(self, dvs): 5. Verify counter is automatically removed """ self.setup_dbs(dvs) - meta_data = counter_group_meta['policer_counter'] - - # Enable flex counter group for policer - self.set_flex_counter_group_status(meta_data['key'], meta_data['name_map']) - # Create a test policer self.config_db.create_entry('POLICER', 'policer1', { 'meter_type': 'bytes', @@ -786,7 +769,11 @@ def test_create_remove_policer_counter(self, dvs): 'cbs': '600000', 'red_packet_action': 'drop' }) + meta_data = counter_group_meta['policer_counter'] + # Enable flex counter group for policer + self.set_flex_counter_group_status(meta_data['key'], meta_data['name_map']) + # Wait for counter to be created and verify for _ in range(NUMBER_OF_RETRIES): counter_oid = self.counters_db.db_connection.hget(meta_data['name_map'], 'policer1') @@ -806,6 +793,4 @@ def test_create_remove_policer_counter(self, dvs): if not counter_oid: break time.sleep(1) - assert counter_oid is None, "Policer counter was not removed" - self.wait_for_id_list_remove(meta_data['group_name'], 'policer1', counter_oid) \ No newline at end of file