Skip to content

Commit

Permalink
Handling sflow enable
Browse files Browse the repository at this point in the history
UT Fixes

Fixing build

Build fix
  • Loading branch information
dgsudharsan committed Oct 7, 2019
1 parent 5d0960c commit 42a289f
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 38 deletions.
124 changes: 98 additions & 26 deletions orchagent/copporch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <inttypes.h>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace swss;
using namespace std;
Expand Down Expand Up @@ -88,14 +89,15 @@ const vector<sai_hostif_trap_type_t> default_trap_ids = {
SAI_HOSTIF_TRAP_TYPE_TTL_ERROR
};

CoppOrch::CoppOrch(DBConnector *db, string tableName) :
Orch(db, tableName)
CoppOrch::CoppOrch(vector<TableConnector> &tableConnectors) :
Orch( tableConnectors)
{
SWSS_LOG_ENTER();

initDefaultHostIntfTable();
initDefaultTrapGroup();
initDefaultTrapIds();
enable_sflow_trap = false;
};

void CoppOrch::initDefaultHostIntfTable()
Expand Down Expand Up @@ -189,31 +191,18 @@ void CoppOrch::getTrapIdList(vector<string> &trap_id_name_list, vector<sai_hosti
}
}

bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
const vector<sai_hostif_trap_type_t> &trap_id_list,
vector<sai_attribute_t> &trap_id_attribs)
bool CoppOrch::createGenetlinkHostifTable(vector<string> &trap_id_name_list)
{
for (auto trap_id : trap_id_list)
{
sai_attribute_t attr;
vector<sai_attribute_t> attrs;

attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_TYPE;
attr.value.s32 = trap_id;
attrs.push_back(attr);
SWSS_LOG_ENTER();

attrs.insert(attrs.end(), trap_id_attribs.begin(), trap_id_attribs.end());
vector<sai_hostif_trap_type_t> trap_id_list;

sai_object_id_t hostif_trap_id;
sai_status_t status = sai_hostif_api->create_hostif_trap(&hostif_trap_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create trap %d, rv:%d", trap_id, status);
return false;
}
m_syncdTrapIds[trap_id] = trap_group_id;
getTrapIdList(trap_id_name_list, trap_id_list);

for (auto trap_id : trap_id_list)
{
auto hostTbl_entry = m_trapid_hostif_table_map.find(trap_id);
sai_object_id_t trap_group_id = m_syncdTrapIds[trap_id].trap_group_obj;

if (hostTbl_entry == m_trapid_hostif_table_map.end())
{
Expand All @@ -229,7 +218,7 @@ bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
sai_host_table_attr.push_back(attr);

attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TRAP_ID;
attr.value.oid = hostif_trap_id;
attr.value.oid = m_syncdTrapIds[trap_id].trap_obj;
sai_host_table_attr.push_back(attr);

attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_CHANNEL_TYPE;
Expand All @@ -244,7 +233,6 @@ bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
gSwitchId,
(uint32_t)sai_host_table_attr.size(),
sai_host_table_attr.data());

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create hostif table entry failed, rv %d", status);
Expand All @@ -254,7 +242,34 @@ bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
}
}
}
return true;
}

bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
const vector<sai_hostif_trap_type_t> &trap_id_list,
vector<sai_attribute_t> &trap_id_attribs)
{
for (auto trap_id : trap_id_list)
{
sai_attribute_t attr;
vector<sai_attribute_t> attrs;

attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_TYPE;
attr.value.s32 = trap_id;
attrs.push_back(attr);

attrs.insert(attrs.end(), trap_id_attribs.begin(), trap_id_attribs.end());

sai_object_id_t hostif_trap_id;
sai_status_t status = sai_hostif_api->create_hostif_trap(&hostif_trap_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create trap %d, rv:%d", trap_id, status);
return false;
}
m_syncdTrapIds[trap_id].trap_group_obj = trap_group_id;
m_syncdTrapIds[trap_id].trap_obj = hostif_trap_id;
}
return true;
}

