Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions orchagent/crmorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const map<CrmResourceType, string> crmResTypeNameMap =
{ CrmResourceType::CRM_SRV6_MY_SID_ENTRY, "SRV6_MY_SID_ENTRY" },
{ CrmResourceType::CRM_SRV6_NEXTHOP, "SRV6_NEXTHOP" },
{ CrmResourceType::CRM_NEXTHOP_GROUP_MAP, "NEXTHOP_GROUP_MAP" },
{ CrmResourceType::CRM_EXT_TABLE, "EXTENSION_TABLE" },
};

const map<CrmResourceType, uint32_t> crmResSaiAvailAttrMap =
Expand Down Expand Up @@ -93,6 +94,7 @@ const map<CrmResourceType, sai_object_type_t> crmResSaiObjAttrMap =
{ CrmResourceType::CRM_SRV6_MY_SID_ENTRY, SAI_OBJECT_TYPE_MY_SID_ENTRY },
{ CrmResourceType::CRM_SRV6_NEXTHOP, SAI_OBJECT_TYPE_NEXT_HOP },
{ CrmResourceType::CRM_NEXTHOP_GROUP_MAP, SAI_OBJECT_TYPE_NEXT_HOP_GROUP_MAP },
{ CrmResourceType::CRM_EXT_TABLE, SAI_OBJECT_TYPE_GENERIC_PROGRAMMABLE },
};

const map<CrmResourceType, sai_attr_id_t> crmResAddrFamilyAttrMap =
Expand Down Expand Up @@ -134,6 +136,7 @@ const map<string, CrmResourceType> crmThreshTypeResMap =
{ "srv6_my_sid_entry_threshold_type", CrmResourceType::CRM_SRV6_MY_SID_ENTRY },
{ "srv6_nexthop_threshold_type", CrmResourceType::CRM_SRV6_NEXTHOP },
{ "nexthop_group_map_threshold_type", CrmResourceType::CRM_NEXTHOP_GROUP_MAP },
{ "extension_table_threshold_type", CrmResourceType::CRM_EXT_TABLE },
};

const map<string, CrmResourceType> crmThreshLowResMap =
Expand All @@ -159,6 +162,7 @@ const map<string, CrmResourceType> crmThreshLowResMap =
{"srv6_my_sid_entry_low_threshold", CrmResourceType::CRM_SRV6_MY_SID_ENTRY },
{"srv6_nexthop_low_threshold", CrmResourceType::CRM_SRV6_NEXTHOP },
{"nexthop_group_map_low_threshold", CrmResourceType::CRM_NEXTHOP_GROUP_MAP },
{"extension_table_low_threshold", CrmResourceType::CRM_EXT_TABLE },
};

const map<string, CrmResourceType> crmThreshHighResMap =
Expand All @@ -184,6 +188,7 @@ const map<string, CrmResourceType> crmThreshHighResMap =
{"srv6_my_sid_entry_high_threshold", CrmResourceType::CRM_SRV6_MY_SID_ENTRY },
{"srv6_nexthop_high_threshold", CrmResourceType::CRM_SRV6_NEXTHOP },
{"nexthop_group_map_high_threshold", CrmResourceType::CRM_NEXTHOP_GROUP_MAP },
{"extension_table_high_threshold", CrmResourceType::CRM_EXT_TABLE },
};

const map<string, CrmThresholdType> crmThreshTypeMap =
Expand Down Expand Up @@ -216,6 +221,7 @@ const map<string, CrmResourceType> crmAvailCntsTableMap =
{ "crm_stats_srv6_my_sid_entry_available", CrmResourceType::CRM_SRV6_MY_SID_ENTRY },
{ "crm_stats_srv6_nexthop_available", CrmResourceType::CRM_SRV6_NEXTHOP },
{ "crm_stats_nexthop_group_map_available", CrmResourceType::CRM_NEXTHOP_GROUP_MAP },
{ "crm_stats_extension_table_available", CrmResourceType::CRM_EXT_TABLE },
};

const map<string, CrmResourceType> crmUsedCntsTableMap =
Expand All @@ -241,6 +247,7 @@ const map<string, CrmResourceType> crmUsedCntsTableMap =
{ "crm_stats_srv6_my_sid_entry_used", CrmResourceType::CRM_SRV6_MY_SID_ENTRY },
{ "crm_stats_srv6_nexthop_used", CrmResourceType::CRM_SRV6_NEXTHOP },
{ "crm_stats_nexthop_group_map_used", CrmResourceType::CRM_NEXTHOP_GROUP_MAP },
{ "crm_stats_extension_table_used", CrmResourceType::CRM_EXT_TABLE },
};

