-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathSwitchStateBase.cpp
4085 lines (3025 loc) · 121 KB
/
SwitchStateBase.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 "SwitchStateBase.h"
#include "swss/logger.h"
#include "meta/sai_serialize.h"
#include <net/if.h>
#include <unistd.h>
#include <algorithm>
#define SAI_VS_MAX_PORTS 1024
using namespace saivs;
SwitchStateBase::SwitchStateBase(
_In_ sai_object_id_t switch_id,
_In_ std::shared_ptr<RealObjectIdManager> manager,
_In_ std::shared_ptr<SwitchConfig> config):
SwitchState(switch_id, config),
m_realObjectIdManager(manager)
{
SWSS_LOG_ENTER();
m_macsecManager.cleanup_macsec_device();
}
SwitchStateBase::SwitchStateBase(
_In_ sai_object_id_t switch_id,
_In_ std::shared_ptr<RealObjectIdManager> manager,
_In_ std::shared_ptr<SwitchConfig> config,
_In_ std::shared_ptr<WarmBootState> warmBootState):
SwitchState(switch_id, config),
m_realObjectIdManager(manager)
{
SWSS_LOG_ENTER();
m_macsecManager.cleanup_macsec_device();
if (warmBootState)
{
for (auto& kvp: warmBootState->m_objectHash)
{
// we write only existing ones, since base constructor
// created empty entries for non existing object types
m_objectHash[kvp.first] = kvp.second;
}
if (m_switchConfig->m_useTapDevice)
{
m_fdb_info_set = warmBootState->m_fdbInfoSet;
// TODO populate m_hostif_info_map - need to be able to remove port after warm boot
// should be auto populated vs_recreate_hostif_tap_interfaces on create_switch
}
}
}
SwitchStateBase::~SwitchStateBase()
{
SWSS_LOG_ENTER();
m_macsecManager.cleanup_macsec_device();
}
sai_status_t SwitchStateBase::create(
_In_ sai_object_type_t object_type,
_Out_ sai_object_id_t *object_id,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
if (object_type == SAI_OBJECT_TYPE_SWITCH)
{
SWSS_LOG_THROW("this method can't be used to create switch");
}
*object_id = m_realObjectIdManager->allocateNewObjectId(object_type, switch_id);
auto sid = sai_serialize_object_id(*object_id);
return create_internal(object_type, sid, switch_id, attr_count, attr_list);
}
uint32_t SwitchStateBase::getNewDebugCounterIndex()
{
SWSS_LOG_ENTER();
for (uint32_t i = 0; i < maxDebugCounters; i++)
{
if (m_indices.find(i) == m_indices.end())
{
m_indices.insert(i);
return i;
}
}
return UINT32_MAX;
}
sai_status_t SwitchStateBase::createDebugCounter(
_In_ sai_object_id_t object_id,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
auto index = getNewDebugCounterIndex();
if (index > maxDebugCounters)
{
SWSS_LOG_ERROR("Cannot create more than %d debug counters", maxDebugCounters);
return SAI_STATUS_FAILURE;
}
auto sid = sai_serialize_object_id(object_id);
CHECK_STATUS(create_internal(SAI_OBJECT_TYPE_DEBUG_COUNTER, sid, switch_id, attr_count, attr_list));
sai_attribute_t attr;
attr.id = SAI_DEBUG_COUNTER_ATTR_INDEX;
attr.value.u32 = index;
return set(SAI_OBJECT_TYPE_DEBUG_COUNTER, sid, &attr);
}
sai_status_t SwitchStateBase::create(
_In_ sai_object_type_t object_type,
_In_ const std::string &serializedObjectId,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
if (object_type == SAI_OBJECT_TYPE_DEBUG_COUNTER)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createDebugCounter(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_PORT)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createPort(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_HOSTIF)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createHostif(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_MACSEC_PORT)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createMACsecPort(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_MACSEC_SC)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createMACsecSC(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_MACSEC_SA)
{
sai_object_id_t object_id;
sai_deserialize_object_id(serializedObjectId, object_id);
return createMACsecSA(object_id, switch_id, attr_count, attr_list);
}
if (object_type == SAI_OBJECT_TYPE_NEIGHBOR_ENTRY && m_system_port_list.size())
{
// Neighbor entry programming for VOQ systems
return createVoqSystemNeighborEntry(serializedObjectId, switch_id, attr_count, attr_list);
}
return create_internal(object_type, serializedObjectId, switch_id, attr_count, attr_list);
}
sai_status_t SwitchStateBase::create_internal(
_In_ sai_object_type_t object_type,
_In_ const std::string &serializedObjectId,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
auto &objectHash = m_objectHash.at(object_type);
if (m_switchConfig->m_resourceLimiter)
{
size_t limit = m_switchConfig->m_resourceLimiter->getObjectTypeLimit(object_type);
if (objectHash.size() >= limit)
{
SWSS_LOG_ERROR("too many %s, created %zu is resource limit",
sai_serialize_object_type(object_type).c_str(),
limit);
return SAI_STATUS_INSUFFICIENT_RESOURCES;
}
}
auto it = objectHash.find(serializedObjectId);
if (object_type != SAI_OBJECT_TYPE_SWITCH)
{
/*
* Switch is special, and object is already created by init.
*
* XXX revisit this.
*/
if (it != objectHash.end())
{
SWSS_LOG_ERROR("create failed, object already exists, object type: %s: id: %s",
sai_serialize_object_type(object_type).c_str(),
serializedObjectId.c_str());
return SAI_STATUS_ITEM_ALREADY_EXISTS;
}
}
if (objectHash.find(serializedObjectId) == objectHash.end())
{
/*
* Number of attributes may be zero, so see if actual entry was created
* with empty hash.
*/
objectHash[serializedObjectId] = {};
}
for (uint32_t i = 0; i < attr_count; ++i)
{
auto a = std::make_shared<SaiAttrWrap>(object_type, &attr_list[i]);
objectHash[serializedObjectId][a->getAttrMetadata()->attridname] = a;
}
return SAI_STATUS_SUCCESS;
}
void SwitchStateBase::releaseDebugCounterIndex(
_In_ uint32_t index)
{
SWSS_LOG_ENTER();
m_indices.erase(index);
}
sai_status_t SwitchStateBase::remove(
_In_ sai_object_type_t object_type,
_In_ sai_object_id_t objectId)
{
SWSS_LOG_ENTER();
auto sid = sai_serialize_object_id(objectId);
return remove(object_type, sid);
}
sai_status_t SwitchStateBase::removeDebugCounter(
_In_ sai_object_id_t objectId)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
attr.id = SAI_DEBUG_COUNTER_ATTR_INDEX;
CHECK_STATUS(get(SAI_OBJECT_TYPE_DEBUG_COUNTER, objectId, 1, &attr));
auto sid = sai_serialize_object_id(objectId);
CHECK_STATUS(remove_internal(SAI_OBJECT_TYPE_DEBUG_COUNTER, sid));
releaseDebugCounterIndex(attr.value.u32);
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::createPort(
_In_ sai_object_id_t object_id,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
auto sid = sai_serialize_object_id(object_id);
CHECK_STATUS(create_internal(SAI_OBJECT_TYPE_PORT, sid, switch_id, attr_count, attr_list));
return create_port_dependencies(object_id);
}
sai_status_t SwitchStateBase::removePort(
_In_ sai_object_id_t objectId)
{
SWSS_LOG_ENTER();
if (!isPortReadyToBeRemove(objectId))
{
SWSS_LOG_ERROR("port %s still have some active dependencies, can't remove",
sai_serialize_object_id(objectId).c_str());
return SAI_STATUS_OBJECT_IN_USE;
}
auto dep = getPortDependencies(objectId);
auto sid = sai_serialize_object_id(objectId);
CHECK_STATUS(remove_internal(SAI_OBJECT_TYPE_PORT, sid));
SWSS_LOG_NOTICE("port %s was successfully removed, removing depending objects now",
sai_serialize_object_id(objectId).c_str());
for (auto oid: dep)
{
// meta_sai_remove_oid automatically removed related oids internally
// so we just need to execute remove for virtual switch db
auto status = remove(objectTypeQuery(oid), oid);
if (status != SAI_STATUS_SUCCESS)
{
// we can't continue, there is a bug somewhere if we can't remove
// port related objects: queues, ipgs, sg
SWSS_LOG_THROW("FATAL: failed to removed port related oid: %s: %s, bug!",
sai_serialize_object_type(objectTypeQuery(oid)).c_str(),
sai_serialize_object_id(oid).c_str());
}
}
SWSS_LOG_NOTICE("successfully removed all %zu port related objects", dep.size());
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::remove(
_In_ sai_object_type_t object_type,
_In_ const std::string &serializedObjectId)
{
SWSS_LOG_ENTER();
if (object_type == SAI_OBJECT_TYPE_DEBUG_COUNTER)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removeDebugCounter(objectId);
}
if (object_type == SAI_OBJECT_TYPE_PORT)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removePort(objectId);
}
if (object_type == SAI_OBJECT_TYPE_HOSTIF)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removeHostif(objectId);
}
if (object_type == SAI_OBJECT_TYPE_MACSEC_PORT)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removeMACsecPort(objectId);
}
else if (object_type == SAI_OBJECT_TYPE_MACSEC_SC)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removeMACsecSC(objectId);
}
else if (object_type == SAI_OBJECT_TYPE_MACSEC_SA)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return removeMACsecSA(objectId);
}
return remove_internal(object_type, serializedObjectId);
}
sai_status_t SwitchStateBase::remove_internal(
_In_ sai_object_type_t object_type,
_In_ const std::string &serializedObjectId)
{
SWSS_LOG_ENTER();
SWSS_LOG_INFO("removing object: %s", serializedObjectId.c_str());
auto &objectHash = m_objectHash.at(object_type);
auto it = objectHash.find(serializedObjectId);
if (it == objectHash.end())
{
SWSS_LOG_ERROR("not found %s:%s",
sai_serialize_object_type(object_type).c_str(),
serializedObjectId.c_str());
return SAI_STATUS_ITEM_NOT_FOUND;
}
objectHash.erase(it);
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::setPort(
_In_ sai_object_id_t portId,
_In_ const sai_attribute_t* attr)
{
SWSS_LOG_ENTER();
if (attr && attr->id == SAI_PORT_ATTR_ADMIN_STATE && m_switchConfig->m_useTapDevice)
{
bool up = attr->value.booldata;
// find corresponding host if interface and bring it down !
for (auto& kvp: m_hostif_info_map)
{
auto tapname = kvp.first;
if (kvp.second->m_portId == portId)
{
std::string vethname = vs_get_veth_name(tapname, portId);
if (ifup(vethname.c_str(), portId, up, false))
{
SWSS_LOG_ERROR("if admin %s failed on %s failed: %s", (up ? "UP" : "DOWN"), vethname.c_str(), strerror(errno));
return SAI_STATUS_FAILURE;
}
SWSS_LOG_NOTICE("if admin %s success on %s", (up ? "UP" : "DOWN"), vethname.c_str());
break;
}
}
}
else if (attr && attr->id == SAI_PORT_ATTR_MTU && m_switchConfig->m_useTapDevice)
{
uint32_t mtu = attr->value.u32;
std::string name;
if (getTapNameFromPortId(portId, name))
{
SWSS_LOG_INFO("setting new MTU: %d on %s", mtu, name.c_str());
std::string vname = vs_get_veth_name(name, portId);
if (vs_set_dev_mtu(vname.c_str(), mtu) < 0)
{
SWSS_LOG_ERROR("failed to set MTU on portId %s",
sai_serialize_object_id(portId).c_str());
return SAI_STATUS_FAILURE;
}
}
}
auto sid = sai_serialize_object_id(portId);
return set_internal(SAI_OBJECT_TYPE_PORT, sid, attr);
}
sai_status_t SwitchStateBase::setAclEntry(
_In_ sai_object_id_t entry_id,
_In_ const sai_attribute_t* attr)
{
SWSS_LOG_ENTER();
if (attr && attr->id == SAI_ACL_ENTRY_ATTR_ACTION_MACSEC_FLOW)
{
return setAclEntryMACsecFlowActive(entry_id, attr);
}
auto sid = sai_serialize_object_id(entry_id);
return set_internal(SAI_OBJECT_TYPE_ACL_ENTRY, sid, attr);
}
sai_status_t SwitchStateBase::set(
_In_ sai_object_type_t objectType,
_In_ const std::string &serializedObjectId,
_In_ const sai_attribute_t* attr)
{
SWSS_LOG_ENTER();
if (objectType == SAI_OBJECT_TYPE_PORT)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return setPort(objectId, attr);
}
if (objectType == SAI_OBJECT_TYPE_ACL_ENTRY)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return setAclEntry(objectId, attr);
}
if (objectType == SAI_OBJECT_TYPE_MACSEC_SA)
{
sai_object_id_t objectId;
sai_deserialize_object_id(serializedObjectId, objectId);
return setMACsecSA(objectId, attr);
}
return set_internal(objectType, serializedObjectId, attr);
}
sai_status_t SwitchStateBase::set_internal(
_In_ sai_object_type_t objectType,
_In_ const std::string &serializedObjectId,
_In_ const sai_attribute_t* attr)
{
SWSS_LOG_ENTER();
auto it = m_objectHash.at(objectType).find(serializedObjectId);
if (it == m_objectHash.at(objectType).end())
{
SWSS_LOG_ERROR("not found %s:%s",
sai_serialize_object_type(objectType).c_str(),
serializedObjectId.c_str());
return SAI_STATUS_ITEM_NOT_FOUND;
}
auto &attrHash = it->second;
auto a = std::make_shared<SaiAttrWrap>(objectType, attr);
// set have only one attribute
attrHash[a->getAttrMetadata()->attridname] = a;
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::set(
_In_ sai_object_type_t objectType,
_In_ sai_object_id_t objectId,
_In_ const sai_attribute_t* attr)
{
SWSS_LOG_ENTER();
auto sid = sai_serialize_object_id(objectId);
return set(objectType, sid, attr);
}
sai_status_t SwitchStateBase::get(
_In_ sai_object_type_t object_type,
_In_ sai_object_id_t object_id,
_In_ uint32_t attr_count,
_Out_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
auto sid = sai_serialize_object_id(object_id);
return get(object_type, sid, attr_count, attr_list);
}
sai_status_t SwitchStateBase::get(
_In_ sai_object_type_t objectType,
_In_ const std::string &serializedObjectId,
_In_ uint32_t attr_count,
_Out_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
const auto &objectHash = m_objectHash.at(objectType);
auto it = objectHash.find(serializedObjectId);
if (it == objectHash.end())
{
SWSS_LOG_ERROR("not found %s:%s",
sai_serialize_object_type(objectType).c_str(),
serializedObjectId.c_str());
return SAI_STATUS_ITEM_NOT_FOUND;
}
/*
* We need reference here since we can potentially update attr hash for RO
* object.
*/
auto& attrHash = it->second;
/*
* Some of the list query maybe for length, so we can't do
* normal serialize, maybe with count only.
*/
sai_status_t final_status = SAI_STATUS_SUCCESS;
for (uint32_t idx = 0; idx < attr_count; ++idx)
{
sai_attr_id_t id = attr_list[idx].id;
auto meta = sai_metadata_get_attr_metadata(objectType, id);
if (meta == NULL)
{
SWSS_LOG_ERROR("failed to find attribute %d for %s:%s", id,
sai_serialize_object_type(objectType).c_str(),
serializedObjectId.c_str());
return SAI_STATUS_FAILURE;
}
sai_status_t status;
if (SAI_HAS_FLAG_READ_ONLY(meta->flags))
{
/*
* Read only attributes may require recalculation.
* Metadata makes sure that non object id's can't have
* read only attributes. So here is definitely OID.
*/
sai_object_id_t oid;
sai_deserialize_object_id(serializedObjectId, oid);
status = refresh_read_only(meta, oid);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_INFO("%s read only not implemented on %s",
meta->attridname,
serializedObjectId.c_str());
return status;
}
}
auto ait = attrHash.find(meta->attridname);
if (ait == attrHash.end())
{
SWSS_LOG_WARN("%s not implemented on %s",
meta->attridname,
serializedObjectId.c_str());
return SAI_STATUS_NOT_IMPLEMENTED;
}
auto attr = ait->second->getAttr();
status = transfer_attributes(objectType, 1, attr, &attr_list[idx], false);
if (status == SAI_STATUS_BUFFER_OVERFLOW)
{
/*
* This is considered partial success, since we get correct list
* length. Note that other items ARE processes on the list.
*/
SWSS_LOG_NOTICE("BUFFER_OVERFLOW %s: %s",
serializedObjectId.c_str(),
meta->attridname);
/*
* We still continue processing other attributes for get as long as
* we only will be getting buffer overflow error.
*/
final_status = status;
continue;
}
if (status != SAI_STATUS_SUCCESS)
{
// all other errors
SWSS_LOG_ERROR("get failed %s: %s: %s",
serializedObjectId.c_str(),
meta->attridname,
sai_serialize_status(status).c_str());
return status;
}
}
return final_status;
}
sai_status_t SwitchStateBase::bulkCreate(
_In_ sai_object_id_t switch_id,
_In_ sai_object_type_t object_type,
_In_ const std::vector<std::string> &serialized_object_ids,
_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();
uint32_t object_count = (uint32_t) serialized_object_ids.size();
if (!object_count || !attr_count || !attr_list || !object_statuses)
{
SWSS_LOG_ERROR("Invalid arguments");
return SAI_STATUS_FAILURE;
}
sai_status_t status = SAI_STATUS_SUCCESS;
uint32_t it;
for (it = 0; it < object_count; it++)
{
object_statuses[it] = create(object_type, serialized_object_ids[it], switch_id, attr_count[it], attr_list[it]);
if (object_statuses[it] != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create object with type = %u", object_type);
status = SAI_STATUS_FAILURE;
if (mode == SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR)
{
break;
}
}
}
while (++it < object_count)
{
object_statuses[it] = SAI_STATUS_NOT_EXECUTED;
}
return status;
}
sai_status_t SwitchStateBase::bulkRemove(
_In_ sai_object_type_t object_type,
_In_ const std::vector<std::string> &serialized_object_ids,
_In_ sai_bulk_op_error_mode_t mode,
_Out_ sai_status_t *object_statuses)
{
SWSS_LOG_ENTER();
uint32_t object_count = (uint32_t) serialized_object_ids.size();
if (!object_count || !object_statuses)
{
SWSS_LOG_ERROR("Invalid arguments");
return SAI_STATUS_FAILURE;
}
sai_status_t status = SAI_STATUS_SUCCESS;
uint32_t it;
for (it = 0; it < object_count; it++)
{
object_statuses[it] = remove(object_type, serialized_object_ids[it]);
if (object_statuses[it] != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove object with type = %u", object_type);
status = SAI_STATUS_FAILURE;
if (mode == SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR)
{
break;
}
}
}
while (++it < object_count)
{
object_statuses[it] = SAI_STATUS_NOT_EXECUTED;
}
return status;
}
sai_status_t SwitchStateBase::bulkSet(
_In_ sai_object_type_t object_type,
_In_ const std::vector<std::string> &serialized_object_ids,
_In_ const sai_attribute_t *attr_list,
_In_ sai_bulk_op_error_mode_t mode,
_Out_ sai_status_t *object_statuses)
{
SWSS_LOG_ENTER();
uint32_t object_count = (uint32_t) serialized_object_ids.size();
if (!object_count || !attr_list || !object_statuses)
{
SWSS_LOG_ERROR("Invalid arguments");
return SAI_STATUS_FAILURE;
}
sai_status_t status = SAI_STATUS_SUCCESS;
uint32_t it;
for (it = 0; it < object_count; it++)
{
object_statuses[it] = set_internal(object_type, serialized_object_ids[it], &attr_list[it]);
if (object_statuses[it] != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set attribute for object with type = %u", object_type);
status = SAI_STATUS_FAILURE;
if (mode == SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR)
{
break;
}
}
}
while (++it < object_count)
{
object_statuses[it] = SAI_STATUS_NOT_EXECUTED;
}
return status;
}
sai_status_t SwitchStateBase::bulkGet(
_In_ sai_object_type_t object_type,
_In_ const std::vector<std::string> &serialized_object_ids,
_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)
{
SWSS_LOG_ENTER();
uint32_t it;
uint32_t object_count = (uint32_t) serialized_object_ids.size();
sai_status_t status = SAI_STATUS_SUCCESS;
if (!object_count || !attr_list || !attr_count || !object_statuses)
{
SWSS_LOG_ERROR("Invalid arguments");
return SAI_STATUS_FAILURE;
}
for (it = 0; it < object_count; it++)
{
if (!attr_list[it] || !attr_count[it])
{
SWSS_LOG_ERROR("Invalid arguments");
return SAI_STATUS_FAILURE;
}
}
for (it = 0; it < object_count; it++)
{
object_statuses[it] = get(object_type, serialized_object_ids[it], attr_count[it], attr_list[it]);
if (object_statuses[it] != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get attribute for object with type = %u", object_type);
status = SAI_STATUS_FAILURE;
if (mode == SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR)
{
break;
}
}
}
while (++it < object_count)
{
object_statuses[it] = SAI_STATUS_NOT_EXECUTED;
}
return status;
}
int SwitchStateBase::get_default_gw_mac_address(
_Out_ sai_mac_t& mac)
{
SWSS_LOG_ENTER();
auto file = std::ifstream("/proc/net/route");
if (!file)
{
return -1;
}
std::string buf;
while (std::getline(file, buf))
{
char iface[IF_NAMESIZE];
long destination, gateway;
if (std::sscanf(buf.c_str(), "%s %lx %lx", iface, &destination, &gateway) == 3)
{
if (destination == 0)
{
file = std::ifstream("/sys/class/net/" + std::string(iface) + "/address");
if ( !file )
{
return -1;
}
file >> buf;
return std::sscanf(buf.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6 ? 0 : -1;
}
}
}
return -1;
}
sai_status_t SwitchStateBase::set_switch_mac_address()
{
SWSS_LOG_ENTER();
SWSS_LOG_INFO("create switch src mac address");
sai_attribute_t attr;
attr.id = SAI_SWITCH_ATTR_SRC_MAC_ADDRESS;
if (get_default_gw_mac_address(attr.value.mac) < 0)
{
attr.value.mac[0] = 0x22;
attr.value.mac[1] = 0x33;
attr.value.mac[2] = 0x44;
attr.value.mac[3] = 0x55;
attr.value.mac[4] = 0x66;
attr.value.mac[5] = 0x77;
}
return set(SAI_OBJECT_TYPE_SWITCH, m_switch_id, &attr);
}
sai_status_t SwitchStateBase::set_vxlan_default_router_mac()
{
SWSS_LOG_ENTER();
SWSS_LOG_INFO("create switch vxlan default router mac address");
sai_attribute_t attr;
attr.id = SAI_SWITCH_ATTR_VXLAN_DEFAULT_ROUTER_MAC;
// NOTE if there is default vxlan mac present like in case of get_default_gw_mac_address
// then that mac should be used
{
attr.value.mac[0] = 0x12;
attr.value.mac[1] = 0x23;
attr.value.mac[2] = 0x34;
attr.value.mac[3] = 0x45;
attr.value.mac[4] = 0x56;
attr.value.mac[5] = 0x67;
}
return set(SAI_OBJECT_TYPE_SWITCH, m_switch_id, &attr);
}
sai_status_t SwitchStateBase::set_switch_supported_object_types()
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
// Fill this with supported SAI_OBJECT_TYPEs
sai_object_type_t supported_obj_list[] = {
SAI_OBJECT_TYPE_PORT,