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

[Dynamic buffer calc] Support dynamic buffer calculation #361

Merged
merged 12 commits into from
Dec 2, 2020
86 changes: 86 additions & 0 deletions common/json.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <limits>

#include "common/json.h"
Expand Down Expand Up @@ -36,4 +39,87 @@ void JSon::readJson(const string &jsonstr, vector<FieldValueTuple> &fv)
}
}

bool JSon::loadJsonFromFile(ifstream &fs, vector<KeyOpFieldsValuesTuple> &db_items)
stephenxs marked this conversation as resolved.
Show resolved Hide resolved
stephenxs marked this conversation as resolved.
Show resolved Hide resolved
{
nlohmann::json json_array;

try
{
fs >> json_array;
}
catch (const std::logic_error &e)
{
SWSS_LOG_ERROR("Unable to parse json from the input stream: %s", e.what());
return false;
}
catch (const std::bad_alloc &e)
{
SWSS_LOG_ERROR("Unable to parse json from the input stream: %s", e.what());
return false;
}

if (!json_array.is_array())
{
SWSS_LOG_ERROR("Root element must be an array.");
return false;
}

for (size_t i = 0; i < json_array.size(); i++)
{
auto &arr_item = json_array[i];

if (arr_item.is_object())
{
if (el_count != arr_item.size())
{
SWSS_LOG_ERROR("Child elements must have both key and op entry. %s",
arr_item.dump().c_str());
return false;
}

db_items.emplace_back();
auto &cur_db_item = db_items.back();

for (auto child_it = arr_item.begin(); child_it != arr_item.end(); child_it++)
{
auto cur_obj_key = child_it.key();
auto &cur_obj = child_it.value();

if (cur_obj.is_object())
{
kfvKey(cur_db_item) = cur_obj_key;
for (nlohmann::json::iterator cur_obj_it = cur_obj.begin(); cur_obj_it != cur_obj.end(); cur_obj_it++)
{
string field_str = cur_obj_it.key();
string value_str;
if ((*cur_obj_it).is_number())
value_str = to_string((*cur_obj_it).get<int>());
else if ((*cur_obj_it).is_string())
value_str = (*cur_obj_it).get<string>();
kfvFieldsValues(cur_db_item).emplace_back(field_str, value_str);
}
}
else
{
auto op = cur_obj.get<string>();

if (op != "SET" && op != "DEL")
{
SWSS_LOG_ERROR("Child elements' op field must be SET or DEL, but got %s, ignored", op.c_str());
db_items.pop_back();
break;
}
neethajohn marked this conversation as resolved.
Show resolved Hide resolved
kfvOp(cur_db_item) = op;
}
}
}
else
{
SWSS_LOG_ERROR("Child elements must be objects. element:%s", arr_item.dump().c_str());
return false;
}
}
return true;
}

}
47 changes: 45 additions & 2 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __JSON__

#include <string>
#include <fstream>
#include <vector>

#include "table.h"
Expand All @@ -10,9 +11,51 @@ namespace swss {

class JSon
{
private:
/*
* el_count represents the number of elements in each object,
* which means each object have exactly 2 elements: the data and the operator
*/
static const int el_count = 2;
stephenxs marked this conversation as resolved.
Show resolved Hide resolved
public:
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
/*
bool loadJsonFromFile(std::ifstream &fs, std::vector<KeyOpFieldsValuesTuple> &db_items);

parse the json file and construct a vector of KeyOpFieldsValuesTuple as the result
the json file should be a list objects with each consisting of a data field and an operator field
- the data should be a dictionary
- the operator field should be a string, "SET" or "DEL"
an example:
[
{
"QOS_TABLE:TC_TO_QUEUE_MAP_TABLE:AZURE": {
"5": "1",
"6": "1"
},
"OP": "SET"
},
{
"QOS_TABLE:DSCP_TO_TC_MAP_TABLE:AZURE": {
"7":"5",
"6":"5",
"3":"3",
"8":"7",
"9":"8"
},
"OP": "SET"
}
]

parameters:
fs: the input ifstream representing the json file
fv: the output vector
return: boolean
True: the input json file has been succefully parsed
False: there are some errors found
*/
static bool loadJsonFromFile(std::ifstream &fs, std::vector<KeyOpFieldsValuesTuple> &db_items);
};

}
Expand Down
22 changes: 15 additions & 7 deletions common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ namespace swss {
#define APP_MACSEC_EGRESS_SA_TABLE_NAME "MACSEC_EGRESS_SA_TABLE"
#define APP_MACSEC_INGRESS_SA_TABLE_NAME "MACSEC_INGRESS_SA_TABLE"

#define APP_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
#define APP_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
#define APP_BUFFER_PG_TABLE_NAME "BUFFER_PG_TABLE"
#define APP_BUFFER_QUEUE_TABLE_NAME "BUFFER_QUEUE_TABLE"
#define APP_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST_TABLE"
#define APP_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST_TABLE"

#define APP_NEIGH_RESOLVE_TABLE_NAME "NEIGH_RESOLVE_TABLE"

/***** TO BE REMOVED *****/
stephenxs marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -101,13 +108,6 @@ namespace swss {
#define APP_PFC_PRIORITY_TO_PRIORITY_GROUP_MAP_NAME "PFC_PRIORITY_TO_PRIORITY_GROUP_MAP_TABLE"
#define APP_PFC_PRIORITY_TO_QUEUE_MAP_NAME "MAP_PFC_PRIORITY_TO_QUEUE"

#define APP_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
#define APP_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
#define APP_BUFFER_QUEUE_TABLE_NAME "BUFFER_QUEUE_TABLE"
#define APP_BUFFER_PG_TABLE_NAME "BUFFER_PG_TABLE"
#define APP_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST"
#define APP_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST"

/***** COUNTER DATABASE *****/

#define COUNTERS_PORT_NAME_MAP "COUNTERS_PORT_NAME_MAP"
Expand Down Expand Up @@ -243,6 +243,8 @@ namespace swss {
#define CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST"
#define CFG_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST"

#define CFG_DEFAULT_LOSSLESS_BUFFER_PARAMETER "DEFAULT_LOSSLESS_BUFFER_PARAMETER"

#define CFG_POLICER_TABLE_NAME "POLICER"

#define CFG_WARM_RESTART_TABLE_NAME "WARM_RESTART"
Expand Down Expand Up @@ -344,6 +346,12 @@ namespace swss {
#define STATE_MACSEC_EGRESS_SC_TABLE_NAME "MACSEC_EGRESS_SC_TABLE"
#define STATE_MACSEC_EGRESS_SA_TABLE_NAME "MACSEC_EGRESS_SA_TABLE"

#define STATE_ASIC_TABLE "ASIC_TABLE"
#define STATE_BUFFER_MAXIMUM_VALUE_TABLE "BUFFER_MAX_PARAM_TABLE"
#define STATE_PERIPHERAL_TABLE "PERIPHERAL_TABLE"
#define STATE_PORT_PERIPHERAL_TABLE "PORT_PERIPHERAL_TABLE"
#define STATE_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
#define STATE_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
/***** MISC *****/

#define IPV4_NAME "IPv4"
Expand Down