Skip to content

Commit

Permalink
[Dynamic buffer calc] Support dynamic buffer calculation (#361)
Browse files Browse the repository at this point in the history
1. Database schema updates.
2. Move the json file parsing from swss to swss-common.
  • Loading branch information
stephenxs committed Dec 2, 2020
1 parent a161a64 commit cf9cc37
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 9 deletions.
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)
{
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;
}
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;
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 @@ -90,6 +90,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 *****/
Expand All @@ -104,13 +111,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 @@ -248,6 +248,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 @@ -349,6 +351,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

0 comments on commit cf9cc37

Please sign in to comment.