Skip to content

Commit d6fc624

Browse files
committed
Add a check for DPU
1 parent 138d5fb commit d6fc624

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

scripts/reboot

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ function get_dpu_ip()
199199

200200
# Function to retrieve GNMI port from CONFIG_DB
201201
function get_gnmi_port() {
202-
port=$(sonic-db-cli CONFIG_DB HGET "DPU_PORT|<TODO: PORT_NAME>" "gnmi")
202+
local DPU_NAME=$1
203+
port=$(sonic-db-cli CONFIG_DB HGET "DPU_PORT|$DPU_NAME" "gnmi")
203204
if [ $? -ne 0 ] || [ -z "$port" ]; then
204205
echo "Error: Failed to retrieve GNMI port"
205206
exit ${EXIT_ERROR}
@@ -250,7 +251,12 @@ function reboot_dpu_module()
250251

251252
# Retrieve DPU IP and GNMI port
252253
dpu_ip=$(get_dpu_ip "${DPU_NAME}")
253-
port=$(get_gnmi_port)
254+
port=$(get_gnmi_port "${DPU_NAME}")
255+
256+
if [ -z "$dpu_ip" ] || [ -z "$port" ]; then
257+
echo "Error: Failed to retrieve DPU IP or GNMI port for ${DPU_NAME}"
258+
exit ${EXIT_ERROR}
259+
fi
254260

255261
# Issue GNOI client command to reboot the DPU
256262
gnoi_client -target ${dpu_ip}:${port} -logtostderr -insecure -rpc Reboot -jsonin '{"method":3}'
@@ -261,6 +267,10 @@ function reboot_dpu_module()
261267

262268
# Retrieve dpu_halt_services_timeout value using jq
263269
dpu_halt_services_timeout=$(jq -r '.dpu_halt_services_timeout' "$PLATFORM_JSON_PATH" 2>/dev/null)
270+
if [ $? -ne 0 ]; then
271+
echo "Error: Failed to retrieve dpu_halt_services_timeout from ${PLATFORM_JSON_PATH}"
272+
exit ${EXIT_ERROR}
273+
fi
264274

265275
# Poll on reboot status response with a timeout mechanism
266276
poll_interval=5
@@ -286,9 +296,11 @@ function reboot_dpu_module()
286296

287297
# Update STATE_DB and handle PCIe removal and rescan
288298
sonic-db-cli state_db set "PCIE_DETACH_INFO|${DPU_NAME}" '{"dpu_id": "'${DPU_INDEX}'", "dpu_state": "detaching", "bus_info": "'${DPU_BUS_INFO}'"}'
299+
289300
echo 1 > /sys/bus/pci/devices/${DPU_BUS_INFO}/remove
290301
reboot_platform_module "${DPU_NAME}"
291302
echo 1 > /sys/bus/pci/rescan
303+
292304
sonic-db-cli state_db del "PCIE_DETACH_INFO|${DPU_NAME}"
293305
}
294306

@@ -351,15 +363,19 @@ function reboot_all_dpus() {
351363

352364
# Function to handle scenarios on smart switch
353365
function handle_smart_switch() {
354-
# TODO: Check if this is DPU or not, proceed only if PRE-SHUTDOWN is set on DPU, else throw error
355-
356366
if [ -f "$PLATFORM_JSON_PATH" ]; then
357367
NUM_DPU=$(jq -r '.DPUS | length' "$PLATFORM_JSON_PATH" 2>/dev/null)
358368
if [ "$NUM_DPU" -gt 0 ]; then
359369
SMART_SWITCH="yes"
360370
fi
361371
fi
362372

373+
is_dpu=$(python3 -c "import reboot_helper; reboot_helper.is_dpu()")
374+
if [[ "$is_dpu" == "True" ] && [ "$PRE_SHUTDOWN" != "yes" ]]; then
375+
echo "Invalid, '-p' option not specified for a DPU"
376+
exit ${EXIT_ERROR}
377+
fi
378+
363379
if [[ "$REBOOT_DPU" == "yes" ]]; then
364380
if [[ "$SMART_SWITCH" == "yes" ]]; then
365381
echo "User requested to reboot the device ${DPU_MODULE_NAME}"

scripts/reboot_helper.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def reboot_module(module_name):
5454
"""Reboot the specified module by invoking the platform API"""
5555

5656
# Load the platform chassis if not already loaded
57-
if not platform_chassis and not load_platform_chassis():
57+
if not load_platform_chassis():
5858
log_err("Failed to load platform chassis")
5959
return False
6060

@@ -89,6 +89,25 @@ def reboot_module(module_name):
8989
return False
9090

9191

92+
def is_dpu():
93+
"""Check if script is running on DPU module"""
94+
95+
# Load the platform chassis if not already loaded
96+
if not load_platform_chassis():
97+
log_err("Failed to load platform chassis")
98+
return False
99+
100+
if not platform_chassis.is_smartswitch():
101+
return False
102+
103+
# Iterate over the modules
104+
for module in platform_chassis.get_all_modules():
105+
if "DPU" in module.get_name():
106+
return True
107+
108+
return False
109+
110+
92111
if __name__ == "__main__":
93112
if len(sys.argv) < 3:
94113
print("Usage: reboot_helper.py <command> <module_name>")
@@ -103,6 +122,11 @@ def reboot_module(module_name):
103122
sys.exit(1)
104123
else:
105124
print("Reboot command sent for module {module_name}")
125+
elif command == "is_dpu":
126+
if is_dpu():
127+
print("Script is running on DPU module")
128+
else:
129+
sys.exit(1)
106130
else:
107131
print("Unknown command: {command}")
108132
sys.exit(1)

0 commit comments

Comments
 (0)