forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabricportsorch.cpp
More file actions
1508 lines (1373 loc) · 54 KB
/
Copy pathfabricportsorch.cpp
File metadata and controls
1508 lines (1373 loc) · 54 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
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "fabricportsorch.h"
#include <inttypes.h>
#include <fstream>
#include <sstream>
#include <tuple>
#include "logger.h"
#include "schema.h"
#include "sai_serialize.h"
#include "timer.h"
#include "saihelper.h"
#include "converter.h"
#include "stringutility.h"
#include <chrono>
#include <math.h>
#define FABRIC_POLLING_INTERVAL_DEFAULT (30)
#define FABRIC_PORT_PREFIX "PORT"
#define FABRIC_PORT_ERROR 0
#define FABRIC_PORT_SUCCESS 1
#define FABRIC_PORT_STAT_COUNTER_FLEX_COUNTER_GROUP "FABRIC_PORT_STAT_COUNTER"
#define FABRIC_PORT_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS 10000
#define FABRIC_QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP "FABRIC_QUEUE_STAT_COUNTER"
#define FABRIC_QUEUE_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS 100000
#define FABRIC_DEBUG_POLLING_INTERVAL_DEFAULT (60)
#define FABRIC_MONITOR_DATA "FABRIC_MONITOR_DATA"
#define APPL_FABRIC_PORT_PREFIX "Fabric"
// constants for link monitoring
#define MAX_SKIP_CRCERR_ON_LNKUP_POLLS 20
#define MAX_SKIP_FECERR_ON_LNKUP_POLLS 20
// the follow constants will be replaced with the number in config_db
#define FEC_ISOLATE_POLLS 2
#define FEC_UNISOLATE_POLLS 8
#define ISOLATION_POLLS_CFG 1
#define RECOVERY_POLLS_CFG 8
#define ERROR_RATE_CRC_CELLS_CFG 1
#define ERROR_RATE_RX_CELLS_CFG 61035156
#define FABRIC_LINK_RATE 44316
extern sai_object_id_t gSwitchId;
extern sai_switch_api_t *sai_switch_api;
extern sai_port_api_t *sai_port_api;
extern sai_queue_api_t *sai_queue_api;
const vector<sai_port_stat_t> port_stat_ids =
{
SAI_PORT_STAT_IF_IN_OCTETS,
SAI_PORT_STAT_IF_IN_ERRORS,
SAI_PORT_STAT_IF_IN_FABRIC_DATA_UNITS,
SAI_PORT_STAT_IF_IN_FEC_CORRECTABLE_FRAMES,
SAI_PORT_STAT_IF_IN_FEC_NOT_CORRECTABLE_FRAMES,
SAI_PORT_STAT_IF_IN_FEC_SYMBOL_ERRORS,
SAI_PORT_STAT_IF_OUT_OCTETS,
SAI_PORT_STAT_IF_OUT_FABRIC_DATA_UNITS,
};
static const vector<sai_queue_stat_t> queue_stat_ids =
{
SAI_QUEUE_STAT_WATERMARK_LEVEL,
SAI_QUEUE_STAT_CURR_OCCUPANCY_BYTES,
SAI_QUEUE_STAT_CURR_OCCUPANCY_LEVEL,
};
FabricPortsOrch::FabricPortsOrch(DBConnector *appl_db, vector<table_name_with_pri_t> &tableNames,
bool fabricPortStatEnabled, bool fabricQueueStatEnabled) :
Orch(appl_db, tableNames),
port_stat_manager(FABRIC_PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ,
FABRIC_PORT_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS, true),
queue_stat_manager(FABRIC_QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ,
FABRIC_QUEUE_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS, true),
m_timer(new SelectableTimer(timespec { .tv_sec = FABRIC_POLLING_INTERVAL_DEFAULT, .tv_nsec = 0 })),
m_debugTimer(new SelectableTimer(timespec { .tv_sec = FABRIC_DEBUG_POLLING_INTERVAL_DEFAULT, .tv_nsec = 0 }))
{
SWSS_LOG_ENTER();
SWSS_LOG_NOTICE( "FabricPortsOrch constructor" );
m_state_db = shared_ptr<DBConnector>(new DBConnector("STATE_DB", 0));
m_stateTable = unique_ptr<Table>(new Table(m_state_db.get(), APP_FABRIC_PORT_TABLE_NAME));
m_fabricCapacityTable = unique_ptr<Table>(new Table(m_state_db.get(), STATE_FABRIC_CAPACITY_TABLE_NAME));
m_counter_db = shared_ptr<DBConnector>(new DBConnector("COUNTERS_DB", 0));
m_portNameQueueCounterTable = unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_FABRIC_QUEUE_NAME_MAP));
m_portNamePortCounterTable = unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_FABRIC_PORT_NAME_MAP));
m_fabricCounterTable = unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_TABLE));
m_appl_db = shared_ptr<DBConnector>(new DBConnector("APPL_DB", 0));
m_applTable = unique_ptr<Table>(new Table(m_appl_db.get(), APP_FABRIC_MONITOR_PORT_TABLE_NAME));
m_applMonitorConstTable = unique_ptr<Table>(new Table(m_appl_db.get(), APP_FABRIC_MONITOR_DATA_TABLE_NAME));
m_fabricPortStatEnabled = fabricPortStatEnabled;
m_fabricQueueStatEnabled = fabricQueueStatEnabled;
getFabricPortList();
auto executor = new ExecutableTimer(m_timer, this, "FABRIC_POLL");
Orch::addExecutor(executor);
m_timer->start();
auto debug_executor = new ExecutableTimer(m_debugTimer, this, "FABRIC_DEBUG_POLL");
Orch::addExecutor(debug_executor);
bool fabricPortMonitor = checkFabricPortMonState();
if (fabricPortMonitor)
{
m_debugTimer->start();
SWSS_LOG_INFO("Fabric monitor starts at init time");
}
}
bool FabricPortsOrch::checkFabricPortMonState()
{
bool enabled = false;
std::vector<FieldValueTuple> constValues;
bool setCfgVal = m_applMonitorConstTable->get("FABRIC_MONITOR_DATA", constValues);
if (!setCfgVal)
{
return enabled;
}
SWSS_LOG_INFO("FabricPortsOrch::checkFabricPortMonState starts");
for (auto cv : constValues)
{
if (fvField(cv) == "monState")
{
if (fvValue(cv) == "enable")
{
enabled = true;
return enabled;
}
}
}
return enabled;
}
int FabricPortsOrch::getFabricPortList()
{
SWSS_LOG_ENTER();
if (m_getFabricPortListDone) {
return FABRIC_PORT_SUCCESS;
}
uint32_t i;
sai_status_t status;
sai_attribute_t attr;
attr.id = SAI_SWITCH_ATTR_NUMBER_OF_FABRIC_PORTS;
status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get fabric port number, rv:%d", status);
task_process_status handle_status = handleSaiGetStatus(SAI_API_SWITCH, status);
if (handle_status != task_process_status::task_success)
{
return FABRIC_PORT_ERROR;
}
}
m_fabricPortCount = attr.value.u32;
SWSS_LOG_NOTICE("Get %d fabric ports", m_fabricPortCount);
vector<sai_object_id_t> fabric_port_list;
fabric_port_list.resize(m_fabricPortCount);
attr.id = SAI_SWITCH_ATTR_FABRIC_PORT_LIST;
attr.value.objlist.count = (uint32_t)fabric_port_list.size();
attr.value.objlist.list = fabric_port_list.data();
status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
task_process_status handle_status = handleSaiGetStatus(SAI_API_SWITCH, status);
if (handle_status != task_process_status::task_success)
{
throw runtime_error("FabricPortsOrch get port list failure");
}
}
for (i = 0; i < m_fabricPortCount; i++)
{
sai_uint32_t lanes[1] = { 0 };
attr.id = SAI_PORT_ATTR_HW_LANE_LIST;
attr.value.u32list.count = 1;
attr.value.u32list.list = lanes;
status = sai_port_api->get_port_attribute(fabric_port_list[i], 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
task_process_status handle_status = handleSaiGetStatus(SAI_API_PORT, status);
if (handle_status != task_process_status::task_success)
{
throw runtime_error("FabricPortsOrch get port lane failure");
}
}
int lane = attr.value.u32list.list[0];
m_fabricLanePortMap[lane] = fabric_port_list[i];
}
generatePortStats();
m_getFabricPortListDone = true;
return FABRIC_PORT_SUCCESS;
}
bool FabricPortsOrch::allPortsReady()
{
return m_getFabricPortListDone;
}
void FabricPortsOrch::generatePortStats()
{
if (!m_fabricPortStatEnabled) return;
SWSS_LOG_NOTICE("Generate fabric port stats");
vector<FieldValueTuple> portNamePortCounterMap;
for (auto p : m_fabricLanePortMap)
{
int lane = p.first;
sai_object_id_t port = p.second;
std::ostringstream portName;
portName << FABRIC_PORT_PREFIX << lane;
portNamePortCounterMap.emplace_back(portName.str(), sai_serialize_object_id(port));
// Install flex counters for port stats
std::unordered_set<std::string> counter_stats;
for (const auto& it: port_stat_ids)
{
counter_stats.emplace(sai_serialize_port_stat(it));
}
port_stat_manager.setCounterIdList(port, CounterType::PORT, counter_stats);
}
m_portNamePortCounterTable->set("", portNamePortCounterMap);
}
void FabricPortsOrch::generateQueueStats()
{
if (!m_fabricQueueStatEnabled) return;
if (m_isQueueStatsGenerated) return;
if (!m_getFabricPortListDone) return;
SWSS_LOG_NOTICE("Generate queue map for fabric ports");
sai_status_t status;
sai_attribute_t attr;
for (auto p : m_fabricLanePortMap)
{
int lane = p.first;
sai_object_id_t port = p.second;
// Each serdes has some pipes (queues) for unicast and multicast.
// But normally fabric serdes uses only one pipe.
attr.id = SAI_PORT_ATTR_QOS_NUMBER_OF_QUEUES;
status = sai_port_api->get_port_attribute(port, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
throw runtime_error("FabricPortsOrch get port queue number failure");
}
int num_queues = attr.value.u32;
if (num_queues > 0)
{
vector<sai_object_id_t> m_queue_ids;
m_queue_ids.resize(num_queues);
attr.id = SAI_PORT_ATTR_QOS_QUEUE_LIST;
attr.value.objlist.count = (uint32_t) num_queues;
attr.value.objlist.list = m_queue_ids.data();
status = sai_port_api->get_port_attribute(port, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
throw runtime_error("FabricPortsOrch get port queue list failure");
}
// Maintain queue map and install flex counters for queue stats
vector<FieldValueTuple> portNameQueueMap;
// Fabric serdes queue type is SAI_QUEUE_TYPE_FABRIC_TX. Since we always
// maintain only one queue for fabric serdes, m_queue_ids size is 1.
// And so, there is no need to query SAI_QUEUE_ATTR_TYPE and SAI_QUEUE_ATTR_INDEX
// for queue. Actually, SAI does not support query these attributes on fabric serdes.
int queueIndex = 0;
std::ostringstream portName;
portName << FABRIC_PORT_PREFIX << lane << ":" << queueIndex;
const auto queue = sai_serialize_object_id(m_queue_ids[queueIndex]);
portNameQueueMap.emplace_back(portName.str(), queue);
// We collect queue counters like occupancy level
std::unordered_set<string> counter_stats;
for (const auto& it: queue_stat_ids)
{
counter_stats.emplace(sai_serialize_queue_stat(it));
}
queue_stat_manager.setCounterIdList(m_queue_ids[queueIndex], CounterType::QUEUE, counter_stats);
m_portNameQueueCounterTable->set("", portNameQueueMap);
}
}
m_isQueueStatsGenerated = true;
}
void FabricPortsOrch::updateFabricPortState()
{
if (!m_getFabricPortListDone) return;
SWSS_LOG_ENTER();
sai_status_t status;
sai_attribute_t attr;
time_t now;
struct timespec time_now;
if (clock_gettime(CLOCK_MONOTONIC, &time_now) < 0)
{
return;
}
now = time_now.tv_sec;
for (auto p : m_fabricLanePortMap)
{
int lane = p.first;
sai_object_id_t port = p.second;
string key = FABRIC_PORT_PREFIX + to_string(lane);
std::vector<FieldValueTuple> values;
uint32_t remote_peer = 0;
uint32_t remote_port = 0;
attr.id = SAI_PORT_ATTR_FABRIC_ATTACHED;
status = sai_port_api->get_port_attribute(port, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
// Port may not be ready for query
SWSS_LOG_ERROR("Failed to get fabric port (%d) status, rv:%d", lane, status);
task_process_status handle_status = handleSaiGetStatus(SAI_API_PORT, status);
if (handle_status != task_process_status::task_success)
{
return;
}
}
if (m_portStatus.find(lane) != m_portStatus.end() &&
m_portStatus[lane] && !attr.value.booldata)
{
m_portDownCount[lane] ++;
m_portDownSeenLastTime[lane] = now;
}
m_portStatus[lane] = attr.value.booldata;
if (m_portStatus[lane])
{
attr.id = SAI_PORT_ATTR_FABRIC_ATTACHED_SWITCH_ID;
status = sai_port_api->get_port_attribute(port, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
task_process_status handle_status = handleSaiGetStatus(SAI_API_PORT, status);
if (handle_status != task_process_status::task_success)
{
throw runtime_error("FabricPortsOrch get remote id failure");
}
}
remote_peer = attr.value.u32;
attr.id = SAI_PORT_ATTR_FABRIC_ATTACHED_PORT_INDEX;
status = sai_port_api->get_port_attribute(port, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
task_process_status handle_status = handleSaiGetStatus(SAI_API_PORT, status);
if (handle_status != task_process_status::task_success)
{
throw runtime_error("FabricPortsOrch get remote port index failure");
}
}
remote_port = attr.value.u32;
}
values.emplace_back("STATUS", m_portStatus[lane] ? "up" : "down");
if (m_portStatus[lane])
{
values.emplace_back("REMOTE_MOD", to_string(remote_peer));
values.emplace_back("REMOTE_PORT", to_string(remote_port));
}
if (m_portDownCount[lane] > 0)
{
values.emplace_back("PORT_DOWN_COUNT", to_string(m_portDownCount[lane]));
values.emplace_back("PORT_DOWN_SEEN_LAST_TIME",
to_string(m_portDownSeenLastTime[lane]));
}
m_stateTable->set(key, values);
}
}
void FabricPortsOrch::updateFabricDebugCounters()
{
if (!m_getFabricPortListDone) return;
SWSS_LOG_ENTER();
// Get time
time_t now;
struct timespec time_now;
if (clock_gettime(CLOCK_MONOTONIC, &time_now) < 0)
{
return;
}
now = time_now.tv_sec;
int fecIsolatedPolls = FEC_ISOLATE_POLLS; // monPollThreshIsolation
int fecUnisolatePolls = FEC_UNISOLATE_POLLS; // monPollThreshRecovery
int isolationPollsCfg = ISOLATION_POLLS_CFG; // monPollThreshIsolation
int recoveryPollsCfg = RECOVERY_POLLS_CFG; // monPollThreshRecovery
int errorRateCrcCellsCfg = ERROR_RATE_CRC_CELLS_CFG; // monErrThreshCrcCells
int errorRateRxCellsCfg = ERROR_RATE_RX_CELLS_CFG; // monErrThreshRxCells
string applConstKey = FABRIC_MONITOR_DATA;
std::vector<FieldValueTuple> constValues;
SWSS_LOG_INFO("updateFabricDebugCounters");
bool setCfgVal = m_applMonitorConstTable->get("FABRIC_MONITOR_DATA", constValues);
if (!setCfgVal)
{
SWSS_LOG_INFO("applConstKey %s default values not set", applConstKey.c_str());
}
else
{
SWSS_LOG_INFO("applConstKey %s default values get set", applConstKey.c_str());
}
string configVal = "1";
for (auto cv : constValues)
{
configVal = fvValue(cv);
if (fvField(cv) == "monErrThreshCrcCells")
{
errorRateCrcCellsCfg = stoi(configVal);
SWSS_LOG_INFO("monErrThreshCrcCells: %s %s", configVal.c_str(), fvField(cv).c_str());
continue;
}
if (fvField(cv) == "monErrThreshRxCells")
{
errorRateRxCellsCfg = stoi(configVal);
SWSS_LOG_INFO("monErrThreshRxCells: %s %s", configVal.c_str(), fvField(cv).c_str());
continue;
}
if (fvField(cv) == "monPollThreshIsolation")
{
fecIsolatedPolls = stoi(configVal);
isolationPollsCfg = stoi(configVal);
SWSS_LOG_INFO("monPollThreshIsolation: %s %s", configVal.c_str(), fvField(cv).c_str());
continue;
}
if (fvField(cv) == "monPollThreshRecovery")
{
fecUnisolatePolls = stoi(configVal);
recoveryPollsCfg = stoi(configVal);
SWSS_LOG_INFO("monPollThreshRecovery: %s", configVal.c_str());
continue;
}
}
// Get debug countesrs (e.g. # of cells with crc errors, # of cells)
for (auto p : m_fabricLanePortMap)
{
int lane = p.first;
sai_object_id_t port = p.second;
string key = FABRIC_PORT_PREFIX + to_string(lane);
// so basically port is the oid
vector<FieldValueTuple> fieldValues;
static const array<string, 3> cntNames =
{
"SAI_PORT_STAT_IF_IN_ERRORS", // cells with crc errors
"SAI_PORT_STAT_IF_IN_FABRIC_DATA_UNITS", // rx data cells
"SAI_PORT_STAT_IF_IN_FEC_NOT_CORRECTABLE_FRAMES" // cell with uncorrectable errors
};
if (!m_fabricCounterTable->get(sai_serialize_object_id(port), fieldValues))
{
SWSS_LOG_INFO("no port %s", sai_serialize_object_id(port).c_str());
}
uint64_t rxCells = 0;
uint64_t crcErrors = 0;
uint64_t codeErrors = 0;
for (const auto& fv : fieldValues)
{
const auto field = fvField(fv);
const auto value = fvValue(fv);
for (size_t cnt = 0; cnt != cntNames.size(); cnt++)
{
if (field == "SAI_PORT_STAT_IF_IN_ERRORS")
{
crcErrors = stoull(value);
}
else if (field == "SAI_PORT_STAT_IF_IN_FABRIC_DATA_UNITS")
{
rxCells = stoull(value);
}
else if (field == "SAI_PORT_STAT_IF_IN_FEC_NOT_CORRECTABLE_FRAMES")
{
codeErrors = stoull(value);
}
SWSS_LOG_INFO("port %s %s %lld %lld %lld at %s",
sai_serialize_object_id(port).c_str(), field.c_str(), (long long)crcErrors,
(long long)rxCells, (long long)codeErrors, asctime(gmtime(&now)));
}
}
// now we get the values of:
// *totalNumCells *cellsWithCrcErrors *cellsWithUncorrectableErrors
//
// Check if the error rate (crcErrors/numRxCells) is greater than configured error threshold
// (errorRateCrcCellsCfg/errorRateRxCellsCfg).
// This is changing to check (crcErrors * errorRateRxCellsCfg) > (numRxCells * errorRateCrcCellsCfg)
// Default value is: (crcErrors * 61035156) > (numRxCells * 1)
// numRxCells = snmpBcmRxDataCells + snmpBcmRxControlCells
// As we don't have snmpBcmRxControlCells polled right now,
// we can use snmpBcmRxDataCells only and add snmpBcmRxControlCells later when it is getting polled.
//
// In STATE_DB, add several new attribute for each port:
// consecutivePollsWithErrors POLL_WITH_ERRORS
// consecutivePollsWithNoErrors POLL_WITH_NO_ERRORS
// consecutivePollsWithFecErrs POLL_WITH_FEC_ERRORS
// consecutivePollsWithNoFecErrs POLL_WITH_NOFEC_ERRORS
//
// skipErrorsOnLinkupCount SKIP_ERR_ON_LNKUP_CNT -- for skip all errors during boot up time
// skipCrcErrorsOnLinkupCount SKIP_CRC_ERR_ON_LNKUP_CNT
// skipFecErrorsOnLinkupCount SKIP_FEC_ERR_ON_LNKUP_CNT
// removeProblemLinkCount RM_PROBLEM_LNK_CNT -- this is for feature of remove a flaky link permanently
//
// cfgIsolated CONFIG_ISOLATED
int consecutivePollsWithErrors = 0;
int consecutivePollsWithNoErrors = 0;
int consecutivePollsWithFecErrs = 0;
int consecutivePollsWithNoFecErrs = 0;
int skipCrcErrorsOnLinkupCount = 0;
int skipFecErrorsOnLinkupCount = 0;
uint64_t prevRxCells = 0;
uint64_t prevCrcErrors = 0;
uint64_t prevCodeErrors = 0;
uint64_t testCrcErrors = 0;
uint64_t testCodeErrors = 0;
int autoIsolated = 0;
int cfgIsolated = 0;
int isolated = 0;
int origIsolated = 0;
string lnkStatus = "down";
string testState = "product";
// Get appl_db values, and update state_db later with other attributes
string applKey = APPL_FABRIC_PORT_PREFIX + to_string(lane);
std::vector<FieldValueTuple> applValues;
string applResult = "False";
bool exist = m_applTable->get(applKey, applValues);
if (!exist)
{
SWSS_LOG_NOTICE("No app infor for port %s", applKey.c_str());
}
else
{
for (auto v : applValues)
{
applResult = fvValue(v);
if (fvField(v) == "isolateStatus")
{
if (applResult == "True")
{
cfgIsolated = 1;
}
else
{
cfgIsolated = 0;
}
SWSS_LOG_INFO("Port %s isolateStatus: %s %d",
applKey.c_str(), applResult.c_str(), cfgIsolated);
}
}
}
// Get the consecutive polls from the state db
std::vector<FieldValueTuple> values;
string valuePt;
exist = m_stateTable->get(key, values);
if (!exist)
{
SWSS_LOG_INFO("No state infor for port %s", key.c_str());
return;
}
for (auto val : values)
{
valuePt = fvValue(val);
if (fvField(val) == "STATUS")
{
lnkStatus = valuePt;
continue;
}
if (fvField(val) == "POLL_WITH_ERRORS")
{
consecutivePollsWithErrors = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "POLL_WITH_NO_ERRORS")
{
consecutivePollsWithNoErrors = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "POLL_WITH_FEC_ERRORS")
{
consecutivePollsWithFecErrs = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "POLL_WITH_NOFEC_ERRORS")
{
consecutivePollsWithNoFecErrs = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "SKIP_CRC_ERR_ON_LNKUP_CNT")
{
skipCrcErrorsOnLinkupCount = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "SKIP_FEC_ERR_ON_LNKUP_CNT")
{
skipFecErrorsOnLinkupCount = to_uint<uint8_t>(valuePt);
continue;
}
if (fvField(val) == "RX_CELLS")
{
prevRxCells = to_uint<uint64_t>(valuePt);
continue;
}
if (fvField(val) == "CRC_ERRORS")
{
prevCrcErrors = to_uint<uint64_t>(valuePt);
continue;
}
if (fvField(val) == "CODE_ERRORS")
{
prevCodeErrors = to_uint<uint64_t>(valuePt);
continue;
}
if (fvField(val) == "AUTO_ISOLATED")
{
autoIsolated = to_uint<uint8_t>(valuePt);
SWSS_LOG_INFO("port %s currently autoisolated: %s", key.c_str(),valuePt.c_str());
continue;
}
if (fvField(val) == "ISOLATED")
{
origIsolated = to_uint<uint8_t>(valuePt);
SWSS_LOG_INFO("port %s currently isolated: %s", key.c_str(),valuePt.c_str());
continue;
}
if (fvField(val) == "TEST_CRC_ERRORS")
{
testCrcErrors = to_uint<uint64_t>(valuePt);
continue;
}
if (fvField(val) == "TEST_CODE_ERRORS")
{
testCodeErrors = to_uint<uint64_t>(valuePt);
continue;
}
if (fvField(val) == "TEST")
{
testState = valuePt;
continue;
}
}
// checking crc errors
int maxSkipCrcCnt = MAX_SKIP_CRCERR_ON_LNKUP_POLLS;
if (testState == "TEST"){
maxSkipCrcCnt = 2;
}
if (skipCrcErrorsOnLinkupCount < maxSkipCrcCnt)
{
skipCrcErrorsOnLinkupCount += 1;
valuePt = to_string(skipCrcErrorsOnLinkupCount);
m_stateTable->hset(key, "SKIP_CRC_ERR_ON_LNKUP_CNT", valuePt.c_str());
SWSS_LOG_INFO("port %s updates SKIP_CRC_ERR_ON_LNKUP_CNT to %s %d",
key.c_str(), valuePt.c_str(), skipCrcErrorsOnLinkupCount);
// update error counters.
prevCrcErrors = crcErrors;
}
else
{
uint64_t diffRxCells = 0;
uint64_t diffCrcCells = 0;
diffRxCells = rxCells - prevRxCells;
if (testState == "TEST"){
diffCrcCells = testCrcErrors - prevCrcErrors;
prevCrcErrors = 0;
isolationPollsCfg = isolationPollsCfg + 1;
}
else
{
diffCrcCells = crcErrors - prevCrcErrors;
prevCrcErrors = crcErrors;
}
bool isErrorRateMore =
((diffCrcCells * errorRateRxCellsCfg) >
(diffRxCells * errorRateCrcCellsCfg));
if (isErrorRateMore)
{
if (consecutivePollsWithErrors < isolationPollsCfg)
{
consecutivePollsWithErrors += 1;
consecutivePollsWithNoErrors = 0;
}
} else {
if (consecutivePollsWithNoErrors < recoveryPollsCfg)
{
consecutivePollsWithNoErrors += 1;
consecutivePollsWithErrors = 0;
}
}
SWSS_LOG_INFO("port %s diffCrcCells %lld", key.c_str(), (long long)diffCrcCells);
SWSS_LOG_INFO("consecutivePollsWithCRCErrs %d consecutivePollsWithNoCRCErrs %d",
consecutivePollsWithErrors, consecutivePollsWithNoErrors);
}
// checking FEC errors
int maxSkipFecCnt = MAX_SKIP_FECERR_ON_LNKUP_POLLS;
if (testState == "TEST"){
maxSkipFecCnt = 2;
}
if (skipFecErrorsOnLinkupCount < maxSkipFecCnt)
{
skipFecErrorsOnLinkupCount += 1;
valuePt = to_string(skipFecErrorsOnLinkupCount);
m_stateTable->hset(key, "SKIP_FEC_ERR_ON_LNKUP_CNT", valuePt.c_str());
SWSS_LOG_INFO("port %s updates SKIP_FEC_ERR_ON_LNKUP_CNT to %s",
key.c_str(), valuePt.c_str());
// update error counters
prevCodeErrors = codeErrors;
}
else
{
uint64_t diffCodeErrors = 0;
if (testState == "TEST"){
diffCodeErrors = testCodeErrors - prevCodeErrors;
prevCodeErrors = 0;
fecIsolatedPolls = fecIsolatedPolls + 1;
}
else
{
diffCodeErrors = codeErrors - prevCodeErrors;
prevCodeErrors = codeErrors;
}
SWSS_LOG_INFO("port %s diffCodeErrors %lld", key.c_str(), (long long)diffCodeErrors);
if (diffCodeErrors > 0)
{
if (consecutivePollsWithFecErrs < fecIsolatedPolls)
{
consecutivePollsWithFecErrs += 1;
consecutivePollsWithNoFecErrs = 0;
}
}
else if (diffCodeErrors <= 0)
{
if (consecutivePollsWithNoFecErrs < fecUnisolatePolls)
{
consecutivePollsWithNoFecErrs += 1;
consecutivePollsWithFecErrs = 0;
}
}
SWSS_LOG_INFO("consecutivePollsWithFecErrs %d consecutivePollsWithNoFecErrs %d",
consecutivePollsWithFecErrs,consecutivePollsWithNoFecErrs);
SWSS_LOG_INFO("fecUnisolatePolls %d", fecUnisolatePolls);
}
// take care serdes link shut state setting
if (lnkStatus == "up")
{
// debug information
SWSS_LOG_INFO("port %s status up autoIsolated %d",
key.c_str(), autoIsolated);
SWSS_LOG_INFO("consecutivePollsWithErrors %d consecutivePollsWithFecErrs %d",
consecutivePollsWithErrors, consecutivePollsWithFecErrs);
SWSS_LOG_INFO("consecutivePollsWithNoErrors %d consecutivePollsWithNoFecErrs %d",
consecutivePollsWithNoErrors, consecutivePollsWithNoFecErrs);
if (autoIsolated == 0 && (consecutivePollsWithErrors >= isolationPollsCfg
|| consecutivePollsWithFecErrs >= fecIsolatedPolls))
{
// Link needs to be isolated.
SWSS_LOG_INFO("port %s auto isolated", key.c_str());
autoIsolated = 1;
valuePt = to_string(autoIsolated);
m_stateTable->hset(key, "AUTO_ISOLATED", valuePt);
SWSS_LOG_NOTICE("port %s set AUTO_ISOLATED %s", key.c_str(), valuePt.c_str());
}
else if (autoIsolated == 1 && consecutivePollsWithNoErrors >= recoveryPollsCfg
&& consecutivePollsWithNoFecErrs >= fecUnisolatePolls)
{
// Link is isolated, but no longer needs to be.
SWSS_LOG_INFO("port %s healthy again", key.c_str());
autoIsolated = 0;
valuePt = to_string(autoIsolated);
m_stateTable->hset(key, "AUTO_ISOLATED", valuePt);
SWSS_LOG_INFO("port %s set AUTO_ISOLATED %s", key.c_str(), valuePt.c_str());
}
if (cfgIsolated == 1)
{
isolated = 1;
SWSS_LOG_INFO("port %s keep isolated due to configuation",key.c_str());
}
else
{
if (autoIsolated == 1)
{
isolated = 1;
SWSS_LOG_INFO("port %s keep isolated due to autoisolation",key.c_str());
}
else
{
isolated = 0;
SWSS_LOG_INFO("port %s unisolated",key.c_str());
}
}
// if "ISOLATED" is true, Call SAI api here to actually isolated the link
// if "ISOLATED" is false, Call SAP api to actually unisolate the link
if (origIsolated != isolated)
{
sai_attribute_t attr;
attr.id = SAI_PORT_ATTR_FABRIC_ISOLATE;
bool setVal = false;
if (isolated == 1)
{
setVal = true;
}
attr.value.booldata = setVal;
SWSS_LOG_NOTICE("Set fabric port %d with isolate %d ", lane, isolated);
if (m_fabricLanePortMap.find(lane) == m_fabricLanePortMap.end())
{
SWSS_LOG_NOTICE("NOT find fabric lane %d ", lane);
}
else
{
sai_status_t status = sai_port_api->set_port_attribute(m_fabricLanePortMap[lane], &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set admin status");
}
SWSS_LOG_NOTICE("Set fabric port %d state done %d ", lane, isolated);
}
}
else
{
SWSS_LOG_INFO( "Same isolation status for %d", lane);
}
}
else
{
SWSS_LOG_INFO("link down");
}
// Update state_db with new data
valuePt = to_string(consecutivePollsWithErrors);
m_stateTable->hset(key, "POLL_WITH_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set POLL_WITH_ERRORS %s", key.c_str(), valuePt.c_str());
valuePt = to_string(consecutivePollsWithNoErrors);
m_stateTable->hset(key, "POLL_WITH_NO_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set POLL_WITH_NO_ERRORS %s", key.c_str(), valuePt.c_str());
valuePt = to_string(consecutivePollsWithFecErrs);
m_stateTable->hset(key, "POLL_WITH_FEC_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set POLL_WITH_FEC_ERRORS %s", key.c_str(), valuePt.c_str());
valuePt = to_string(consecutivePollsWithNoFecErrs);
m_stateTable->hset(key, "POLL_WITH_NOFEC_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set POLL_WITH_NOFEC_ERRORS %s",
key.c_str(), valuePt.c_str());
valuePt = to_string(rxCells);
m_stateTable->hset(key, "RX_CELLS", valuePt.c_str());
SWSS_LOG_INFO("port %s set RX_CELLS %s",
key.c_str(), valuePt.c_str());
valuePt = to_string(prevCrcErrors);
m_stateTable->hset(key, "CRC_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set CRC_ERRORS %s",
key.c_str(), valuePt.c_str());
valuePt = to_string(prevCodeErrors);
m_stateTable->hset(key, "CODE_ERRORS", valuePt.c_str());
SWSS_LOG_INFO("port %s set CODE_ERRORS %s",
key.c_str(), valuePt.c_str());
valuePt = to_string(cfgIsolated);
m_stateTable->hset(key, "CONFIG_ISOLATED", valuePt.c_str());
SWSS_LOG_INFO("port %s set CONFIG_ISOLATED %s",
key.c_str(), valuePt.c_str());
valuePt = to_string(isolated);
m_stateTable->hset(key, "ISOLATED", valuePt.c_str());
SWSS_LOG_INFO("port %s set ISOLATED %s",
key.c_str(), valuePt.c_str());
}
}
void FabricPortsOrch::updateFabricCapacity()
{
// Init value for fabric capacity monitoring
int capacity = 0;
int downCapacity = 0;
string lnkStatus = "down";
string configIsolated = "0";
string isolated = "0";
string autoIsolated = "0";
int operating_links = 0;
int total_links = 0;
int threshold = 100;
std::vector<FieldValueTuple> constValues;
string applKey = FABRIC_MONITOR_DATA;
// Get capacity warning threshold from APPL_DB table FABRIC_MONITOR_DATA
// By default, this threshold is 100 (percentage).
bool cfgVal = m_applMonitorConstTable->get("FABRIC_MONITOR_DATA", constValues);
if(!cfgVal)
{
SWSS_LOG_INFO("%s default values not set", applKey.c_str());
}
else
{
SWSS_LOG_INFO("%s has default values", applKey.c_str());
}
string configVal = "1";
for (auto cv : constValues)
{
configVal = fvValue(cv);
if (fvField(cv) == "monCapacityThreshWarn")
{
threshold = stoi(configVal);
SWSS_LOG_INFO("monCapacityThreshWarn: %s %s", configVal.c_str(), fvField(cv).c_str());
continue;
}
}
// Check fabric capacity.
SWSS_LOG_INFO("FabricPortsOrch::updateFabricCapacity start");
for (auto p : m_fabricLanePortMap)
{
int lane = p.first;
string key = FABRIC_PORT_PREFIX + to_string(lane);
std::vector<FieldValueTuple> values;
string valuePt;
// Get fabric serdes link status from STATE_DB
bool exist = m_stateTable->get(key, values);
if (!exist)
{
SWSS_LOG_INFO("No state infor for port %s", key.c_str());
return;
}
for (auto val : values)
{
valuePt = fvValue(val);
if (fvField(val) == "STATUS")
{
lnkStatus = valuePt;
continue;
}
if (fvField(val) == "CONFIG_ISOLATED")
{
configIsolated = valuePt;
continue;
}
if (fvField(val) == "ISOLATED")
{
isolated = valuePt;
continue;
}
if (fvField(val) == "AUTO_ISOLATED")
{
autoIsolated = valuePt;
continue;
}
}
// Calculate total number of serdes link, number of operational links,
// total fabric capacity.
bool linkIssue = false;
if (configIsolated == "1" || isolated == "1" || autoIsolated == "1")
{
linkIssue = true;
}
if (lnkStatus == "down" || linkIssue == true)
{
downCapacity += FABRIC_LINK_RATE;
}
else
{
capacity += FABRIC_LINK_RATE;
operating_links += 1;