From c98babb298efbb0cd4e9168d1981ec0123cf20a7 Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Fri, 26 Jul 2019 11:50:27 -0700 Subject: [PATCH] Copp sflow changes undoing revert indentation Code review fixes Addressing code review comments Latest copp changes based on headers --- orchagent/copporch.cpp | 164 ++++++++++++++++++++++++++ orchagent/copporch.h | 14 +++ swssconfig/sample/00-copp.config.json | 16 +++ 3 files changed, 194 insertions(+) diff --git a/orchagent/copporch.cpp b/orchagent/copporch.cpp index 9d001ca82a2..ef995bae362 100644 --- a/orchagent/copporch.cpp +++ b/orchagent/copporch.cpp @@ -212,6 +212,41 @@ bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id, return false; } m_syncdTrapIds[trap_id] = trap_group_id; + + auto hostTbl_entry = m_trapId_hostTblEntry_map.find(trap_id); + + if (hostTbl_entry == m_trapId_hostTblEntry_map.end()) + { + auto hostif_map = m_trap_group_hostif_map.find(trap_group_id); + if (hostif_map != m_trap_group_hostif_map.end()) + { + sai_object_id_t hostif_table_entry = SAI_NULL_OBJECT_ID; + sai_attribute_t sai_host_table_attr[4]; + + sai_host_table_attr[0].id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TYPE; + sai_host_table_attr[0].value.s32 = SAI_HOSTIF_TABLE_ENTRY_TYPE_TRAP_ID; + + sai_host_table_attr[1].id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TRAP_ID; + sai_host_table_attr[1].value.oid = hostif_trap_id; + + sai_host_table_attr[2].id = SAI_HOSTIF_TABLE_ENTRY_ATTR_CHANNEL_TYPE; + sai_host_table_attr[2].value.s32 = SAI_HOSTIF_TABLE_ENTRY_CHANNEL_TYPE_GENETLINK; + + sai_host_table_attr[3].id = SAI_HOSTIF_TABLE_ENTRY_ATTR_HOST_IF; + sai_host_table_attr[3].value.oid = hostif_map->second; + + sai_status_t status = sai_hostif_api->create_hostif_table_entry(&hostif_table_entry, + gSwitchId, 4, + sai_host_table_attr); + + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("Failed to create hostif table entry failed, rv %d", status); + return false; + } + m_trapId_hostTblEntry_map[trap_id] = hostif_table_entry; + } + } } return true; @@ -319,6 +354,69 @@ bool CoppOrch::createPolicer(string trap_group_name, vector &po return true; } +bool CoppOrch::createGenetlinkHostIf(string trap_group_name, vector &genetlink_attribs) +{ + SWSS_LOG_ENTER(); + + sai_object_id_t hostif_id; + sai_status_t sai_status; + + sai_status = sai_hostif_api->create_hostif(&hostif_id, gSwitchId, + (uint32_t)genetlink_attribs.size(), + genetlink_attribs.data()); + if (sai_status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("Failed to create genetlink hostif for trap group %s, rc=%d", + trap_group_name.c_str(), sai_status); + return false; + } + + m_trap_group_hostif_map[m_trap_group_map[trap_group_name]] = hostif_id; + return true; +} + +bool CoppOrch::removeGenetlinkHostIf(string trap_group_name) +{ + SWSS_LOG_ENTER(); + + sai_status_t sai_status; + + for (auto it : m_syncdTrapIds) + { + if (it.second == m_trap_group_map[trap_group_name]) + { + auto hostTableEntry = m_trapId_hostTblEntry_map.find(it.first); + if (hostTableEntry != m_trapId_hostTblEntry_map.end()) + { + sai_status = sai_hostif_api->remove_hostif_table_entry(hostTableEntry->second); + if(sai_status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("Failed to delete hostif table entry %ld \ + on trap group %s. rc=%d", hostTableEntry->second, + trap_group_name.c_str(), sai_status); + return false; + } + m_trapId_hostTblEntry_map.erase(it.first); + } + } + } + + auto hostInfo = m_trap_group_hostif_map.find(m_trap_group_map[trap_group_name]); + if(hostInfo != m_trap_group_hostif_map.end()) + { + sai_status = sai_hostif_api->remove_hostif(hostInfo->second); + if(sai_status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("Failed to delete host info %ld on trap group %s. rc=%d", + hostInfo->second, trap_group_name.c_str(), sai_status); + return false; + } + m_trap_group_hostif_map.erase(m_trap_group_map[trap_group_name]); + } + + return true; +} + task_process_status CoppOrch::processCoppRule(Consumer& consumer) { SWSS_LOG_ENTER(); @@ -334,6 +432,7 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer) vector trap_gr_attribs; vector trap_id_attribs; vector policer_attribs; + vector genetlink_attribs; if (op == SET_COMMAND) { @@ -441,6 +540,25 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer) attr.value.s32 = policer_action; policer_attribs.push_back(attr); } + else if (fvField(*i) == copp_genetlink_name) + { + attr.id = SAI_HOSTIF_ATTR_TYPE; + attr.value.s32 = SAI_HOSTIF_TYPE_GENETLINK; + genetlink_attribs.push_back(attr); + + attr.id = SAI_HOSTIF_ATTR_NAME; + strncpy(attr.value.chardata, fvValue(*i).c_str(), + sizeof(attr.value.chardata)); + genetlink_attribs.push_back(attr); + + } + else if (fvField(*i) == copp_genetlink_mcgrp_name) + { + attr.id = SAI_HOSTIF_ATTR_GENETLINK_MCGRP_NAME; + strncpy(attr.value.chardata, fvValue(*i).c_str(), + sizeof(attr.value.chardata)); + genetlink_attribs.push_back(attr); + } else { SWSS_LOG_ERROR("Unknown copp field specified:%s\n", fvField(*i).c_str()); @@ -480,6 +598,38 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer) } } } + if (!genetlink_attribs.empty()) + { + auto hostif_map = m_trap_group_hostif_map.find(m_trap_group_map[trap_group_name]); + + if (hostif_map != m_trap_group_hostif_map.end()) + { + for(sai_uint32_t idx = 0; idx < genetlink_attribs.size(); idx++) + { + auto hostif_attr = genetlink_attribs[idx]; + sai_status = sai_hostif_api->set_hostif_attribute(hostif_map->second, + &hostif_attr); + if(sai_status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("Failed to apply attribute[%d].id=%d to hostif for \ + trap group %s, error:%d", idx, hostif_attr.id, + trap_group_name.c_str(), sai_status); + + return task_process_status::task_failed; + } + } + } + else + { + if (!genetlink_attribs.empty()) + { + if (!createGenetlinkHostIf(trap_group_name, genetlink_attribs)) + { + return task_process_status::task_failed; + } + } + } + } for (sai_uint32_t ind = 0; ind < trap_gr_attribs.size(); ind++) { @@ -517,6 +667,14 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer) return task_process_status::task_failed; } } + + if (!genetlink_attribs.empty()) + { + if (!createGenetlinkHostIf(trap_group_name, genetlink_attribs)) + { + return task_process_status::task_failed; + } + } } /* Apply traps to trap group */ @@ -534,6 +692,12 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer) return task_process_status::task_failed; } + if (!removeGenetlinkHostIf(trap_group_name)) + { + SWSS_LOG_ERROR("Failed to remove hostif from trap group %s", trap_group_name.c_str()); + return task_process_status::task_failed; + } + /* Do not remove default trap group */ if (trap_group_name == default_trap_group) { diff --git a/orchagent/copporch.h b/orchagent/copporch.h index 7f02935c84c..67812d5be41 100644 --- a/orchagent/copporch.h +++ b/orchagent/copporch.h @@ -24,10 +24,18 @@ const string copp_policer_action_green_field = "green_action"; const string copp_policer_action_red_field = "red_action"; const 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"; + /* TrapGroupPolicerTable: trap group ID, policer ID */ typedef map TrapGroupPolicerTable; /* TrapIdTrapGroupTable: trap ID, trap group ID */ typedef map TrapIdTrapGroupTable; +/* TrapGroupHostIfMap: trap group ID to host interface ID */ +typedef map TrapGroupHostIfMap; +/* TrapGroupHostIfMap: trap type to host table entry */ +typedef map TrapHostTblEntryMap; class CoppOrch : public Orch { @@ -39,6 +47,9 @@ class CoppOrch : public Orch TrapGroupPolicerTable m_trap_group_policer_map; TrapIdTrapGroupTable m_syncdTrapIds; + TrapGroupHostIfMap m_trap_group_hostif_map; + TrapHostTblEntryMap m_trapId_hostTblEntry_map; + void initDefaultHostIntfTable(); void initDefaultTrapGroup(); void initDefaultTrapIds(); @@ -54,6 +65,9 @@ class CoppOrch : public Orch sai_object_id_t getPolicer(string trap_group_name); + bool createGenetlinkHostIf(string trap_group_name, vector &hostif_attribs); + bool removeGenetlinkHostIf(string trap_group_name); + virtual void doTask(Consumer& consumer); }; #endif /* SWSS_COPPORCH_H */ diff --git a/swssconfig/sample/00-copp.config.json b/swssconfig/sample/00-copp.config.json index 1fade2d172a..1eac05fabc6 100644 --- a/swssconfig/sample/00-copp.config.json +++ b/swssconfig/sample/00-copp.config.json @@ -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" } ]