Skip to content

Commit

Permalink
[intfsorch] add RIF flex counter group (#765)
Browse files Browse the repository at this point in the history
* [intfsorch] add RIF flex counter group

Signed-off-by: Mykola Faryma <[email protected]>
  • Loading branch information
mykolaf authored and prsunny committed Feb 20, 2019
1 parent 8f06b89 commit 920c3a4
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions orchagent/flexcounterorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
extern sai_port_api_t *sai_port_api;

extern PortsOrch *gPortsOrch;
extern IntfsOrch *gIntfsOrch;

unordered_map<string, string> flexCounterGroupMap =
{
Expand All @@ -18,6 +19,7 @@ unordered_map<string, string> flexCounterGroupMap =
{"PFCWD", PFC_WD_FLEX_COUNTER_GROUP},
{"QUEUE_WATERMARK", QUEUE_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP},
{"PG_WATERMARK", PG_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP},
{"RIF", RIF_STAT_COUNTER_FLEX_COUNTER_GROUP},
};


Expand Down Expand Up @@ -78,6 +80,7 @@ void FlexCounterOrch::doTask(Consumer &consumer)
// The queue maps will be generated as soon as counters are enabled
gPortsOrch->generateQueueMap();
gPortsOrch->generatePriorityGroupMap();
gIntfsOrch->generateInterfaceMap();

vector<FieldValueTuple> fieldValues;
fieldValues.emplace_back(FLEX_COUNTER_STATUS_FIELD, value);
Expand Down
139 changes: 139 additions & 0 deletions orchagent/intfsorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <map>
#include <net/if.h>

#include "sai_serialize.h"
#include "intfsorch.h"
#include "ipprefix.h"
#include "logger.h"
Expand All @@ -30,10 +31,47 @@ extern BufferOrch *gBufferOrch;

const int intfsorch_pri = 35;

#define RIF_FLEX_STAT_COUNTER_POLL_MSECS "1000"
#define UPDATE_MAPS_SEC 1

static const vector<sai_router_interface_stat_t> rifStatIds =
{
SAI_ROUTER_INTERFACE_STAT_IN_PACKETS,
SAI_ROUTER_INTERFACE_STAT_IN_OCTETS,
SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS,
SAI_ROUTER_INTERFACE_STAT_IN_ERROR_OCTETS,
SAI_ROUTER_INTERFACE_STAT_OUT_PACKETS,
SAI_ROUTER_INTERFACE_STAT_OUT_OCTETS,
SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS,
SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_OCTETS,
};

IntfsOrch::IntfsOrch(DBConnector *db, string tableName, VRFOrch *vrf_orch) :
Orch(db, tableName, intfsorch_pri), m_vrfOrch(vrf_orch)
{
SWSS_LOG_ENTER();

/* Initialize DB connectors */
m_counter_db = shared_ptr<DBConnector>(new DBConnector(COUNTERS_DB, DBConnector::DEFAULT_UNIXSOCKET, 0));
m_flex_db = shared_ptr<DBConnector>(new DBConnector(FLEX_COUNTER_DB, DBConnector::DEFAULT_UNIXSOCKET, 0));
m_asic_db = shared_ptr<DBConnector>(new DBConnector(ASIC_DB, DBConnector::DEFAULT_UNIXSOCKET, 0));
/* Initialize COUNTER_DB tables */
m_rifNameTable = unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_RIF_NAME_MAP));
m_rifTypeTable = unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_RIF_TYPE_MAP));

m_vidToRidTable = unique_ptr<Table>(new Table(m_asic_db.get(), "VIDTORID"));
auto intervT = timespec { .tv_sec = UPDATE_MAPS_SEC , .tv_nsec = 0 };
m_updateMapsTimer = new SelectableTimer(intervT);
auto executorT = new ExecutableTimer(m_updateMapsTimer, this, "UPDATE_MAPS_TIMER");
Orch::addExecutor(executorT);
/* Initialize FLEX_COUNTER_DB tables */
m_flexCounterTable = unique_ptr<ProducerTable>(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_TABLE));
m_flexCounterGroupTable = unique_ptr<ProducerTable>(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_GROUP_TABLE));

vector<FieldValueTuple> fieldValues;
fieldValues.emplace_back(POLL_INTERVAL_FIELD, RIF_FLEX_STAT_COUNTER_POLL_MSECS);
fieldValues.emplace_back(STATS_MODE_FIELD, STATS_MODE_READ);
m_flexCounterGroupTable->set(RIF_STAT_COUNTER_FLEX_COUNTER_GROUP, fieldValues);
}

sai_object_id_t IntfsOrch::getRouterIntfsId(const string &alias)
Expand Down Expand Up @@ -445,6 +483,7 @@ bool IntfsOrch::addRouterIntfs(sai_object_id_t vrf_id, Port &port)
port.m_vr_id = vrf_id;

gPortsOrch->setPort(port.m_alias, port);
m_rifsToAdd.push_back(port);

SWSS_LOG_NOTICE("Create router interface %s MTU %u", port.m_alias.c_str(), port.m_mtu);

Expand All @@ -461,6 +500,9 @@ bool IntfsOrch::removeRouterIntfs(Port &port)
return false;
}

const auto id = sai_serialize_object_id(port.m_rif_id);
removeRifFromFlexCounter(id, port.m_alias);

