fix possible cpld race access issue (#15371)
Why I did it
fix possible cpld race read issue between watchdog and reboot cause
process
How I did it
Use fcntl.flock to limit parallel access to cpld sys file
How to verify it
It can be simulated and verified with following python script
``` python3
import fcntl
import signal
import threading
exit_flag = False
def get_cpld_reg_value(getreg_path, register):
file = open(getreg_path, 'w+')
# Acquire an exclusive lock on the file
fcntl.flock(file, fcntl.LOCK_EX)
try:
file.write(register + '\n')
file.flush()
# Seek to the beginning of the file
file.seek(0)
# Read the content of the file
result = file.readline().strip()
finally:
# Release the lock and close the file
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
return result
def cpld_read(thread_num, cpld_reg, expect_val):
while not exit_flag:
val
= get_cpld_reg_value("/sys/devices/platform/dx010_cpld/getreg",
cpld_reg)
#print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}")
if val != expect_val:
print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}, expect_val {expect_val}")
def signal_handler(sig, frame):
global exit_flag
print("Ctrl+C detected. Quitting...")
exit_flag = True
if __name__ == '__main__':
# Register the signal handler for Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
t1 = threading.Thread(target=cpld_read, args=(1, '0x103', '0x11',))
t2 = threading.Thread(target=cpld_read, args=(2, '0x141', '0x00',))
t1.start()
t2.start()
t1.join()
t2.join()
```
2023-06-07 13:29:18 -05:00
|
|
|
import fcntl
|
2020-05-22 05:50:43 -05:00
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import subprocess
|
|
|
|
from mmap import *
|
|
|
|
|
2020-08-03 13:43:12 -05:00
|
|
|
from sonic_py_common import device_info
|
|
|
|
|
2022-12-09 09:30:20 -06:00
|
|
|
HOST_CHK_CMD = ["docker"]
|
2020-05-22 05:50:43 -05:00
|
|
|
EMPTY_STRING = ""
|
|
|
|
|
|
|
|
|
|
|
|
class APIHelper():
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-08-03 13:43:12 -05:00
|
|
|
(self.platform, self.hwsku) = device_info.get_platform_and_hwsku()
|
2020-05-22 05:50:43 -05:00
|
|
|
|
|
|
|
def is_host(self):
|
2022-12-09 09:30:20 -06:00
|
|
|
try:
|
|
|
|
subprocess.call(HOST_CHK_CMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return False
|
|
|
|
return True
|
2020-05-22 05:50:43 -05:00
|
|
|
|
|
|
|
def pci_get_value(self, resource, offset):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
fd = os.open(resource, os.O_RDWR)
|
|
|
|
mm = mmap(fd, 0)
|
|
|
|
mm.seek(int(offset))
|
|
|
|
read_data_stream = mm.read(4)
|
|
|
|
result = struct.unpack('I', read_data_stream)
|
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def run_command(self, cmd):
|
|
|
|
status = True
|
|
|
|
result = ""
|
|
|
|
try:
|
|
|
|
p = subprocess.Popen(
|
2022-12-09 09:30:20 -06:00
|
|
|
cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2020-05-22 05:50:43 -05:00
|
|
|
raw_data, err = p.communicate()
|
|
|
|
if err == '':
|
|
|
|
result = raw_data.strip()
|
|
|
|
except:
|
|
|
|
status = False
|
|
|
|
return status, result
|
|
|
|
|
|
|
|
def read_txt_file(self, file_path):
|
|
|
|
try:
|
|
|
|
with open(file_path, 'r') as fd:
|
|
|
|
data = fd.read()
|
|
|
|
return data.strip()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
def read_one_line_file(self, file_path):
|
|
|
|
try:
|
|
|
|
with open(file_path, 'r') as fd:
|
|
|
|
data = fd.readline()
|
|
|
|
return data.strip()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2020-09-17 10:56:52 -05:00
|
|
|
def write_txt_file(self, file_path, value):
|
|
|
|
try:
|
|
|
|
with open(file_path, 'w') as fd:
|
|
|
|
fd.write(str(value))
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_cpld_reg_value(self, getreg_path, register):
|
fix possible cpld race access issue (#15371)
Why I did it
fix possible cpld race read issue between watchdog and reboot cause
process
How I did it
Use fcntl.flock to limit parallel access to cpld sys file
How to verify it
It can be simulated and verified with following python script
``` python3
import fcntl
import signal
import threading
exit_flag = False
def get_cpld_reg_value(getreg_path, register):
file = open(getreg_path, 'w+')
# Acquire an exclusive lock on the file
fcntl.flock(file, fcntl.LOCK_EX)
try:
file.write(register + '\n')
file.flush()
# Seek to the beginning of the file
file.seek(0)
# Read the content of the file
result = file.readline().strip()
finally:
# Release the lock and close the file
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
return result
def cpld_read(thread_num, cpld_reg, expect_val):
while not exit_flag:
val
= get_cpld_reg_value("/sys/devices/platform/dx010_cpld/getreg",
cpld_reg)
#print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}")
if val != expect_val:
print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}, expect_val {expect_val}")
def signal_handler(sig, frame):
global exit_flag
print("Ctrl+C detected. Quitting...")
exit_flag = True
if __name__ == '__main__':
# Register the signal handler for Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
t1 = threading.Thread(target=cpld_read, args=(1, '0x103', '0x11',))
t2 = threading.Thread(target=cpld_read, args=(2, '0x141', '0x00',))
t1.start()
t2.start()
t1.join()
t2.join()
```
2023-06-07 13:29:18 -05:00
|
|
|
file = open(getreg_path, 'w+')
|
|
|
|
# Acquire an exclusive lock on the file
|
|
|
|
fcntl.flock(file, fcntl.LOCK_EX)
|
|
|
|
|
|
|
|
try:
|
2022-12-09 09:30:20 -06:00
|
|
|
file.write(register + '\n')
|
fix possible cpld race access issue (#15371)
Why I did it
fix possible cpld race read issue between watchdog and reboot cause
process
How I did it
Use fcntl.flock to limit parallel access to cpld sys file
How to verify it
It can be simulated and verified with following python script
``` python3
import fcntl
import signal
import threading
exit_flag = False
def get_cpld_reg_value(getreg_path, register):
file = open(getreg_path, 'w+')
# Acquire an exclusive lock on the file
fcntl.flock(file, fcntl.LOCK_EX)
try:
file.write(register + '\n')
file.flush()
# Seek to the beginning of the file
file.seek(0)
# Read the content of the file
result = file.readline().strip()
finally:
# Release the lock and close the file
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
return result
def cpld_read(thread_num, cpld_reg, expect_val):
while not exit_flag:
val
= get_cpld_reg_value("/sys/devices/platform/dx010_cpld/getreg",
cpld_reg)
#print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}")
if val != expect_val:
print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value
{val}, expect_val {expect_val}")
def signal_handler(sig, frame):
global exit_flag
print("Ctrl+C detected. Quitting...")
exit_flag = True
if __name__ == '__main__':
# Register the signal handler for Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
t1 = threading.Thread(target=cpld_read, args=(1, '0x103', '0x11',))
t2 = threading.Thread(target=cpld_read, args=(2, '0x141', '0x00',))
t1.start()
t2.start()
t1.join()
t2.join()
```
2023-06-07 13:29:18 -05:00
|
|
|
file.flush()
|
|
|
|
|
|
|
|
# Seek to the beginning of the file
|
|
|
|
file.seek(0)
|
|
|
|
|
|
|
|
# Read the content of the file
|
|
|
|
result = file.readline().strip()
|
|
|
|
finally:
|
|
|
|
# Release the lock and close the file
|
|
|
|
fcntl.flock(file, fcntl.LOCK_UN)
|
|
|
|
file.close()
|
|
|
|
|
2022-12-09 09:30:20 -06:00
|
|
|
return result
|
2020-09-17 10:56:52 -05:00
|
|
|
|