-
Notifications
You must be signed in to change notification settings - Fork 547
/
Copy pathcrmorch.cpp
699 lines (616 loc) · 26.3 KB
/
crmorch.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
#include <sstream>
#include <inttypes.h>
#include "crmorch.h"
#include "converter.h"
#include "timer.h"
#define CRM_POLLING_INTERVAL "polling_interval"
#define CRM_COUNTERS_TABLE_KEY "STATS"
#define CRM_POLLING_INTERVAL_DEFAULT (5 * 60)
#define CRM_THRESHOLD_TYPE_DEFAULT CrmThresholdType::CRM_PERCENTAGE
#define CRM_THRESHOLD_LOW_DEFAULT 70
#define CRM_THRESHOLD_HIGH_DEFAULT 85
#define CRM_EXCEEDED_MSG_MAX 10
#define CRM_ACL_RESOURCE_COUNT 256
extern sai_object_id_t gSwitchId;
extern sai_switch_api_t *sai_switch_api;
extern sai_acl_api_t *sai_acl_api;
using namespace std;
using namespace swss;
const map<CrmResourceType, string> crmResTypeNameMap =
{
{ CrmResourceType::CRM_IPV4_ROUTE, "IPV4_ROUTE" },
{ CrmResourceType::CRM_IPV6_ROUTE, "IPV6_ROUTE" },
{ CrmResourceType::CRM_IPV4_NEXTHOP, "IPV4_NEXTHOP" },
{ CrmResourceType::CRM_IPV6_NEXTHOP, "IPV6_NEXTHOP" },
{ CrmResourceType::CRM_IPV4_NEIGHBOR, "IPV4_NEIGHBOR" },
{ CrmResourceType::CRM_IPV6_NEIGHBOR, "IPV6_NEIGHBOR" },
{ CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER, "NEXTHOP_GROUP_MEMBER" },
{ CrmResourceType::CRM_NEXTHOP_GROUP, "NEXTHOP_GROUP" },
{ CrmResourceType::CRM_ACL_TABLE, "ACL_TABLE" },
{ CrmResourceType::CRM_ACL_GROUP, "ACL_GROUP" },
{ CrmResourceType::CRM_ACL_ENTRY, "ACL_ENTRY" },
{ CrmResourceType::CRM_ACL_COUNTER, "ACL_COUNTER" },
{ CrmResourceType::CRM_FDB_ENTRY, "FDB_ENTRY" },
{ CrmResourceType::CRM_IPMC_ENTRY, "IPMC_ENTRY" },
{ CrmResourceType::CRM_SNAT_ENTRY, "SNAT_ENTRY" },
{ CrmResourceType::CRM_DNAT_ENTRY, "DNAT_ENTRY" }
};
const map<CrmResourceType, uint32_t> crmResSaiAvailAttrMap =
{
{ CrmResourceType::CRM_IPV4_ROUTE, SAI_SWITCH_ATTR_AVAILABLE_IPV4_ROUTE_ENTRY },
{ CrmResourceType::CRM_IPV6_ROUTE, SAI_SWITCH_ATTR_AVAILABLE_IPV6_ROUTE_ENTRY },
{ CrmResourceType::CRM_IPV4_NEXTHOP, SAI_SWITCH_ATTR_AVAILABLE_IPV4_NEXTHOP_ENTRY },
{ CrmResourceType::CRM_IPV6_NEXTHOP, SAI_SWITCH_ATTR_AVAILABLE_IPV6_NEXTHOP_ENTRY },
{ CrmResourceType::CRM_IPV4_NEIGHBOR, SAI_SWITCH_ATTR_AVAILABLE_IPV4_NEIGHBOR_ENTRY },
{ CrmResourceType::CRM_IPV6_NEIGHBOR, SAI_SWITCH_ATTR_AVAILABLE_IPV6_NEIGHBOR_ENTRY },
{ CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER, SAI_SWITCH_ATTR_AVAILABLE_NEXT_HOP_GROUP_MEMBER_ENTRY },
{ CrmResourceType::CRM_NEXTHOP_GROUP, SAI_SWITCH_ATTR_AVAILABLE_NEXT_HOP_GROUP_ENTRY },
{ CrmResourceType::CRM_ACL_TABLE, SAI_SWITCH_ATTR_AVAILABLE_ACL_TABLE },
{ CrmResourceType::CRM_ACL_GROUP, SAI_SWITCH_ATTR_AVAILABLE_ACL_TABLE_GROUP },
{ CrmResourceType::CRM_ACL_ENTRY, SAI_ACL_TABLE_ATTR_AVAILABLE_ACL_ENTRY },
{ CrmResourceType::CRM_ACL_COUNTER, SAI_ACL_TABLE_ATTR_AVAILABLE_ACL_COUNTER },
{ CrmResourceType::CRM_FDB_ENTRY, SAI_SWITCH_ATTR_AVAILABLE_FDB_ENTRY },
{ CrmResourceType::CRM_IPMC_ENTRY, SAI_SWITCH_ATTR_AVAILABLE_IPMC_ENTRY},
{ CrmResourceType::CRM_SNAT_ENTRY, SAI_SWITCH_ATTR_AVAILABLE_SNAT_ENTRY },
{ CrmResourceType::CRM_DNAT_ENTRY, SAI_SWITCH_ATTR_AVAILABLE_DNAT_ENTRY }
};
const map<string, CrmResourceType> crmThreshTypeResMap =
{
{ "ipv4_route_threshold_type", CrmResourceType::CRM_IPV4_ROUTE },
{ "ipv6_route_threshold_type", CrmResourceType::CRM_IPV6_ROUTE },
{ "ipv4_nexthop_threshold_type", CrmResourceType::CRM_IPV4_NEXTHOP },
{ "ipv6_nexthop_threshold_type", CrmResourceType::CRM_IPV6_NEXTHOP },
{ "ipv4_neighbor_threshold_type", CrmResourceType::CRM_IPV4_NEIGHBOR },
{ "ipv6_neighbor_threshold_type", CrmResourceType::CRM_IPV6_NEIGHBOR },
{ "nexthop_group_member_threshold_type", CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER },
{ "nexthop_group_threshold_type", CrmResourceType::CRM_NEXTHOP_GROUP },
{ "acl_table_threshold_type", CrmResourceType::CRM_ACL_TABLE },
{ "acl_group_threshold_type", CrmResourceType::CRM_ACL_GROUP },
{ "acl_entry_threshold_type", CrmResourceType::CRM_ACL_ENTRY },
{ "acl_counter_threshold_type", CrmResourceType::CRM_ACL_COUNTER },
{ "fdb_entry_threshold_type", CrmResourceType::CRM_FDB_ENTRY },
{ "ipmc_entry_threshold_type", CrmResourceType::CRM_IPMC_ENTRY },
{ "snat_entry_threshold_type", CrmResourceType::CRM_SNAT_ENTRY },
{ "dnat_entry_threshold_type", CrmResourceType::CRM_DNAT_ENTRY }
};
const map<string, CrmResourceType> crmThreshLowResMap =
{
{"ipv4_route_low_threshold", CrmResourceType::CRM_IPV4_ROUTE },
{"ipv6_route_low_threshold", CrmResourceType::CRM_IPV6_ROUTE },
{"ipv4_nexthop_low_threshold", CrmResourceType::CRM_IPV4_NEXTHOP },
{"ipv6_nexthop_low_threshold", CrmResourceType::CRM_IPV6_NEXTHOP },
{"ipv4_neighbor_low_threshold", CrmResourceType::CRM_IPV4_NEIGHBOR },
{"ipv6_neighbor_low_threshold", CrmResourceType::CRM_IPV6_NEIGHBOR },
{"nexthop_group_member_low_threshold", CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER },
{"nexthop_group_low_threshold", CrmResourceType::CRM_NEXTHOP_GROUP },
{"acl_table_low_threshold", CrmResourceType::CRM_ACL_TABLE },
{"acl_group_low_threshold", CrmResourceType::CRM_ACL_GROUP },
{"acl_entry_low_threshold", CrmResourceType::CRM_ACL_ENTRY },
{"acl_counter_low_threshold", CrmResourceType::CRM_ACL_COUNTER },
{"fdb_entry_low_threshold", CrmResourceType::CRM_FDB_ENTRY },
{"ipmc_entry_low_threshold", CrmResourceType::CRM_IPMC_ENTRY },
{"snat_entry_low_threshold", CrmResourceType::CRM_SNAT_ENTRY },
{"dnat_entry_low_threshold", CrmResourceType::CRM_DNAT_ENTRY }
};
const map<string, CrmResourceType> crmThreshHighResMap =
{
{"ipv4_route_high_threshold", CrmResourceType::CRM_IPV4_ROUTE },
{"ipv6_route_high_threshold", CrmResourceType::CRM_IPV6_ROUTE },
{"ipv4_nexthop_high_threshold", CrmResourceType::CRM_IPV4_NEXTHOP },
{"ipv6_nexthop_high_threshold", CrmResourceType::CRM_IPV6_NEXTHOP },
{"ipv4_neighbor_high_threshold", CrmResourceType::CRM_IPV4_NEIGHBOR },
{"ipv6_neighbor_high_threshold", CrmResourceType::CRM_IPV6_NEIGHBOR },
{"nexthop_group_member_high_threshold", CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER },
{"nexthop_group_high_threshold", CrmResourceType::CRM_NEXTHOP_GROUP },
{"acl_table_high_threshold", CrmResourceType::CRM_ACL_TABLE },
{"acl_group_high_threshold", CrmResourceType::CRM_ACL_GROUP },
{"acl_entry_high_threshold", CrmResourceType::CRM_ACL_ENTRY },
{"acl_counter_high_threshold", CrmResourceType::CRM_ACL_COUNTER },
{"fdb_entry_high_threshold", CrmResourceType::CRM_FDB_ENTRY },
{"ipmc_entry_high_threshold", CrmResourceType::CRM_IPMC_ENTRY },
{"snat_entry_high_threshold", CrmResourceType::CRM_SNAT_ENTRY },
{"dnat_entry_high_threshold", CrmResourceType::CRM_DNAT_ENTRY }
};
const map<string, CrmThresholdType> crmThreshTypeMap =
{
{ "percentage", CrmThresholdType::CRM_PERCENTAGE },
{ "used", CrmThresholdType::CRM_USED },
{ "free", CrmThresholdType::CRM_FREE }
};
const map<string, CrmResourceType> crmAvailCntsTableMap =
{
{ "crm_stats_ipv4_route_available", CrmResourceType::CRM_IPV4_ROUTE },
{ "crm_stats_ipv6_route_available", CrmResourceType::CRM_IPV6_ROUTE },
{ "crm_stats_ipv4_nexthop_available", CrmResourceType::CRM_IPV4_NEXTHOP },
{ "crm_stats_ipv6_nexthop_available", CrmResourceType::CRM_IPV6_NEXTHOP },
{ "crm_stats_ipv4_neighbor_available", CrmResourceType::CRM_IPV4_NEIGHBOR },
{ "crm_stats_ipv6_neighbor_available", CrmResourceType::CRM_IPV6_NEIGHBOR },
{ "crm_stats_nexthop_group_member_available", CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER },
{ "crm_stats_nexthop_group_available", CrmResourceType::CRM_NEXTHOP_GROUP },
{ "crm_stats_acl_table_available", CrmResourceType::CRM_ACL_TABLE },
{ "crm_stats_acl_group_available", CrmResourceType::CRM_ACL_GROUP },
{ "crm_stats_acl_entry_available", CrmResourceType::CRM_ACL_ENTRY },
{ "crm_stats_acl_counter_available", CrmResourceType::CRM_ACL_COUNTER },
{ "crm_stats_fdb_entry_available", CrmResourceType::CRM_FDB_ENTRY },
{ "crm_stats_ipmc_entry_available", CrmResourceType::CRM_IPMC_ENTRY },
{ "crm_stats_snat_entry_available", CrmResourceType::CRM_SNAT_ENTRY },
{ "crm_stats_dnat_entry_available", CrmResourceType::CRM_DNAT_ENTRY }
};
const map<string, CrmResourceType> crmUsedCntsTableMap =
{
{ "crm_stats_ipv4_route_used", CrmResourceType::CRM_IPV4_ROUTE },
{ "crm_stats_ipv6_route_used", CrmResourceType::CRM_IPV6_ROUTE },
{ "crm_stats_ipv4_nexthop_used", CrmResourceType::CRM_IPV4_NEXTHOP },
{ "crm_stats_ipv6_nexthop_used", CrmResourceType::CRM_IPV6_NEXTHOP },
{ "crm_stats_ipv4_neighbor_used", CrmResourceType::CRM_IPV4_NEIGHBOR },
{ "crm_stats_ipv6_neighbor_used", CrmResourceType::CRM_IPV6_NEIGHBOR },
{ "crm_stats_nexthop_group_member_used", CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER },
{ "crm_stats_nexthop_group_used", CrmResourceType::CRM_NEXTHOP_GROUP },
{ "crm_stats_acl_table_used", CrmResourceType::CRM_ACL_TABLE },
{ "crm_stats_acl_group_used", CrmResourceType::CRM_ACL_GROUP },
{ "crm_stats_acl_entry_used", CrmResourceType::CRM_ACL_ENTRY },
{ "crm_stats_acl_counter_used", CrmResourceType::CRM_ACL_COUNTER },
{ "crm_stats_fdb_entry_used", CrmResourceType::CRM_FDB_ENTRY },
{ "crm_stats_ipmc_entry_used", CrmResourceType::CRM_IPMC_ENTRY },
{ "crm_stats_snat_entry_used", CrmResourceType::CRM_SNAT_ENTRY },
{ "crm_stats_dnat_entry_used", CrmResourceType::CRM_DNAT_ENTRY }
};
CrmOrch::CrmOrch(DBConnector *db, string tableName):
Orch(db, tableName),
m_countersDb(new DBConnector("COUNTERS_DB", 0)),
m_countersCrmTable(new Table(m_countersDb.get(), COUNTERS_CRM_TABLE)),
m_timer(new SelectableTimer(timespec { .tv_sec = CRM_POLLING_INTERVAL_DEFAULT, .tv_nsec = 0 }))
{
SWSS_LOG_ENTER();
m_pollingInterval = chrono::seconds(CRM_POLLING_INTERVAL_DEFAULT);
for (const auto &res : crmResTypeNameMap)
{
m_resourcesMap.emplace(res.first, CrmResourceEntry(res.second, CRM_THRESHOLD_TYPE_DEFAULT, CRM_THRESHOLD_LOW_DEFAULT, CRM_THRESHOLD_HIGH_DEFAULT));
}
// The CRM stats needs to be populated again
m_countersCrmTable->del(CRM_COUNTERS_TABLE_KEY);
// Note: ExecutableTimer will hold m_timer pointer and release the object later
auto executor = new ExecutableTimer(m_timer, this, "CRM_COUNTERS_POLL");
Orch::addExecutor(executor);
m_timer->start();
}
CrmOrch::CrmResourceEntry::CrmResourceEntry(string name, CrmThresholdType thresholdType, uint32_t lowThreshold, uint32_t highThreshold):
name(name),
thresholdType(thresholdType),
lowThreshold(lowThreshold),
highThreshold(highThreshold)
{
if ((thresholdType == CrmThresholdType::CRM_PERCENTAGE) && ((lowThreshold > 100) || (highThreshold > 100)))
{
throw runtime_error("CRM percentage threshold value must be <= 100%%");
}
if (!(lowThreshold < highThreshold))
{
throw runtime_error("CRM low threshold must be less then high threshold");
}
}
void CrmOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
string table_name = consumer.getTableName();
if (table_name != CFG_CRM_TABLE_NAME)
{
SWSS_LOG_ERROR("Invalid table %s", table_name.c_str());
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
string key = kfvKey(t);
string op = kfvOp(t);
if (op == SET_COMMAND)
{
handleSetCommand(key, kfvFieldsValues(t));
}
else if (op == DEL_COMMAND)
{
SWSS_LOG_ERROR("Unsupported operation type %s\n", op.c_str());
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s\n", op.c_str());
}
consumer.m_toSync.erase(it++);
}
}
void CrmOrch::handleSetCommand(const string& key, const vector<FieldValueTuple>& data)
{
SWSS_LOG_ENTER();
for (auto i : data)
{
const auto &field = fvField(i);
const auto &value = fvValue(i);
try
{
if (field == CRM_POLLING_INTERVAL)
{
m_pollingInterval = chrono::seconds(to_uint<uint32_t>(value));
auto interv = timespec { .tv_sec = (time_t)m_pollingInterval.count(), .tv_nsec = 0 };
m_timer->setInterval(interv);
m_timer->reset();
}
else if (crmThreshTypeResMap.find(field) != crmThreshTypeResMap.end())
{
auto resourceType = crmThreshTypeResMap.at(field);
auto thresholdType = crmThreshTypeMap.at(value);
m_resourcesMap.at(resourceType).thresholdType = thresholdType;
}
else if (crmThreshLowResMap.find(field) != crmThreshLowResMap.end())
{
auto resourceType = crmThreshLowResMap.at(field);
auto thresholdValue = to_uint<uint32_t>(value);
m_resourcesMap.at(resourceType).lowThreshold = thresholdValue;
}
else if (crmThreshHighResMap.find(field) != crmThreshHighResMap.end())
{
auto resourceType = crmThreshHighResMap.at(field);
auto thresholdValue = to_uint<uint32_t>(value);
m_resourcesMap.at(resourceType).highThreshold = thresholdValue;
}
else
{
SWSS_LOG_ERROR("Failed to parse CRM %s configuration. Unknown attribute %s.\n", key.c_str(), field.c_str());
}
}
catch (const exception& e)
{
SWSS_LOG_ERROR("Failed to parse CRM %s attribute %s error: %s.", key.c_str(), field.c_str(), e.what());
return;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to parse CRM %s attribute %s. Unknown error has been occurred", key.c_str(), field.c_str());
return;
}
}
}
void CrmOrch::incCrmResUsedCounter(CrmResourceType resource)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[CRM_COUNTERS_TABLE_KEY].usedCounter++;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to increment \"used\" counter for the %s CRM resource.", crmResTypeNameMap.at(resource).c_str());
return;
}
}
void CrmOrch::decCrmResUsedCounter(CrmResourceType resource)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[CRM_COUNTERS_TABLE_KEY].usedCounter--;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to decrement \"used\" counter for the %s CRM resource.", crmResTypeNameMap.at(resource).c_str());
return;
}
}
void CrmOrch::incCrmAclUsedCounter(CrmResourceType resource, sai_acl_stage_t stage, sai_acl_bind_point_type_t point)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[getCrmAclKey(stage, point)].usedCounter++;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to increment \"used\" counter for the %s CRM resource.", crmResTypeNameMap.at(resource).c_str());
return;
}
}
void CrmOrch::decCrmAclUsedCounter(CrmResourceType resource, sai_acl_stage_t stage, sai_acl_bind_point_type_t point, sai_object_id_t oid)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[getCrmAclKey(stage, point)].usedCounter--;
// remove acl_entry and acl_counter in this acl table
if (resource == CrmResourceType::CRM_ACL_TABLE)
{
for (auto &resourcesMap : m_resourcesMap)
{
if ((resourcesMap.first == (CrmResourceType::CRM_ACL_ENTRY))
|| (resourcesMap.first == (CrmResourceType::CRM_ACL_COUNTER)))
{
auto &cntMap = resourcesMap.second.countersMap;
for (auto it = cntMap.begin(); it != cntMap.end(); ++it)
{
if (it->second.id == oid)
{
cntMap.erase(it);
break;
}
}
}
}
// remove ACL_TABLE_STATS in crm database
m_countersCrmTable->del(getCrmAclTableKey(oid));
}
}
catch (...)
{
SWSS_LOG_ERROR("Failed to decrement \"used\" counter for the %s CRM resource.", crmResTypeNameMap.at(resource).c_str());
return;
}
}
void CrmOrch::incCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_t tableId)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[getCrmAclTableKey(tableId)].usedCounter++;
m_resourcesMap.at(resource).countersMap[getCrmAclTableKey(tableId)].id = tableId;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to increment \"used\" counter for the %s CRM resource (tableId:%" PRIx64 ").", crmResTypeNameMap.at(resource).c_str(), tableId);
return;
}
}
void CrmOrch::decCrmAclTableUsedCounter(CrmResourceType resource, sai_object_id_t tableId)
{
SWSS_LOG_ENTER();
try
{
m_resourcesMap.at(resource).countersMap[getCrmAclTableKey(tableId)].usedCounter--;
}
catch (...)
{
SWSS_LOG_ERROR("Failed to decrement \"used\" counter for the %s CRM resource (tableId:%" PRIx64 ").", crmResTypeNameMap.at(resource).c_str(), tableId);
return;
}
}
void CrmOrch::doTask(SelectableTimer &timer)
{
SWSS_LOG_ENTER();
getResAvailableCounters();
updateCrmCountersTable();
checkCrmThresholds();
}
void CrmOrch::getResAvailableCounters()
{
SWSS_LOG_ENTER();
for (auto &res : m_resourcesMap)
{
// ignore unsupported resources
if (res.second.resStatus != CrmResourceStatus::CRM_RES_SUPPORTED)
{
continue;
}
sai_attribute_t attr;
attr.id = crmResSaiAvailAttrMap.at(res.first);
switch (attr.id)
{
case SAI_SWITCH_ATTR_AVAILABLE_IPV4_ROUTE_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPV6_ROUTE_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPV4_NEXTHOP_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPV6_NEXTHOP_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPV4_NEIGHBOR_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPV6_NEIGHBOR_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_NEXT_HOP_GROUP_MEMBER_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_NEXT_HOP_GROUP_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_FDB_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_IPMC_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_SNAT_ENTRY:
case SAI_SWITCH_ATTR_AVAILABLE_DNAT_ENTRY:
{
sai_status_t status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
if((status == SAI_STATUS_NOT_SUPPORTED) ||
(status == SAI_STATUS_NOT_IMPLEMENTED) ||
SAI_STATUS_IS_ATTR_NOT_SUPPORTED(status) ||
SAI_STATUS_IS_ATTR_NOT_IMPLEMENTED(status))
{
// mark unsupported resources
res.second.resStatus = CrmResourceStatus::CRM_RES_NOT_SUPPORTED;
SWSS_LOG_NOTICE("Switch attribute %u not supported", attr.id);
break;
}
SWSS_LOG_ERROR("Failed to get switch attribute %u , rv:%d", attr.id, status);
task_process_status handle_status = handleSaiGetStatus(SAI_API_SWITCH, status);
if (handle_status != task_process_status::task_success)
{
break;
}
}
res.second.countersMap[CRM_COUNTERS_TABLE_KEY].availableCounter = attr.value.u32;
break;
}
case SAI_SWITCH_ATTR_AVAILABLE_ACL_TABLE:
case SAI_SWITCH_ATTR_AVAILABLE_ACL_TABLE_GROUP:
{
vector<sai_acl_resource_t> resources(CRM_ACL_RESOURCE_COUNT);
attr.value.aclresource.count = CRM_ACL_RESOURCE_COUNT;
attr.value.aclresource.list = resources.data();
sai_status_t status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status == SAI_STATUS_BUFFER_OVERFLOW)
{
resources.resize(attr.value.aclresource.count);
attr.value.aclresource.list = resources.data();
status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
}
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get switch attribute %u , rv:%d", attr.id, status);
task_process_status handle_status = handleSaiGetStatus(SAI_API_SWITCH, status);
if (handle_status != task_process_status::task_success)
{
break;
}
}
for (uint32_t i = 0; i < attr.value.aclresource.count; i++)
{
string key = getCrmAclKey(attr.value.aclresource.list[i].stage, attr.value.aclresource.list[i].bind_point);
res.second.countersMap[key].availableCounter = attr.value.aclresource.list[i].avail_num;
}
break;
}
case SAI_ACL_TABLE_ATTR_AVAILABLE_ACL_ENTRY:
case SAI_ACL_TABLE_ATTR_AVAILABLE_ACL_COUNTER:
{
for (auto &cnt : res.second.countersMap)
{
sai_status_t status = sai_acl_api->get_acl_table_attribute(cnt.second.id, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get ACL table attribute %u , rv:%d", attr.id, status);
break;
}
cnt.second.availableCounter = attr.value.u32;
}
break;
}
default:
SWSS_LOG_ERROR("Failed to get CRM attribute %u. Unknown attribute.\n", attr.id);
return;
}
}
}
void CrmOrch::updateCrmCountersTable()
{
SWSS_LOG_ENTER();
// Update CRM used counters in COUNTERS_DB
for (const auto &i : crmUsedCntsTableMap)
{
try
{
for (const auto &cnt : m_resourcesMap.at(i.second).countersMap)
{
FieldValueTuple attr(i.first, to_string(cnt.second.usedCounter));
vector<FieldValueTuple> attrs = { attr };
m_countersCrmTable->set(cnt.first, attrs);
}
}
catch(const out_of_range &e)
{
// expected when a resource is unavailable
}
}
// Update CRM available counters in COUNTERS_DB
for (const auto &i : crmAvailCntsTableMap)
{
try
{
for (const auto &cnt : m_resourcesMap.at(i.second).countersMap)
{
FieldValueTuple attr(i.first, to_string(cnt.second.availableCounter));
vector<FieldValueTuple> attrs = { attr };
m_countersCrmTable->set(cnt.first, attrs);
}
}
catch(const out_of_range &e)
{
// expected when a resource is unavailable
}
}
}
void CrmOrch::checkCrmThresholds()
{
SWSS_LOG_ENTER();
for (auto &i : m_resourcesMap)
{
auto &res = i.second;
for (const auto &j : i.second.countersMap)
{
auto &cnt = j.second;
uint64_t utilization = 0;
uint32_t percentageUtil = 0;
string threshType = "";
if (cnt.usedCounter != 0)
{
uint32_t dvsr = cnt.usedCounter + cnt.availableCounter;
if (dvsr != 0)
{
percentageUtil = (cnt.usedCounter * 100) / dvsr;
}
else
{
SWSS_LOG_WARN("%s Exception occurred (div by Zero): Used count %u free count %u",
res.name.c_str(), cnt.usedCounter, cnt.availableCounter);
}
}
switch (res.thresholdType)
{
case CrmThresholdType::CRM_PERCENTAGE:
utilization = percentageUtil;
threshType = "TH_PERCENTAGE";
break;
case CrmThresholdType::CRM_USED:
utilization = cnt.usedCounter;
threshType = "TH_USED";
break;
case CrmThresholdType::CRM_FREE:
utilization = cnt.availableCounter;
threshType = "TH_FREE";
break;
default:
throw runtime_error("Unknown threshold type for CRM resource");
}
if ((utilization >= res.highThreshold) && (res.exceededLogCounter < CRM_EXCEEDED_MSG_MAX))
{
SWSS_LOG_WARN("%s THRESHOLD_EXCEEDED for %s %u%% Used count %u free count %u",
res.name.c_str(), threshType.c_str(), percentageUtil, cnt.usedCounter, cnt.availableCounter);
res.exceededLogCounter++;
}
else if ((utilization <= res.lowThreshold) && (res.exceededLogCounter > 0))
{
SWSS_LOG_WARN("%s THRESHOLD_CLEAR for %s %u%% Used count %u free count %u",
res.name.c_str(), threshType.c_str(), percentageUtil, cnt.usedCounter, cnt.availableCounter);
res.exceededLogCounter = 0;
}
} // end of counters loop
} // end of resources loop
}
string CrmOrch::getCrmAclKey(sai_acl_stage_t stage, sai_acl_bind_point_type_t bindPoint)
{
string key = "ACL_STATS";
switch(stage)
{
case SAI_ACL_STAGE_INGRESS:
key += ":INGRESS";
break;
case SAI_ACL_STAGE_EGRESS:
key += ":EGRESS";
break;
default:
return "";
}
switch(bindPoint)
{
case SAI_ACL_BIND_POINT_TYPE_PORT:
key += ":PORT";
break;
case SAI_ACL_BIND_POINT_TYPE_LAG:
key += ":LAG";
break;
case SAI_ACL_BIND_POINT_TYPE_VLAN:
key += ":VLAN";
break;
case SAI_ACL_BIND_POINT_TYPE_ROUTER_INTERFACE:
key += ":RIF";
break;
case SAI_ACL_BIND_POINT_TYPE_SWITCH:
key += ":SWITCH";
break;
default:
return "";
}
return key;
}
string CrmOrch::getCrmAclTableKey(sai_object_id_t id)
{
std::stringstream ss;
ss << "ACL_TABLE_STATS:" << "0x" << std::hex << id;
return ss.str();
}