-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathpfcactionhandler.cpp
855 lines (706 loc) · 26.6 KB
/
pfcactionhandler.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#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 SwitchOrch *gSwitchOrch;
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 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 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);
}
PfcWdSaiDlrInitHandler::PfcWdSaiDlrInitHandler(sai_object_id_t port, sai_object_id_t queue,
uint8_t queueId, shared_ptr<Table> countersTable):
PfcWdZeroBufferHandler(port, queue, queueId, countersTable)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_PFC_DLR_INIT;
attr.value.booldata = true;
// Set DLR init to true to start PFC deadlock recovery
sai_status_t status = sai_queue_api->set_queue_attribute(queue, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set PFC DLR INIT on port 0x%" PRIx64 " queue 0x%" PRIx64
" queueId %d : %d",
port, queue, queueId, status);
return;
}
}
PfcWdSaiDlrInitHandler::~PfcWdSaiDlrInitHandler(void)
{
SWSS_LOG_ENTER();
sai_object_id_t port = getPort();
sai_object_id_t queue = getQueue();
uint8_t queueId = getQueueId();
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_PFC_DLR_INIT;
attr.value.booldata = false;
// Set DLR init to false to stop PFC deadlock recovery
sai_status_t status = sai_queue_api->set_queue_attribute(getQueue(), &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to clear PFC DLR INIT on port 0x%" PRIx64 " queue 0x%" PRIx64
" queueId %d : %d", port, queue, queueId, status);
return;
}
}
PfcWdDlrHandler::PfcWdDlrHandler(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();
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_PFC_DLR_INIT;
attr.value.booldata = true;
// Set DLR init to true to start PFC deadlock recovery
sai_status_t status = sai_queue_api->set_queue_attribute(queue, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set PFC DLR INIT on port 0x%" PRIx64 " queue 0x%" PRIx64
" queueId %d : %d",
port, queue, queueId, status);
return;
}
}
PfcWdDlrHandler::~PfcWdDlrHandler(void)
{
SWSS_LOG_ENTER();
sai_object_id_t port = getPort();
sai_object_id_t queue = getQueue();
uint8_t queueId = getQueueId();
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_PFC_DLR_INIT;
attr.value.booldata = false;
// Set DLR init to false to stop PFC deadlock recovery
sai_status_t status = sai_queue_api->set_queue_attribute(getQueue(), &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to clear PFC DLR INIT on port 0x%" PRIx64 " queue 0x%" PRIx64
" queueId %d : %d", port, queue, queueId, status);
return;
}
}
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();
string table_type;
string queuestr = to_string(queueId);
m_strRule = "Rule_PfcWdAclHandler_" + queuestr;
// Ingress table/rule creation
table_type = TABLE_TYPE_DROP;
m_strIngressTable = INGRESS_TABLE_DROP;
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<AclRulePacket> newRule = make_shared<AclRulePacket>(gAclOrch, m_strRule, m_strIngressTable);
createPfcAclRule(newRule, queueId, m_strIngressTable, port);
}
else
{
AclRule* rule = gAclOrch->getAclRule(m_strIngressTable, m_strRule);
if (rule == nullptr)
{
shared_ptr<AclRulePacket> newRule = make_shared<AclRulePacket>(gAclOrch, m_strRule, m_strIngressTable);
createPfcAclRule(newRule, queueId, m_strIngressTable, port);
}
else
{
gAclOrch->updateAclRule(m_strIngressTable, m_strRule, MATCH_IN_PORTS, &port, RULE_OPER_ADD);
}
}
// Egress table/rule creation
table_type = TABLE_TYPE_PFCWD;
// Use shared egress acl table for BRCM DNX platform.
string platform = getenv("platform") ? getenv("platform") : "";
string sub_platform = getenv("sub_platform") ? getenv("sub_platform") : "";
shared_egress_acl_table = (platform == BRCM_PLATFORM_SUBSTRING &&
sub_platform == BRCM_DNX_PLATFORM_SUBSTRING);
if (shared_egress_acl_table)
{
Port p;
if (!gPortsOrch->getPort(port, p))
{
SWSS_LOG_ERROR("Failed to get port structure from port oid 0x%" PRIx64, port);
return;
}
m_strEgressRule = "Egress_Rule_PfcWdAclHandler_" + p.m_alias + "_" + queuestr;
m_strEgressTable = "EgressTable_PfcWdAclHandler";
found = m_aclTables.find(m_strEgressTable);
if (found == m_aclTables.end())
{
// First time of handling PFC, create ACL table and also ACL rule.
createPfcAclTable(port, m_strEgressTable, false);
shared_ptr<AclRulePacket> newRule = make_shared<AclRulePacket>(gAclOrch, m_strEgressRule, m_strEgressTable);
createPfcAclRule(newRule, queueId, m_strEgressTable, port);
}
else
{
// ACL table already exists. Add ACL rule if needed.
AclRule* rule = gAclOrch->getAclRule(m_strEgressTable, m_strEgressRule);
if (rule == nullptr)
{
shared_ptr<AclRulePacket> newRule = make_shared<AclRulePacket>(gAclOrch, m_strEgressRule, m_strEgressTable);
createPfcAclRule(newRule, queueId, m_strEgressTable, port);
}
}
}
else
{
m_strEgressTable = "EgressTable_PfcWdAclHandler_" + queuestr;
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<AclRulePacket> newRule = make_shared<AclRulePacket>(gAclOrch, m_strRule, m_strEgressTable);
createPfcAclRule(newRule, queueId, m_strEgressTable, port);
}
else
{
// Otherwise just bind ACL table with the port
found->second.bind(port);
}
}
}
PfcWdAclHandler::~PfcWdAclHandler(void)
{
SWSS_LOG_ENTER();
AclRule* rule = gAclOrch->getAclRule(m_strIngressTable, m_strRule);
if (rule == nullptr)
{
SWSS_LOG_THROW("ACL Rule does not exist for rule %s", m_strRule.c_str());
}
vector<sai_object_id_t> port_set = rule->getInPorts();
sai_object_id_t port = getPort();
if ((port_set.size() == 1) && (port_set[0] == port))
{
gAclOrch->removeAclRule(m_strIngressTable, m_strRule);
}
else
{
gAclOrch->updateAclRule(m_strIngressTable, m_strRule, MATCH_IN_PORTS, &port, RULE_OPER_DELETE);
}
if (shared_egress_acl_table)
{
rule = gAclOrch->getAclRule(m_strEgressTable, m_strEgressRule);
if (rule == nullptr)
{
SWSS_LOG_THROW("Egress ACL Rule does not exist for rule %s", m_strEgressRule.c_str());
}
gAclOrch->removeAclRule(m_strEgressTable, m_strEgressRule);
}
else
{
auto found = m_aclTables.find(m_strEgressTable);
found->second.unbind(port);
}
}
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(gAclOrch, strTable));
assert(inserted.second);
AclTable& aclTable = inserted.first->second;
sai_object_id_t table_oid = gAclOrch->getTableById(strTable);
if (ingress && table_oid != SAI_NULL_OBJECT_ID)
{
// DROP ACL table is already created
SWSS_LOG_NOTICE("ACL table %s exists, reuse the same", strTable.c_str());
aclTable = *(gAclOrch->getTableByOid(table_oid));
return;
}
// Link port only for ingress ACL table or unshared egress ACL table.
if (ingress || !shared_egress_acl_table)
{
aclTable.link(port);
}
if (ingress)
{
auto dropType = gAclOrch->getAclTableType(TABLE_TYPE_DROP);
assert(dropType);
aclTable.validateAddType(*dropType);
aclTable.stage = ACL_STAGE_INGRESS;
}
else
{
auto pfcwdType = gAclOrch->getAclTableType(TABLE_TYPE_PFCWD);
assert(pfcwdType);
aclTable.validateAddType(*pfcwdType);
aclTable.stage = ACL_STAGE_EGRESS;
}
gAclOrch->addAclTable(aclTable);
}
void PfcWdAclHandler::createPfcAclRule(shared_ptr<AclRulePacket> rule, uint8_t queueId, string strTable, sai_object_id_t portOid)
{
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);
// Add MATCH_IN_PORTS as match criteria for ingress table and MATCH_OUT_PORT as match creiteria for shared egress table.
if (strTable == INGRESS_TABLE_DROP || shared_egress_acl_table)
{
Port p;
if (strTable == INGRESS_TABLE_DROP)
{
attr_name = MATCH_IN_PORTS;
}
else if (shared_egress_acl_table) {
attr_name = MATCH_OUT_PORT;
}
if (!gPortsOrch->getPort(portOid, p))
{
SWSS_LOG_ERROR("Failed to get port structure from port oid 0x%" PRIx64, portOid);
return;
}
attr_value = p.m_alias;
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();
string platform = getenv("platform") ? getenv("platform") : "";
if (platform == CISCO_8000_PLATFORM_SUBSTRING || ((platform == BRCM_PLATFORM_SUBSTRING) && (gSwitchOrch->checkPfcDlrInitEnable())))
{
SWSS_LOG_DEBUG("Skipping in constructor PfcWdLossyHandler for platform %s on port 0x%" PRIx64,
platform.c_str(), port);
return;
}
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();
string platform = getenv("platform") ? getenv("platform") : "";
if (platform == CISCO_8000_PLATFORM_SUBSTRING || ((platform == BRCM_PLATFORM_SUBSTRING) && (gSwitchOrch->checkPfcDlrInitEnable())))
{
SWSS_LOG_DEBUG("Skipping in destructor PfcWdLossyHandler for platform %s on port 0x%" PRIx64,
platform.c_str(), getPort());
return;
}
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;
}
setQueueLockFlag(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();
// 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;
}
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;
}
setQueueLockFlag(portInstance, false);
}
void PfcWdZeroBufferHandler::setQueueLockFlag(Port& port, bool isLocked) const
{
// set lock bits on queue
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();
// Destroy egress profiles and pools
destroyZeroBufferProfile();
}
PfcWdZeroBufferHandler::ZeroBufferProfile &PfcWdZeroBufferHandler::ZeroBufferProfile::getInstance(void)
{
SWSS_LOG_ENTER();
static ZeroBufferProfile instance;
return instance;
}
sai_object_id_t PfcWdZeroBufferHandler::ZeroBufferProfile::getZeroBufferProfile()
{
SWSS_LOG_ENTER();
if (getInstance().getProfile() == SAI_NULL_OBJECT_ID)
{
getInstance().createZeroBufferProfile();
}
return getInstance().getProfile();
}
void PfcWdZeroBufferHandler::ZeroBufferProfile::createZeroBufferProfile()
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
vector<sai_attribute_t> attribs;
sai_status_t status;
// 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 = 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);
status = sai_buffer_api->create_buffer_pool(
&getPool(),
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();
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.u64 = 0;
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_SHARED_DYNAMIC_TH;
attr.value.s8 = -8;
attribs.push_back(attr);
status = sai_buffer_api->create_buffer_profile(
&getProfile(),
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()
{
SWSS_LOG_ENTER();
sai_status_t status = sai_buffer_api->remove_buffer_profile(getProfile());
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());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove static zero buffer pool for PFC WD: %d", status);
}
}