Skip to content

Commit e665ee8

Browse files
authored
Initial version of pcied (#60)
* Initial version of pcied * Fix LGTM error * Fix LGTM issues * Update to read the state_db for the pcie device status check after calling `pcieutil pcie-check`. * fix LGTM * Update the pcie_status in STATE_DB * Add logic to exit the daemon if the pcie configuration file doesn't exist. * review comments - spaces * review comments * fixed lgtm * typo * typo * use subprocess call * review comment
1 parent c530587 commit e665ee8

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

sonic-pcied/scripts/pcied

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env python2
2+
3+
"""
4+
pcied
5+
PCIe device monitoring daemon for SONiC
6+
"""
7+
8+
try:
9+
import os
10+
import signal
11+
import subprocess
12+
import sys
13+
import threading
14+
15+
import swsssdk
16+
from sonic_daemon_base.daemon_base import Logger
17+
from sonic_daemon_base.daemon_base import DaemonBase
18+
except ImportError, e:
19+
raise ImportError(str(e) + " - required module not found")
20+
21+
#
22+
# Constants ====================================================================
23+
#
24+
SYSLOG_IDENTIFIER = "pcied"
25+
26+
PCIE_RESULT_REGEX = "PCIe Device Checking All Test"
27+
PCIE_TABLE_NAME = "PCIE_STATUS"
28+
29+
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
30+
PCIE_CONF_FILE = 'pcie.yaml'
31+
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
32+
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
33+
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
34+
35+
PCIED_MAIN_THREAD_SLEEP_SECS = 60
36+
REDIS_HOSTIP = "127.0.0.1"
37+
38+
# Global logger class instance
39+
logger = Logger(SYSLOG_IDENTIFIER)
40+
41+
#
42+
# Daemon =======================================================================
43+
#
44+
45+
46+
class DaemonPcied(DaemonBase):
47+
def __init__(self):
48+
DaemonBase.__init__(self)
49+
50+
platform, hwsku = DaemonBase.get_platform_and_hwsku(self)
51+
pciefilePath = "/".join([PLATFORM_ROOT_PATH, platform, "plugins", PCIE_CONF_FILE])
52+
sys.path.append(os.path.abspath(pciefilePath))
53+
if not os.path.exists(pciefilePath):
54+
logger.log_error("Platform pcie configuration file doesn't exist! exit pcied")
55+
sys.exit("Platform PCIe Configuration file doesn't exist!")
56+
57+
self.timeout = PCIED_MAIN_THREAD_SLEEP_SECS
58+
self.stop_event = threading.Event()
59+
60+
self.state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP)
61+
self.state_db.connect("STATE_DB")
62+
63+
def check_pcie_devices(self):
64+
cmd = [ 'sudo', 'pcieutil', 'pcie-check' ]
65+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
66+
resultInfo, err = p.communicate()
67+
pcie_db_state = self.read_state_db("PCIE_STATUS|", "PCIE_DEVICES")
68+
69+
for line in resultInfo.splitlines():
70+
if PCIE_RESULT_REGEX in line:
71+
if "PASSED" in line and "PASSED" not in pcie_db_state:
72+
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "PASSED")
73+
logger.log_info("PCIe device status check : PASSED")
74+
elif "FAILED" in line and "PASSED" in pcie_db_state:
75+
self.update_state_db("PCIE_STATUS|", "PCIE_DEVICES", "FAILED")
76+
logger.log_info("PCIe device status check : FAILED")
77+
78+
def read_state_db(self, key1, key2):
79+
return self.state_db.get('STATE_DB', key1, key2)
80+
81+
def update_state_db(self, key1, key2, value):
82+
self.state_db.set('STATE_DB', key1, key2, value)
83+
84+
# Signal handler
85+
def signal_handler(self, sig, frame):
86+
if sig == signal.SIGHUP:
87+
logger.log_info("Caught SIGHUP - ignoring...")
88+
elif sig == signal.SIGINT:
89+
logger.log_info("Caught SIGINT - exiting...")
90+
self.stop_event.set()
91+
elif sig == signal.SIGTERM:
92+
logger.log_info("Caught SIGTERM - exiting...")
93+
self.stop_event.set()
94+
else:
95+
logger.log_warning("Caught unhandled signal '" + sig + "'")
96+
97+
# Initialize daemon
98+
def init(self):
99+
logger.log_info("Start daemon init...")
100+
101+
# Deinitialize daemon
102+
def deinit(self):
103+
logger.log_info("Start daemon deinit...")
104+
105+
# Run daemon
106+
def run(self):
107+
logger.log_info("Starting up...")
108+
109+
# Start daemon initialization sequence
110+
self.init()
111+
112+
# Start main loop
113+
logger.log_info("Start daemon main loop")
114+
115+
while not self.stop_event.wait(self.timeout):
116+
# Check the Pcie device status
117+
self.check_pcie_devices()
118+
119+
logger.log_info("Stop daemon main loop")
120+
121+
# Start daemon deinitialization sequence
122+
self.deinit()
123+
124+
logger.log_info("Shutting down...")
125+
126+
#
127+
# Main =========================================================================
128+
#
129+
130+
131+
def main():
132+
pcied = DaemonPcied()
133+
pcied.run()
134+
135+
if __name__ == '__main__':
136+
main()

sonic-pcied/setup.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='sonic-pcied',
5+
version='1.0',
6+
description='PCIe check daemon for SONiC',
7+
license='Apache 2.0',
8+
author='SONiC Team',
9+
author_email='[email protected]',
10+
url='https://github.com/Azure/sonic-platform-daemons',
11+
maintainer='Sujin Kang',
12+
maintainer_email='[email protected]',
13+
scripts=[
14+
'scripts/pcied',
15+
],
16+
classifiers=[
17+
'Development Status :: 4 - Beta',
18+
'Environment :: No Input/Output (Daemon)',
19+
'Intended Audience :: Developers',
20+
'Intended Audience :: Information Technology',
21+
'Intended Audience :: System Administrators',
22+
'License :: OSI Approved :: Apache Software License',
23+
'Natural Language :: English',
24+
'Operating System :: POSIX :: Linux',
25+
'Programming Language :: Python :: 2.7',
26+
'Topic :: System :: Hardware',
27+
],
28+
keywords='sonic SONiC PCIe pcie PCIED pcied',
29+
)

0 commit comments

Comments
 (0)