-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathRedisRemoteSaiInterface.cpp
More file actions
2154 lines (1567 loc) · 67.7 KB
/
RedisRemoteSaiInterface.cpp
File metadata and controls
2154 lines (1567 loc) · 67.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
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 "RedisRemoteSaiInterface.h"
#include "Utils.h"
#include "Recorder.h"
#include "VirtualObjectIdManager.h"
#include "SkipRecordAttrContainer.h"
#include "SwitchContainer.h"
#include "ZeroMQChannel.h"
#include "sairediscommon.h"
#include "meta/NotificationFactory.h"
#include "meta/sai_serialize.h"
#include "meta/SaiAttributeList.h"
#include "meta/PerformanceIntervalTimer.h"
#include "meta/Globals.h"
#include "config.h"
#include <inttypes.h>
using namespace sairedis;
using namespace saimeta;
using namespace sairediscommon;
using namespace std::placeholders;
std::vector<swss::FieldValueTuple> serialize_counter_id_list(
_In_ const sai_enum_metadata_t *stats_enum,
_In_ uint32_t count,
_In_ const sai_stat_id_t *counter_id_list);
RedisRemoteSaiInterface::RedisRemoteSaiInterface(
_In_ std::shared_ptr<ContextConfig> contextConfig,
_In_ std::function<sai_switch_notifications_t(std::shared_ptr<Notification>)> notificationCallback,
_In_ std::shared_ptr<Recorder> recorder):
m_contextConfig(contextConfig),
m_redisCommunicationMode(SAI_REDIS_COMMUNICATION_MODE_REDIS_ASYNC),
m_recorder(recorder),
m_notificationCallback(notificationCallback)
{
SWSS_LOG_ENTER();
SWSS_LOG_NOTICE("sairedis git revision %s, SAI git revision: %s", SAIREDIS_GIT_REVISION, SAI_GIT_REVISION);
m_initialized = false;
initialize(0, nullptr);
}
RedisRemoteSaiInterface::~RedisRemoteSaiInterface()
{
SWSS_LOG_ENTER();
if (m_initialized)
{
uninitialize();
}
}
sai_status_t RedisRemoteSaiInterface::initialize(
_In_ uint64_t flags,
_In_ const sai_service_method_table_t *service_method_table)
{
SWSS_LOG_ENTER();
if (m_initialized)
{
SWSS_LOG_ERROR("already initialized");
return SAI_STATUS_FAILURE;
}
m_skipRecordAttrContainer = std::make_shared<SkipRecordAttrContainer>();
m_asicInitViewMode = false; // default mode is apply mode
m_useTempView = false;
m_syncMode = false;
m_redisCommunicationMode = SAI_REDIS_COMMUNICATION_MODE_REDIS_ASYNC;
if (m_contextConfig->m_zmqEnable)
{
m_communicationChannel = std::make_shared<ZeroMQChannel>(
m_contextConfig->m_zmqEndpoint,
m_contextConfig->m_zmqNtfEndpoint,
std::bind(&RedisRemoteSaiInterface::handleNotification, this, _1, _2, _3));
SWSS_LOG_NOTICE("zmq enabled, forcing sync mode");
m_syncMode = true;
}
else
{
m_communicationChannel = std::make_shared<RedisChannel>(
m_contextConfig->m_dbAsic,
std::bind(&RedisRemoteSaiInterface::handleNotification, this, _1, _2, _3));
}
m_responseTimeoutMs = m_communicationChannel->getResponseTimeout();
m_db = std::make_shared<swss::DBConnector>(m_contextConfig->m_dbAsic, 0);
m_redisVidIndexGenerator = std::make_shared<RedisVidIndexGenerator>(m_db, REDIS_KEY_VIDCOUNTER);
clear_local_state();
// TODO what will happen when we receive notification in init view mode ?
m_initialized = true;
return SAI_STATUS_SUCCESS;
}
sai_status_t RedisRemoteSaiInterface::uninitialize(void)
{
SWSS_LOG_ENTER();
SWSS_LOG_NOTICE("begin");
if (!m_initialized)
{
SWSS_LOG_ERROR("not initialized");
return SAI_STATUS_FAILURE;
}
m_communicationChannel = nullptr; // will stop thread
// clear local state after stopping threads
clear_local_state();
m_initialized = false;
SWSS_LOG_NOTICE("end");
return SAI_STATUS_SUCCESS;
}
sai_status_t RedisRemoteSaiInterface::create(
_In_ sai_object_type_t objectType,
_Out_ sai_object_id_t* objectId,
_In_ sai_object_id_t switchId,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
*objectId = SAI_NULL_OBJECT_ID;
if (objectType == SAI_OBJECT_TYPE_SWITCH)
{
// for given hardware info we always return same switch id,
// this is required since we could be performing warm boot here
auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list);
if (hwinfo.size())
{
m_recorder->recordComment("SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO=" + hwinfo);
}
switchId = m_virtualObjectIdManager->allocateNewSwitchObjectId(hwinfo);
*objectId = switchId;
if (switchId == SAI_NULL_OBJECT_ID)
{
SWSS_LOG_ERROR("switch ID allocation failed");
return SAI_STATUS_FAILURE;
}
auto *attr = sai_metadata_get_attr_by_id(
SAI_SWITCH_ATTR_INIT_SWITCH,
attr_count,
attr_list);
if (attr && attr->value.booldata == false)
{
if (m_switchContainer->contains(*objectId))
{
SWSS_LOG_NOTICE("switch container already contains switch %s",
sai_serialize_object_id(*objectId).c_str());
return SAI_STATUS_SUCCESS;
}
refreshTableDump();
if (m_tableDump.find(switchId) == m_tableDump.end())
{
SWSS_LOG_ERROR("failed to find switch %s to connect (init=false)",
sai_serialize_object_id(switchId).c_str());
m_virtualObjectIdManager->releaseObjectId(switchId);
return SAI_STATUS_FAILURE;
}
// when init is false, don't send query to syncd, just return success
// that we found switch and we connected to it
auto sw = std::make_shared<Switch>(*objectId, attr_count, attr_list);
m_switchContainer->insert(sw);
return SAI_STATUS_SUCCESS;
}
}
else
{
*objectId = m_virtualObjectIdManager->allocateNewObjectId(objectType, switchId);
}
if (*objectId == SAI_NULL_OBJECT_ID)
{
SWSS_LOG_ERROR("failed to create %s, with switch id: %s",
sai_serialize_object_type(objectType).c_str(),
sai_serialize_object_id(switchId).c_str());
return SAI_STATUS_INSUFFICIENT_RESOURCES;
}
// NOTE: objectId was allocated by the caller
auto status = create(
objectType,
sai_serialize_object_id(*objectId),
attr_count,
attr_list);
if (objectType == SAI_OBJECT_TYPE_SWITCH && status == SAI_STATUS_SUCCESS)
{
/*
* When doing CREATE operation user may want to update notification
* pointers, since notifications can be defined per switch we need to
* update them.
*
* TODO: should be moved inside to redis_generic_create
*/
auto sw = std::make_shared<Switch>(*objectId, attr_count, attr_list);
m_switchContainer->insert(sw);
}
else if (status != SAI_STATUS_SUCCESS)
{
// if create failed, then release allocated object
m_virtualObjectIdManager->releaseObjectId(*objectId);
}
return status;
}
sai_status_t RedisRemoteSaiInterface::remove(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId)
{
SWSS_LOG_ENTER();
auto status = remove(
objectType,
sai_serialize_object_id(objectId));
if (objectType == SAI_OBJECT_TYPE_SWITCH && status == SAI_STATUS_SUCCESS)
{
SWSS_LOG_NOTICE("removing switch id %s", sai_serialize_object_id(objectId).c_str());
m_virtualObjectIdManager->releaseObjectId(objectId);
// remove switch from container
m_switchContainer->removeSwitch(objectId);
}
return status;
}
sai_status_t RedisRemoteSaiInterface::setRedisExtensionAttribute(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const sai_attribute_t *attr)
{
SWSS_LOG_ENTER();
if (attr == nullptr)
{
SWSS_LOG_ERROR("attr pointer is null");
return SAI_STATUS_FAILURE;
}
/*
* NOTE: that this will work without
* switch being created.
*/
switch (attr->id)
{
case SAI_REDIS_SWITCH_ATTR_PERFORM_LOG_ROTATE:
if (m_recorder)
{
m_recorder->requestLogRotate();
}
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_RECORD:
if (m_recorder)
{
m_recorder->enableRecording(attr->value.booldata);
}
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_NOTIFY_SYNCD:
return sai_redis_notify_syncd(objectId, attr);
case SAI_REDIS_SWITCH_ATTR_USE_TEMP_VIEW:
m_useTempView = attr->value.booldata;
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_RECORD_STATS:
m_recorder->recordStats(attr->value.booldata);
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_SYNC_OPERATION_RESPONSE_TIMEOUT:
m_responseTimeoutMs = attr->value.u64;
m_communicationChannel->setResponseTimeout(m_responseTimeoutMs);
SWSS_LOG_NOTICE("set response timeout to %lu ms", m_responseTimeoutMs);
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_SYNC_MODE:
SWSS_LOG_WARN("sync mode is depreacated, use communication mode");
m_syncMode = attr->value.booldata;
if (m_contextConfig->m_zmqEnable)
{
SWSS_LOG_NOTICE("zmq enabled, forcing sync mode");
m_syncMode = true;
}
if (m_syncMode)
{
SWSS_LOG_NOTICE("disabling buffered pipeline in sync mode");
m_communicationChannel->setBuffered(false);
}
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_REDIS_COMMUNICATION_MODE:
m_redisCommunicationMode = (sai_redis_communication_mode_t)attr->value.s32;
if (m_contextConfig->m_zmqEnable)
{
SWSS_LOG_NOTICE("zmq enabled via context config");
m_redisCommunicationMode = SAI_REDIS_COMMUNICATION_MODE_ZMQ_SYNC;
}
m_communicationChannel = nullptr;
switch (m_redisCommunicationMode)
{
case SAI_REDIS_COMMUNICATION_MODE_REDIS_ASYNC:
SWSS_LOG_NOTICE("enabling redis async mode");
m_syncMode = false;
m_communicationChannel = std::make_shared<RedisChannel>(
m_contextConfig->m_dbAsic,
std::bind(&RedisRemoteSaiInterface::handleNotification, this, _1, _2, _3));
m_communicationChannel->setResponseTimeout(m_responseTimeoutMs);
m_communicationChannel->setBuffered(true);
return SAI_STATUS_SUCCESS;
case SAI_REDIS_COMMUNICATION_MODE_REDIS_SYNC:
SWSS_LOG_NOTICE("enabling redis sync mode");
m_syncMode = true;
m_communicationChannel = std::make_shared<RedisChannel>(
m_contextConfig->m_dbAsic,
std::bind(&RedisRemoteSaiInterface::handleNotification, this, _1, _2, _3));
m_communicationChannel->setResponseTimeout(m_responseTimeoutMs);
m_communicationChannel->setBuffered(false);
return SAI_STATUS_SUCCESS;
case SAI_REDIS_COMMUNICATION_MODE_ZMQ_SYNC:
m_contextConfig->m_zmqEnable = true;
// main communication channel was created at initialize method
// so this command will replace it with zmq channel
m_communicationChannel = std::make_shared<ZeroMQChannel>(
m_contextConfig->m_zmqEndpoint,
m_contextConfig->m_zmqNtfEndpoint,
std::bind(&RedisRemoteSaiInterface::handleNotification, this, _1, _2, _3));
m_communicationChannel->setResponseTimeout(m_responseTimeoutMs);
SWSS_LOG_NOTICE("zmq enabled, forcing sync mode");
m_syncMode = true;
SWSS_LOG_NOTICE("disabling buffered pipeline in sync mode");
m_communicationChannel->setBuffered(false);
return SAI_STATUS_SUCCESS;
default:
SWSS_LOG_ERROR("invalid communication mode value: %d", m_redisCommunicationMode);
return SAI_STATUS_NOT_SUPPORTED;
}
case SAI_REDIS_SWITCH_ATTR_USE_PIPELINE:
if (m_syncMode)
{
SWSS_LOG_WARN("use pipeline is not supported in sync mode");
return SAI_STATUS_NOT_SUPPORTED;
}
m_communicationChannel->setBuffered(attr->value.booldata);
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_FLUSH:
m_communicationChannel->flush();
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR:
if (m_recorder)
{
m_recorder->setRecordingOutputDirectory(*attr);
}
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_RECORDING_FILENAME:
if (m_recorder)
{
m_recorder->setRecordingFilename(*attr);
}
return SAI_STATUS_SUCCESS;
case SAI_REDIS_SWITCH_ATTR_FLEX_COUNTER_GROUP:
return notifyCounterGroupOperations(objectId,
reinterpret_cast<sai_redis_flex_counter_group_parameter_t*>(attr->value.ptr));
case SAI_REDIS_SWITCH_ATTR_FLEX_COUNTER:
return notifyCounterOperations(objectId,
reinterpret_cast<sai_redis_flex_counter_parameter_t*>(attr->value.ptr));
default:
break;
}
SWSS_LOG_ERROR("unknown redis extension attribute: %d", attr->id);
return SAI_STATUS_FAILURE;
}
sai_status_t RedisRemoteSaiInterface::setLinkEventDampingConfig(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const std::vector<swss::FieldValueTuple> &values)
{
SWSS_LOG_ENTER();
std::string key = sai_serialize_object_type(objectType) + ":" + sai_serialize_object_id(objectId);
m_communicationChannel->set(key, values, REDIS_ASIC_STATE_COMMAND_DAMPING_CONFIG_SET);
if (m_syncMode)
{
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_DAMPING_CONFIG_SET, kco);
m_recorder->recordGenericSetResponse(status);
return status;
}
return SAI_STATUS_SUCCESS;
}
sai_status_t RedisRemoteSaiInterface::setRedisPortExtensionAttribute(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const sai_attribute_t *attr)
{
SWSS_LOG_ENTER();
if (attr == nullptr)
{
SWSS_LOG_ERROR("attr pointer is null");
return SAI_STATUS_INVALID_PARAMETER;
}
std::string str_attr_id = sai_serialize_redis_port_attr_id(
static_cast<sai_redis_port_attr_t>(attr->id));
switch (attr->id)
{
case SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGORITHM:
{
std::string str_attr_value = sai_serialize_redis_link_event_damping_algorithm(
static_cast<sai_redis_link_event_damping_algorithm_t>(attr->value.s32));
return setLinkEventDampingConfig(
objectType, objectId, {swss::FieldValueTuple(str_attr_id, str_attr_value)});
}
case SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG:
{
sai_redis_link_event_damping_algo_aied_config_t *config =
(sai_redis_link_event_damping_algo_aied_config_t *)attr->value.ptr;
if (config == NULL)
{
SWSS_LOG_ERROR("invalid link damping config attr value NULL");
return SAI_STATUS_INVALID_PARAMETER;
}
std::string str_attr_value = sai_serialize_redis_link_event_damping_aied_config(*config);
return setLinkEventDampingConfig(
objectType, objectId, {swss::FieldValueTuple(str_attr_id, str_attr_value)});
}
default:
break;
}
SWSS_LOG_ERROR("unknown redis port extension attribute: %d", attr->id);
return SAI_STATUS_INVALID_PARAMETER;
}
bool RedisRemoteSaiInterface::isSaiS8ListValidString(
_In_ const sai_s8_list_t &s8list)
{
SWSS_LOG_ENTER();
if (s8list.list != nullptr && s8list.count > 0)
{
size_t len = strnlen((const char *)s8list.list, s8list.count);
if (len == (size_t)s8list.count)
{
return true;
}
else
{
SWSS_LOG_ERROR("Count (%u) is different than strnlen (%zu)", s8list.count, len);
}
}
return false;
}
bool RedisRemoteSaiInterface::emplaceStrings(
_In_ const sai_s8_list_t &field,
_In_ const sai_s8_list_t &value,
_Out_ std::vector<swss::FieldValueTuple> &entries)
{
SWSS_LOG_ENTER();
bool result = false;
if (isSaiS8ListValidString(field) && isSaiS8ListValidString(value))
{
entries.emplace_back(std::string((const char*)field.list, field.count), std::string((const char*)value.list, value.count));
result = true;
}
return result;
}
bool RedisRemoteSaiInterface::emplaceStrings(
_In_ const char *field,
_In_ const sai_s8_list_t &value,
_Out_ std::vector<swss::FieldValueTuple> &entries)
{
SWSS_LOG_ENTER();
bool result = false;
if (isSaiS8ListValidString(value))
{
entries.emplace_back(field, std::string((const char*)value.list, value.count));
result = true;
}
return result;
}
sai_status_t RedisRemoteSaiInterface::notifyCounterGroupOperations(
_In_ sai_object_id_t objectId,
_In_ const sai_redis_flex_counter_group_parameter_t *flexCounterGroupParam)
{
SWSS_LOG_ENTER();
std::vector<swss::FieldValueTuple> entries;
if (flexCounterGroupParam == nullptr || !isSaiS8ListValidString(flexCounterGroupParam->counter_group_name))
{
SWSS_LOG_ERROR("Invalid parameters when handling counter group operation");
return SAI_STATUS_FAILURE;
}
std::string key((const char*)flexCounterGroupParam->counter_group_name.list, flexCounterGroupParam->counter_group_name.count);
emplaceStrings(POLL_INTERVAL_FIELD, flexCounterGroupParam->poll_interval, entries);
emplaceStrings(STATS_MODE_FIELD, flexCounterGroupParam->stats_mode, entries);
emplaceStrings(flexCounterGroupParam->plugin_name, flexCounterGroupParam->plugins, entries);
emplaceStrings(FLEX_COUNTER_STATUS_FIELD, flexCounterGroupParam->operation, entries);
m_recorder->recordGenericSet(key, entries);
m_communicationChannel->set(key,
entries,
(entries.size() != 0) ? REDIS_FLEX_COUNTER_COMMAND_SET_GROUP : REDIS_FLEX_COUNTER_COMMAND_DEL_GROUP);
return waitForResponse(SAI_COMMON_API_SET);
}
sai_status_t RedisRemoteSaiInterface::notifyCounterOperations(
_In_ sai_object_id_t objectId,
_In_ const sai_redis_flex_counter_parameter_t *flexCounterParam)
{
SWSS_LOG_ENTER();
if (flexCounterParam == nullptr || !isSaiS8ListValidString(flexCounterParam->counter_key))
{
SWSS_LOG_ERROR("Invalid parameters when handling counter operation");
return SAI_STATUS_FAILURE;
}
std::vector<swss::FieldValueTuple> entries;
std::string key((const char*)flexCounterParam->counter_key.list, flexCounterParam->counter_key.count);
std::string command;
if (emplaceStrings(flexCounterParam->counter_field_name, flexCounterParam->counter_ids, entries))
{
command = REDIS_FLEX_COUNTER_COMMAND_START_POLL;
emplaceStrings(STATS_MODE_FIELD, flexCounterParam->stats_mode, entries);
}
else
{
command = REDIS_FLEX_COUNTER_COMMAND_STOP_POLL;
}
m_recorder->recordGenericSet(key, entries);
m_communicationChannel->set(key, entries, command);
return waitForResponse(SAI_COMMON_API_SET);
}
sai_status_t RedisRemoteSaiInterface::set(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const sai_attribute_t *attr)
{
SWSS_LOG_ENTER();
if (RedisRemoteSaiInterface::isRedisAttribute(objectType, attr))
{
return setRedisExtensionAttribute(objectType, objectId, attr);
}
if (RedisRemoteSaiInterface::isRedisPortAttribute(objectType, attr))
{
return setRedisPortExtensionAttribute(objectType, objectId, attr);
}
auto status = set(
objectType,
sai_serialize_object_id(objectId),
attr);
if (objectType == SAI_OBJECT_TYPE_SWITCH && status == SAI_STATUS_SUCCESS)
{
auto sw = m_switchContainer->getSwitch(objectId);
if (!sw)
{
SWSS_LOG_THROW("failed to find switch %s in container",
sai_serialize_object_id(objectId).c_str());
}
/*
* When doing SET operation user may want to update notification
* pointers.
*/
sw->updateNotifications(1, attr);
}
return status;
}
sai_status_t RedisRemoteSaiInterface::get(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ uint32_t attr_count,
_Inout_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
return get(
objectType,
sai_serialize_object_id(objectId),
attr_count,
attr_list);
}
#define DECLARE_REMOVE_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::remove( \
_In_ const sai_ ## ot ## _t* ot) \
{ \
SWSS_LOG_ENTER(); \
return remove( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot)); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_REMOVE_ENTRY);
#define DECLARE_CREATE_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::create( \
_In_ const sai_ ## ot ## _t* ot, \
_In_ uint32_t attr_count, \
_In_ const sai_attribute_t *attr_list) \
{ \
SWSS_LOG_ENTER(); \
return create( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot), \
attr_count, \
attr_list); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_CREATE_ENTRY);
#define DECLARE_SET_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::set( \
_In_ const sai_ ## ot ## _t* ot, \
_In_ const sai_attribute_t *attr) \
{ \
SWSS_LOG_ENTER(); \
return set( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot), \
attr); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_SET_ENTRY);
#define DECLARE_BULK_CREATE_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::bulkCreate( \
_In_ uint32_t object_count, \
_In_ const sai_ ## ot ## _t *ot, \
_In_ const uint32_t *attr_count, \
_In_ const sai_attribute_t **attr_list, \
_In_ sai_bulk_op_error_mode_t mode, \
_Out_ sai_status_t *object_statuses) \
{ \
SWSS_LOG_ENTER(); \
static PerformanceIntervalTimer timer("RedisRemoteSaiInterface::bulkCreate(" #ot ")"); \
timer.start(); \
std::vector<std::string> serialized_object_ids; \
for (uint32_t idx = 0; idx < object_count; idx++) \
{ \
std::string str_object_id = sai_serialize_ ##ot (ot[idx]); \
serialized_object_ids.push_back(str_object_id); \
} \
auto status = bulkCreate( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
serialized_object_ids, \
attr_count, \
attr_list, \
mode, \
object_statuses); \
timer.stop(); \
timer.inc(object_count); \
return status; \
}
SAIREDIS_DECLARE_EVERY_BULK_ENTRY(DECLARE_BULK_CREATE_ENTRY);
#define DECLARE_BULK_REMOVE_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::bulkRemove( \
_In_ uint32_t object_count, \
_In_ const sai_ ## ot ## _t *ot, \
_In_ sai_bulk_op_error_mode_t mode, \
_Out_ sai_status_t *object_statuses) \
{ \
SWSS_LOG_ENTER(); \
std::vector<std::string> serializedObjectIds; \
for (uint32_t idx = 0; idx < object_count; idx++) \
{ \
serializedObjectIds.emplace_back(sai_serialize_ ##ot (ot[idx])); \
} \
return bulkRemove((sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, serializedObjectIds, mode, object_statuses); \
}
SAIREDIS_DECLARE_EVERY_BULK_ENTRY(DECLARE_BULK_REMOVE_ENTRY);
#define DECLARE_BULK_SET_ENTRY(OT,ot) \
sai_status_t RedisRemoteSaiInterface::bulkSet( \
_In_ uint32_t object_count, \
_In_ const sai_ ## ot ## _t *ot, \
_In_ const sai_attribute_t *attr_list, \
_In_ sai_bulk_op_error_mode_t mode, \
_Out_ sai_status_t *object_statuses) \
{ \
SWSS_LOG_ENTER(); \
std::vector<std::string> serializedObjectIds; \
for (uint32_t idx = 0; idx < object_count; idx++) \
{ \
serializedObjectIds.emplace_back(sai_serialize_ ##ot (ot[idx])); \
} \
return bulkSet((sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, serializedObjectIds, attr_list, mode, object_statuses); \
}
SAIREDIS_DECLARE_EVERY_BULK_ENTRY(DECLARE_BULK_SET_ENTRY);
sai_status_t RedisRemoteSaiInterface::create(
_In_ sai_object_type_t object_type,
_In_ const std::string& serializedObjectId,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
auto entry = SaiAttributeList::serialize_attr_list(
object_type,
attr_count,
attr_list,
false);
if (entry.empty())
{
// make sure that we put object into db
// even if there are no attributes set
swss::FieldValueTuple null("NULL", "NULL");
entry.push_back(null);
}
auto serializedObjectType = sai_serialize_object_type(object_type);
const std::string key = serializedObjectType + ":" + serializedObjectId;
SWSS_LOG_DEBUG("generic create key: %s, fields: %" PRIu64, key.c_str(), entry.size());
m_recorder->recordGenericCreate(key, entry);
m_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_CREATE);
auto status = waitForResponse(SAI_COMMON_API_CREATE);
m_recorder->recordGenericCreateResponse(status);
return status;
}
sai_status_t RedisRemoteSaiInterface::remove(
_In_ sai_object_type_t objectType,
_In_ const std::string& serializedObjectId)
{
SWSS_LOG_ENTER();
auto serializedObjectType = sai_serialize_object_type(objectType);
const std::string key = serializedObjectType + ":" + serializedObjectId;
SWSS_LOG_DEBUG("generic remove key: %s", key.c_str());
m_recorder->recordGenericRemove(key);
m_communicationChannel->del(key, REDIS_ASIC_STATE_COMMAND_REMOVE);
auto status = waitForResponse(SAI_COMMON_API_REMOVE);
m_recorder->recordGenericRemoveResponse(status);
return status;
}
sai_status_t RedisRemoteSaiInterface::set(
_In_ sai_object_type_t objectType,
_In_ const std::string &serializedObjectId,
_In_ const sai_attribute_t *attr)
{
SWSS_LOG_ENTER();
auto entry = SaiAttributeList::serialize_attr_list(
objectType,
1,
attr,
false);
auto serializedObjectType = sai_serialize_object_type(objectType);
std::string key = serializedObjectType + ":" + serializedObjectId;
SWSS_LOG_DEBUG("generic set key: %s, fields: %lu", key.c_str(), entry.size());
m_recorder->recordGenericSet(key, entry);
m_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_SET);
auto status = waitForResponse(SAI_COMMON_API_SET);
m_recorder->recordGenericSetResponse(status);
return status;
}
sai_status_t RedisRemoteSaiInterface::waitForResponse(
_In_ sai_common_api_t api)
{
SWSS_LOG_ENTER();
if (m_syncMode)
{
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_GETRESPONSE, kco);
m_recorder->recordGenericResponse(status);
return status;
}
/*
* By default sync mode is disabled and all create/set/remove are
* considered success operations.
*/
return SAI_STATUS_SUCCESS;
}
sai_status_t RedisRemoteSaiInterface::waitForGetResponse(
_In_ sai_object_type_t objectType,
_In_ uint32_t attr_count,
_Inout_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_GETRESPONSE, kco);
auto &values = kfvFieldsValues(kco);
if (status == SAI_STATUS_SUCCESS)
{
if (values.size() == 0)
{
SWSS_LOG_THROW("logic error, get response returned 0 values!, send api response or sync/async issue?");
}
SaiAttributeList list(objectType, values, false);