-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathchassis.py
1352 lines (1144 loc) · 51.5 KB
/
chassis.py
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
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#############################################################################
# Mellanox
#
# Module contains an implementation of SONiC Platform Base API and
# provides the Chassis information which are available in the platform
#
#############################################################################
try:
from sonic_platform_base.chassis_base import ChassisBase
from sonic_py_common.logger import Logger
import os
from functools import reduce
from .utils import extract_RJ45_ports_index
from . import module_host_mgmt_initializer
from . import utils
from .device_data import DeviceDataManager
import re
import select
import threading
import time
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
RJ45_TYPE = "RJ45"
VPD_DATA_FILE = "/var/run/hw-management/eeprom/vpd_data"
REVISION = "REV"
HWMGMT_SYSTEM_ROOT = '/var/run/hw-management/system/'
#reboot cause related definitions
REBOOT_CAUSE_ROOT = HWMGMT_SYSTEM_ROOT
REBOOT_CAUSE_MAX_WAIT_TIME = 45
REBOOT_CAUSE_CHECK_INTERVAL = 5
REBOOT_CAUSE_READY_FILE = '/run/hw-management/config/reset_attr_ready'
REBOOT_TYPE_KEXEC_FILE = "/proc/cmdline"
REBOOT_TYPE_KEXEC_PATTERN_WARM = ".*SONIC_BOOT_TYPE=(warm|fastfast).*"
REBOOT_TYPE_KEXEC_PATTERN_FAST = ".*SONIC_BOOT_TYPE=(fast|fast-reboot).*"
# Global logger class instance
logger = Logger()
class Chassis(ChassisBase):
"""Platform-specific Chassis class"""
# System status LED
_led = None
# System UID LED
_led_uid = None
chassis_instance = None
def __init__(self):
super(Chassis, self).__init__()
# Initialize vpd data
self.vpd_data = None
# move the initialization of each components to their dedicated initializer
# which will be called from platform
#
# Multiple scenarios need to be taken into consideration regarding the SFP modules initialization.
# - Platform daemons
# - Can access multiple or all SFP modules
# - sfputil
# - Sometimes can access only one SFP module
# - Call get_sfp to get one SFP module object
#
# We should initialize all SFP modules only if it is necessary because initializing SFP module is time-consuming.
# This means,
# - If get_sfp is called,
# - If the _sfp_list isn't initialized, initialize it first.
# - Only the SFP module being required should be initialized.
# - If get_all_sfps is called,
# - If the _sfp_list isn't initialized, initialize it first.
# - All SFP modules need to be initialized.
# But the SFP modules that have already been initialized should not be initialized for the second time.
# This can caused by get_sfp being called before.
#
# Due to the complexity of SFP modules initialization, we have to introduce two initialized flags for SFP modules
# - sfp_module_partial_initialized:
# - False: The _sfp_list is [] (SFP stuff has never been accessed)
# - True: The _sfp_list is a list whose length is number of SFP modules supported by the platform
# - sfp_module_full_initialized:
# - False: All SFP modules have not been created
# - True: All SFP modules have been created
#
self.sfp_initialized_count = 0
self.sfp_event = None
self.reboot_cause_initialized = False
self.sfp_module = None
self.sfp_lock = threading.Lock()
# Build the RJ45 port list from platform.json and hwsku.json
self._RJ45_port_inited = False
self._RJ45_port_list = None
Chassis.chassis_instance = self
self.module_host_mgmt_initializer = module_host_mgmt_initializer.ModuleHostMgmtInitializer()
self.poll_obj = None
self.registered_fds = None
logger.log_info("Chassis loaded successfully")
def __del__(self):
if self.sfp_event:
self.sfp_event.deinitialize()
@property
def RJ45_port_list(self):
if not self._RJ45_port_inited:
self._RJ45_port_list = extract_RJ45_ports_index()
self._RJ45_port_inited = True
return self._RJ45_port_list
##############################################
# PSU methods
##############################################
def initialize_psu(self):
if not self._psu_list:
from .psu import Psu, FixedPsu
psu_count = DeviceDataManager.get_psu_count()
hot_swapable = DeviceDataManager.is_psu_hotswapable()
# Initialize PSU list
for index in range(psu_count):
if hot_swapable:
psu = Psu(index)
else:
psu = FixedPsu(index)
self._psu_list.append(psu)
def get_num_psus(self):
"""
Retrieves the number of power supply units available on this chassis
Returns:
An integer, the number of power supply units available on this
chassis
"""
self.initialize_psu()
return len(self._psu_list)
def get_all_psus(self):
"""
Retrieves all power supply units available on this chassis
Returns:
A list of objects derived from PsuBase representing all power
supply units available on this chassis
"""
self.initialize_psu()
return self._psu_list
def get_psu(self, index):
"""
Retrieves power supply unit represented by (0-based) index <index>
Args:
index: An integer, the index (0-based) of the power supply unit to
retrieve
Returns:
An object dervied from PsuBase representing the specified power
supply unit
"""
self.initialize_psu()
return super(Chassis, self).get_psu(index)
##############################################
# Fan methods
##############################################
def initialize_fan(self):
if not self._fan_drawer_list:
from .fan import Fan
from .fan_drawer import RealDrawer, VirtualDrawer
hot_swapable = DeviceDataManager.is_fan_hotswapable()
drawer_num = DeviceDataManager.get_fan_drawer_count()
fan_num = DeviceDataManager.get_fan_count()
fan_num_per_drawer = fan_num // drawer_num
drawer_ctor = RealDrawer if hot_swapable else VirtualDrawer
fan_index = 0
for drawer_index in range(drawer_num):
drawer = drawer_ctor(drawer_index)
self._fan_drawer_list.append(drawer)
for index in range(fan_num_per_drawer):
fan = Fan(fan_index, drawer, index + 1)
fan_index += 1
drawer._fan_list.append(fan)
def get_num_fan_drawers(self):
"""
Retrieves the number of fan drawers available on this chassis
Returns:
An integer, the number of fan drawers available on this chassis
"""
return DeviceDataManager.get_fan_drawer_count()
def get_all_fan_drawers(self):
"""
Retrieves all fan drawers available on this chassis
Returns:
A list of objects derived from FanDrawerBase representing all fan
drawers available on this chassis
"""
self.initialize_fan()
return self._fan_drawer_list
def get_fan_drawer(self, index):
"""
Retrieves fan drawers represented by (0-based) index <index>
Args:
index: An integer, the index (0-based) of the fan drawer to
retrieve
Returns:
An object dervied from FanDrawerBase representing the specified fan
drawer
"""
self.initialize_fan()
return super(Chassis, self).get_fan_drawer(index)
##############################################
# SFP methods
##############################################
def _import_sfp_module(self):
if not self.sfp_module:
from . import sfp as sfp_module
self.sfp_module = sfp_module
return self.sfp_module
def initialize_single_sfp(self, index):
sfp_count = self.get_num_sfps()
# Use double checked locking mechanism for:
# 1. protect shared resource self._sfp_list
# 2. performance (avoid locking every time)
if index < sfp_count:
if not self._sfp_list or not self._sfp_list[index]:
with self.sfp_lock:
if not self._sfp_list:
self._sfp_list = [None] * sfp_count
if not self._sfp_list[index]:
sfp_module = self._import_sfp_module()
if self.RJ45_port_list and index in self.RJ45_port_list:
self._sfp_list[index] = sfp_module.RJ45Port(index)
else:
self._sfp_list[index] = sfp_module.SFP(index)
self.sfp_initialized_count += 1
def initialize_sfp(self):
sfp_count = self.get_num_sfps()
# Use double checked locking mechanism for:
# 1. protect shared resource self._sfp_list
# 2. performance (avoid locking every time)
if sfp_count != self.sfp_initialized_count:
with self.sfp_lock:
if sfp_count != self.sfp_initialized_count:
if not self._sfp_list:
sfp_module = self._import_sfp_module()
for index in range(sfp_count):
if self.RJ45_port_list and index in self.RJ45_port_list:
sfp_object = sfp_module.RJ45Port(index)
else:
sfp_object = sfp_module.SFP(index)
self._sfp_list.append(sfp_object)
self.sfp_initialized_count = sfp_count
elif self.sfp_initialized_count != len(self._sfp_list):
sfp_module = self._import_sfp_module()
for index in range(len(self._sfp_list)):
if self._sfp_list[index] is None:
if self.RJ45_port_list and index in self.RJ45_port_list:
self._sfp_list[index] = sfp_module.RJ45Port(index)
else:
self._sfp_list[index] = sfp_module.SFP(index)
self.sfp_initialized_count = len(self._sfp_list)
def get_num_sfps(self):
"""
Retrieves the number of sfps available on this chassis
Returns:
An integer, the number of sfps available on this chassis
"""
if not self._RJ45_port_inited:
self._RJ45_port_list = extract_RJ45_ports_index()
self._RJ45_port_inited = True
if self._RJ45_port_list is not None:
return DeviceDataManager.get_sfp_count() + len(self._RJ45_port_list)
else:
return DeviceDataManager.get_sfp_count()
def get_all_sfps(self):
"""
Retrieves all sfps available on this chassis
Returns:
A list of objects derived from SfpBase representing all sfps
available on this chassis
"""
if DeviceDataManager.is_module_host_management_mode():
self.module_host_mgmt_initializer.initialize(self)
else:
self.initialize_sfp()
return self._sfp_list
def get_sfp(self, index):
"""
Retrieves sfp represented by (1-based) index <index>
Args:
index: An integer, the index (1-based) of the sfp to retrieve.
The index should be the sequence of a physical port in a chassis,
starting from 1.
For example, 1 for Ethernet0, 2 for Ethernet4 and so on.
Returns:
An object dervied from SfpBase representing the specified sfp
"""
index = index - 1
if DeviceDataManager.is_module_host_management_mode():
self.module_host_mgmt_initializer.initialize(self)
else:
self.initialize_single_sfp(index)
return super(Chassis, self).get_sfp(index)
def get_port_or_cage_type(self, index):
"""
Retrieves sfp port or cage type corresponding to physical port <index>
Args:
index: An integer (>=0), the index of the sfp to retrieve.
The index should correspond to the physical port in a chassis.
For example:-
1 for Ethernet0, 2 for Ethernet4 and so on for one platform.
0 for Ethernet0, 1 for Ethernet4 and so on for another platform.
Returns:
The masks of all types of port or cage that can be supported on the port
Types are defined in sfp_base.py
Eg.
Both SFP and SFP+ are supported on the port, the return value should be 0x0a
which is 0x02 | 0x08
"""
index = index - 1
if self.RJ45_port_list and index in self.RJ45_port_list:
from sonic_platform_base.sfp_base import SfpBase
return SfpBase.SFP_PORT_TYPE_BIT_RJ45
raise NotImplementedError
def get_change_event(self, timeout=0):
"""
Returns a nested dictionary containing all devices which have
experienced a change at chassis level
Args:
timeout: Timeout in milliseconds (optional). If timeout == 0,
this method will block until a change is detected.
Returns:
(bool, dict):
- True if call successful, False if not; - Deprecated, will always return True
- A nested dictionary where key is a device type,
value is a dictionary with key:value pairs in the format of
{'device_id':'device_event'},
where device_id is the device ID for this device and
device_event,
status='1' represents device inserted,
status='0' represents device removed.
Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}}
indicates that fan 0 has been removed, fan 2
has been inserted and sfp 11 has been removed.
"""
if DeviceDataManager.is_module_host_management_mode():
self.module_host_mgmt_initializer.initialize(self)
return self.get_change_event_for_module_host_management_mode(timeout)
else:
self.initialize_sfp()
return self.get_change_event_legacy(timeout)
def get_change_event_for_module_host_management_mode(self, timeout):
"""Get SFP change event when module host management mode is enabled.
Args:
timeout: Timeout in milliseconds (optional). If timeout == 0,
this method will block until a change is detected.
Returns:
(bool, dict):
- True if call successful, False if not; - Deprecated, will always return True
- A nested dictionary where key is a device type,
value is a dictionary with key:value pairs in the format of
{'device_id':'device_event'},
where device_id is the device ID for this device and
device_event,
status='1' represents device inserted,
status='0' represents device removed.
Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}}
indicates that fan 0 has been removed, fan 2
has been inserted and sfp 11 has been removed.
"""
if not self.poll_obj:
self.poll_obj = select.poll()
self.registered_fds = {}
for s in self._sfp_list:
fds = s.get_fds_for_poling()
for fd_type, fd in fds.items():
self.poll_obj.register(fd, select.POLLERR | select.POLLPRI)
self.registered_fds[fd.fileno()] = (s.sdk_index, fd, fd_type)
logger.log_debug(f'Registered SFP file descriptors for polling: {self.registered_fds}')
from . import sfp
wait_forever = (timeout == 0)
# poll timeout should be no more than 1000ms to ensure fast shutdown flow
timeout = 1000.0 if timeout >= 1000 else float(timeout)
port_dict = {}
error_dict = {}
begin = time.time()
wait_ready_task = sfp.SFP.get_wait_ready_task()
while True:
fds_events = self.poll_obj.poll(timeout)
for fileno, _ in fds_events:
if fileno not in self.registered_fds:
logger.log_error(f'Unknown file no {fileno} from poll event, registered files are {self.registered_fds}')
continue
sfp_index, fd, fd_type = self.registered_fds[fileno]
s = self._sfp_list[sfp_index]
fd.seek(0)
fd_value = int(fd.read().strip())
# Detecting dummy event
if s.is_dummy_event(fd_type, fd_value):
# Ignore dummy event for the first poll, assume SDK only provide 1 dummy event
logger.log_debug(f'Ignore dummy event {fd_type}:{fd_value} for SFP {sfp_index}')
continue
logger.log_notice(f'Got SFP event: index={sfp_index}, type={fd_type}, value={fd_value}')
if fd_type == 'hw_present':
# event could be EVENT_NOT_PRESENT or EVENT_PRESENT
event = sfp.EVENT_NOT_PRESENT if fd_value == 0 else sfp.EVENT_PRESENT
if fd_value == 1:
s.processing_insert_event = True
s.on_event(event)
elif fd_type == 'present':
if str(fd_value) == sfp.SFP_STATUS_ERROR:
# FW control cable got an error, no need trigger state machine
sfp_status, error_desc = s.get_error_info_from_sdk_error_type()
port_dict[sfp_index + 1] = sfp_status
if error_desc:
error_dict[sfp_index + 1] = error_desc
continue
elif str(fd_value) == sfp.SFP_STATUS_INSERTED:
# FW control cable got present, only case is that the cable is recovering
# from an error. FW control cable has no transition from "Not Present" to "Present"
# because "Not Present" cable is always "software control" and should always poll
# hw_present sysfs instead of present sysfs.
port_dict[sfp_index + 1] = sfp.SFP_STATUS_INSERTED
continue
else:
s.on_event(sfp.EVENT_NOT_PRESENT)
else:
# event could be EVENT_POWER_GOOD or EVENT_POWER_BAD
event = sfp.EVENT_POWER_BAD if fd_value == 0 else sfp.EVENT_POWER_GOOD
s.on_event(event)
if s.in_stable_state():
self.sfp_module.SFP.wait_sfp_eeprom_ready([s], 2)
s.fill_change_event(port_dict)
s.refresh_poll_obj(self.poll_obj, self.registered_fds)
else:
logger.log_debug(f'SFP {sfp_index} does not reach stable state, state={s.state}')
ready_sfp_set = wait_ready_task.get_ready_set()
for sfp_index in ready_sfp_set:
s = self._sfp_list[sfp_index]
s.on_event(sfp.EVENT_RESET_DONE)
if s.in_stable_state():
self.sfp_module.SFP.wait_sfp_eeprom_ready([s], 2)
s.fill_change_event(port_dict)
s.refresh_poll_obj(self.poll_obj, self.registered_fds)
else:
logger.log_error(f'SFP {sfp_index} failed to reach stable state, state={s.state}')
if port_dict:
logger.log_notice(f'Sending SFP change event: {port_dict}, error event: {error_dict}')
self.reinit_sfps(port_dict)
return True, {
'sfp': port_dict,
'sfp_error': error_dict
}
else:
if not wait_forever:
elapse = time.time() - begin
if elapse * 1000 >= timeout:
return True, {'sfp': {}}
def get_change_event_legacy(self, timeout):
"""Get SFP change event when module host management is disabled.
Args:
timeout (int): polling timeout in ms
Returns:
(bool, dict):
- True if call successful, False if not; - Deprecated, will always return True
- A nested dictionary where key is a device type,
value is a dictionary with key:value pairs in the format of
{'device_id':'device_event'},
where device_id is the device ID for this device and
device_event,
status='1' represents device inserted,
status='0' represents device removed.
Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}}
indicates that fan 0 has been removed, fan 2
has been inserted and sfp 11 has been removed.
"""
if not self.poll_obj:
self.poll_obj = select.poll()
self.registered_fds = {}
# SDK always sent event for the first time polling. Such event should not be sent to xcvrd.
# Store SFP state before first time polling so that we can detect dummy event.
self.sfp_states_before_first_poll = {}
for s in self._sfp_list:
fd = s.get_fd_for_polling_legacy()
self.poll_obj.register(fd, select.POLLERR | select.POLLPRI)
self.registered_fds[fd.fileno()] = (s.sdk_index, fd)
self.sfp_states_before_first_poll[s.sdk_index] = s.get_module_status()
logger.log_debug(f'Registered SFP file descriptors for polling: {self.registered_fds}')
from . import sfp
wait_forever = (timeout == 0)
# poll timeout should be no more than 1000ms to ensure fast shutdown flow
timeout = 1000.0 if timeout >= 1000 else float(timeout)
port_dict = {}
error_dict = {}
begin = time.time()
while True:
fds_events = self.poll_obj.poll(timeout)
for fileno, _ in fds_events:
if fileno not in self.registered_fds:
logger.log_error(f'Unknown file no {fileno} from poll event, registered files are {self.registered_fds}')
continue
sfp_index, fd = self.registered_fds[fileno]
fd.seek(0)
fd.read()
s = self._sfp_list[sfp_index]
sfp_status = s.get_module_status()
if sfp_index in self.sfp_states_before_first_poll:
# Detecting dummy event
sfp_state_before_poll = self.sfp_states_before_first_poll[sfp_index]
self.sfp_states_before_first_poll.pop(sfp_index)
if sfp_state_before_poll == sfp_status:
# Ignore dummy event for the first poll, assume SDK only provide 1 dummy event
logger.log_debug(f'Ignore dummy event {sfp_status} for SFP {sfp_index}')
continue
logger.log_notice(f'Got SFP event: index={sfp_index}, value={sfp_status}')
if sfp_status == sfp.SFP_STATUS_UNKNOWN:
# in the following sequence, STATUS_UNKNOWN can be returned.
# so we shouldn't raise exception here.
# 1. some sfp module is inserted
# 2. sfp_event gets stuck and fails to fetch the change event instantaneously
# 3. and then the sfp module is removed
# 4. sfp_event starts to try fetching the change event
logger.log_notice("unknown module state, maybe the port suffers two adjacent insertion/removal")
continue
if sfp_status == sfp.SFP_STATUS_ERROR:
sfp_status, error_desc = s.get_error_info_from_sdk_error_type()
if error_desc:
error_dict[sfp_index + 1] = error_desc
port_dict[sfp_index + 1] = sfp_status
if port_dict:
logger.log_notice(f'Sending SFP change event: {port_dict}, error event: {error_dict}')
self.reinit_sfps(port_dict)
return True, {
'sfp': port_dict,
'sfp_error': error_dict
}
else:
if not wait_forever:
elapse = time.time() - begin
if elapse * 1000 >= timeout:
return True, {'sfp': {}}
def reinit_sfps(self, port_dict):
"""
Re-initialize SFP if there is any newly inserted SFPs
:param port_dict: SFP event data
:return:
"""
from . import sfp
for index, status in port_dict.items():
if status == sfp.SFP_STATUS_REMOVED:
try:
self._sfp_list[int(index) - 1].reinit()
except Exception as e:
logger.log_error("Fail to re-initialize SFP {} - {}".format(index, repr(e)))
def _show_capabilities(self):
"""
This function is for debug purpose
Some features require a xSFP module to support some capabilities but it's unrealistic to
check those modules one by one.
So this function is introduce to show some capabilities of all xSFP modules mounted on the device.
"""
self.initialize_sfp()
for s in self._sfp_list:
try:
print("index {} tx disable {} dom {} calibration {} temp {} volt {} power (tx {} rx {})".format(s.index,
s.dom_tx_disable_supported,
s.dom_supported,
s.calibration,
s.dom_temp_supported,
s.dom_volt_supported,
s.dom_rx_power_supported,
s.dom_tx_power_supported
))
except:
print("fail to retrieve capabilities for module index {}".format(s.index))
##############################################
# THERMAL methods
##############################################
def initialize_thermals(self):
if not self._thermal_list:
from .thermal import initialize_chassis_thermals
# Initialize thermals
self._thermal_list = initialize_chassis_thermals()
def get_num_thermals(self):
"""
Retrieves the number of thermals available on this chassis
Returns:
An integer, the number of thermals available on this chassis
"""
self.initialize_thermals()
return len(self._thermal_list)
def get_all_thermals(self):
"""
Retrieves all thermals available on this chassis
Returns:
A list of objects derived from ThermalBase representing all thermals
available on this chassis
"""
self.initialize_thermals()
return self._thermal_list
def get_thermal(self, index):
"""
Retrieves thermal unit represented by (0-based) index <index>
Args:
index: An integer, the index (0-based) of the thermal to
retrieve
Returns:
An object dervied from ThermalBase representing the specified thermal
"""
self.initialize_thermals()
return super(Chassis, self).get_thermal(index)
##############################################
# EEPROM methods
##############################################
def initialize_eeprom(self):
if not self._eeprom:
from .eeprom import Eeprom
# Initialize EEPROM
self._eeprom = Eeprom()
def get_eeprom(self):
"""
Retreives eeprom device on this chassis
Returns:
An object derived from WatchdogBase representing the hardware
eeprom device
"""
self.initialize_eeprom()
return self._eeprom
def get_name(self):
"""
Retrieves the name of the device
Returns:
string: The name of the device
"""
self.initialize_eeprom()
return self._eeprom.get_product_name()
def get_model(self):
"""
Retrieves the model number (or part number) of the device
Returns:
string: Model/part number of device
"""
self.initialize_eeprom()
return self._eeprom.get_part_number()
def get_base_mac(self):
"""
Retrieves the base MAC address for the chassis
Returns:
A string containing the MAC address in the format
'XX:XX:XX:XX:XX:XX'
"""
self.initialize_eeprom()
return self._eeprom.get_base_mac()
def get_serial(self):
"""
Retrieves the hardware serial number for the chassis
Returns:
A string containing the hardware serial number for this chassis.
"""
self.initialize_eeprom()
return self._eeprom.get_serial_number()
def get_system_eeprom_info(self):
"""
Retrieves the full content of system EEPROM information for the chassis
Returns:
A dictionary where keys are the type code defined in
OCP ONIE TlvInfo EEPROM format and values are their corresponding
values.
"""
self.initialize_eeprom()
return self._eeprom.get_system_eeprom_info()
##############################################
# Component methods
##############################################
def initialize_components(self):
if not utils.is_host():
return
if not self._component_list:
# Initialize component list
from .component import ComponentONIE, ComponentSSD, ComponentBIOS, ComponentCPLD
self._component_list.append(ComponentONIE())
self._component_list.append(ComponentSSD())
self._component_list.append(DeviceDataManager.get_bios_component())
self._component_list.extend(DeviceDataManager.get_cpld_component_list())
def get_num_components(self):
"""
Retrieves the number of components available on this chassis
Returns:
An integer, the number of components available on this chassis
"""
self.initialize_components()
return len(self._component_list)
def get_all_components(self):
"""
Retrieves all components available on this chassis
Returns:
A list of objects derived from ComponentBase representing all components
available on this chassis
"""
self.initialize_components()
return self._component_list
def get_component(self, index):
"""
Retrieves component represented by (0-based) index <index>
Args:
index: An integer, the index (0-based) of the component to retrieve
Returns:
An object dervied from ComponentBase representing the specified component
"""
self.initialize_components()
return super(Chassis, self).get_component(index)
##############################################
# System LED methods
##############################################
def initizalize_system_led(self):
if not Chassis._led:
from .led import SystemLed, \
SystemUidLed
Chassis._led = SystemLed()
Chassis._led_uid = SystemUidLed()
def set_status_led(self, color):
"""
Sets the state of the system LED
Args:
color: A string representing the color with which to set the
system LED
Returns:
bool: True if system LED state is set successfully, False if not
"""
self.initizalize_system_led()
return False if not Chassis._led else Chassis._led.set_status(color)
def get_status_led(self):
"""
Gets the state of the system LED
Returns:
A string, one of the valid LED color strings which could be vendor
specified.
"""
self.initizalize_system_led()
return None if not Chassis._led else Chassis._led.get_status()
def set_uid_led(self, color):
"""
Sets the state of the system UID LED
Args:
color: A string representing the color with which to set the
system UID LED
Returns:
bool: True if system LED state is set successfully, False if not
"""
self.initizalize_system_led()
return False if not Chassis._led_uid else Chassis._led_uid.set_status(color)
def get_uid_led(self):
"""
Gets the state of the system UID LED
Returns:
A string, one of the valid LED color strings which could be vendor
specified.
"""
self.initizalize_system_led()
return None if not Chassis._led_uid else Chassis._led_uid.get_status()
def get_watchdog(self):
"""
Retrieves hardware watchdog device on this chassis
Returns:
An object derived from WatchdogBase representing the hardware
watchdog device
Note:
We overload this method to ensure that watchdog is only initialized
when it is referenced. Currently, only one daemon can open the watchdog.
To initialize watchdog in the constructor causes multiple daemon
try opening watchdog when loading and constructing a chassis object
and fail. By doing so we can eliminate that risk.
"""
try:
if self._watchdog is None:
from .watchdog import get_watchdog
self._watchdog = get_watchdog()
except Exception as e:
logger.log_info("Fail to load watchdog due to {}".format(repr(e)))
return self._watchdog
def get_revision(self):
"""
Retrieves the hardware revision of the device
Returns:
string: Revision value of device
"""
if not self.vpd_data:
self.vpd_data = self._parse_vpd_data(VPD_DATA_FILE)
return self.vpd_data.get(REVISION, "N/A")
def _parse_vpd_data(self, filename):
"""
Read vpd_data and returns a dictionary of values
Returns:
A dictionary containing the dmi table of the switch chassis info
"""
result = {}
try:
if not os.access(filename, os.R_OK):
return result
result = utils.read_key_value_file(filename, delimeter=": ")
except Exception as e:
logger.log_error("Fail to decode vpd_data {} due to {}".format(filename, repr(e)))
return result
def _verify_reboot_cause(self, filename):
'''
Open and read the reboot cause file in
/var/run/hwmanagement/system (which is defined as REBOOT_CAUSE_ROOT)
If a reboot cause file doesn't exists, returns '0'.
'''
return bool(utils.read_int_from_file(os.path.join(REBOOT_CAUSE_ROOT, filename), log_func=None))
def initialize_reboot_cause(self):
self.reboot_major_cause_dict = {
'reset_main_pwr_fail' : self.REBOOT_CAUSE_POWER_LOSS,
'reset_aux_pwr_or_ref' : self.REBOOT_CAUSE_POWER_LOSS,
'reset_aux_pwr_or_fu' : self.REBOOT_CAUSE_POWER_LOSS,
'reset_comex_pwr_fail' : self.REBOOT_CAUSE_POWER_LOSS,
'reset_asic_thermal' : self.REBOOT_CAUSE_THERMAL_OVERLOAD_ASIC,
'reset_comex_thermal' : self.REBOOT_CAUSE_THERMAL_OVERLOAD_CPU,
'reset_hotswap_or_wd' : self.REBOOT_CAUSE_WATCHDOG,
'reset_comex_wd' : self.REBOOT_CAUSE_WATCHDOG,
'reset_swb_wd' : self.REBOOT_CAUSE_WATCHDOG,
'reset_sff_wd' : self.REBOOT_CAUSE_WATCHDOG,
'reset_hotswap_or_halt' : self.REBOOT_CAUSE_HARDWARE_OTHER,
'reset_voltmon_upgrade_fail': self.REBOOT_CAUSE_HARDWARE_OTHER,
'reset_reload_bios' : self.REBOOT_CAUSE_HARDWARE_BIOS,
'reset_fw_reset' : self.REBOOT_CAUSE_HARDWARE_RESET_FROM_ASIC,
'reset_from_asic' : self.REBOOT_CAUSE_HARDWARE_RESET_FROM_ASIC,
'reset_long_pb' : self.REBOOT_CAUSE_HARDWARE_BUTTON,
'reset_short_pb' : self.REBOOT_CAUSE_HARDWARE_BUTTON
}
self.reboot_minor_cause_dict = {}
self.reboot_by_software = 'reset_sw_reset'
self.reboot_cause_initialized = True
def _parse_warmfast_reboot_from_proc_cmdline(self):
if os.path.isfile(REBOOT_TYPE_KEXEC_FILE):
with open(REBOOT_TYPE_KEXEC_FILE) as cause_file:
cause_file_kexec = cause_file.readline()
m = re.search(REBOOT_TYPE_KEXEC_PATTERN_WARM, cause_file_kexec)
if m and m.group(1):
return 'warm-reboot'
m = re.search(REBOOT_TYPE_KEXEC_PATTERN_FAST, cause_file_kexec)
if m and m.group(1):
return 'fast-reboot'
return None
def _wait_reboot_cause_ready(self):
max_wait_time = REBOOT_CAUSE_MAX_WAIT_TIME
while max_wait_time > 0:
if utils.read_int_from_file(REBOOT_CAUSE_READY_FILE, log_func=None) == 1:
return True
time.sleep(REBOOT_CAUSE_CHECK_INTERVAL)
max_wait_time -= REBOOT_CAUSE_CHECK_INTERVAL
return False