Expand All @@ -274,6 +289,7 @@ bool CoppOrch::applyTrapIds(sai_object_id_t trap_group, vector<string> &trap_id_
return applyAttributesToTrapIds(trap_group, trap_id_list, trap_id_attribs);
}


bool CoppOrch::removePolicer(string trap_group_name)
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -389,7 +405,7 @@ bool CoppOrch::removeGenetlinkHostIf(string trap_group_name)

for (auto it : m_syncdTrapIds)
{
if (it.second == m_trap_group_map[trap_group_name])
if (it.second.trap_group_obj == m_trap_group_map[trap_group_name])
{
auto hostTableEntry = m_trapid_hostif_table_map.find(it.first);
if (hostTableEntry != m_trapid_hostif_table_map.end())
Expand Down Expand Up @@ -449,6 +465,14 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
if (fvField(*i) == copp_trap_id_list)
{
trap_id_list = tokenize(fvValue(*i), list_item_delimiter);
auto it = std::find(trap_id_list.begin(), trap_id_list.end(), "sample_packet");
if (it != trap_id_list.end())
{
if (!enable_sflow_trap)
{
return task_process_status::task_need_retry;
}
}
}
else if (fvField(*i) == copp_queue_field)
{
Expand Down Expand Up @@ -688,6 +712,14 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
{
return task_process_status::task_failed;
}

if (!genetlink_attribs.empty())
{
if (!createGenetlinkHostifTable(trap_id_list))
{
return task_process_status::task_failed;
}
}
}
else if (op == DEL_COMMAND)
{
Expand Down Expand Up @@ -715,10 +747,16 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
vector<sai_hostif_trap_type_t> trap_ids_to_reset;
for (auto it : m_syncdTrapIds)
{
if (it.second == m_trap_group_map[trap_group_name])
if (it.second.trap_group_obj == m_trap_group_map[trap_group_name])
{
trap_ids_to_reset.push_back(it.first);
}
sai_status = sai_hostif_api->remove_hostif_trap(it.second.trap_obj);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove trap object %ld", it.second.trap_obj);
return task_process_status::task_failed;
}
}

sai_attribute_t attr;
Expand Down Expand Up @@ -756,15 +794,49 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
return task_process_status::task_success;
}

/* Program Sflow trap once we get sflow enable command */
void CoppOrch::coppProcessSflow(Consumer &consumer)
{
auto it = consumer.m_toSync.begin();

while (it != consumer.m_toSync.end())
{
auto tuple = it->second;
string op = kfvOp(tuple);

if (op == SET_COMMAND)
{
for (auto i : kfvFieldsValues(tuple))
{
if (fvField(i) == "admin_state")
{
if (fvValue(i) == "enable")
{
enable_sflow_trap = true;
}
}
}
}
it = consumer.m_toSync.erase(it);
}
}

void CoppOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
string table_name = consumer.getTableName();

if (!gPortsOrch->allPortsReady())
{
return;
}

if (table_name == CFG_SFLOW_TABLE_NAME)
{
coppProcessSflow(consumer);
return;
}

auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
Expand Down
31 changes: 20 additions & 11 deletions orchagent/copporch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,34 @@ const std::string copp_policer_action_red_field = "red_action";
const std::string copp_policer_action_yellow_field = "yellow_action";

// genetlink fields
const string copp_genetlink_name = "genetlink_name";
const string copp_genetlink_mcgrp_name = "genetlink_mcgrp_name";
const std::string copp_genetlink_name = "genetlink_name";
const std::string copp_genetlink_mcgrp_name = "genetlink_mcgrp_name";

struct copp_trap_objects
{
sai_object_id_t trap_obj;
sai_object_id_t trap_group_obj;
};

/* TrapGroupPolicerTable: trap group ID, policer ID */
typedef std::map<sai_object_id_t, sai_object_id_t> TrapGroupPolicerTable;
/* TrapIdTrapGroupTable: trap ID, trap group ID */
typedef std::map<sai_hostif_trap_type_t, sai_object_id_t> TrapIdTrapGroupTable;
/* TrapGroupHostIfMap: trap group ID to host interface ID */
/* TrapIdTrapObjectsTable: trap ID, copp trap objects */
typedef std::map<sai_hostif_trap_type_t, copp_trap_objects> TrapIdTrapObjectsTable;
/* TrapGroupHostIfMap: trap group ID, host interface ID */
typedef std::map<sai_object_id_t, sai_object_id_t> TrapGroupHostIfMap;
/* TrapGroupHostIfMap: trap type to host table entry */
typedef std::map<sai_hostif_trap_type_t, sai_object_id_t> TrapHostTblEntryMap;
/* TrapIdHostIfTableMap: trap type, host table entry ID*/
typedef std::map<sai_hostif_trap_type_t, sai_object_id_t> TrapIdHostIfTableMap;

class CoppOrch : public Orch
{
public:
CoppOrch(swss::DBConnector *db, std::string tableName);
CoppOrch(std::vector<TableConnector> &tableConnectors);
protected:
object_map m_trap_group_map;
bool enable_sflow_trap;

TrapGroupPolicerTable m_trap_group_policer_map;
TrapIdTrapGroupTable m_syncdTrapIds;
TrapIdTrapObjectsTable m_syncdTrapIds;

TrapGroupHostIfMap m_trap_group_hostif_map;
TrapIdHostIfTableMap m_trapid_hostif_table_map;
Expand All @@ -65,8 +72,10 @@ class CoppOrch : public Orch

sai_object_id_t getPolicer(std::string trap_group_name);

bool createGenetlinkHostIf(string trap_group_name, vector<sai_attribute_t> &hostif_attribs);
bool removeGenetlinkHostIf(string trap_group_name);
bool createGenetlinkHostIf(std::string trap_group_name, std::vector<sai_attribute_t> &hostif_attribs);
bool removeGenetlinkHostIf(std::string trap_group_name);
bool createGenetlinkHostifTable(std::vector<std::string> &trap_id_name_list);
void coppProcessSflow(Consumer& consumer);

virtual void doTask(Consumer& consumer);
};
Expand Down
10 changes: 9 additions & 1 deletion orchagent/orchdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ bool OrchDaemon::init()
gIntfsOrch = new IntfsOrch(m_applDb, APP_INTF_TABLE_NAME, vrf_orch);
gNeighOrch = new NeighOrch(m_applDb, APP_NEIGH_TABLE_NAME, gIntfsOrch);
gRouteOrch = new RouteOrch(m_applDb, APP_ROUTE_TABLE_NAME, gNeighOrch);
CoppOrch *copp_orch = new CoppOrch(m_applDb, APP_COPP_TABLE_NAME);

TableConnector confDbSflowTable(m_configDb, CFG_SFLOW_TABLE_NAME);
TableConnector appCoppTable(m_applDb, APP_COPP_TABLE_NAME);

vector<TableConnector> copp_table_connectors = {
confDbSflowTable,
appCoppTable
};
CoppOrch *copp_orch = new CoppOrch(copp_table_connectors);
TunnelDecapOrch *tunnel_decap_orch = new TunnelDecapOrch(m_applDb, APP_TUNNEL_DECAP_TABLE_NAME);

VxlanTunnelOrch *vxlan_tunnel_orch = new VxlanTunnelOrch(m_applDb, APP_VXLAN_TUNNEL_TABLE_NAME);
Expand Down
16 changes: 16 additions & 0 deletions swssconfig/sample/00-copp.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,21 @@
"red_action":"drop"
},
"OP": "SET"
},
{
"COPP_TABLE:trap.group.sflow": {
"trap_ids": "sample_packet",
"trap_action":"trap",
"trap_priority":"1",
"queue": "2",
"meter_type":"packets",
"mode":"sr_tcm",
"cir":"1000",
"cbs":"1000",
"red_action":"drop",
"genetlink_name":"psample",
"genetlink_mcgrp_name":"packets"
},
"OP": "SET"
}
]

0 comments on commit 42a289f

Please sign in to comment.