forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferorch.cpp
More file actions
814 lines (752 loc) · 31.7 KB
/
bufferorch.cpp
File metadata and controls
814 lines (752 loc) · 31.7 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
#include "tokenize.h"
#include "bufferorch.h"
#include "logger.h"
#include "sai_serialize.h"
#include <sstream>
#include <iostream>
using namespace std;
extern sai_port_api_t *sai_port_api;
extern sai_queue_api_t *sai_queue_api;
extern sai_switch_api_t *sai_switch_api;
extern sai_buffer_api_t *sai_buffer_api;
extern PortsOrch *gPortsOrch;
extern sai_object_id_t gSwitchId;
#define BUFFER_POOL_WATERMARK_FLEX_STAT_COUNTER_POLL_MSECS "10000"
static const vector<sai_buffer_pool_stat_t> bufferPoolWatermarkStatIds =
{
SAI_BUFFER_POOL_STAT_WATERMARK_BYTES,
};
type_map BufferOrch::m_buffer_type_maps = {
{CFG_BUFFER_POOL_TABLE_NAME, new object_map()},
{CFG_BUFFER_PROFILE_TABLE_NAME, new object_map()},
{CFG_BUFFER_QUEUE_TABLE_NAME, new object_map()},
{CFG_BUFFER_PG_TABLE_NAME, new object_map()},
{CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME, new object_map()},
{CFG_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME, new object_map()}
};
BufferOrch::BufferOrch(DBConnector *db, vector<string> &tableNames) :
Orch(db, tableNames),
m_flexCounterDb(new DBConnector(FLEX_COUNTER_DB, DBConnector::DEFAULT_UNIXSOCKET, 0)),
m_flexCounterTable(new ProducerTable(m_flexCounterDb.get(), FLEX_COUNTER_TABLE)),
m_flexCounterGroupTable(new ProducerTable(m_flexCounterDb.get(), FLEX_COUNTER_GROUP_TABLE)),
m_countersDb(new DBConnector(COUNTERS_DB, DBConnector::DEFAULT_UNIXSOCKET, 0)),
m_countersDbRedisClient(m_countersDb.get())
{
SWSS_LOG_ENTER();
initTableHandlers();
initBufferReadyLists(db);
initFlexCounterGroupTable();
};
void BufferOrch::initTableHandlers()
{
SWSS_LOG_ENTER();
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_POOL_TABLE_NAME, &BufferOrch::processBufferPool));
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_PROFILE_TABLE_NAME, &BufferOrch::processBufferProfile));
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_QUEUE_TABLE_NAME, &BufferOrch::processQueue));
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_PG_TABLE_NAME, &BufferOrch::processPriorityGroup));
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME, &BufferOrch::processIngressBufferProfileList));
m_bufferHandlerMap.insert(buffer_handler_pair(CFG_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME, &BufferOrch::processEgressBufferProfileList));
}
void BufferOrch::initBufferReadyLists(DBConnector *db)
{
SWSS_LOG_ENTER();
Table pg_table(db, CFG_BUFFER_PG_TABLE_NAME);
initBufferReadyList(pg_table);
Table queue_table(db, CFG_BUFFER_QUEUE_TABLE_NAME);
initBufferReadyList(queue_table);
}
void BufferOrch::initBufferReadyList(Table& table)
{
SWSS_LOG_ENTER();
std::vector<std::string> keys;
table.getKeys(keys);
// populate the lists with buffer configuration information
for (const auto& key: keys)
{
m_ready_list[key] = false;
auto tokens = tokenize(key, config_db_key_delimiter);
if (tokens.size() != 2)
{
SWSS_LOG_ERROR("Wrong format of a table '%s' key '%s'. Skip it", table.getTableName().c_str(), key.c_str());
continue;
}
auto port_names = tokenize(tokens[0], list_item_delimiter);
for(const auto& port_name: port_names)
{
m_port_ready_list_ref[port_name].push_back(key);
}
}
}
void BufferOrch::initFlexCounterGroupTable(void)
{
string bufferPoolWmPluginName = "watermark_bufferpool.lua";
try
{
string bufferPoolLuaScript = swss::loadLuaScript(bufferPoolWmPluginName);
string bufferPoolWmSha = swss::loadRedisScript(m_countersDb.get(), bufferPoolLuaScript);
vector<FieldValueTuple> fvTuples;
fvTuples.emplace_back(BUFFER_POOL_PLUGIN_FIELD, bufferPoolWmSha);
fvTuples.emplace_back(POLL_INTERVAL_FIELD, BUFFER_POOL_WATERMARK_FLEX_STAT_COUNTER_POLL_MSECS);
string statsMode = STATS_MODE_READ_AND_CLEAR;
// Some platforms do not support buffer pool watermark clear operation
// Need the SAI API support to query the capability
// Before this capability is in place, we feed the platform info into the orchagent
// daemon and check against it
char *device = getenv("device");
if (device && (strstr(device, "7050") || strstr(device, "6000")))
{
statsMode = STATS_MODE_READ;
}
fvTuples.emplace_back(STATS_MODE_FIELD, statsMode);
m_flexCounterGroupTable->set(BUFFER_POOL_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP, fvTuples);
}
catch (...)
{
SWSS_LOG_WARN("Buffer pool watermark lua script and/or flex counter group not set successfully");
}
}
bool BufferOrch::isPortReady(const std::string& port_name) const
{
SWSS_LOG_ENTER();
const auto it = m_port_ready_list_ref.find(port_name);
if (it == m_port_ready_list_ref.cend())
{
// we got a port name which wasn't in our gPortsOrch->getAllPorts() list
// so make the port ready, because we don't have any buffer configuration for it
return true;
}
const auto& list_of_keys = it->second;
bool result = true;
for (const auto& key: list_of_keys)
{
result = result && m_ready_list.at(key);
}
return result;
}
void BufferOrch::generateBufferPoolWatermarkCounterIdList(void)
{
// This function will be called in FlexCounterOrch when field:value tuple "FLEX_COUNTER_STATUS":"enable"
// is received on buffer pool watermark key under table "FLEX_COUNTER_GROUP_TABLE"
// Because the SubscriberStateTable listens to the entire keyspace of "BUFFER_POOL_WATERMARK", any update
// to field value tuples under key "BUFFER_POOL_WATERMARK" will cause this tuple to be heard again
// To avoid resync the coutner ID list a second time, we introduce a data member variable to mark whether
// this operation has already been done or not yet
if (m_isBufferPoolWatermarkCounterIdListGenerated)
{
return;
}
// Detokenize the SAI watermark stats to a string, separated by comma
string statList;
for (const auto &it : bufferPoolWatermarkStatIds)
{
statList += (sai_serialize_buffer_pool_stat(it) + list_item_delimiter);
}
if (!statList.empty())
{
statList.pop_back();
}
vector<FieldValueTuple> fvTuples;
fvTuples.emplace_back(BUFFER_POOL_COUNTER_ID_LIST, statList);
// Push buffer pool watermark COUNTER_ID_LIST to FLEX_COUNTER_TABLE on a per buffer pool basis
for (const auto &it : *(m_buffer_type_maps[CFG_BUFFER_POOL_TABLE_NAME]))
{
string key = BUFFER_POOL_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP ":" + sai_serialize_object_id(it.second);
m_flexCounterTable->set(key, fvTuples);
}
m_isBufferPoolWatermarkCounterIdListGenerated = true;
}
const object_map &BufferOrch::getBufferPoolNameOidMap(void)
{
// In the case different Orches are running in
// different threads, caller may need to grab a read lock
// before calling this function
return *m_buffer_type_maps[CFG_BUFFER_POOL_TABLE_NAME];
}
task_process_status BufferOrch::processBufferPool(Consumer &consumer)
{
SWSS_LOG_ENTER();
sai_status_t sai_status;
sai_object_id_t sai_object = SAI_NULL_OBJECT_ID;
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
string map_type_name = consumer.getTableName();
string object_name = kfvKey(tuple);
string op = kfvOp(tuple);
SWSS_LOG_DEBUG("object name:%s", object_name.c_str());
if (m_buffer_type_maps[map_type_name]->find(object_name) != m_buffer_type_maps[map_type_name]->end())
{
sai_object = (*(m_buffer_type_maps[map_type_name]))[object_name];
SWSS_LOG_DEBUG("found existing object:%s of type:%s", object_name.c_str(), map_type_name.c_str());
}
SWSS_LOG_DEBUG("processing command:%s", op.c_str());
if (op == SET_COMMAND)
{
vector<sai_attribute_t> attribs;
for (auto i = kfvFieldsValues(tuple).begin(); i != kfvFieldsValues(tuple).end(); i++)
{
string field = fvField(*i);
string value = fvValue(*i);
SWSS_LOG_DEBUG("field:%s, value:%s", field.c_str(), value.c_str());
sai_attribute_t attr;
if (field == buffer_size_field_name)
{
attr.id = SAI_BUFFER_POOL_ATTR_SIZE;
attr.value.u32 = (uint32_t)stoul(value);
attribs.push_back(attr);
}
else if (field == buffer_pool_type_field_name)
{
string type = value;
if (type == buffer_value_ingress)
{
attr.value.u32 = SAI_BUFFER_POOL_TYPE_INGRESS;
}
else if (type == buffer_value_egress)
{
attr.value.u32 = SAI_BUFFER_POOL_TYPE_EGRESS;
}
else
{
SWSS_LOG_ERROR("Unknown pool type specified:%s", type.c_str());
return task_process_status::task_invalid_entry;
}
attr.id = SAI_BUFFER_POOL_ATTR_TYPE;
attribs.push_back(attr);
}
else if (field == buffer_pool_mode_field_name)
{
string mode = value;
if (mode == buffer_pool_mode_dynamic_value)
{
attr.value.u32 = SAI_BUFFER_POOL_THRESHOLD_MODE_DYNAMIC;
}
else if (mode == buffer_pool_mode_static_value)
{
attr.value.u32 = SAI_BUFFER_POOL_THRESHOLD_MODE_STATIC;
}
else
{
SWSS_LOG_ERROR("Unknown pool mode specified:%s", mode.c_str());
return task_process_status::task_invalid_entry;
}
attr.id = SAI_BUFFER_POOL_ATTR_THRESHOLD_MODE;
attribs.push_back(attr);
}
else if (field == buffer_pool_xoff_field_name)
{
attr.value.u32 = (uint32_t)stoul(value);
attr.id = SAI_BUFFER_POOL_ATTR_XOFF_SIZE;
attribs.push_back(attr);
}
else
{
SWSS_LOG_ERROR("Unknown pool field specified:%s, ignoring", field.c_str());
continue;
}
}
if (SAI_NULL_OBJECT_ID != sai_object)
{
sai_status = sai_buffer_api->set_buffer_pool_attribute(sai_object, &attribs[0]);
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to modify buffer pool, name:%s, sai object:%lx, status:%d", object_name.c_str(), sai_object, sai_status);
return task_process_status::task_failed;
}
SWSS_LOG_DEBUG("Modified existing pool:%lx, type:%s name:%s ", sai_object, map_type_name.c_str(), object_name.c_str());
}
else
{
sai_status = sai_buffer_api->create_buffer_pool(&sai_object, gSwitchId, (uint32_t)attribs.size(), attribs.data());
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to create buffer pool %s with type %s, rv:%d", object_name.c_str(), map_type_name.c_str(), sai_status);
return task_process_status::task_failed;
}
(*(m_buffer_type_maps[map_type_name]))[object_name] = sai_object;
SWSS_LOG_NOTICE("Created buffer pool %s with type %s", object_name.c_str(), map_type_name.c_str());
// Here we take the PFC watchdog approach to update the COUNTERS_DB metadata (e.g., PFC_WD_DETECTION_TIME per queue)
// at initialization (creation and registration phase)
// Specifically, we push the buffer pool name to oid mapping upon the creation of the oid
// In pg and queue case, this mapping installment is deferred to FlexCounterOrch at a reception of field
// "FLEX_COUNTER_STATUS"
m_countersDbRedisClient.hset(COUNTERS_BUFFER_POOL_NAME_MAP, object_name, sai_serialize_object_id(sai_object));
}
}
else if (op == DEL_COMMAND)
{
sai_status = sai_buffer_api->remove_buffer_pool(sai_object);
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to remove buffer pool %s with type %s, rv:%d", object_name.c_str(), map_type_name.c_str(), sai_status);
return task_process_status::task_failed;
}
SWSS_LOG_NOTICE("Removed buffer pool %s with type %s", object_name.c_str(), map_type_name.c_str());
auto it_to_delete = (m_buffer_type_maps[map_type_name])->find(object_name);
(m_buffer_type_maps[map_type_name])->erase(it_to_delete);
m_countersDbRedisClient.hdel(COUNTERS_BUFFER_POOL_NAME_MAP, object_name);
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s", op.c_str());
return task_process_status::task_invalid_entry;
}
return task_process_status::task_success;
}
task_process_status BufferOrch::processBufferProfile(Consumer &consumer)
{
SWSS_LOG_ENTER();
sai_status_t sai_status;
sai_object_id_t sai_object = SAI_NULL_OBJECT_ID;
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
string map_type_name = consumer.getTableName();
string object_name = kfvKey(tuple);
string op = kfvOp(tuple);
SWSS_LOG_DEBUG("object name:%s", object_name.c_str());
if (m_buffer_type_maps[map_type_name]->find(object_name) != m_buffer_type_maps[map_type_name]->end())
{
sai_object = (*(m_buffer_type_maps[map_type_name]))[object_name];
SWSS_LOG_DEBUG("found existing object:%s of type:%s", object_name.c_str(), map_type_name.c_str());
}
SWSS_LOG_DEBUG("processing command:%s", op.c_str());
if (op == SET_COMMAND)
{
vector<sai_attribute_t> attribs;
for (auto i = kfvFieldsValues(tuple).begin(); i != kfvFieldsValues(tuple).end(); i++)
{
string field = fvField(*i);
string value = fvValue(*i);
SWSS_LOG_DEBUG("field:%s, value:%s", field.c_str(), value.c_str());
sai_attribute_t attr;
if (field == buffer_pool_field_name)
{
sai_object_id_t sai_pool;
ref_resolve_status resolve_result = resolveFieldRefValue(m_buffer_type_maps, buffer_pool_field_name, tuple, sai_pool);
if (ref_resolve_status::success != resolve_result)
{
if(ref_resolve_status::not_resolved == resolve_result)
{
SWSS_LOG_INFO("Missing or invalid pool reference specified");
return task_process_status::task_need_retry;
}
SWSS_LOG_ERROR("Resolving pool reference failed");
return task_process_status::task_failed;
}
attr.id = SAI_BUFFER_PROFILE_ATTR_POOL_ID;
attr.value.oid = sai_pool;
attribs.push_back(attr);
}
else if (field == buffer_xon_field_name)
{
attr.value.u32 = (uint32_t)stoul(value);
attr.id = SAI_BUFFER_PROFILE_ATTR_XON_TH;
attribs.push_back(attr);
}
else if (field == buffer_xon_offset_field_name)
{
attr.value.u32 = (uint32_t)stoul(value);
attr.id = SAI_BUFFER_PROFILE_ATTR_XON_OFFSET_TH;
attribs.push_back(attr);
}
else if (field == buffer_xoff_field_name)
{
attr.value.u32 = (uint32_t)stoul(value);
attr.id = SAI_BUFFER_PROFILE_ATTR_XOFF_TH;
attribs.push_back(attr);
}
else if (field == buffer_size_field_name)
{
attr.id = SAI_BUFFER_PROFILE_ATTR_BUFFER_SIZE;
attr.value.u32 = (uint32_t)stoul(value);
attribs.push_back(attr);
}
else if (field == buffer_dynamic_th_field_name)
{
attr.id = SAI_BUFFER_PROFILE_ATTR_THRESHOLD_MODE;
attr.value.s32 = SAI_BUFFER_PROFILE_THRESHOLD_MODE_DYNAMIC;
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_SHARED_DYNAMIC_TH;
attr.value.s8 = (sai_int8_t)stol(value);
attribs.push_back(attr);
}
else if (field == buffer_static_th_field_name)
{
attr.id = SAI_BUFFER_PROFILE_ATTR_THRESHOLD_MODE;
attr.value.s32 = SAI_BUFFER_PROFILE_THRESHOLD_MODE_STATIC;
attribs.push_back(attr);
attr.id = SAI_BUFFER_PROFILE_ATTR_SHARED_STATIC_TH;
attr.value.u32 = (uint32_t)stoul(value);
attribs.push_back(attr);
}
else
{
SWSS_LOG_ERROR("Unknown buffer profile field specified:%s, ignoring", field.c_str());
continue;
}
}
if (SAI_NULL_OBJECT_ID != sai_object)
{
SWSS_LOG_DEBUG("Modifying existing sai object:%lx ", sai_object);
sai_status = sai_buffer_api->set_buffer_profile_attribute(sai_object, &attribs[0]);
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to modify buffer profile, name:%s, sai object:%lx, status:%d", object_name.c_str(), sai_object, sai_status);
return task_process_status::task_failed;
}
}
else
{
sai_status = sai_buffer_api->create_buffer_profile(&sai_object, gSwitchId, (uint32_t)attribs.size(), attribs.data());
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to create buffer profile %s with type %s, rv:%d", object_name.c_str(), map_type_name.c_str(), sai_status);
return task_process_status::task_failed;
}
(*(m_buffer_type_maps[map_type_name]))[object_name] = sai_object;
SWSS_LOG_NOTICE("Created buffer profile %s with type %s", object_name.c_str(), map_type_name.c_str());
}
}
else if (op == DEL_COMMAND)
{
sai_status = sai_buffer_api->remove_buffer_profile(sai_object);
if (SAI_STATUS_SUCCESS != sai_status)
{
SWSS_LOG_ERROR("Failed to remove buffer profile %s with type %s, rv:%d", object_name.c_str(), map_type_name.c_str(), sai_status);
return task_process_status::task_failed;
}
SWSS_LOG_NOTICE("Remove buffer profile %s with type %s", object_name.c_str(), map_type_name.c_str());
auto it_to_delete = (m_buffer_type_maps[map_type_name])->find(object_name);
(m_buffer_type_maps[map_type_name])->erase(it_to_delete);
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s", op.c_str());
return task_process_status::task_invalid_entry;
}
return task_process_status::task_success;
}
/*
Input sample "BUFFER_QUEUE|Ethernet4,Ethernet45|10-15"
*/
task_process_status BufferOrch::processQueue(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
sai_object_id_t sai_buffer_profile;
const string key = kfvKey(tuple);
string op = kfvOp(tuple);
vector<string> tokens;
sai_uint32_t range_low, range_high;
SWSS_LOG_DEBUG("Processing:%s", key.c_str());
tokens = tokenize(key, config_db_key_delimiter);
if (tokens.size() != 2)
{
SWSS_LOG_ERROR("malformed key:%s. Must contain 2 tokens", key.c_str());
return task_process_status::task_invalid_entry;
}
vector<string> port_names = tokenize(tokens[0], list_item_delimiter);
if (!parseIndexRange(tokens[1], range_low, range_high))
{
return task_process_status::task_invalid_entry;
}
ref_resolve_status resolve_result = resolveFieldRefValue(m_buffer_type_maps, buffer_profile_field_name, tuple, sai_buffer_profile);
if (ref_resolve_status::success != resolve_result)
{
if(ref_resolve_status::not_resolved == resolve_result)
{
SWSS_LOG_INFO("Missing or invalid queue buffer profile reference specified");
return task_process_status::task_need_retry;
}
SWSS_LOG_ERROR("Resolving queue profile reference failed");
return task_process_status::task_failed;
}
sai_attribute_t attr;
attr.id = SAI_QUEUE_ATTR_BUFFER_PROFILE_ID;
attr.value.oid = sai_buffer_profile;
for (string port_name : port_names)
{
Port port;
SWSS_LOG_DEBUG("processing port:%s", port_name.c_str());
if (!gPortsOrch->getPort(port_name, port))
{
SWSS_LOG_ERROR("Port with alias:%s not found", port_name.c_str());
return task_process_status::task_invalid_entry;
}
for (size_t ind = range_low; ind <= range_high; ind++)
{
sai_object_id_t queue_id;
SWSS_LOG_DEBUG("processing queue:%zd", ind);
if (port.m_queue_ids.size() <= ind)
{
SWSS_LOG_ERROR("Invalid queue index specified:%zd", ind);
return task_process_status::task_invalid_entry;
}
queue_id = port.m_queue_ids[ind];
SWSS_LOG_DEBUG("Applying buffer profile:0x%lx to queue index:%zd, queue sai_id:0x%lx", sai_buffer_profile, ind, queue_id);
sai_status_t sai_status = sai_queue_api->set_queue_attribute(queue_id, &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set queue's buffer profile attribute, status:%d", sai_status);
return task_process_status::task_failed;
}
}
}
if (m_ready_list.find(key) != m_ready_list.end())
{
m_ready_list[key] = true;
}
else
{
SWSS_LOG_ERROR("Queue profile '%s' was inserted after BufferOrch init", key.c_str());
}
return task_process_status::task_success;
}
/*
Input sample "BUFFER_PG_TABLE|Ethernet4,Ethernet45|10-15"
*/
task_process_status BufferOrch::processPriorityGroup(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
sai_object_id_t sai_buffer_profile;
const string key = kfvKey(tuple);
string op = kfvOp(tuple);
vector<string> tokens;
sai_uint32_t range_low, range_high;
if (op != SET_COMMAND)
{
return task_process_status::task_success;
}
SWSS_LOG_DEBUG("processing:%s", key.c_str());
tokens = tokenize(key, config_db_key_delimiter);
if (tokens.size() != 2)
{
SWSS_LOG_ERROR("malformed key:%s. Must contain 2 tokens", key.c_str());
return task_process_status::task_invalid_entry;
}
vector<string> port_names = tokenize(tokens[0], list_item_delimiter);
if (!parseIndexRange(tokens[1], range_low, range_high))
{
SWSS_LOG_ERROR("Failed to obtain pg range values");
return task_process_status::task_invalid_entry;
}
ref_resolve_status resolve_result = resolveFieldRefValue(m_buffer_type_maps, buffer_profile_field_name, tuple, sai_buffer_profile);
if (ref_resolve_status::success != resolve_result)
{
if(ref_resolve_status::not_resolved == resolve_result)
{
SWSS_LOG_INFO("Missing or invalid pg profile reference specified");
return task_process_status::task_need_retry;
}
SWSS_LOG_ERROR("Resolving pg profile reference failed");
return task_process_status::task_failed;
}
sai_attribute_t attr;
attr.id = SAI_INGRESS_PRIORITY_GROUP_ATTR_BUFFER_PROFILE;
attr.value.oid = sai_buffer_profile;
for (string port_name : port_names)
{
Port port;
SWSS_LOG_DEBUG("processing port:%s", port_name.c_str());
if (!gPortsOrch->getPort(port_name, port))
{
SWSS_LOG_ERROR("Port with alias:%s not found", port_name.c_str());
return task_process_status::task_invalid_entry;
}
for (size_t ind = range_low; ind <= range_high; ind++)
{
sai_object_id_t pg_id;
SWSS_LOG_DEBUG("processing pg:%zd", ind);
if (port.m_priority_group_ids.size() <= ind)
{
SWSS_LOG_ERROR("Invalid pg index specified:%zd", ind);
return task_process_status::task_invalid_entry;
}
pg_id = port.m_priority_group_ids[ind];
SWSS_LOG_DEBUG("Applying buffer profile:0x%lx to port:%s pg index:%zd, pg sai_id:0x%lx", sai_buffer_profile, port_name.c_str(), ind, pg_id);
sai_status_t sai_status = sai_buffer_api->set_ingress_priority_group_attribute(pg_id, &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set port:%s pg:%zd buffer profile attribute, status:%d", port_name.c_str(), ind, sai_status);
return task_process_status::task_failed;
}
}
}
if (m_ready_list.find(key) != m_ready_list.end())
{
m_ready_list[key] = true;
}
else
{
SWSS_LOG_ERROR("PG profile '%s' was inserted after BufferOrch init", key.c_str());
}
return task_process_status::task_success;
}
/*
Input sample:"[BUFFER_PROFILE_TABLE:i_port.profile0],[BUFFER_PROFILE_TABLE:i_port.profile1]"
*/
task_process_status BufferOrch::processIngressBufferProfileList(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
Port port;
string key = kfvKey(tuple);
string op = kfvOp(tuple);
SWSS_LOG_DEBUG("processing:%s", key.c_str());
if (consumer.getTableName() != CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME)
{
SWSS_LOG_ERROR("Key with invalid table type passed in %s, expected:%s", key.c_str(), CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME);
return task_process_status::task_invalid_entry;
}
vector<string> port_names = tokenize(key, list_item_delimiter);
vector<sai_object_id_t> profile_list;
ref_resolve_status resolve_status = resolveFieldRefArray(m_buffer_type_maps, buffer_profile_list_field_name, tuple, profile_list);
if (ref_resolve_status::success != resolve_status)
{
if(ref_resolve_status::not_resolved == resolve_status)
{
SWSS_LOG_INFO("Missing or invalid ingress buffer profile reference specified for:%s", key.c_str());
return task_process_status::task_need_retry;
}
SWSS_LOG_ERROR("Failed resolving ingress buffer profile reference specified for:%s", key.c_str());
return task_process_status::task_failed;
}
sai_attribute_t attr;
attr.id = SAI_PORT_ATTR_QOS_INGRESS_BUFFER_PROFILE_LIST;
attr.value.objlist.count = (uint32_t)profile_list.size();
attr.value.objlist.list = profile_list.data();
for (string port_name : port_names)
{
if (!gPortsOrch->getPort(port_name, port))
{
SWSS_LOG_ERROR("Port with alias:%s not found", port_name.c_str());
return task_process_status::task_invalid_entry;
}
sai_status_t sai_status = sai_port_api->set_port_attribute(port.m_port_id, &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set ingress buffer profile list on port, status:%d, key:%s", sai_status, port_name.c_str());
return task_process_status::task_failed;
}
}
return task_process_status::task_success;
}
/*
Input sample:"[BUFFER_PROFILE_TABLE:e_port.profile0],[BUFFER_PROFILE_TABLE:e_port.profile1]"
*/
task_process_status BufferOrch::processEgressBufferProfileList(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
Port port;
string key = kfvKey(tuple);
string op = kfvOp(tuple);
SWSS_LOG_DEBUG("processing:%s", key.c_str());
vector<string> port_names = tokenize(key, list_item_delimiter);
vector<sai_object_id_t> profile_list;
ref_resolve_status resolve_status = resolveFieldRefArray(m_buffer_type_maps, buffer_profile_list_field_name, tuple, profile_list);
if (ref_resolve_status::success != resolve_status)
{
if(ref_resolve_status::not_resolved == resolve_status)
{
SWSS_LOG_INFO("Missing or invalid egress buffer profile reference specified for:%s", key.c_str());
return task_process_status::task_need_retry;
}
SWSS_LOG_ERROR("Failed resolving egress buffer profile reference specified for:%s", key.c_str());
return task_process_status::task_failed;
}
sai_attribute_t attr;
attr.id = SAI_PORT_ATTR_QOS_EGRESS_BUFFER_PROFILE_LIST;
attr.value.objlist.count = (uint32_t)profile_list.size();
attr.value.objlist.list = profile_list.data();
for (string port_name : port_names)
{
if (!gPortsOrch->getPort(port_name, port))
{
SWSS_LOG_ERROR("Port with alias:%s not found", port_name.c_str());
return task_process_status::task_invalid_entry;
}
sai_status_t sai_status = sai_port_api->set_port_attribute(port.m_port_id, &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set egress buffer profile list on port, status:%d, key:%s", sai_status, port_name.c_str());
return task_process_status::task_failed;
}
}
return task_process_status::task_success;
}
void BufferOrch::doTask()
{
// The hidden dependency tree:
// ref: https://github.com/opencomputeproject/SAI/blob/master/doc/QOS/SAI-Proposal-buffers-Ver4.docx
// 2 SAI model
// 3.1 Ingress priority group (PG) configuration
// 3.2.1 Buffer profile configuration
//
// buffer pool
// └── buffer profile
// ├── buffer port ingress profile list
// ├── buffer port egress profile list
// ├── buffer queue
// └── buffer pq table
auto pool_consumer = getExecutor((CFG_BUFFER_POOL_TABLE_NAME));
pool_consumer->drain();
auto profile_consumer = getExecutor(CFG_BUFFER_PROFILE_TABLE_NAME);
profile_consumer->drain();
for(auto &it : m_consumerMap)
{
auto consumer = it.second.get();
if (consumer == profile_consumer)
continue;
if (consumer == pool_consumer)
continue;
consumer->drain();
}
}
void BufferOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->isInitDone())
{
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
/* Make sure the handler is initialized for the task */
auto map_type_name = consumer.getTableName();
if (m_bufferHandlerMap.find(map_type_name) == m_bufferHandlerMap.end())
{
SWSS_LOG_ERROR("No handler for key:%s found.", map_type_name.c_str());
it = consumer.m_toSync.erase(it);
continue;
}
auto task_status = (this->*(m_bufferHandlerMap[map_type_name]))(consumer);
switch(task_status)
{
case task_process_status::task_success :
it = consumer.m_toSync.erase(it);
break;
case task_process_status::task_invalid_entry:
SWSS_LOG_ERROR("Failed to process invalid buffer task");
it = consumer.m_toSync.erase(it);
break;
case task_process_status::task_failed:
SWSS_LOG_ERROR("Failed to process buffer task, drop it");
it = consumer.m_toSync.erase(it);
return;
case task_process_status::task_need_retry:
SWSS_LOG_INFO("Failed to process buffer task, retry it");
it++;
break;
default:
SWSS_LOG_ERROR("Invalid task status %d", task_status);
it = consumer.m_toSync.erase(it);
break;
}
}
}