fix(dsc): bcmcmd was eating the port list, so only one port was scanned

The scan loop read its port file on stdin. bcmcmd reads stdin too, so the
first call swallowed the rest of the file and the loop ended after a
single port.

Nothing about that looked wrong. The log held one PORT marker and one dsc
block, the marker-to-dump comparison passed because both counts shrank
together, the exit code was 0, and a 445-port scan just finished in
seconds.

The loop now reads on fd 3 (done 3< "$PORTFILE") and bcmcmd is given
< /dev/null. A second check compares the iteration count against the port
list count - the marker/dump comparison cannot see a loop that ends
early, so it needed a number that does not shrink with it. The Saved:
line reports dumps out of TOTAL rather than out of markers.

This is the same bcmcmd trap blanton_traffic_linespeed.sh hit at v0.3.1,
where only the first VLAN was configured.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014RetWKFZFG1ZHcitQAyhwM
This commit is contained in:
2026-09-07 10:02:25 +08:00
co-authored by Claude Opus 5
parent b84188322d
commit 79a3604ac4
@@ -12,6 +12,9 @@
# 避免灌爆 Tera Term logopen 的 log
# V1.2.0 20260906 新增 -f 自訂 log 檔名(支援 %u / %t 佔位符)與
# -F 強制覆蓋;預設不覆蓋既有檔案,自動加序號
# V1.3.0 20260907 修正 bcmcmd 讀取 stdin 導致掃描迴圈在第一筆之後
# 提前結束的問題(迴圈改用 fd 3,bcmcmd 導向
# /dev/null);新增實際迭代筆數與清單筆數的檢查
#
# Description:
# 在 Blanton SONiC DUT 上批次執行 Broadcom diag shell 的
@@ -278,16 +281,18 @@ for U in $UNITS; do
: > "$LOG" || { echo "ERROR: cannot write $LOG" >&2; exit 2; }
LOGS="$LOGS $LOG"
printf '### SCAN_BEGIN unit=%s time=%s total=%s script=bcm_dsc_scan_V1.1.0\n' \
printf '### SCAN_BEGIN unit=%s time=%s total=%s script=bcm_dsc_scan_V1.3.0\n' \
"$U" "$(date '+%Y-%m-%d %H:%M:%S')" "$TOTAL" >> "$LOG"
echo "[unit $U] scanning $TOTAL ports -> $LOG" >&2
SEQ=0
while IFS=, read -r P SPD; do
while IFS=, read -r P SPD <&3; do
SEQ=$((SEQ + 1))
printf '### PORT unit=%s port=%s speed=%s seq=%s/%s\n' "$U" "$P" "$SPD" "$SEQ" "$TOTAL" >> "$LOG"
bcmcmd -n "$U" -c "dsh -c 'phydiag $P dsc'" >> "$LOG" 2>&1 || RC=1
# bcmcmd 會讀取 stdin,若不導成 /dev/null 會把 PORTFILE 剩餘內容吸走,
# 導致迴圈在第一筆之後就讀到 EOF 而提前結束。
bcmcmd -n "$U" -c "dsh -c 'phydiag $P dsc'" >> "$LOG" 2>&1 < /dev/null || RC=1
if [ "$TTY" -eq 1 ]; then
printf '\r[unit %s] %s/%s port=%-5s' "$U" "$SEQ" "$TOTAL" "$P" >&2
@@ -296,19 +301,26 @@ for U in $UNITS; do
fi
sleep "$DELAY"
done < "$PORTFILE"
done 3< "$PORTFILE"
[ "$TTY" -eq 1 ] && printf '\n' >&2
printf '### SCAN_END unit=%s time=%s\n' "$U" "$(date '+%Y-%m-%d %H:%M:%S')" >> "$LOG"
# 概略檢查:每筆 PORT marker 應對應一個 dsc 區塊
# 檢查 1:實際迭代筆數是否等於 port 清單筆數。
# 迴圈若因 stdin 被子程序吸走而提前結束,只靠下面的 marker/dump 比對抓不到。
if [ "$SEQ" -ne "$TOTAL" ]; then
echo "ERROR: unit $U 只跑了 $SEQ/$TOTAL 筆,迴圈提前結束" >&2
RC=1
fi
# 檢查 2:每筆 PORT marker 應對應一個 dsc 區塊
MARK=$(grep -c '^### PORT ' "$LOG" || true)
DUMP=$(grep -c 'SERDES DISPLAY DIAG DATA END' "$LOG" || true)
if [ "$MARK" -ne "$DUMP" ]; then
echo "WARN: unit $U markers=$MARK dsc_blocks=$DUMP (有 port 未取得完整 dump)" >&2
RC=1
fi
echo "Saved: $LOG (${DUMP}/${MARK} dumps)" >&2
echo "Saved: $LOG (${DUMP}/${TOTAL} dumps)" >&2
done
# ----------------------------------------------------------------------------