-
Notifications
You must be signed in to change notification settings - Fork 543
/
watermarkorch.cpp
348 lines (310 loc) · 10.3 KB
/
watermarkorch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "watermarkorch.h"
#include "sai_serialize.h"
#include "portsorch.h"
#include "notifier.h"
#include "converter.h"
#include "bufferorch.h"
#include <inttypes.h>
#define DEFAULT_TELEMETRY_INTERVAL 120
#define CLEAR_PG_HEADROOM_REQUEST "PG_HEADROOM"
#define CLEAR_PG_SHARED_REQUEST "PG_SHARED"
#define CLEAR_QUEUE_SHARED_UNI_REQUEST "Q_SHARED_UNI"
#define CLEAR_QUEUE_SHARED_MULTI_REQUEST "Q_SHARED_MULTI"
#define CLEAR_QUEUE_SHARED_ALL_REQUEST "Q_SHARED_ALL"
#define CLEAR_BUFFER_POOL_REQUEST "BUFFER_POOL"
#define CLEAR_HEADROOM_POOL_REQUEST "HEADROOM_POOL"
extern PortsOrch *gPortsOrch;
extern BufferOrch *gBufferOrch;
WatermarkOrch::WatermarkOrch(DBConnector *db, const vector<string> &tables):
Orch(db, tables)
{
SWSS_LOG_ENTER();
m_countersDb = make_shared<DBConnector>("COUNTERS_DB", 0);
m_appDb = make_shared<DBConnector>("APPL_DB", 0);
m_countersTable = make_shared<Table>(m_countersDb.get(), COUNTERS_TABLE);
m_periodicWatermarkTable = make_shared<Table>(m_countersDb.get(), PERIODIC_WATERMARKS_TABLE);
m_persistentWatermarkTable = make_shared<Table>(m_countersDb.get(), PERSISTENT_WATERMARKS_TABLE);
m_userWatermarkTable = make_shared<Table>(m_countersDb.get(), USER_WATERMARKS_TABLE);
m_clearNotificationConsumer = new swss::NotificationConsumer(
m_appDb.get(),
"WATERMARK_CLEAR_REQUEST");
auto clearNotifier = new Notifier(m_clearNotificationConsumer, this, "WM_CLEAR_NOTIFIER");
Orch::addExecutor(clearNotifier);
auto intervT = timespec { .tv_sec = DEFAULT_TELEMETRY_INTERVAL , .tv_nsec = 0 };
m_telemetryTimer = new SelectableTimer(intervT);
auto executorT = new ExecutableTimer(m_telemetryTimer, this, "WM_TELEMETRY_TIMER");
Orch::addExecutor(executorT);
}
WatermarkOrch::~WatermarkOrch()
{
SWSS_LOG_ENTER();
}
void WatermarkOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->allPortsReady())
{
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
string key = kfvKey(t);
string op = kfvOp(t);
std::vector<FieldValueTuple> fvt = kfvFieldsValues(t);
if (op == SET_COMMAND)
{
if (consumer.getTableName() == CFG_WATERMARK_TABLE_NAME)
{
handleWmConfigUpdate(key, fvt);
}
else if (consumer.getTableName() == CFG_FLEX_COUNTER_TABLE_NAME)
{
handleFcConfigUpdate(key, fvt);
}
}
else if (op == DEL_COMMAND)
{
SWSS_LOG_WARN("Unsupported op %s", op.c_str());
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s\n", op.c_str());
}
consumer.m_toSync.erase(it++);
}
}
void WatermarkOrch::handleWmConfigUpdate(const std::string &key, const std::vector<FieldValueTuple> &fvt)
{
SWSS_LOG_ENTER();
if (key == "TELEMETRY_INTERVAL")
{
for (std::pair<std::basic_string<char>, std::basic_string<char> > i: fvt)
{
if (i.first == "interval")
{
auto intervT = timespec { .tv_sec = static_cast<time_t>(to_uint<uint32_t>(i.second.c_str())), .tv_nsec = 0 };
m_telemetryTimer->setInterval(intervT);
// reset the timer interval when current timer expires
m_timerChanged = true;
}
else
{
SWSS_LOG_WARN("Unsupported key: %s", i.first.c_str());
}
}
}
}
void WatermarkOrch::handleFcConfigUpdate(const std::string &key, const std::vector<FieldValueTuple> &fvt)
{
SWSS_LOG_ENTER();
uint8_t prevStatus = m_wmStatus;
if (key == "QUEUE_WATERMARK" || key == "PG_WATERMARK")
{
for (std::pair<std::basic_string<char>, std::basic_string<char> > i: fvt)
{
if (i.first == "FLEX_COUNTER_STATUS")
{
if (i.second == "enable")
{
m_wmStatus = (uint8_t) (m_wmStatus | groupToMask.at(key));
}
else if (i.second == "disable")
{
m_wmStatus = (uint8_t) (m_wmStatus & ~(groupToMask.at(key)));
}
}
}
if (!prevStatus && m_wmStatus)
{
m_telemetryTimer->start();
}
SWSS_LOG_DEBUG("Status of WMs: %u", m_wmStatus);
}
}
void WatermarkOrch::doTask(NotificationConsumer &consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->allPortsReady())
{
return;
}
if (m_pg_ids.empty())
{
init_pg_ids();
}
if (m_multicast_queue_ids.empty() and m_unicast_queue_ids.empty() and m_all_queue_ids.empty())
{
init_queue_ids();
}
std::string op;
std::string data;
std::vector<swss::FieldValueTuple> values;
consumer.pop(op, data, values);
Table * table = NULL;
if (op == "PERSISTENT")
{
table = m_persistentWatermarkTable.get();
}
else if (op == "USER")
{
table = m_userWatermarkTable.get();
}
else
{
SWSS_LOG_WARN("Unknown watermark clear request op: %s", op.c_str());
return;
}
if (data == CLEAR_PG_HEADROOM_REQUEST)
{
clearSingleWm(table,
"SAI_INGRESS_PRIORITY_GROUP_STAT_XOFF_ROOM_WATERMARK_BYTES",
m_pg_ids);
}
else if (data == CLEAR_PG_SHARED_REQUEST)
{
clearSingleWm(table,
"SAI_INGRESS_PRIORITY_GROUP_STAT_SHARED_WATERMARK_BYTES",
m_pg_ids);
}
else if (data == CLEAR_QUEUE_SHARED_UNI_REQUEST)
{
clearSingleWm(table,
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_unicast_queue_ids);
}
else if (data == CLEAR_QUEUE_SHARED_MULTI_REQUEST)
{
clearSingleWm(table,
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_multicast_queue_ids);
}
else if (data == CLEAR_QUEUE_SHARED_ALL_REQUEST)
{
clearSingleWm(table,
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_all_queue_ids);
}
else if (data == CLEAR_BUFFER_POOL_REQUEST)
{
clearSingleWm(table,
"SAI_BUFFER_POOL_STAT_WATERMARK_BYTES",
gBufferOrch->getBufferPoolNameOidMap());
}
else if (data == CLEAR_HEADROOM_POOL_REQUEST)
{
clearSingleWm(table,
"SAI_BUFFER_POOL_STAT_XOFF_ROOM_WATERMARK_BYTES",
gBufferOrch->getBufferPoolNameOidMap());
}
else
{
SWSS_LOG_WARN("Unknown watermark clear request data: %s", data.c_str());
return;
}
}
void WatermarkOrch::doTask(SelectableTimer &timer)
{
SWSS_LOG_ENTER();
if (m_pg_ids.empty())
{
init_pg_ids();
}
if (m_multicast_queue_ids.empty() and m_unicast_queue_ids.empty() and m_all_queue_ids.empty())
{
init_queue_ids();
}
if (&timer == m_telemetryTimer)
{
if (m_timerChanged)
{
m_telemetryTimer->reset();
m_timerChanged = false;
}
if (!m_wmStatus)
{
m_telemetryTimer->stop();
}
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_INGRESS_PRIORITY_GROUP_STAT_XOFF_ROOM_WATERMARK_BYTES",
m_pg_ids);
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_INGRESS_PRIORITY_GROUP_STAT_SHARED_WATERMARK_BYTES",
m_pg_ids);
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_unicast_queue_ids);
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_multicast_queue_ids);
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES",
m_all_queue_ids);
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_BUFFER_POOL_STAT_WATERMARK_BYTES",
gBufferOrch->getBufferPoolNameOidMap());
clearSingleWm(m_periodicWatermarkTable.get(),
"SAI_BUFFER_POOL_STAT_XOFF_ROOM_WATERMARK_BYTES",
gBufferOrch->getBufferPoolNameOidMap());
SWSS_LOG_DEBUG("Periodic watermark cleared by timer!");
}
}
void WatermarkOrch::init_pg_ids()
{
SWSS_LOG_ENTER();
std::vector<FieldValueTuple> values;
Table pg_index_table(m_countersDb.get(), COUNTERS_PG_INDEX_MAP);
pg_index_table.get("", values);
for (auto fv: values)
{
sai_object_id_t id;
sai_deserialize_object_id(fv.first, id);
m_pg_ids.push_back(id);
}
}
void WatermarkOrch::init_queue_ids()
{
SWSS_LOG_ENTER();
std::vector<FieldValueTuple> values;
Table m_queue_type_table(m_countersDb.get(), COUNTERS_QUEUE_TYPE_MAP);
m_queue_type_table.get("", values);
for (auto fv: values)
{
sai_object_id_t id;
sai_deserialize_object_id(fv.first, id);
if (fv.second == "SAI_QUEUE_TYPE_UNICAST")
{
m_unicast_queue_ids.push_back(id);
}
else if (fv.second == "SAI_QUEUE_TYPE_MULTICAST")
{
m_multicast_queue_ids.push_back(id);
}
else if (fv.second == "SAI_QUEUE_TYPE_ALL")
{
m_all_queue_ids.push_back(id);
}
}
}
void WatermarkOrch::clearSingleWm(Table *table, string wm_name, vector<sai_object_id_t> &obj_ids)
{
/* Zero-out some WM in some table for some vector of object ids*/
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("clear WM %s, for %zu obj ids", wm_name.c_str(), obj_ids.size());
vector<FieldValueTuple> vfvt = {{wm_name, "0"}};
for (sai_object_id_t id: obj_ids)
{
table->set(sai_serialize_object_id(id), vfvt);
}
}
void WatermarkOrch::clearSingleWm(Table *table, string wm_name, const object_reference_map &nameOidMap)
{
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("clear WM %s, for %zu obj ids", wm_name.c_str(), nameOidMap.size());
vector<FieldValueTuple> fvTuples = {{wm_name, "0"}};
for (const auto &it : nameOidMap)
{
table->set(sai_serialize_object_id(it.second.m_saiObjectId), fvTuples);
}
}