forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfcactionhandler.cpp
More file actions
669 lines (537 loc) · 20.5 KB
/
pfcactionhandler.cpp
File metadata and controls
669 lines (537 loc) · 20.5 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include <unordered_map>
#include "pfcactionhandler.h"
#include "logger.h"
#include "sai_serialize.h"
#include "portsorch.h"
#include <vector>
#include <inttypes.h>
#define PFC_WD_QUEUE_STATUS "PFC_WD_STATUS"
#define PFC_WD_QUEUE_STATUS_OPERATIONAL "operational"
#define PFC_WD_QUEUE_STATUS_STORMED "stormed"
#define PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED "PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED"
#define PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED "PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED"
#define PFC_WD_QUEUE_STATS_TX_PACKETS "PFC_WD_QUEUE_STATS_TX_PACKETS"
#define PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS "PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS"
#define PFC_WD_QUEUE_STATS_RX_PACKETS "PFC_WD_QUEUE_STATS_RX_PACKETS"
#define PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS "PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS"
#define PFC_WD_QUEUE_STATS_TX_PACKETS_LAST "PFC_WD_QUEUE_STATS_TX_PACKETS_LAST"
#define PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS_LAST "PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS_LAST"
#define PFC_WD_QUEUE_STATS_RX_PACKETS_LAST "PFC_WD_QUEUE_STATS_RX_PACKETS_LAST"
#define PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS_LAST "PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS_LAST"
extern sai_object_id_t gSwitchId;
extern PortsOrch *gPortsOrch;
extern AclOrch * gAclOrch;
extern sai_port_api_t *sai_port_api;
extern sai_queue_api_t *sai_queue_api;
extern sai_buffer_api_t *sai_buffer_api;
PfcWdActionHandler::PfcWdActionHandler(sai_object_id_t port, sai_object_id_t queue,
uint8_t queueId, shared_ptr<Table> countersTable):
m_port(port),
m_queue(queue),
m_queueId(queueId),
m_countersTable(countersTable)
{
SWSS_LOG_ENTER();
}
PfcWdActionHandler::~PfcWdActionHandler(void)
{
SWSS_LOG_ENTER();
}
void PfcWdActionHandler::initCounters(void)
{
SWSS_LOG_ENTER();
if (!getHwCounters(m_hwStats))
{
return;
}
auto wdQueueStats = getQueueStats(m_countersTable, sai_serialize_object_id(m_queue));
// initCounters() is called when the event channel receives
// a storm signal. This can happen when there is a true new storm or
// when there is an existing storm ongoing before warm-reboot. In the latter case,
// we treat the storm as an old storm. In particular,
// we do not increment the detectCount so as to clamp the
// gap between detectCount and restoreCount by 1 at maximum
if (!(wdQueueStats.detectCount > wdQueueStats.restoreCount))
{
wdQueueStats.detectCount++;
wdQueueStats.txPktLast = 0;
wdQueueStats.txDropPktLast = 0;
wdQueueStats.rxPktLast = 0;
wdQueueStats.rxDropPktLast = 0;
}
wdQueueStats.operational = false;
updateWdCounters(sai_serialize_object_id(m_queue), wdQueueStats);
}
void PfcWdActionHandler::commitCounters(bool periodic /* = false */)
{
SWSS_LOG_ENTER();
PfcWdHwStats hwStats;
if (!getHwCounters(hwStats))
{
return;
}
auto finalStats = getQueueStats(m_countersTable, sai_serialize_object_id(m_queue));
if (!periodic)
{
finalStats.restoreCount++;
}
finalStats.operational = !periodic;
finalStats.txPktLast += hwStats.txPkt - m_hwStats.txPkt;
finalStats.txDropPktLast += hwStats.txDropPkt - m_hwStats.txDropPkt;
finalStats.rxPktLast += hwStats.rxPkt - m_hwStats.rxPkt;
finalStats.rxDropPktLast += hwStats.rxDropPkt - m_hwStats.rxDropPkt;
finalStats.txPkt += hwStats.txPkt - m_hwStats.txPkt;
finalStats.txDropPkt += hwStats.txDropPkt - m_hwStats.txDropPkt;
finalStats.rxPkt += hwStats.rxPkt - m_hwStats.rxPkt;
finalStats.rxDropPkt += hwStats.rxDropPkt - m_hwStats.rxDropPkt;
m_hwStats = hwStats;
updateWdCounters(sai_serialize_object_id(m_queue), finalStats);
}
PfcWdActionHandler::PfcWdQueueStats PfcWdActionHandler::getQueueStats(shared_ptr<Table> countersTable, const string &queueIdStr)
{
SWSS_LOG_ENTER();
PfcWdQueueStats stats;
memset(&stats, 0, sizeof(PfcWdQueueStats));
stats.operational = true;
vector<FieldValueTuple> fieldValues;
if (!countersTable->get(queueIdStr, fieldValues))
{
return move(stats);
}
for (const auto& fv : fieldValues)
{
const auto field = fvField(fv);
const auto value = fvValue(fv);
if (field == PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED)
{
stats.detectCount = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED)
{
stats.restoreCount = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATUS)
{
stats.operational = value == PFC_WD_QUEUE_STATUS_OPERATIONAL ? true : false;
}
else if (field == PFC_WD_QUEUE_STATS_TX_PACKETS)
{
stats.txPkt = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS)
{
stats.txDropPkt = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_RX_PACKETS)
{
stats.rxPkt = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS)
{
stats.rxDropPkt = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_TX_PACKETS_LAST)
{
stats.txPktLast = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS_LAST)
{
stats.txDropPktLast = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_RX_PACKETS_LAST)
{
stats.rxPktLast = stoul(value);
}
else if (field == PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS_LAST)
{
stats.rxDropPktLast = stoul(value);
}
}
return move(stats);
}
void PfcWdActionHandler::initWdCounters(shared_ptr<Table> countersTable, const string &queueIdStr)
{
SWSS_LOG_ENTER();
vector<FieldValueTuple> resultFvValues;
auto stats = getQueueStats(countersTable, queueIdStr);
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED, to_string(stats.detectCount));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED, to_string(stats.restoreCount));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATUS, PFC_WD_QUEUE_STATUS_OPERATIONAL);
countersTable->set(queueIdStr, resultFvValues);
}
void PfcWdActionHandler::updateWdCounters(const string& queueIdStr, const PfcWdQueueStats& stats)
{
SWSS_LOG_ENTER();
vector<FieldValueTuple> resultFvValues;
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED, to_string(stats.detectCount));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED, to_string(stats.restoreCount));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_TX_PACKETS, to_string(stats.txPkt));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS, to_string(stats.txDropPkt));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_RX_PACKETS, to_string(stats.rxPkt));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS, to_string(stats.rxDropPkt));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_TX_PACKETS_LAST, to_string(stats.txPktLast));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS_LAST, to_string(stats.txDropPktLast));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_RX_PACKETS_LAST, to_string(stats.rxPktLast));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS_LAST, to_string(stats.rxDropPktLast));
resultFvValues.emplace_back(PFC_WD_QUEUE_STATUS, stats.operational ?
PFC_WD_QUEUE_STATUS_OPERATIONAL :
PFC_WD_QUEUE_STATUS_STORMED);
m_countersTable->set(queueIdStr, resultFvValues);
}
PfcWdAclHandler::PfcWdAclHandler(sai_object_id_t port, sai_object_id_t queue,
uint8_t queueId, shared_ptr<Table> countersTable):
PfcWdLossyHandler(port, queue, queueId, countersTable)
{
SWSS_LOG_ENTER();
acl_table_type_t table_type = ACL_TABLE_PFCWD;
// There is one handler instance per queue ID
string queuestr = to_string(queueId);
m_strIngressTable = "IngressTable_PfcWdAclHandler_" + queuestr;
m_strEgressTable = "EgressTable_PfcWdAclHandler_" + queuestr;
m_strRule = "Rule_PfcWdAclHandler_" + queuestr;
auto found = m_aclTables.find(m_strIngressTable);
if (found == m_aclTables.end())
{
// First time of handling PFC for this queue, create ACL table, and bind
createPfcAclTable(port, m_strIngressTable, true);
shared_ptr<AclRulePfcwd> newRule = make_shared<AclRulePfcwd>(gAclOrch, m_strRule, m_strIngressTable, table_type);
createPfcAclRule(newRule, queueId, m_strIngressTable);
}
else
{
// Otherwise just bind ACL table with the port
found->second.bind(port);
}
found = m_aclTables.find(m_strEgressTable);
if (found == m_aclTables.end())
{
// First time of handling PFC for this queue, create ACL table, and bind
createPfcAclTable(port, m_strEgressTable, false);
shared_ptr<AclRulePfcwd> newRule = make_shared<AclRulePfcwd>(gAclOrch, m_strRule, m_strEgressTable, table_type);
createPfcAclRule(newRule, queueId, m_strEgressTable);
}
else
{
// Otherwise just bind ACL table with the port
found->second.bind(port);
}
}
PfcWdAclHandler::~PfcWdAclHandler(void)
{
SWSS_LOG_ENTER();
auto found = m_aclTables.find(m_strIngressTable);
found->second.unbind(getPort());
found = m_aclTables.find(m_strEgressTable);
found->second.unbind(getPort());
}
void PfcWdAclHandler::clear()
{
SWSS_LOG_ENTER();
for (auto& tablepair: m_aclTables)
{
auto& table = tablepair.second;
gAclOrch->removeAclTable(table.getId());
}
}
void PfcWdAclHandler::createPfcAclTable(sai_object_id_t port, string strTable, bool ingress)
{
SWSS_LOG_ENTER();
auto inserted = m_aclTables.emplace(piecewise_construct,
std::forward_as_tuple(strTable),
std::forward_as_tuple());
assert(inserted.second);
AclTable& aclTable = inserted.first->second;
aclTable.type = ACL_TABLE_PFCWD;
aclTable.link(port);
aclTable.id = strTable;
aclTable.stage = ingress ? ACL_STAGE_INGRESS : ACL_STAGE_EGRESS;
gAclOrch->addAclTable(aclTable);
}
void PfcWdAclHandler::createPfcAclRule(shared_ptr<AclRulePfcwd> rule, uint8_t queueId, string strTable)
{
SWSS_LOG_ENTER();
string attr_name, attr_value;
attr_name = RULE_PRIORITY;
attr_value = "999";
rule->validateAddPriority(attr_name, attr_value);
attr_name = MATCH_TC;
attr_value = to_string(queueId);
rule->validateAddMatch(attr_name, attr_value);
attr_name = ACTION_PACKET_ACTION;
attr_value = PACKET_ACTION_DROP;
rule->validateAddAction(attr_name, attr_value);
gAclOrch->addAclRule(rule, strTable);
}
std::map<std::string, AclTable> PfcWdAclHandler::m_aclTables;
PfcWdLossyHandler::PfcWdLossyHandler(sai_object_id_t port, sai_object_id_t queue,
uint8_t queueId, shared_ptr<Table> countersTable):
PfcWdActionHandler(port, queue, queueId, countersTable)
{
SWSS_LOG_ENTER();
uint8_t pfcMask = 0;
if (!gPortsOrch->getPortPfc(port, &pfcMask))
{
SWSS_LOG_ERROR("Failed to get PFC mask on port 0x%" PRIx64, port);
}
pfcMask = static_cast<uint8_t>(pfcMask & ~(1 << queueId));
if (!gPortsOrch->setPortPfc(port, pfcMask))
{
SWSS_LOG_ERROR("Failed to set PFC mask on port 0x%" PRIx64, port);
}
}
PfcWdLossyHandler::~PfcWdLossyHandler(void)
{
SWSS_LOG_ENTER();
uint8_t pfcMask = 0;
if (!gPortsOrch->getPortPfc(getPort(), &pfcMask))
{
SWSS_LOG_ERROR("Failed to get PFC mask on port 0x%" PRIx64, getPort());
}
pfcMask = static_cast<uint8_t>(pfcMask | (1 << getQueueId()));
if (!gPortsOrch->setPortPfc(getPort(), pfcMask))
{
SWSS_LOG_ERROR("Failed to set PFC mask on port 0x%" PRIx64, getPort());
}
}
bool PfcWdLossyHandler::getHwCounters(PfcWdHwStats& counters)
{
SWSS_LOG_ENTER();
static const vector<sai_stat_id_t> queueStatIds =
{
SAI_QUEUE_STAT_PACKETS,
SAI_QUEUE_STAT_DROPPED_PACKETS,
};
static const vector<sai_stat_id_t> pgStatIds =
{
SAI_INGRESS_PRIORITY_GROUP_STAT_PACKETS,
SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS,
};
vector<uint64_t> queueStats;
queueStats.resize(queueStatIds.size());
sai_status_t status = sai_queue_api->get_queue_stats(
getQueue(),
static_cast<uint32_t>(queueStatIds.size()),
queueStatIds.data(),
queueStats.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to fetch queue 0x%" PRIx64 " stats: %d", getQueue(), status);
return false;
}
// PG counters not yet supported in Mellanox platform
Port portInstance;
if (!gPortsOrch->getPort(getPort(), portInstance))
{
SWSS_LOG_ERROR("Cannot get port by ID 0x%" PRIx64, getPort());
return false;
}
sai_object_id_t pg = portInstance.m_priority_group_ids[static_cast <size_t> (getQueueId())];
vector<uint64_t> pgStats;
pgStats.resize(pgStatIds.size());
status = sai_buffer_api->get_ingress_priority_group_stats(
pg,
static_cast<uint32_t>(pgStatIds.size()),
pgStatIds.data(),
pgStats.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to fetch pg 0x%" PRIx64 " stats: %d", pg, status);
return false;
}
counters.txPkt = queueStats[0];
counters.txDropPkt = queueStats[1];
counters.rxPkt = pgStats[0];
counters.rxDropPkt = pgStats[1];
return true;
}
PfcWdZeroBufferHandler::PfcWdZeroBufferHandler(sai_object_id_t port,
sai_object_id_t queue, uint8_t queueId, shared_ptr<Table> countersTable):
PfcWdLossyHandler(port, queue, queueId, countersTable)
{
SWSS_LOG_ENTER();
Port portInstance;
if (!gPortsOrch->getPort(port, portInstance))
{
SWSS_LOG_ERROR("Cannot get port by ID 0x%" PRIx64, port);
return;
}
setPriorityGroupAndQueueLockFlag(portInstance, true);
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_BUFFER_PROFILE_ID;
// Get queue's buffer profile ID
sai_status_t status = sai_queue_api->get_queue_attribute(queue, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get buffer profile ID on queue 0x%" PRIx64 ": %d", queue, status);
return;
}
sai_object_id_t oldQueueProfileId = attr.value.oid;
attr.id = SAI_QUEUE_ATTR_BUFFER_PROFILE_ID;
attr.value.oid = ZeroBufferProfile::getZeroBufferProfile(false);
// Set our zero buffer profile
status = sai_queue_api->set_queue_attribute(queue, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set buffer profile ID on queue 0x%" PRIx64 ": %d", queue, status);
return;
}
// Save original buffer profile
m_originalQueueBufferProfile = oldQueueProfileId;
// Get PG
sai_object_id_t pg = portInstance.m_priority_group_ids[static_cast <size_t> (queueId)];
attr.id = SAI_INGRESS_PRIORITY_GROUP_ATTR_BUFFER_PROFILE;
// Get PG's buffer profile
status = sai_buffer_api->get_ingress_priority_group_attribute(pg, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get buffer profile ID on PG 0x%" PRIx64 ": %d", pg, status);
return;
}
// Set zero profile to PG
sai_object_id_t oldPgProfileId = attr.value.oid;
attr.id = SAI_INGRESS_PRIORITY_GROUP_ATTR_BUFFER_PROFILE;
attr.value.oid = ZeroBufferProfile::getZeroBufferProfile(true);
status = sai_buffer_api->set_ingress_priority_group_attribute(pg, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set buffer profile ID on pg 0x%" PRIx64 ": %d", pg, status);
return;
}
// Save original buffer profile
m_originalPgBufferProfile = oldPgProfileId;
}
PfcWdZeroBufferHandler::~PfcWdZeroBufferHandler(void)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_BUFFER_PROFILE_ID;
attr.value.oid = m_originalQueueBufferProfile;
// Set our zero buffer profile on a queue
sai_status_t status = sai_queue_api->set_queue_attribute(getQueue(), &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set buffer profile ID on queue 0x%" PRIx64 ": %d", getQueue(), status);
return;
}
Port portInstance;
if (!gPortsOrch->getPort(getPort(), portInstance))
{
SWSS_LOG_ERROR("Cannot get port by ID 0x%" PRIx64, getPort());
return;
}
sai_object_id_t pg = portInstance.m_priority_group_ids[size_t(getQueueId())];
attr.id = SAI_INGRESS_PRIORITY_GROUP_ATTR_BUFFER_PROFILE;
attr.value.oid = m_originalPgBufferProfile;
// Set our zero buffer profile
status = sai_buffer_api->set_ingress_priority_group_attribute(pg, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set buffer profile ID on queue 0x%" PRIx64 ": %d", getQueue(), status);
return;
}
setPriorityGroupAndQueueLockFlag(portInstance, false);
}
void PfcWdZeroBufferHandler::setPriorityGroupAndQueueLockFlag(Port& port, bool isLocked) const
{
// set lock bits on PG and queue
port.m_priority_group_lock[static_cast<size_t>(getQueueId())] = isLocked;
for (size_t i = 0; i < port.m_queue_ids.size(); ++i)
{
if (port.m_queue_ids[i] == getQueue())
{
port.m_queue_lock[i] = isLocked;
}
}
gPortsOrch->setPort(port.m_alias, port);
}
PfcWdZeroBufferHandler::ZeroBufferProfile::ZeroBufferProfile(void)
{
SWSS_LOG_ENTER();
}
PfcWdZeroBufferHandler::ZeroBufferProfile::~ZeroBufferProfile(void)
{
SWSS_LOG_ENTER();
// Destory ingress and egress prifiles and pools
destroyZeroBufferProfile(true);
destroyZeroBufferProfile(false);
}
PfcWdZeroBufferHandler::ZeroBufferProfile &PfcWdZeroBufferHandler::ZeroBufferProfile::getInstance(void)
{
SWSS_LOG_ENTER();
static ZeroBufferProfile instance;
return instance;
}
sai_object_id_t PfcWdZeroBufferHandler::ZeroBufferProfile::getZeroBufferProfile(bool ingress)
{
SWSS_LOG_ENTER();
if (getInstance().getProfile(ingress) == SAI_NULL_OBJECT_ID)
{
getInstance().createZeroBufferProfile(ingress);
}
return getInstance().getProfile(ingress);
}
void PfcWdZeroBufferHandler::ZeroBufferProfile::createZeroBufferProfile(bool ingress)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
vector<sai_attribute_t> attribs;
// Create zero pool
attr.id = SAI_BUFFER_POOL_ATTR_SIZE;
attr.value.u64 = 0;
attribs.push_back(attr);
attr.id = SAI_BUFFER_POOL_ATTR_TYPE;
attr.value.u32 = ingress ? SAI_BUFFER_POOL_TYPE_INGRESS : SAI_BUFFER_POOL_TYPE_EGRESS;
attribs.push_back(attr);
attr.id = SAI_BUFFER_POOL_ATTR_THRESHOLD_MODE;
attr.value.u32 = SAI_BUFFER_POOL_THRESHOLD_MODE_DYNAMIC;
attribs.push_back(attr);
sai_status_t status = sai_buffer_api->create_buffer_pool(
&getPool(ingress),
gSwitchId,
static_cast<uint32_t>(attribs.size()),
attribs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create dynamic zero buffer pool for PFC WD: %d", status);
return;
}
// Create zero profile
attribs.clear();
attr.id = SAI_BUFFER_PROFILE_ATTR_POOL_ID;
attr.value.oid = getPool(ingress);
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_THRESHOLD_MODE;
attr.value.u32 = SAI_BUFFER_PROFILE_THRESHOLD_MODE_DYNAMIC;
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_BUFFER_SIZE;
attr.value.u32 = 0;
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_SHARED_DYNAMIC_TH;
attr.value.s8 = -8; // ALPHA_0
attribs.push_back(attr);
status = sai_buffer_api->create_buffer_profile(
&getProfile(ingress),
gSwitchId,
static_cast<uint32_t>(attribs.size()),
attribs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create dynamic zero buffer profile for PFC WD: %d", status);
return;
}
}
void PfcWdZeroBufferHandler::ZeroBufferProfile::destroyZeroBufferProfile(bool ingress)
{
SWSS_LOG_ENTER();
sai_status_t status = sai_buffer_api->remove_buffer_profile(getProfile(ingress));
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove static zero buffer profile for PFC WD: %d", status);
return;
}
status = sai_buffer_api->remove_buffer_pool(getPool(ingress));
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove static zero buffer pool for PFC WD: %d", status);
}
}