-
Notifications
You must be signed in to change notification settings - Fork 274
/
Syncd.cpp
5391 lines (4092 loc) · 163 KB
/
Syncd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
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 "Syncd.h"
#include "VidManager.h"
#include "NotificationHandler.h"
#include "Workaround.h"
#include "ComparisonLogic.h"
#include "HardReiniter.h"
#include "RedisClient.h"
#include "RequestShutdown.h"
#include "WarmRestartTable.h"
#include "ContextConfigContainer.h"
#include "BreakConfigParser.h"
#include "RedisNotificationProducer.h"
#include "ZeroMQNotificationProducer.h"
#include "WatchdogScope.h"
#include "sairediscommon.h"
#include "swss/logger.h"
#include "swss/select.h"
#include "swss/tokenize.h"
#include "swss/notificationproducer.h"
#include "swss/exec.h"
#include "meta/sai_serialize.h"
#include "meta/ZeroMQSelectableChannel.h"
#include "meta/RedisSelectableChannel.h"
#include "meta/PerformanceIntervalTimer.h"
#include "vslib/saivs.h"
#include "config.h"
#include <unistd.h>
#include <inttypes.h>
#include <iterator>
#include <algorithm>
#define DEF_SAI_WARM_BOOT_DATA_FILE "/var/warmboot/sai-warmboot.bin"
#define SAI_FAILURE_DUMP_SCRIPT "/usr/bin/sai_failure_dump.sh"
using namespace syncd;
using namespace saimeta;
using namespace sairediscommon;
using namespace std::placeholders;
#ifdef ASAN_ENABLED
#define WD_DELAY_FACTOR 2
#else
#define WD_DELAY_FACTOR 1
#endif
Syncd::Syncd(
_In_ std::shared_ptr<sairedis::SaiInterface> vendorSai,
_In_ std::shared_ptr<CommandLineOptions> cmd,
_In_ bool isWarmStart):
m_commandLineOptions(cmd),
m_isWarmStart(isWarmStart),
m_firstInitWasPerformed(false),
m_asicInitViewMode(false), // by default we are in APPLY view mode
m_vendorSai(vendorSai),
m_veryFirstRun(false),
m_enableSyncMode(false),
m_timerWatchdog(cmd->m_watchdogWarnTimeSpan * WD_DELAY_FACTOR)
{
SWSS_LOG_ENTER();
SWSS_LOG_NOTICE("sairedis git revision %s, SAI git revision: %s", SAIREDIS_GIT_REVISION, SAI_GIT_REVISION);
setSaiApiLogLevel();
SWSS_LOG_NOTICE("command line: %s", m_commandLineOptions->getCommandLineString().c_str());
auto ccc = sairedis::ContextConfigContainer::loadFromFile(m_commandLineOptions->m_contextConfig.c_str());
m_contextConfig = ccc->get(m_commandLineOptions->m_globalContext);
if (m_contextConfig == nullptr)
{
SWSS_LOG_THROW("no context config defined at global context %u", m_commandLineOptions->m_globalContext);
}
if (m_contextConfig->m_zmqEnable && m_commandLineOptions->m_enableSyncMode)
{
SWSS_LOG_NOTICE("disabling command line sync mode, since context zmq enabled");
m_commandLineOptions->m_enableSyncMode = false;
m_commandLineOptions->m_redisCommunicationMode = SAI_REDIS_COMMUNICATION_MODE_ZMQ_SYNC;
}
if (m_commandLineOptions->m_enableSyncMode)
{
SWSS_LOG_WARN("enable sync mode is deprecated, please use communication mode, FORCING redis sync mode");
m_enableSyncMode = true;
m_contextConfig->m_zmqEnable = false;
m_commandLineOptions->m_redisCommunicationMode = SAI_REDIS_COMMUNICATION_MODE_REDIS_SYNC;
}
if (m_commandLineOptions->m_redisCommunicationMode == SAI_REDIS_COMMUNICATION_MODE_ZMQ_SYNC)
{
SWSS_LOG_NOTICE("zmq sync mode enabled via cmd line");
m_contextConfig->m_zmqEnable = true;
m_enableSyncMode = true;
}
m_manager = std::make_shared<FlexCounterManager>(m_vendorSai, m_contextConfig->m_dbCounters, m_commandLineOptions->m_supportingBulkCounterGroups);
loadProfileMap();
m_profileIter = m_profileMap.begin();
// we need STATE_DB ASIC_DB and COUNTERS_DB
m_dbAsic = std::make_shared<swss::DBConnector>(m_contextConfig->m_dbAsic, 0);
m_mdioIpcServer = std::make_shared<MdioIpcServer>(m_vendorSai, m_commandLineOptions->m_globalContext);
if (m_contextConfig->m_zmqEnable)
{
m_notifications = std::make_shared<ZeroMQNotificationProducer>(m_contextConfig->m_zmqNtfEndpoint);
SWSS_LOG_NOTICE("zmq enabled, forcing sync mode");
m_enableSyncMode = true;
m_selectableChannel = std::make_shared<sairedis::ZeroMQSelectableChannel>(m_contextConfig->m_zmqEndpoint);
}
else
{
m_notifications = std::make_shared<RedisNotificationProducer>(m_contextConfig->m_dbAsic);
m_enableSyncMode = m_commandLineOptions->m_redisCommunicationMode == SAI_REDIS_COMMUNICATION_MODE_REDIS_SYNC;
bool modifyRedis = m_enableSyncMode ? false : true;
m_selectableChannel = std::make_shared<sairedis::RedisSelectableChannel>(
m_dbAsic,
ASIC_STATE_TABLE,
REDIS_TABLE_GETRESPONSE,
TEMP_PREFIX,
modifyRedis);
}
m_client = std::make_shared<RedisClient>(m_dbAsic);
m_processor = std::make_shared<NotificationProcessor>(m_notifications, m_client, std::bind(&Syncd::syncProcessNotification, this, _1));
m_handler = std::make_shared<NotificationHandler>(m_processor);
m_sn.onFdbEvent = std::bind(&NotificationHandler::onFdbEvent, m_handler.get(), _1, _2);
m_sn.onNatEvent = std::bind(&NotificationHandler::onNatEvent, m_handler.get(), _1, _2);
m_sn.onPortStateChange = std::bind(&NotificationHandler::onPortStateChange, m_handler.get(), _1, _2);
m_sn.onQueuePfcDeadlock = std::bind(&NotificationHandler::onQueuePfcDeadlock, m_handler.get(), _1, _2);
m_sn.onSwitchAsicSdkHealthEvent = std::bind(&NotificationHandler::onSwitchAsicSdkHealthEvent, m_handler.get(), _1, _2, _3, _4, _5, _6);
m_sn.onSwitchShutdownRequest = std::bind(&NotificationHandler::onSwitchShutdownRequest, m_handler.get(), _1);
m_sn.onSwitchStateChange = std::bind(&NotificationHandler::onSwitchStateChange, m_handler.get(), _1, _2);
m_sn.onBfdSessionStateChange = std::bind(&NotificationHandler::onBfdSessionStateChange, m_handler.get(), _1, _2);
m_sn.onPortHostTxReady = std::bind(&NotificationHandler::onPortHostTxReady, m_handler.get(), _1, _2, _3);
m_sn.onTwampSessionEvent = std::bind(&NotificationHandler::onTwampSessionEvent, m_handler.get(), _1, _2);
m_handler->setSwitchNotifications(m_sn.getSwitchNotifications());
m_restartQuery = std::make_shared<swss::NotificationConsumer>(m_dbAsic.get(), SYNCD_NOTIFICATION_CHANNEL_RESTARTQUERY_PER_DB(m_contextConfig->m_dbAsic));
// TODO to be moved to ASIC_DB
m_dbFlexCounter = std::make_shared<swss::DBConnector>(m_contextConfig->m_dbFlex, 0);
m_flexCounter = std::make_shared<swss::ConsumerTable>(m_dbFlexCounter.get(), FLEX_COUNTER_TABLE);
m_flexCounterGroup = std::make_shared<swss::ConsumerTable>(m_dbFlexCounter.get(), FLEX_COUNTER_GROUP_TABLE);
m_flexCounterTable = std::make_shared<swss::Table>(m_dbFlexCounter.get(), FLEX_COUNTER_TABLE);
m_flexCounterGroupTable = std::make_shared<swss::Table>(m_dbFlexCounter.get(), FLEX_COUNTER_GROUP_TABLE);
m_switchConfigContainer = std::make_shared<sairedis::SwitchConfigContainer>();
m_redisVidIndexGenerator = std::make_shared<sairedis::RedisVidIndexGenerator>(m_dbAsic, REDIS_KEY_VIDCOUNTER);
m_virtualObjectIdManager =
std::make_shared<sairedis::VirtualObjectIdManager>(
m_commandLineOptions->m_globalContext,
m_switchConfigContainer,
m_redisVidIndexGenerator);
// TODO move to syncd object
m_translator = std::make_shared<VirtualOidTranslator>(m_client, m_virtualObjectIdManager, vendorSai);
m_processor->m_translator = m_translator; // TODO as param
m_veryFirstRun = isVeryFirstRun();
performStartupLogic();
m_smt.profileGetValue = std::bind(&Syncd::profileGetValue, this, _1, _2);
m_smt.profileGetNextValue = std::bind(&Syncd::profileGetNextValue, this, _1, _2, _3);
m_test_services = m_smt.getServiceMethodTable();
sai_status_t status = vendorSai->apiInitialize(0, &m_test_services);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("FATAL: failed to sai_api_initialize: %s",
sai_serialize_status(status).c_str());
abort();
}
sai_api_version_t apiVersion = SAI_VERSION(0,0,0); // invalid version
status = m_vendorSai->queryApiVersion(&apiVersion);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_WARN("failed to obtain libsai api version: %s", sai_serialize_status(status).c_str());
}
else
{
SWSS_LOG_NOTICE("libsai api version: %lu", apiVersion);
}
m_handler->setApiVersion(apiVersion);
m_breakConfig = BreakConfigParser::parseBreakConfig(m_commandLineOptions->m_breakConfig);
SWSS_LOG_NOTICE("syncd started");
}
Syncd::~Syncd()
{
SWSS_LOG_ENTER();
// empty
}
void Syncd::performStartupLogic()
{
SWSS_LOG_ENTER();
// ignore warm logic here if syncd starts in fast-boot, express-boot or Mellanox fastfast boot mode
if (m_isWarmStart && m_commandLineOptions->m_startType != SAI_START_TYPE_FASTFAST_BOOT &&
m_commandLineOptions->m_startType != SAI_START_TYPE_EXPRESS_BOOT &&
m_commandLineOptions->m_startType != SAI_START_TYPE_FAST_BOOT)
{
SWSS_LOG_WARN("override command line startType=%s via SAI_START_TYPE_WARM_BOOT",
CommandLineOptions::startTypeToString(m_commandLineOptions->m_startType).c_str());
m_commandLineOptions->m_startType = SAI_START_TYPE_WARM_BOOT;
}
if (m_commandLineOptions->m_startType == SAI_START_TYPE_WARM_BOOT)
{
const char *warmBootReadFile = profileGetValue(0, SAI_KEY_WARM_BOOT_READ_FILE);
SWSS_LOG_NOTICE("using warmBootReadFile: '%s'", warmBootReadFile);
if (warmBootReadFile == NULL || access(warmBootReadFile, F_OK) == -1)
{
SWSS_LOG_WARN("user requested warmStart but warmBootReadFile is not specified or not accessible, forcing cold start");
m_commandLineOptions->m_startType = SAI_START_TYPE_COLD_BOOT;
}
}
if (m_commandLineOptions->m_startType == SAI_START_TYPE_WARM_BOOT && m_veryFirstRun)
{
SWSS_LOG_WARN("warm start requested, but this is very first syncd start, forcing cold start");
/*
* We force cold start since if it's first run then redis db is not
* complete so redis asic view will not reflect warm boot asic state,
* if this happen then orch agent needs to be restarted as well to
* repopulate asic view.
*/
m_commandLineOptions->m_startType = SAI_START_TYPE_COLD_BOOT;
}
if (m_commandLineOptions->m_startType == SAI_START_TYPE_FASTFAST_BOOT)
{
/*
* Mellanox SAI requires to pass SAI_WARM_BOOT as SAI_BOOT_KEY
* to start 'fastfast'
*/
m_profileMap[SAI_KEY_BOOT_TYPE] = std::to_string(SAI_START_TYPE_WARM_BOOT);
}
else
{
m_profileMap[SAI_KEY_BOOT_TYPE] = std::to_string(m_commandLineOptions->m_startType); // number value is needed
}
}
bool Syncd::getAsicInitViewMode() const
{
SWSS_LOG_ENTER();
return m_asicInitViewMode;
}
void Syncd::setAsicInitViewMode(
_In_ bool enable)
{
SWSS_LOG_ENTER();
m_asicInitViewMode = enable;
}
bool Syncd::isInitViewMode() const
{
SWSS_LOG_ENTER();
return m_asicInitViewMode && m_commandLineOptions->m_enableTempView;
}
void Syncd::processEvent(
_In_ sairedis::SelectableChannel& consumer)
{
SWSS_LOG_ENTER();
std::lock_guard<std::mutex> lock(m_mutex);
do
{
swss::KeyOpFieldsValuesTuple kco;
/*
* In init mode we put all data to TEMP view and we snoop. We need
* to specify temporary view prefix in consumer since consumer puts
* data to redis db.
*/
consumer.pop(kco, isInitViewMode());
processSingleEvent(kco);
}
while (!consumer.empty());
}
sai_status_t Syncd::processSingleEvent(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
auto& key = kfvKey(kco);
auto& op = kfvOp(kco);
SWSS_LOG_INFO("key: %s op: %s", key.c_str(), op.c_str());
if (key.length() == 0)
{
SWSS_LOG_DEBUG("no elements in m_buffer");
return SAI_STATUS_SUCCESS;
}
WatchdogScope ws(m_timerWatchdog, op + ":" + key, &kco);
if (op == REDIS_ASIC_STATE_COMMAND_CREATE)
return processQuadEvent(SAI_COMMON_API_CREATE, kco);
if (op == REDIS_ASIC_STATE_COMMAND_REMOVE)
return processQuadEvent(SAI_COMMON_API_REMOVE, kco);
if (op == REDIS_ASIC_STATE_COMMAND_SET)
return processQuadEvent(SAI_COMMON_API_SET, kco);
if (op == REDIS_ASIC_STATE_COMMAND_GET)
return processQuadEvent(SAI_COMMON_API_GET, kco);
if (op == REDIS_ASIC_STATE_COMMAND_BULK_CREATE)
return processBulkQuadEvent(SAI_COMMON_API_BULK_CREATE, kco);
if (op == REDIS_ASIC_STATE_COMMAND_BULK_REMOVE)
return processBulkQuadEvent(SAI_COMMON_API_BULK_REMOVE, kco);
if (op == REDIS_ASIC_STATE_COMMAND_BULK_SET)
return processBulkQuadEvent(SAI_COMMON_API_BULK_SET, kco);
if (op == REDIS_ASIC_STATE_COMMAND_NOTIFY)
return processNotifySyncd(kco);
if (op == REDIS_ASIC_STATE_COMMAND_GET_STATS)
return processGetStatsEvent(kco);
if (op == REDIS_ASIC_STATE_COMMAND_CLEAR_STATS)
return processClearStatsEvent(kco);
if (op == REDIS_ASIC_STATE_COMMAND_FLUSH)
return processFdbFlush(kco);
if (op == REDIS_ASIC_STATE_COMMAND_ATTR_CAPABILITY_QUERY)
return processAttrCapabilityQuery(kco);
if (op == REDIS_ASIC_STATE_COMMAND_ATTR_ENUM_VALUES_CAPABILITY_QUERY)
return processAttrEnumValuesCapabilityQuery(kco);
if (op == REDIS_ASIC_STATE_COMMAND_OBJECT_TYPE_GET_AVAILABILITY_QUERY)
return processObjectTypeGetAvailabilityQuery(kco);
if (op == REDIS_FLEX_COUNTER_COMMAND_START_POLL)
return processFlexCounterEvent(key, SET_COMMAND, kfvFieldsValues(kco));
if (op == REDIS_FLEX_COUNTER_COMMAND_STOP_POLL)
return processFlexCounterEvent(key, DEL_COMMAND, kfvFieldsValues(kco));
if (op == REDIS_FLEX_COUNTER_COMMAND_SET_GROUP)
return processFlexCounterGroupEvent(key, SET_COMMAND, kfvFieldsValues(kco));
if (op == REDIS_FLEX_COUNTER_COMMAND_DEL_GROUP)
return processFlexCounterGroupEvent(key, DEL_COMMAND, kfvFieldsValues(kco));
SWSS_LOG_THROW("event op '%s' is not implemented, FIXME", op.c_str());
}
sai_status_t Syncd::processAttrCapabilityQuery(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
auto& strSwitchVid = kfvKey(kco);
sai_object_id_t switchVid;
sai_deserialize_object_id(strSwitchVid, switchVid);
sai_object_id_t switchRid = m_translator->translateVidToRid(switchVid);
auto& values = kfvFieldsValues(kco);
if (values.size() != 2)
{
SWSS_LOG_ERROR("Invalid input: expected 2 arguments, received %zu", values.size());
m_selectableChannel->set(sai_serialize_status(SAI_STATUS_INVALID_PARAMETER), {}, REDIS_ASIC_STATE_COMMAND_ATTR_CAPABILITY_RESPONSE);
return SAI_STATUS_INVALID_PARAMETER;
}
sai_object_type_t objectType;
sai_deserialize_object_type(fvValue(values[0]), objectType);
sai_attr_id_t attrId;
sai_deserialize_attr_id(fvValue(values[1]), attrId);
sai_attr_capability_t capability;
sai_status_t status = m_vendorSai->queryAttributeCapability(switchRid, objectType, attrId, &capability);
std::vector<swss::FieldValueTuple> entry;
if (status == SAI_STATUS_SUCCESS)
{
entry =
{
swss::FieldValueTuple("CREATE_IMPLEMENTED", (capability.create_implemented ? "true" : "false")),
swss::FieldValueTuple("SET_IMPLEMENTED", (capability.set_implemented ? "true" : "false")),
swss::FieldValueTuple("GET_IMPLEMENTED", (capability.get_implemented ? "true" : "false"))
};
SWSS_LOG_INFO("Sending response: create_implemented:%d, set_implemented:%d, get_implemented:%d",
capability.create_implemented, capability.set_implemented, capability.get_implemented);
}
m_selectableChannel->set(sai_serialize_status(status), entry, REDIS_ASIC_STATE_COMMAND_ATTR_CAPABILITY_RESPONSE);
return status;
}
sai_status_t Syncd::processAttrEnumValuesCapabilityQuery(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
auto& strSwitchVid = kfvKey(kco);
sai_object_id_t switchVid;
sai_deserialize_object_id(strSwitchVid, switchVid);
sai_object_id_t switchRid = m_translator->translateVidToRid(switchVid);
auto& values = kfvFieldsValues(kco);
if (values.size() != 3)
{
SWSS_LOG_ERROR("Invalid input: expected 3 arguments, received %zu", values.size());
m_selectableChannel->set(sai_serialize_status(SAI_STATUS_INVALID_PARAMETER), {}, REDIS_ASIC_STATE_COMMAND_ATTR_ENUM_VALUES_CAPABILITY_RESPONSE);
return SAI_STATUS_INVALID_PARAMETER;
}
sai_object_type_t objectType;
sai_deserialize_object_type(fvValue(values[0]), objectType);
sai_attr_id_t attrId;
sai_deserialize_attr_id(fvValue(values[1]), attrId);
uint32_t list_size = std::stoi(fvValue(values[2]));
std::vector<int32_t> enum_capabilities_list(list_size);
sai_s32_list_t enumCapList;
enumCapList.count = list_size;
enumCapList.list = enum_capabilities_list.data();
sai_status_t status = m_vendorSai->queryAttributeEnumValuesCapability(switchRid, objectType, attrId, &enumCapList);
std::vector<swss::FieldValueTuple> entry;
if (status == SAI_STATUS_SUCCESS)
{
std::vector<std::string> vec;
std::transform(enumCapList.list, enumCapList.list + enumCapList.count,
std::back_inserter(vec), [](auto&e) { return std::to_string(e); });
std::ostringstream join;
std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(join, ","));
auto strCap = join.str();
entry =
{
swss::FieldValueTuple("ENUM_CAPABILITIES", strCap),
swss::FieldValueTuple("ENUM_COUNT", std::to_string(enumCapList.count))
};
SWSS_LOG_DEBUG("Sending response: capabilities = '%s', count = %d", strCap.c_str(), enumCapList.count);
}
else if (status == SAI_STATUS_BUFFER_OVERFLOW)
{
entry =
{
swss::FieldValueTuple("ENUM_COUNT", std::to_string(enumCapList.count))
};
SWSS_LOG_DEBUG("Sending response: count = %u", enumCapList.count);
}
m_selectableChannel->set(sai_serialize_status(status), entry, REDIS_ASIC_STATE_COMMAND_ATTR_ENUM_VALUES_CAPABILITY_RESPONSE);
return status;
}
sai_status_t Syncd::processObjectTypeGetAvailabilityQuery(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
auto& strSwitchVid = kfvKey(kco);
sai_object_id_t switchVid;
sai_deserialize_object_id(strSwitchVid, switchVid);
const sai_object_id_t switchRid = m_translator->translateVidToRid(switchVid);
std::vector<swss::FieldValueTuple> values = kfvFieldsValues(kco);
// Syncd needs to pop the object type off the end of the list in order to
// retrieve the attribute list
sai_object_type_t objectType;
sai_deserialize_object_type(fvValue(values.back()), objectType);
values.pop_back();
SaiAttributeList list(objectType, values, false);
sai_attribute_t *attr_list = list.get_attr_list();
uint32_t attr_count = list.get_attr_count();
m_translator->translateVidToRid(objectType, attr_count, attr_list);
uint64_t count;
sai_status_t status = m_vendorSai->objectTypeGetAvailability(
switchRid,
objectType,
attr_count,
attr_list,
&count);
std::vector<swss::FieldValueTuple> entry;
if (status == SAI_STATUS_SUCCESS)
{
entry.push_back(swss::FieldValueTuple("OBJECT_COUNT", std::to_string(count)));
SWSS_LOG_DEBUG("Sending response: count = %lu", count);
}
m_selectableChannel->set(sai_serialize_status(status), entry, REDIS_ASIC_STATE_COMMAND_OBJECT_TYPE_GET_AVAILABILITY_RESPONSE);
return status;
}
sai_status_t Syncd::processFdbFlush(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
auto& key = kfvKey(kco);
auto strSwitchVid = key.substr(key.find(":") + 1);
sai_object_id_t switchVid;
sai_deserialize_object_id(strSwitchVid, switchVid);
sai_object_id_t switchRid = m_translator->translateVidToRid(switchVid);
auto& values = kfvFieldsValues(kco);
for (const auto &v: values)
{
SWSS_LOG_DEBUG("attr: %s: %s", fvField(v).c_str(), fvValue(v).c_str());
}
SaiAttributeList list(SAI_OBJECT_TYPE_FDB_FLUSH, values, false);
SaiAttributeList vidlist(SAI_OBJECT_TYPE_FDB_FLUSH, values, false);
/*
* Attribute list can't be const since we will use it to translate VID to
* RID in place.
*/
sai_attribute_t *attr_list = list.get_attr_list();
uint32_t attr_count = list.get_attr_count();
m_translator->translateVidToRid(SAI_OBJECT_TYPE_FDB_FLUSH, attr_count, attr_list);
sai_status_t status = m_vendorSai->flushFdbEntries(switchRid, attr_count, attr_list);
m_selectableChannel->set(sai_serialize_status(status), {} , REDIS_ASIC_STATE_COMMAND_FLUSHRESPONSE);
if (status == SAI_STATUS_SUCCESS)
{
SWSS_LOG_NOTICE("fdb flush succeeded, updating redis database");
// update database right after fdb flush success (not in notification)
// build artificial notification here to reuse code
auto *md = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_FDB_FLUSH, SAI_FDB_FLUSH_ATTR_ENTRY_TYPE);
auto *dv = md ? md->defaultvalue : nullptr;
sai_fdb_flush_entry_type_t type = dv
? (sai_fdb_flush_entry_type_t)dv->s32
: SAI_FDB_FLUSH_ENTRY_TYPE_DYNAMIC;
sai_object_id_t bvId = SAI_NULL_OBJECT_ID;
sai_object_id_t bridgePortId = SAI_NULL_OBJECT_ID;
attr_list = vidlist.get_attr_list();
attr_count = vidlist.get_attr_count();
for (uint32_t i = 0; i < attr_count; i++)
{
switch (attr_list[i].id)
{
case SAI_FDB_FLUSH_ATTR_BRIDGE_PORT_ID:
bridgePortId = attr_list[i].value.oid;
break;
case SAI_FDB_FLUSH_ATTR_BV_ID:
bvId = attr_list[i].value.oid;
break;
case SAI_FDB_FLUSH_ATTR_ENTRY_TYPE:
type = (sai_fdb_flush_entry_type_t)attr_list[i].value.s32;
break;
default:
SWSS_LOG_ERROR("unsupported attribute: %d, skipping", attr_list[i].id);
break;
}
}
m_client->processFlushEvent(switchVid, bridgePortId, bvId, type);
}
return status;
}
sai_status_t Syncd::processClearStatsEvent(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
const std::string &key = kfvKey(kco);
sai_object_meta_key_t metaKey;
sai_deserialize_object_meta_key(key, metaKey);
if (isInitViewMode() && m_createdInInitView.find(metaKey.objectkey.key.object_id) != m_createdInInitView.end())
{
SWSS_LOG_WARN("CLEAR STATS api can't be used on %s since it's created in INIT_VIEW mode", key.c_str());
sai_status_t status = SAI_STATUS_INVALID_OBJECT_ID;
m_selectableChannel->set(sai_serialize_status(status), {}, REDIS_ASIC_STATE_COMMAND_GETRESPONSE);
return status;
}
if (!m_translator->tryTranslateVidToRid(metaKey))
{
SWSS_LOG_WARN("VID to RID translation failure: %s", key.c_str());
sai_status_t status = SAI_STATUS_INVALID_OBJECT_ID;
m_selectableChannel->set(sai_serialize_status(status), {}, REDIS_ASIC_STATE_COMMAND_GETRESPONSE);
return status;
}
auto info = sai_metadata_get_object_type_info(metaKey.objecttype);
if (info->isnonobjectid)
{
SWSS_LOG_THROW("non object id not supported on clear stats: %s, FIXME", key.c_str());
}
std::vector<sai_stat_id_t> counter_ids;
for (auto&v: kfvFieldsValues(kco))
{
int32_t val;
sai_deserialize_enum(fvField(v), info->statenum, val);
counter_ids.push_back(val);
}
auto status = m_vendorSai->clearStats(
metaKey.objecttype,
metaKey.objectkey.key.object_id,
(uint32_t)counter_ids.size(),
counter_ids.data());
m_selectableChannel->set(sai_serialize_status(status), {}, REDIS_ASIC_STATE_COMMAND_GETRESPONSE);
return status;
}
sai_status_t Syncd::processGetStatsEvent(
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
const std::string &key = kfvKey(kco);
sai_object_meta_key_t metaKey;
sai_deserialize_object_meta_key(key, metaKey);
if (isInitViewMode() && m_createdInInitView.find(metaKey.objectkey.key.object_id) != m_createdInInitView.end())
{
SWSS_LOG_WARN("GET STATS api can't be used on %s since it's created in INIT_VIEW mode", key.c_str());
sai_status_t status = SAI_STATUS_INVALID_OBJECT_ID;
m_selectableChannel->set(sai_serialize_status(status), {}, REDIS_ASIC_STATE_COMMAND_GETRESPONSE);
return status;
}
m_translator->translateVidToRid(metaKey);
auto info = sai_metadata_get_object_type_info(metaKey.objecttype);
if (info->isnonobjectid)
{
SWSS_LOG_THROW("non object id not supported on clear stats: %s, FIXME", key.c_str());
}
std::vector<sai_stat_id_t> counter_ids;
for (auto&v: kfvFieldsValues(kco))
{
int32_t val;
sai_deserialize_enum(fvField(v), info->statenum, val);
counter_ids.push_back(val);
}
std::vector<uint64_t> result(counter_ids.size());
auto status = m_vendorSai->getStats(
metaKey.objecttype,
metaKey.objectkey.key.object_id,
(uint32_t)counter_ids.size(),
counter_ids.data(),
result.data());
std::vector<swss::FieldValueTuple> entry;
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_NOTICE("Getting stats error: %s", sai_serialize_status(status).c_str());
}
else
{
const auto& values = kfvFieldsValues(kco);
for (size_t i = 0; i < values.size(); i++)
{
entry.emplace_back(fvField(values[i]), std::to_string(result[i]));
}
}
m_selectableChannel->set(sai_serialize_status(status), entry, REDIS_ASIC_STATE_COMMAND_GETRESPONSE);
return status;
}
sai_status_t Syncd::processBulkQuadEvent(
_In_ sai_common_api_t api,
_In_ const swss::KeyOpFieldsValuesTuple &kco)
{
SWSS_LOG_ENTER();
const std::string& key = kfvKey(kco); // objectType:count
std::string strObjectType = key.substr(0, key.find(":"));
sai_object_type_t objectType;
sai_deserialize_object_type(strObjectType, objectType);
const std::vector<swss::FieldValueTuple> &values = kfvFieldsValues(kco);
std::vector<std::vector<swss::FieldValueTuple>> strAttributes;
// field = objectId
// value = attrid=attrvalue|...
std::vector<std::string> objectIds;
std::vector<std::shared_ptr<SaiAttributeList>> attributes;
for (const auto &fvt: values)
{
std::string strObjectId = fvField(fvt);
std::string joined = fvValue(fvt);
// decode values
auto v = swss::tokenize(joined, '|');
objectIds.push_back(strObjectId);
std::vector<swss::FieldValueTuple> entries; // attributes per object id
for (size_t i = 0; i < v.size(); ++i)
{
const std::string item = v.at(i);
auto start = item.find_first_of("=");
auto field = item.substr(0, start);
auto value = item.substr(start + 1);
entries.emplace_back(field, value);
}
strAttributes.push_back(entries);
// since now we converted this to proper list, we can extract attributes
auto list = std::make_shared<SaiAttributeList>(objectType, entries, false);
attributes.push_back(list);
}
SWSS_LOG_INFO("bulk %s executing with %zu items",
strObjectType.c_str(),
objectIds.size());
if (isInitViewMode())
{
return processBulkQuadEventInInitViewMode(objectType, objectIds, api, attributes, strAttributes);
}
if (api != SAI_COMMON_API_BULK_GET)
{
// translate attributes for all objects
for (auto &list: attributes)
{
sai_attribute_t *attr_list = list->get_attr_list();
uint32_t attr_count = list->get_attr_count();
m_translator->translateVidToRid(objectType, attr_count, attr_list);
}
}
auto info = sai_metadata_get_object_type_info(objectType);
if (info->isobjectid)
{
return processBulkOid(objectType, objectIds, api, attributes, strAttributes);
}
else
{
return processBulkEntry(objectType, objectIds, api, attributes, strAttributes);
}
}
sai_status_t Syncd::processBulkQuadEventInInitViewMode(
_In_ sai_object_type_t objectType,
_In_ const std::vector<std::string>& objectIds,
_In_ sai_common_api_t api,
_In_ const std::vector<std::shared_ptr<saimeta::SaiAttributeList>>& attributes,
_In_ const std::vector<std::vector<swss::FieldValueTuple>>& strAttributes)
{
SWSS_LOG_ENTER();
std::vector<sai_status_t> statuses(objectIds.size());
for (auto &a: statuses)
{
a = SAI_STATUS_SUCCESS;
}
auto info = sai_metadata_get_object_type_info(objectType);
switch (api)
{
case SAI_COMMON_API_BULK_CREATE:
case SAI_COMMON_API_BULK_REMOVE:
case SAI_COMMON_API_BULK_SET:
if (info->isnonobjectid)
{
sendApiResponse(api, SAI_STATUS_SUCCESS, (uint32_t)statuses.size(), statuses.data());
syncUpdateRedisBulkQuadEvent(api, statuses, objectType, objectIds, strAttributes);
return SAI_STATUS_SUCCESS;
}
switch (objectType)
{
case SAI_OBJECT_TYPE_SWITCH:
case SAI_OBJECT_TYPE_PORT:
case SAI_OBJECT_TYPE_SCHEDULER_GROUP:
case SAI_OBJECT_TYPE_INGRESS_PRIORITY_GROUP:
SWSS_LOG_THROW("%s is not supported in init view mode",
sai_serialize_object_type(objectType).c_str());
default:
sendApiResponse(api, SAI_STATUS_SUCCESS, (uint32_t)statuses.size(), statuses.data());
syncUpdateRedisBulkQuadEvent(api, statuses, objectType, objectIds, strAttributes);
for (auto& str: objectIds)
{
sai_object_id_t objectVid;
sai_deserialize_object_id(str, objectVid);
// in init view mode insert every created object except switch
m_createdInInitView.insert(objectVid);
}
return SAI_STATUS_SUCCESS;
}
case SAI_COMMON_API_BULK_GET:
SWSS_LOG_THROW("GET bulk api is not implemented in init view mode, FIXME");
default:
SWSS_LOG_THROW("common bulk api (%s) is not implemented in init view mode",
sai_serialize_common_api(api).c_str());
}
}
sai_status_t Syncd::processBulkCreateEntry(
_In_ sai_object_type_t objectType,
_In_ const std::vector<std::string>& objectIds,
_In_ const std::vector<std::shared_ptr<SaiAttributeList>>& attributes,
_Out_ std::vector<sai_status_t>& statuses)
{
SWSS_LOG_ENTER();
sai_status_t status = SAI_STATUS_SUCCESS;
uint32_t object_count = (uint32_t) objectIds.size();
if (!object_count)
{
SWSS_LOG_ERROR("container with objectIds is empty in processBulkCreateEntry");
return SAI_STATUS_FAILURE;
}
sai_bulk_op_error_mode_t mode = SAI_BULK_OP_ERROR_MODE_IGNORE_ERROR;
std::vector<uint32_t> attr_counts(object_count);
std::vector<const sai_attribute_t*> attr_lists(object_count);
for (uint32_t idx = 0; idx < object_count; idx++)
{
attr_counts[idx] = attributes[idx]->get_attr_count();
attr_lists[idx] = attributes[idx]->get_attr_list();
}