-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathClientSai.cpp
1655 lines (1249 loc) · 53.4 KB
/
ClientSai.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 "ClientSai.h"
#include "SaiInternal.h"
#include "RedisRemoteSaiInterface.h"
#include "ZeroMQChannel.h"
#include "Utils.h"
#include "sairediscommon.h"
#include "ClientConfig.h"
#include "swss/logger.h"
#include "meta/sai_serialize.h"
#include "meta/NotificationFactory.h"
#include "meta/SaiAttributeList.h"
#include "meta/PerformanceIntervalTimer.h"
#include "meta/Globals.h"
#include <inttypes.h>
#define REDIS_CHECK_API_INITIALIZED() \
if (!m_apiInitialized) { \
SWSS_LOG_ERROR("%s: api not initialized", __PRETTY_FUNCTION__); \
return SAI_STATUS_FAILURE; }
using namespace sairedis;
using namespace sairediscommon;
using namespace saimeta;
using namespace std::placeholders;
// TODO how to tell if current SAI is in init view or apply view ?
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);
ClientSai::ClientSai()
{
SWSS_LOG_ENTER();
m_apiInitialized = false;
}
ClientSai::~ClientSai()
{
SWSS_LOG_ENTER();
if (m_apiInitialized)
{
apiUninitialize();
}
}
sai_status_t ClientSai::apiInitialize(
_In_ uint64_t flags,
_In_ const sai_service_method_table_t *service_method_table)
{
MUTEX();
SWSS_LOG_ENTER();
if (m_apiInitialized)
{
SWSS_LOG_ERROR("already initialized");
return SAI_STATUS_FAILURE;
}
if ((service_method_table == NULL) ||
(service_method_table->profile_get_next_value == NULL) ||
(service_method_table->profile_get_value == NULL))
{
SWSS_LOG_ERROR("invalid service_method_table handle passed to SAI API initialize");
return SAI_STATUS_INVALID_PARAMETER;
}
// TODO support context config
m_switchContainer = std::make_shared<SwitchContainer>();
auto clientConfig = service_method_table->profile_get_value(0, SAI_REDIS_KEY_CLIENT_CONFIG);
auto cc = ClientConfig::loadFromFile(clientConfig);
m_communicationChannel = std::make_shared<ZeroMQChannel>(
cc->m_zmqEndpoint,
cc->m_zmqNtfEndpoint,
std::bind(&ClientSai::handleNotification, this, _1, _2, _3));
m_apiInitialized = true;
return SAI_STATUS_SUCCESS;
}
sai_status_t ClientSai::apiUninitialize(void)
{
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
SWSS_LOG_NOTICE("begin");
m_communicationChannel = nullptr;
m_apiInitialized = false;
SWSS_LOG_NOTICE("end");
return SAI_STATUS_SUCCESS;
}
// QUAD API
sai_status_t ClientSai::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)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
// NOTE: in client mode, each create oid must be retrieved from server since
// server is actually creating new oid, and it's value must be transferred
// over communication channel
*objectId = SAI_NULL_OBJECT_ID;
if (objectType == SAI_OBJECT_TYPE_SWITCH && attr_count > 0 && attr_list)
{
auto initSwitchAttr = sai_metadata_get_attr_by_id(SAI_SWITCH_ATTR_INIT_SWITCH, attr_count, attr_list);
if (initSwitchAttr && initSwitchAttr->value.booldata == false)
{
// TODO for connect, we may not need to actually call server, if we
// have hwinfo and context and context container information, we
// could allocate switch object ID locally
auto hwinfo = Globals::getHardwareInfo(attr_count, attr_list);
// TODO support context
SWSS_LOG_NOTICE("request to connect switch (context: 0) and hwinfo='%s'", hwinfo.c_str());
for (uint32_t i = 0; i < attr_count; ++i)
{
auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_SWITCH, attr_list[i].id);
if (meta == NULL)
{
SWSS_LOG_THROW("failed to find metadata for switch attribute %d", attr_list[i].id);
}
if (meta->attrid == SAI_SWITCH_ATTR_INIT_SWITCH)
continue;
if (meta->attrid == SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO)
continue;
if (meta->attrvaluetype == SAI_ATTR_VALUE_TYPE_POINTER)
{
SWSS_LOG_ERROR("notifications not supported yet, FIXME");
return SAI_STATUS_FAILURE;
}
SWSS_LOG_ERROR("attribute %s not supported during INIT_SWITCH=false, expected HARDWARE_INFO or notification pointer", meta->attridname);
return SAI_STATUS_FAILURE;
}
}
else
{
SWSS_LOG_ERROR("creating new switch not supported yet, use SAI_SWITCH_ATTR_INIT_SWITCH=false");
return SAI_STATUS_FAILURE;
}
}
auto status = create(
objectType,
sai_serialize_object_id(switchId), // using switch ID instead of oid to transfer to server
attr_count,
attr_list);
if (status == SAI_STATUS_SUCCESS)
{
// since user requested create, OID value was created remotely and it
// was returned in m_lastCreateOids
*objectId = m_lastCreateOids.at(0);
}
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.
*/
SWSS_LOG_NOTICE("create switch OID = %s", sai_serialize_object_id(*objectId).c_str());
auto sw = std::make_shared<Switch>(*objectId, attr_count, attr_list);
m_switchContainer->insert(sw);
}
return status;
}
sai_status_t ClientSai::remove(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
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());
// remove switch from container
m_switchContainer->removeSwitch(objectId);
}
return status;
}
sai_status_t ClientSai::set(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const sai_attribute_t *attr)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
if (RedisRemoteSaiInterface::isRedisAttribute(objectType, attr))
{
SWSS_LOG_ERROR("sairedis extension attributes are not supported in CLIENT mode");
return SAI_STATUS_FAILURE;
}
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 ClientSai::get(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ uint32_t attr_count,
_Inout_ sai_attribute_t *attr_list)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
return get(
objectType,
sai_serialize_object_id(objectId),
attr_count,
attr_list);
}
// QUAD ENTRY API
#define DECLARE_CREATE_ENTRY(OT,ot) \
sai_status_t ClientSai::create( \
_In_ const sai_ ## ot ## _t* ot, \
_In_ uint32_t attr_count, \
_In_ const sai_attribute_t *attr_list) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
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_REMOVE_ENTRY(OT,ot) \
sai_status_t ClientSai::remove( \
_In_ const sai_ ## ot ## _t* ot) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
return remove( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot)); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_REMOVE_ENTRY);
#define DECLARE_SET_ENTRY(OT,ot) \
sai_status_t ClientSai::set( \
_In_ const sai_ ## ot ## _t* ot, \
_In_ const sai_attribute_t *attr) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
return set( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot), \
attr); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_SET_ENTRY);
#define DECLARE_GET_ENTRY(OT,ot) \
sai_status_t ClientSai::get( \
_In_ const sai_ ## ot ## _t* ot, \
_In_ uint32_t attr_count, \
_Inout_ sai_attribute_t *attr_list) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
return get( \
(sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \
sai_serialize_ ## ot(*ot), \
attr_count, \
attr_list); \
}
SAIREDIS_DECLARE_EVERY_ENTRY(DECLARE_GET_ENTRY);
#define DECLARE_BULK_CREATE_ENTRY(OT,ot) \
sai_status_t ClientSai::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) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
static PerformanceIntervalTimer timer("ClientSai::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 ClientSai::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) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
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 ClientSai::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) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
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_ROUTE_ENTRY, serializedObjectIds, attr_list, mode, object_statuses); \
}
SAIREDIS_DECLARE_EVERY_BULK_ENTRY(DECLARE_BULK_SET_ENTRY)
// BULK GET
#define DECLARE_BULK_GET_ENTRY(OT,ot) \
sai_status_t ClientSai::bulkGet( \
_In_ uint32_t object_count, \
_In_ const sai_ ## ot ## _t *ot, \
_In_ const uint32_t *attr_count, \
_Inout_ sai_attribute_t **attr_list, \
_In_ sai_bulk_op_error_mode_t mode, \
_Out_ sai_status_t *object_statuses) \
{ \
MUTEX(); \
SWSS_LOG_ENTER(); \
REDIS_CHECK_API_INITIALIZED(); \
SWSS_LOG_ERROR("FIXME not implemented"); \
return SAI_STATUS_NOT_IMPLEMENTED; \
}
SAIREDIS_DECLARE_EVERY_BULK_ENTRY(DECLARE_BULK_GET_ENTRY)
// QUAD API HELPERS
sai_status_t ClientSai::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_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_CREATE);
auto status = waitForResponse(SAI_COMMON_API_CREATE);
return status;
}
sai_status_t ClientSai::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_communicationChannel->set(key, {}, REDIS_ASIC_STATE_COMMAND_REMOVE);
auto status = waitForResponse(SAI_COMMON_API_REMOVE);
return status;
}
sai_status_t ClientSai::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_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_SET);
auto status = waitForResponse(SAI_COMMON_API_SET);
return status;
}
sai_status_t ClientSai::get(
_In_ sai_object_type_t objectType,
_In_ const std::string& serializedObjectId,
_In_ uint32_t attr_count,
_Inout_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
/*
* Since user may reuse buffers, then oid list buffers maybe not cleared
* and contain some garbage, let's clean them so we send all oids as null to
* syncd.
*/
Utils::clearOidValues(objectType, attr_count, attr_list);
auto entry = SaiAttributeList::serialize_attr_list(objectType, attr_count, attr_list, false);
std::string serializedObjectType = sai_serialize_object_type(objectType);
std::string key = serializedObjectType + ":" + serializedObjectId;
SWSS_LOG_DEBUG("generic get key: %s, fields: %lu", key.c_str(), entry.size());
// get is special, it will not put data
// into asic view, only to message queue
m_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_GET);
auto status = waitForGetResponse(objectType, attr_count, attr_list);
return status;
}
// QUAD API RESPONSE HELPERS
sai_status_t ClientSai::waitForResponse(
_In_ sai_common_api_t api)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_GETRESPONSE, kco);
if (api == SAI_COMMON_API_CREATE && status == SAI_STATUS_SUCCESS)
{
m_lastCreateOids.clear();
// since last api was create, we need to extract OID that was created in that api
// if create was for entry, oid is NULL
const auto& entry = kfvFieldsValues(kco);
const auto& field = fvField(entry.at(0));
const auto& value = fvValue(entry.at(0));
SWSS_LOG_INFO("parsing response: %s:%s", field.c_str(), value.c_str());
if (field == "oid")
{
sai_object_id_t oid;
sai_deserialize_object_id(value, oid);
m_lastCreateOids.push_back(oid);
}
}
return status;
}
sai_status_t ClientSai::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);
transfer_attributes(objectType, attr_count, list.get_attr_list(), attr_list, false);
}
else if (status == SAI_STATUS_BUFFER_OVERFLOW)
{
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, true);
// no need for id fix since this is overflow
transfer_attributes(objectType, attr_count, list.get_attr_list(), attr_list, true);
}
return status;
}
// FLUSH FDB ENTRIES
sai_status_t ClientSai::flushFdbEntries(
_In_ sai_object_id_t switchId,
_In_ uint32_t attrCount,
_In_ const sai_attribute_t *attrList)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
auto entry = SaiAttributeList::serialize_attr_list(
SAI_OBJECT_TYPE_FDB_FLUSH,
attrCount,
attrList,
false);
std::string serializedObjectId = sai_serialize_object_type(SAI_OBJECT_TYPE_FDB_FLUSH);
// NOTE ! we actually give switch ID since FLUSH is not real object
std::string key = serializedObjectId + ":" + sai_serialize_object_id(switchId);
SWSS_LOG_NOTICE("flush key: %s, fields: %lu", key.c_str(), entry.size());
m_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_FLUSH);
auto status = waitForFlushFdbEntriesResponse();
return status;
}
sai_status_t ClientSai::waitForFlushFdbEntriesResponse()
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_FLUSHRESPONSE, kco);
return status;
}
// OBJECT TYPE GET AVAILABILITY
sai_status_t ClientSai::objectTypeGetAvailability(
_In_ sai_object_id_t switchId,
_In_ sai_object_type_t objectType,
_In_ uint32_t attrCount,
_In_ const sai_attribute_t *attrList,
_Out_ uint64_t *count)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
auto strSwitchId = sai_serialize_object_id(switchId);
auto entry = SaiAttributeList::serialize_attr_list(objectType, attrCount, attrList, false);
entry.push_back(swss::FieldValueTuple("OBJECT_TYPE", sai_serialize_object_type(objectType)));
SWSS_LOG_DEBUG(
"Query arguments: switch: %s, attributes: %s",
strSwitchId.c_str(),
Globals::joinFieldValues(entry).c_str());
// Syncd will pop this argument off before trying to deserialize the attribute list
// This query will not put any data into the ASIC view, just into the
// message queue
m_communicationChannel->set(strSwitchId, entry, REDIS_ASIC_STATE_COMMAND_OBJECT_TYPE_GET_AVAILABILITY_QUERY);
auto status = waitForObjectTypeGetAvailabilityResponse(count);
return status;
}
sai_status_t ClientSai::waitForObjectTypeGetAvailabilityResponse(
_Inout_ uint64_t *count)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_OBJECT_TYPE_GET_AVAILABILITY_RESPONSE, kco);
if (status == SAI_STATUS_SUCCESS)
{
auto &values = kfvFieldsValues(kco);
if (values.size() != 1)
{
SWSS_LOG_THROW("Invalid response from syncd: expected 1 value, received %zu", values.size());
}
const std::string &availability_str = fvValue(values[0]);
*count = std::stoull(availability_str);
SWSS_LOG_DEBUG("Received payload: count = %lu", *count);
}
return status;
}
// QUERY ATTRIBUTE CAPABILITY
sai_status_t ClientSai::queryAttributeCapability(
_In_ sai_object_id_t switchId,
_In_ sai_object_type_t objectType,
_In_ sai_attr_id_t attrId,
_Out_ sai_attr_capability_t *capability)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
auto switchIdStr = sai_serialize_object_id(switchId);
auto objectTypeStr = sai_serialize_object_type(objectType);
auto meta = sai_metadata_get_attr_metadata(objectType, attrId);
if (meta == NULL)
{
SWSS_LOG_ERROR("Failed to find attribute metadata: object type %s, attr id %d", objectTypeStr.c_str(), attrId);
return SAI_STATUS_INVALID_PARAMETER;
}
const std::string attrIdStr = meta->attridname;
const std::vector<swss::FieldValueTuple> entry =
{
swss::FieldValueTuple("OBJECT_TYPE", objectTypeStr),
swss::FieldValueTuple("ATTR_ID", attrIdStr)
};
SWSS_LOG_DEBUG(
"Query arguments: switch %s, object type: %s, attribute: %s",
switchIdStr.c_str(),
objectTypeStr.c_str(),
attrIdStr.c_str()
);
// This query will not put any data into the ASIC view, just into the
// message queue
m_communicationChannel->set(switchIdStr, entry, REDIS_ASIC_STATE_COMMAND_ATTR_CAPABILITY_QUERY);
auto status = waitForQueryAttributeCapabilityResponse(capability);
return status;
}
sai_status_t ClientSai::waitForQueryAttributeCapabilityResponse(
_Out_ sai_attr_capability_t* capability)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_ATTR_CAPABILITY_RESPONSE, kco);
if (status == SAI_STATUS_SUCCESS)
{
const std::vector<swss::FieldValueTuple> &values = kfvFieldsValues(kco);
if (values.size() != 3)
{
SWSS_LOG_ERROR("Invalid response from syncd: expected 3 values, received %zu", values.size());
return SAI_STATUS_FAILURE;
}
capability->create_implemented = (fvValue(values[0]) == "true" ? true : false);
capability->set_implemented = (fvValue(values[1]) == "true" ? true : false);
capability->get_implemented = (fvValue(values[2]) == "true" ? true : false);
SWSS_LOG_DEBUG("Received payload: create_implemented:%s, set_implemented:%s, get_implemented:%s",
(capability->create_implemented ? "true" : "false"),
(capability->set_implemented ? "true" : "false"),
(capability->get_implemented ? "true" : "false"));
}
return status;
}
// QUERY ATTRIBUTE ENUM CAPABILITY
sai_status_t ClientSai::queryAttributeEnumValuesCapability(
_In_ sai_object_id_t switchId,
_In_ sai_object_type_t objectType,
_In_ sai_attr_id_t attrId,
_Inout_ sai_s32_list_t *enumValuesCapability)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
if (enumValuesCapability && enumValuesCapability->list)
{
// clear input list, since we use serialize to transfer values
for (uint32_t idx = 0; idx < enumValuesCapability->count; idx++)
enumValuesCapability->list[idx] = 0;
}
auto switch_id_str = sai_serialize_object_id(switchId);
auto object_type_str = sai_serialize_object_type(objectType);
auto meta = sai_metadata_get_attr_metadata(objectType, attrId);
if (meta == NULL)
{
SWSS_LOG_ERROR("Failed to find attribute metadata: object type %s, attr id %d", object_type_str.c_str(), attrId);
return SAI_STATUS_INVALID_PARAMETER;
}
const std::string attr_id_str = meta->attridname;
const std::string list_size = std::to_string(enumValuesCapability->count);
const std::vector<swss::FieldValueTuple> entry =
{
swss::FieldValueTuple("OBJECT_TYPE", object_type_str),
swss::FieldValueTuple("ATTR_ID", attr_id_str),
swss::FieldValueTuple("LIST_SIZE", list_size)
};
SWSS_LOG_DEBUG(
"Query arguments: switch %s, object type: %s, attribute: %s, count: %s",
switch_id_str.c_str(),
object_type_str.c_str(),
attr_id_str.c_str(),
list_size.c_str()
);
// This query will not put any data into the ASIC view, just into the
// message queue
m_communicationChannel->set(switch_id_str, entry, REDIS_ASIC_STATE_COMMAND_ATTR_ENUM_VALUES_CAPABILITY_QUERY);
auto status = waitForQueryAattributeEnumValuesCapabilityResponse(enumValuesCapability);
return status;
}
sai_status_t ClientSai::waitForQueryAattributeEnumValuesCapabilityResponse(
_Inout_ sai_s32_list_t* enumValuesCapability)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_ATTR_ENUM_VALUES_CAPABILITY_RESPONSE, kco);
if (status == SAI_STATUS_SUCCESS)
{
const std::vector<swss::FieldValueTuple> &values = kfvFieldsValues(kco);
if (values.size() != 2)
{
SWSS_LOG_ERROR("Invalid response from syncd: expected 2 values, received %zu", values.size());
return SAI_STATUS_FAILURE;
}
const std::string &capability_str = fvValue(values[0]);
const uint32_t num_capabilities = std::stoi(fvValue(values[1]));
SWSS_LOG_DEBUG("Received payload: capabilities = '%s', count = %d", capability_str.c_str(), num_capabilities);
enumValuesCapability->count = num_capabilities;
size_t position = 0;
for (uint32_t i = 0; i < num_capabilities; i++)
{
size_t old_position = position;
position = capability_str.find(",", old_position);
std::string capability = capability_str.substr(old_position, position - old_position);
enumValuesCapability->list[i] = std::stoi(capability);
// We have run out of values to add to our list
if (position == std::string::npos)
{
if (num_capabilities != i + 1)
{
SWSS_LOG_WARN("Query returned less attributes than expected: expected %d, received %d", num_capabilities, i+1);
}
break;
}
// Skip the commas
position++;
}
}
else if (status == SAI_STATUS_BUFFER_OVERFLOW)
{
// TODO on sai status overflow we should populate correct count on the list
SWSS_LOG_ERROR("TODO need to handle SAI_STATUS_BUFFER_OVERFLOW, FIXME");
}
return status;
}
// STATS API
sai_status_t ClientSai::getStats(
_In_ sai_object_type_t object_type,
_In_ sai_object_id_t object_id,
_In_ uint32_t number_of_counters,
_In_ const sai_stat_id_t *counter_ids,
_Out_ uint64_t *counters)
{
MUTEX();
SWSS_LOG_ENTER();
REDIS_CHECK_API_INITIALIZED();
auto stats_enum = sai_metadata_get_object_type_info(object_type)->statenum;
auto entry = serialize_counter_id_list(stats_enum, number_of_counters, counter_ids);
std::string str_object_type = sai_serialize_object_type(object_type);
std::string key = str_object_type + ":" + sai_serialize_object_id(object_id);
SWSS_LOG_DEBUG("generic get stats key: %s, fields: %lu", key.c_str(), entry.size());
// get_stats will not put data to asic view, only to message queue
m_communicationChannel->set(key, entry, REDIS_ASIC_STATE_COMMAND_GET_STATS);
return waitForGetStatsResponse(number_of_counters, counters);
}
sai_status_t ClientSai::waitForGetStatsResponse(
_In_ uint32_t number_of_counters,
_Out_ uint64_t *counters)
{
SWSS_LOG_ENTER();
swss::KeyOpFieldsValuesTuple kco;
auto status = m_communicationChannel->wait(REDIS_ASIC_STATE_COMMAND_GETRESPONSE, kco);
if (status == SAI_STATUS_SUCCESS)
{
auto &values = kfvFieldsValues(kco);
if (values.size () != number_of_counters)
{
SWSS_LOG_THROW("wrong number of counters, got %zu, expected %u", values.size(), number_of_counters);