CrmOrch::CrmOrch(DBConnector *db, string tableName):
Expand Down Expand Up @@ -502,6 +509,36 @@ void CrmOrch::decCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_
}
}

void CrmOrch::incCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name)
{
SWSS_LOG_ENTER();

try
{
m_resourcesMap.at(resource).countersMap[getCrmP4rtTableKey(table_name)].usedCounter++;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to increment \"used\" counter for the EXT %s CRM resource.", table_name.c_str());
return;
}
}

void CrmOrch::decCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name)
{
SWSS_LOG_ENTER();

try
{
m_resourcesMap.at(resource).countersMap[getCrmP4rtTableKey(table_name)].usedCounter--;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to decrement \"used\" counter for the EXT %s CRM resource.", table_name.c_str());
return;
}
}

void CrmOrch::doTask(SelectableTimer &timer)
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -673,6 +710,33 @@ void CrmOrch::getResAvailableCounters()
break;
}

case CrmResourceType::CRM_EXT_TABLE:
{
for (auto &cnt : res.second.countersMap)
{
std::string table_name = cnt.first;
sai_object_type_t objType = crmResSaiObjAttrMap.at(res.first);
sai_attribute_t attr;
uint64_t availCount = 0;

attr.id = SAI_GENERIC_PROGRAMMABLE_ATTR_OBJECT_NAME;
attr.value.s8list.count = (uint32_t)table_name.size();
attr.value.s8list.list = (int8_t *)const_cast<char *>(table_name.c_str());

sai_status_t status = sai_object_type_get_availability(
gSwitchId, objType, 1, &attr, &availCount);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get EXT table resource count %s , rv:%d",
table_name.c_str(), status);
break;
}

cnt.second.availableCounter = static_cast<uint32_t>(availCount);
}
break;
}

default:
SWSS_LOG_ERROR("Failed to get CRM resource type %u. Unknown resource type.\n", static_cast<uint32_t>(res.first));
return;
Expand Down Expand Up @@ -839,3 +903,10 @@ string CrmOrch::getCrmAclTableKey(sai_object_id_t id)
ss << "ACL_TABLE_STATS:" << "0x" << std::hex << id;
return ss.str();
}

string CrmOrch::getCrmP4rtTableKey(std::string table_name)
{
std::stringstream ss;
ss << "EXT_TABLE_STATS:" << table_name;
return ss.str();
}
6 changes: 6 additions & 0 deletions orchagent/crmorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum class CrmResourceType
CRM_SRV6_MY_SID_ENTRY,
CRM_SRV6_NEXTHOP,
CRM_NEXTHOP_GROUP_MAP,
CRM_EXT_TABLE,
};

enum class CrmThresholdType
Expand Down Expand Up @@ -63,6 +64,10 @@ class CrmOrch : public Orch
void incCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_t tableId);
// Decrement "used" counter for the per ACL table CRM resources (ACL entry/counter)
void decCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_t tableId);
// Increment "used" counter for the EXT table CRM resources
void incCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name);
// Decrement "used" counter for the EXT table CRM resources
void decCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name);

private:
std::shared_ptr<swss::DBConnector> m_countersDb = nullptr;
Expand Down Expand Up @@ -105,4 +110,5 @@ class CrmOrch : public Orch
void checkCrmThresholds();
std::string getCrmAclKey(sai_acl_stage_t stage, sai_acl_bind_point_type_t bindPoint);
std::string getCrmAclTableKey(sai_object_id_t id);
std::string getCrmP4rtTableKey(std::string table_name);
};
8 changes: 8 additions & 0 deletions orchagent/p4orch/ext_tables_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "logger.h"
#include "tokenize.h"
#include "orch.h"
#include "crmorch.h"
#include "p4orch/p4orch.h"
#include "p4orch/p4orch_util.h"

Expand All @@ -21,6 +22,7 @@ extern sai_generic_programmable_api_t *sai_generic_programmable_api;
extern Directory<Orch *> gDirectory;
extern sai_object_id_t gSwitchId;
extern P4Orch *gP4Orch;
extern CrmOrch *gCrmOrch;