sai_status_t status = sai_router_intfs_api->remove_router_interface(port.m_rif_id);
if (status != SAI_STATUS_SUCCESS)
{
Expand Down Expand Up @@ -685,3 +727,100 @@ void IntfsOrch::removeDirectedBroadcast(const Port &port, const IpPrefix &ip_pre

SWSS_LOG_NOTICE("Remove broadcast route ip:%s", ip_addr.to_string().c_str());
}

void IntfsOrch::addRifToFlexCounter(const string &id, const string &name, const string &type)
{
SWSS_LOG_ENTER();
/* update RIF maps in COUNTERS_DB */
vector<FieldValueTuple> rifNameVector;
vector<FieldValueTuple> rifTypeVector;

rifNameVector.emplace_back(name, id);
rifTypeVector.emplace_back(id, type);

m_rifNameTable->set("", rifNameVector);
m_rifTypeTable->set("", rifTypeVector);

/* update RIF in FLEX_COUNTER_DB */
string key = getRifFlexCounterTableKey(id);


std::ostringstream counters_stream;
for (const auto& it: rifStatIds)
{
counters_stream << sai_serialize_router_interface_stat(it) << comma;
}

/* check the state of intf, if registering the intf to FC will result in runtime error */
vector<FieldValueTuple> fvt;
vector<FieldValueTuple> fieldValues;
fieldValues.emplace_back(RIF_COUNTER_ID_LIST, counters_stream.str());

m_flexCounterTable->set(key, fieldValues);
SWSS_LOG_DEBUG("Registered interface %s to Flex counter", name.c_str());
}

void IntfsOrch::removeRifFromFlexCounter(const string &id, const string &name)
{
SWSS_LOG_ENTER();
/* remove it from COUNTERS_DB maps */
m_rifNameTable->hdel("", name);
m_rifTypeTable->hdel("", id);

/* remove it from FLEX_COUNTER_DB */
string key = getRifFlexCounterTableKey(id);

vector<FieldValueTuple> fieldValues;
fieldValues.emplace_back(RIF_COUNTER_ID_LIST, "");

m_flexCounterTable->set(key, fieldValues);
SWSS_LOG_DEBUG("Unregistered interface %s from Flex counter", name.c_str());
}

string IntfsOrch::getRifFlexCounterTableKey(string key)
{
return string(RIF_STAT_COUNTER_FLEX_COUNTER_GROUP) + ":" + key;
}

void IntfsOrch::generateInterfaceMap()
{
m_updateMapsTimer->start();
}

void IntfsOrch::doTask(SelectableTimer &timer)
{
SWSS_LOG_ENTER();

SWSS_LOG_DEBUG("Registering %ld new intfs", m_rifsToAdd.size());
string value;
for (auto it = m_rifsToAdd.begin(); it != m_rifsToAdd.end(); )
{
const auto id = sai_serialize_object_id(it->m_rif_id);
SWSS_LOG_INFO("Registering %s, id %s", it->m_alias.c_str(), id.c_str());
std::string type;
switch(it->m_type)
{
case Port::PHY:
case Port::LAG:
type = "SAI_ROUTER_INTERFACE_TYPE_PORT";
break;
case Port::VLAN:
type = "SAI_ROUTER_INTERFACE_TYPE_VLAN";
break;
default:
SWSS_LOG_ERROR("Unsupported port type: %d", it->m_type);
type = "";
break;
}
if (m_vidToRidTable->hget("", id, value))
{
SWSS_LOG_INFO("Registering %s it is ready", it->m_alias.c_str());
addRifToFlexCounter(id, it->m_alias, type);
it = m_rifsToAdd.erase(it);
}
else
{
++it;
}
}
}
24 changes: 24 additions & 0 deletions orchagent/intfsorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "orch.h"
#include "portsorch.h"
#include "vrforch.h"
#include "timer.h"

#include "ipaddresses.h"
#include "ipprefix.h"
Expand All @@ -15,6 +16,8 @@
extern sai_object_id_t gVirtualRouterId;
extern MacAddress gMacAddress;

#define RIF_STAT_COUNTER_FLEX_COUNTER_GROUP "RIF_STAT_COUNTER"

struct IntfsEntry
{
std::set<IpPrefix> ip_addresses;
Expand All @@ -35,11 +38,32 @@ class IntfsOrch : public Orch

bool setRouterIntfsMtu(Port &port);
std::set<IpPrefix> getSubnetRoutes();

void generateInterfaceMap();
void addRifToFlexCounter(const string&, const string&, const string&);
void removeRifFromFlexCounter(const string&, const string&);

bool setIntf(const string& alias, sai_object_id_t vrf_id = gVirtualRouterId, const IpPrefix *ip_prefix = nullptr);
private:

SelectableTimer* m_updateMapsTimer = nullptr;
std::vector<Port> m_rifsToAdd;

VRFOrch *m_vrfOrch;
IntfsTable m_syncdIntfses;
void doTask(Consumer &consumer);
void doTask(SelectableTimer &timer);

shared_ptr<DBConnector> m_counter_db;
shared_ptr<DBConnector> m_flex_db;
shared_ptr<DBConnector> m_asic_db;
unique_ptr<Table> m_rifNameTable;
unique_ptr<Table> m_rifTypeTable;
unique_ptr<Table> m_vidToRidTable;
unique_ptr<ProducerTable> m_flexCounterTable;
unique_ptr<ProducerTable> m_flexCounterGroupTable;

std::string getRifFlexCounterTableKey(std::string s);

int getRouterIntfsRefCount(const string&);

Expand Down

0 comments on commit 920c3a4

Please sign in to comment.