Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cbf] Added initial CBF support #2

Merged
merged 8 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions orchagent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ orchagent_SOURCES = \
orchdaemon.cpp \
orch.cpp \
notifications.cpp \
nhgorch.cpp \
nhgorch.cpp \
routeorch.cpp \
neighorch.cpp \
intfsorch.cpp \
Expand Down Expand Up @@ -63,7 +63,8 @@ orchagent_SOURCES = \
debugcounterorch.cpp \
natorch.cpp \
muxorch.cpp \
macsecorch.cpp
macsecorch.cpp \
cbforch.cpp

orchagent_SOURCES += flex_counter/flex_counter_manager.cpp flex_counter/flex_counter_stat_manager.cpp
orchagent_SOURCES += debug_counter/debug_counter.cpp debug_counter/drop_counter.cpp
Expand Down
322 changes: 322 additions & 0 deletions orchagent/cbforch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
#include "cbforch.h"

extern sai_qos_map_api_t *sai_qos_map_api;
extern sai_object_id_t gSwitchId;

/*
* Purpose: Constructor.
*
* Description: Initialize Orch base and members.
*
* Params: IN db - The DB connection to Redis.
* IN tableNames - The Redis tables to handle.
*
* Returns: Nothing.
*/
CbfOrch::CbfOrch(DBConnector *db,
const vector<table_name_with_pri_t> &tableNames) :
Orch(db, tableNames),
m_dscp_map(MapHandler::DSCP),
m_exp_map(MapHandler::EXP)
{
SWSS_LOG_ENTER();
}

/*
* Purpose: Perform the operations requested by APPL_DB users.
*
* Description: Redirect the operations to the appropriate table handling
* method.
*
* Params: IN consumer - The cosumer object.
*
* Returns: Nothing.
*/
void CbfOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();

string table_name = consumer.getTableName();

if (table_name == APP_DSCP_TO_FC_MAP_TABLE_NAME)
{
m_dscp_map.doTask(consumer);
}
else if (table_name == APP_EXP_TO_FC_MAP_TABLE_NAME)
{
m_exp_map.doTask(consumer);
}
}

/*
* Purpose: Perform the map table operations.
*
* Description: Iterate over the untreated operations list and resolve them.
* The operations supported are SET and DEL. If an operation
* could not be resolved, it will either remain in the list, or be
* removed, depending on the case.
*
* Params: IN consumer - The cosumer object.
*
* Returns: Nothing.
*/
void MapHandler::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();

auto it = consumer.m_toSync.begin();

while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;

string map_id = kfvKey(t);
string op = kfvOp(t);
auto map_it = find(map_id);
bool success;

if (op == SET_COMMAND)
{
SWSS_LOG_INFO("Set operator for %s map %s",
getMapName(),
map_id.c_str());

auto map_list = extractMap(t);

if (map_it == end())
{
SWSS_LOG_NOTICE("Creating %s map %s",
getMapName(),
map_id.c_str());

auto sai_oid = createMap(map_list);

if (sai_oid != SAI_NULL_OBJECT_ID)
{
SWSS_LOG_INFO("Successfully created %s map", getMapName());
emplace(map_id, sai_oid);
success = true;
}
else
{
SWSS_LOG_ERROR("Failed to create %s map %s",
getMapName(),
map_id.c_str());
success = false;
}
}
else
{
SWSS_LOG_NOTICE("Updating existing %s map %s",
getMapName(),
map_id.c_str());

success = updateMap(map_it->second, map_list);

if (success)
{
SWSS_LOG_INFO("Successfully updated %s map", getMapName());
}
else
{
SWSS_LOG_ERROR("Failed to update %s map %s",
getMapName(),
map_id.c_str());
}
}

// Remove the allocated list
delete[] map_list.list;
}
else if (op == DEL_COMMAND)
{
if (map_it != end())
{
SWSS_LOG_NOTICE("Deleting %s map %s",
getMapName(),
map_id.c_str());

success = removeMap(map_it->second);

if (success)
{
SWSS_LOG_INFO("Successfully removed %s map", getMapName());
erase(map_it);
}
else
{
SWSS_LOG_ERROR("Failed to remove %s map %s",
getMapName(),
map_id.c_str());
}
}
else
{
SWSS_LOG_WARN("Tried to delete inexistent %s map %s",
getMapName(),
map_id.c_str());
// Mark it as success to remove it from the consumer
success = true;
}
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s", op.c_str());
// Set success to true to remove the operation from the consumer
success = true;
}

if (success)
{
SWSS_LOG_DEBUG("Removing consumer item");
it = consumer.m_toSync.erase(it);
}
else
{
SWSS_LOG_DEBUG("Keeping consumer item");
++it;
}
}
}