P4ExtTableEntry *ExtTablesManager::getP4ExtTableEntry(const std::string &table_name, const std::string &key)
{
Expand Down Expand Up @@ -467,6 +469,9 @@ ReturnCode ExtTablesManager::createP4ExtTableEntry(const P4ExtTableAppDbEntry &a
<< app_db_entry.table_name.c_str()
<< " , entry " << app_db_entry.table_key.c_str();
}
std::string crm_table_name = "EXT_" + app_db_entry.table_name;
boost::algorithm::to_upper(crm_table_name);
gCrmOrch->incCrmExtTableUsedCounter(CrmResourceType::CRM_EXT_TABLE, crm_table_name);


ext_table_entry.sai_entry_oid = sai_generic_programmable_oid;
Expand Down Expand Up @@ -598,6 +603,9 @@ ReturnCode ExtTablesManager::removeP4ExtTableEntry(const std::string &table_name
<< "remove sai api call failed for extension entry table "
<< table_name.c_str() << " , entry " << table_key.c_str();
}
std::string crm_table_name = "EXT_" + table_name;
boost::algorithm::to_upper(crm_table_name);
gCrmOrch->decCrmExtTableUsedCounter(CrmResourceType::CRM_EXT_TABLE, crm_table_name);


auto ext_table_key = KeyGenerator::generateExtTableKey(table_name, table_key);
Expand Down
8 changes: 8 additions & 0 deletions orchagent/p4orch/tests/fake_crmorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ void CrmOrch::decCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_
{
}

void CrmOrch::incCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name)
{
}

void CrmOrch::decCrmExtTableUsedCounter(CrmResourceType resource, std::string table_name)
{
}

void CrmOrch::doTask(Consumer &consumer)
{
}
Expand Down
35 changes: 35 additions & 0 deletions tests/p4rt/test_viplb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
import pytest
import json
import util
import time
import l3
import viplb
import tables_definition

def getCrmCounterValue(dvs, key, counter):

counters_db = swsscommon.DBConnector(swsscommon.COUNTERS_DB, dvs.redis_sock, 0)
crm_stats_table = swsscommon.Table(counters_db, 'CRM')

for k in crm_stats_table.get(key)[1]:
if k[0] == counter:
return int(k[1])

return 0

def crm_update(dvs, field, value):
cfg_db = swsscommon.DBConnector(swsscommon.CONFIG_DB, dvs.redis_sock, 0)
tbl = swsscommon.Table(cfg_db, "CRM")
fvs = swsscommon.FieldValuePairs([(field, value)])
tbl.set("Config", fvs)
time.sleep(1)


class TestP4RTVIPLB(object):

Expand All @@ -30,6 +49,7 @@ def _set_up(self, dvs):
def test_VIPv4LBWithGoodNexthopAddUpdateDeletePass(self, dvs, testlog):
# Initialize L3 objects and database connectors.
self._set_up(dvs)
crm_update(dvs, "polling_interval", "1")

# Create tables definition AppDb entry
tables_definition_key, attr_list = (
Expand Down Expand Up @@ -150,6 +170,11 @@ def test_VIPv4LBWithGoodNexthopAddUpdateDeletePass(self, dvs, testlog):
viplb_oid = self._p4rt_viplb_obj.get_newly_created_programmable_object_oid()
assert viplb_oid is not None

# get crm counters
time.sleep(1)
used_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_used')
avail_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_available')
assert used_counter is 1

# Create another router interface.
router_interface_id, router_intf_key, attr_list = (
Expand Down Expand Up @@ -209,6 +234,11 @@ def test_VIPv4LBWithGoodNexthopAddUpdateDeletePass(self, dvs, testlog):
assert status == True
assert len(fvs) == len(original_key_oid_info) + count

# get crm counters
time.sleep(1)
used_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_used')
avail_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_available')
assert used_counter is 1

# Remove viplb entry.
self._p4rt_viplb_obj.remove_app_db_entry(viplb_key)
Expand All @@ -221,6 +251,11 @@ def test_VIPv4LBWithGoodNexthopAddUpdateDeletePass(self, dvs, testlog):
assert status == True
assert len(fvs) == len(original_key_oid_info) + count

# get crm counters
time.sleep(1)
used_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_used')
avail_counter = getCrmCounterValue(dvs, "EXT_TABLE_STATS:"+self._p4rt_viplb_obj.TBL_NAME, 'crm_stats_extension_table_available')
assert used_counter is 0


def test_VIPv4LBWithBadNexthopAddUpdateDeletePass(self, dvs, testlog):
Expand Down