Skip to content

Commit cf9cc37

Browse files
authored
[Dynamic buffer calc] Support dynamic buffer calculation (#361)
1. Database schema updates. 2. Move the json file parsing from swss to swss-common.
1 parent a161a64 commit cf9cc37

File tree

3 files changed

+146
-9
lines changed

3 files changed

+146
-9
lines changed

common/json.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#include <stdlib.h>
12
#include <sstream>
3+
#include <fstream>
4+
#include <iostream>
25
#include <limits>
36

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

42+
bool JSon::loadJsonFromFile(ifstream &fs, vector<KeyOpFieldsValuesTuple> &db_items)
43+
{
44+
nlohmann::json json_array;
45+
46+
try
47+
{
48+
fs >> json_array;
49+
}
50+
catch (const std::logic_error &e)
51+
{
52+
SWSS_LOG_ERROR("Unable to parse json from the input stream: %s", e.what());
53+
return false;
54+
}
55+
catch (const std::bad_alloc &e)
56+
{
57+
SWSS_LOG_ERROR("Unable to parse json from the input stream: %s", e.what());
58+
return false;
59+
}
60+
61+
if (!json_array.is_array())
62+
{
63+
SWSS_LOG_ERROR("Root element must be an array.");
64+
return false;
65+
}
66+
67+
for (size_t i = 0; i < json_array.size(); i++)
68+
{
69+
auto &arr_item = json_array[i];
70+
71+
if (arr_item.is_object())
72+
{
73+
if (el_count != arr_item.size())
74+
{
75+
SWSS_LOG_ERROR("Child elements must have both key and op entry. %s",
76+
arr_item.dump().c_str());
77+
return false;
78+
}
79+
80+
db_items.emplace_back();
81+
auto &cur_db_item = db_items.back();
82+
83+
for (auto child_it = arr_item.begin(); child_it != arr_item.end(); child_it++)
84+
{
85+
auto cur_obj_key = child_it.key();
86+
auto &cur_obj = child_it.value();
87+
88+
if (cur_obj.is_object())
89+
{
90+
kfvKey(cur_db_item) = cur_obj_key;
91+
for (nlohmann::json::iterator cur_obj_it = cur_obj.begin(); cur_obj_it != cur_obj.end(); cur_obj_it++)
92+
{
93+
string field_str = cur_obj_it.key();
94+
string value_str;
95+
if ((*cur_obj_it).is_number())
96+
value_str = to_string((*cur_obj_it).get<int>());
97+
else if ((*cur_obj_it).is_string())
98+
value_str = (*cur_obj_it).get<string>();
99+
kfvFieldsValues(cur_db_item).emplace_back(field_str, value_str);
100+
}
101+
}
102+
else
103+
{
104+
auto op = cur_obj.get<string>();
105+
106+
if (op != "SET" && op != "DEL")
107+
{
108+
SWSS_LOG_ERROR("Child elements' op field must be SET or DEL, but got %s, ignored", op.c_str());
109+
db_items.pop_back();
110+
break;
111+
}
112+
kfvOp(cur_db_item) = op;
113+
}
114+
}
115+
}
116+
else
117+
{
118+
SWSS_LOG_ERROR("Child elements must be objects. element:%s", arr_item.dump().c_str());
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
39125
}

common/json.h

+45-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __JSON__
33

44
#include <string>
5+
#include <fstream>
56
#include <vector>
67

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

1112
class JSon
1213
{
14+
private:
15+
/*
16+
* el_count represents the number of elements in each object,
17+
* which means each object have exactly 2 elements: the data and the operator
18+
*/
19+
static const int el_count = 2;
1320
public:
14-
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
15-
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
21+
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
22+
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
23+
/*
24+
bool loadJsonFromFile(std::ifstream &fs, std::vector<KeyOpFieldsValuesTuple> &db_items);
25+
26+
parse the json file and construct a vector of KeyOpFieldsValuesTuple as the result
27+
the json file should be a list objects with each consisting of a data field and an operator field
28+
- the data should be a dictionary
29+
- the operator field should be a string, "SET" or "DEL"
30+
an example:
31+
[
32+
{
33+
"QOS_TABLE:TC_TO_QUEUE_MAP_TABLE:AZURE": {
34+
"5": "1",
35+
"6": "1"
36+
},
37+
"OP": "SET"
38+
},
39+
{
40+
"QOS_TABLE:DSCP_TO_TC_MAP_TABLE:AZURE": {
41+
"7":"5",
42+
"6":"5",
43+
"3":"3",
44+
"8":"7",
45+
"9":"8"
46+
},
47+
"OP": "SET"
48+
}
49+
]
50+
51+
parameters:
52+
fs: the input ifstream representing the json file
53+
fv: the output vector
54+
return: boolean
55+
True: the input json file has been succefully parsed
56+
False: there are some errors found
57+
*/
58+
static bool loadJsonFromFile(std::ifstream &fs, std::vector<KeyOpFieldsValuesTuple> &db_items);
1659
};
1760

1861
}

common/schema.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ namespace swss {
9090
#define APP_MACSEC_EGRESS_SA_TABLE_NAME "MACSEC_EGRESS_SA_TABLE"
9191
#define APP_MACSEC_INGRESS_SA_TABLE_NAME "MACSEC_INGRESS_SA_TABLE"
9292

93+
#define APP_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
94+
#define APP_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
95+
#define APP_BUFFER_PG_TABLE_NAME "BUFFER_PG_TABLE"
96+
#define APP_BUFFER_QUEUE_TABLE_NAME "BUFFER_QUEUE_TABLE"
97+
#define APP_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST_TABLE"
98+
#define APP_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST_TABLE"
99+
93100
#define APP_NEIGH_RESOLVE_TABLE_NAME "NEIGH_RESOLVE_TABLE"
94101

95102
/***** TO BE REMOVED *****/
@@ -104,13 +111,6 @@ namespace swss {
104111
#define APP_PFC_PRIORITY_TO_PRIORITY_GROUP_MAP_NAME "PFC_PRIORITY_TO_PRIORITY_GROUP_MAP_TABLE"
105112
#define APP_PFC_PRIORITY_TO_QUEUE_MAP_NAME "MAP_PFC_PRIORITY_TO_QUEUE"
106113

107-
#define APP_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
108-
#define APP_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
109-
#define APP_BUFFER_QUEUE_TABLE_NAME "BUFFER_QUEUE_TABLE"
110-
#define APP_BUFFER_PG_TABLE_NAME "BUFFER_PG_TABLE"
111-
#define APP_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST"
112-
#define APP_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST"
113-
114114
/***** COUNTER DATABASE *****/
115115

116116
#define COUNTERS_PORT_NAME_MAP "COUNTERS_PORT_NAME_MAP"
@@ -248,6 +248,8 @@ namespace swss {
248248
#define CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST"
249249
#define CFG_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST"
250250

251+
#define CFG_DEFAULT_LOSSLESS_BUFFER_PARAMETER "DEFAULT_LOSSLESS_BUFFER_PARAMETER"
252+
251253
#define CFG_POLICER_TABLE_NAME "POLICER"
252254

253255
#define CFG_WARM_RESTART_TABLE_NAME "WARM_RESTART"
@@ -349,6 +351,12 @@ namespace swss {
349351
#define STATE_MACSEC_EGRESS_SC_TABLE_NAME "MACSEC_EGRESS_SC_TABLE"
350352
#define STATE_MACSEC_EGRESS_SA_TABLE_NAME "MACSEC_EGRESS_SA_TABLE"
351353

354+
#define STATE_ASIC_TABLE "ASIC_TABLE"
355+
#define STATE_BUFFER_MAXIMUM_VALUE_TABLE "BUFFER_MAX_PARAM_TABLE"
356+
#define STATE_PERIPHERAL_TABLE "PERIPHERAL_TABLE"
357+
#define STATE_PORT_PERIPHERAL_TABLE "PORT_PERIPHERAL_TABLE"
358+
#define STATE_BUFFER_POOL_TABLE_NAME "BUFFER_POOL_TABLE"
359+
#define STATE_BUFFER_PROFILE_TABLE_NAME "BUFFER_PROFILE_TABLE"
352360
/***** MISC *****/
353361

354362
#define IPV4_NAME "IPv4"

0 commit comments

Comments
 (0)