/*
* Purpose: Extract the QoS map from the Redis's field-value pairs.
*
* Description: Iterate over the field-value pairs and create a SAI QoS map.
abanu-ms marked this conversation as resolved.
Show resolved Hide resolved
*
* Params: IN t - The field-value pairs.
*
* Returns: The SAI QoS map object.
*/
sai_qos_map_list_t MapHandler::extractMap(const KeyOpFieldsValuesTuple &t)
const
{
SWSS_LOG_ENTER();

sai_qos_map_list_t map_list;
const auto &fields_values = kfvFieldsValues(t);
map_list.count = (uint32_t)fields_values.size();
map_list.list = new sai_qos_map_t[map_list.count]();

uint32_t ind = 0;
for (auto i = fields_values.begin();
i != fields_values.end();
i++, ind++)
{
if (m_type == DSCP)
{
map_list.list[ind].key.dscp = (uint8_t)stoi(fvField(*i));
}
else
{
map_list.list[ind].key.mpls_exp = (uint8_t)stoi(fvField(*i));
}

map_list.list[ind].value.fc = (uint8_t)stoi(fvValue(*i));
}

return map_list;
}

/*
* Purpose: Create a QoS map.
*
* Description: Create the QoS map over the SAI interface.
*
* Params: IN map_list - The QoS map to create.
*
* Returns: The SAI ID for the newly created object or SAI_NULL_OBJECt_ID
* if something goes wrong.
*/
sai_object_id_t MapHandler::createMap(const sai_qos_map_list_t &map_list)
{
SWSS_LOG_ENTER();

vector<sai_attribute_t> map_attrs;

sai_attribute_t map_attr;
map_attr.id = SAI_QOS_MAP_ATTR_TYPE;
map_attr.value.u32 = m_type == DSCP ?
SAI_QOS_MAP_TYPE_DSCP_TO_FORWARDING_CLASS :
SAI_QOS_MAP_TYPE_MPLS_EXP_TO_FORWARDING_CLASS;
abanu-ms marked this conversation as resolved.
Show resolved Hide resolved
map_attrs.push_back(map_attr);

map_attr.id = SAI_QOS_MAP_ATTR_MAP_TO_VALUE_LIST;
map_attr.value.qosmap.count = map_list.count;
map_attr.value.qosmap.list = map_list.list;
map_attrs.push_back(map_attr);

sai_object_id_t sai_oid;
auto sai_status = sai_qos_map_api->create_qos_map(
&sai_oid,
gSwitchId,
(uint32_t)map_attrs.size(),
map_attrs.data());
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create map. status: %d", sai_status);
return SAI_NULL_OBJECT_ID;
}

return sai_oid;
}

/*
* Purpose: Update a QoS map.
*
* Description: Update a QoS map over the SAI interface.
*
* Params: IN sai_oid - The SAI ID of the object to update.
* IN map_list - The QoS map to update the object with.
*
* Returns: true, if the update was successful,
* false, otherwise
*/
bool MapHandler::updateMap(sai_object_id_t sai_oid,
const sai_qos_map_list_t &map_list)
{
SWSS_LOG_ENTER();

assert(sai_oid != SAI_NULL_OBJECT_ID);
sai_attribute_t map_attr;
map_attr.id = SAI_QOS_MAP_ATTR_MAP_TO_VALUE_LIST;
map_attr.value.qosmap.count = map_list.count;
map_attr.value.qosmap.list = map_list.list;

auto sai_status = sai_qos_map_api->set_qos_map_attribute(sai_oid,
&map_attr);

if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to update map. status: %d", sai_status);
return false;
}

return true;
}

/*
* Purpose: Delete a QoS map.
*
* Description: Delete a QoS map over the SAI interface.
*
* Params: IN sai_oid - The SAI ID of the object to delete.
*
* Returns: true, if the delete was successful,
* false, otherwise
*/
bool MapHandler::removeMap(sai_object_id_t sai_oid)
{
SWSS_LOG_ENTER();

assert(sai_oid != SAI_NULL_OBJECT_ID);
auto sai_status = sai_qos_map_api->remove_qos_map(sai_oid);

if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove map. status: %d", sai_status);
return false;
}

return true;
}
50 changes: 50 additions & 0 deletions orchagent/cbforch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef SWSS_CBFORCH_H
#define SWSS_CBFORCH_H

#include "orch.h"

using namespace std;
using namespace swss;
abanu-ms marked this conversation as resolved.
Show resolved Hide resolved

/* Class to handle QoS map tasks. */
class MapHandler : public unordered_map<string, sai_object_id_t>
{
public:
/* The map type handled by this handler. */
enum Type
{
DSCP,
EXP
};
abanu-ms marked this conversation as resolved.
Show resolved Hide resolved

MapHandler(Type type) : m_type(type) {}

void doTask(Consumer &consumer);

private:
Type m_type;

/* Get the map name based on map type. */
const char* getMapName() const
{ return m_type == DSCP ? "DSCP_TO_FC" : "EXP_TO_FC"; }

sai_qos_map_list_t extractMap(const KeyOpFieldsValuesTuple &t) const;
sai_object_id_t createMap(const sai_qos_map_list_t &map_list);
bool updateMap(sai_object_id_t sai_oid,
const sai_qos_map_list_t &map_list);
bool removeMap(sai_object_id_t sai_oid);
};

class CbfOrch : public Orch
{
public:
CbfOrch(DBConnector *db, const vector<table_name_with_pri_t> &tableNames);

private:
MapHandler m_dscp_map;
MapHandler m_exp_map;

void doTask(Consumer& consumer) override;
abanu-ms marked this conversation as resolved.
Show resolved Hide resolved
};

#endif /* SWSS_CBFORCH_H */
Loading