Skip to content

Commit b472805

Browse files
authored
[Mellanox][Smartswitch] Mapping script for pci rshim and dpu data (#20923)
- Why I did it Introduce dpumap.sh script, Which provides the following mappings: dpu2pcie - This subcommand provides mapping from dpu to pcie, If dpu name is provided then pcie is returned dpu2rshim - This subcommand provides mapping from dpu to rshim, If dpu name is provided then pcie is returned rshim2dpu - This subcommand provides mapping from rshim to dpu, If rshim is provided then dpu name is returned rshim2pcie - This subcommand provides mapping from rshim to pcie, If rshim is provided then pcie is returned pcie2dpu - This subcommand provides mapping from pcie to dpu, If pcie is provided then dpu name is returned pcie2rshim - This subcommand provides mapping from pcie to rshim, If pcie is provided then rshim is returned The command also accept multiple inputs which returns the output - How I did it Created dpumap.sh script and updated rshim.sh - which uses rshim to pcie mapping, updated dpuctl.sh which uses dpu to pcie mapping, updated sonic-bfb-installer.sh to acacept dpu inputs - How to verify it The example outputs are provided below: admin@r-bobcat-03:~$ dpumap.sh dpu2pcie dpu1,dpu2,dpu3 0000:07:00.0 0000:01:00.0 0000:02:00.0 admin@r-bobcat-03:~$ dpumap.sh dpu2rshim dpu1 rshim1 admin@r-bobcat-03:~$ dpumap.sh pcie2rshim 0000:01:00.0 rshim2 admin@r-bobcat-03:~$ dpumap.sh rshim2dpu rshim2,rshim3 dpu2 dpu3 admin@r-bobcat-03:~$ dpumap.sh rshim2pcie rshim2 0000:01:00.0 admin@r-bobcat-03:~$ dpumap.sh dpu2rshim dpu2 rshim2
1 parent d6ae19f commit b472805

File tree

4 files changed

+151
-34
lines changed

4 files changed

+151
-34
lines changed

files/build_templates/sonic_debian_extension.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,8 @@ sudo install -m 755 platform/mellanox/smartswitch/dpuctl/dpu.conf $FILESYSTEM_RO
11431143
# Install dpuctl services
11441144
sudo cp platform/mellanox/smartswitch/dpuctl/dpuctl.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM/
11451145

1146+
# Install dpumap script
1147+
sudo install -m 755 platform/mellanox/smartswitch/dpumap.sh $FILESYSTEM_ROOT/usr/bin/dpumap.sh
11461148
{% endif %}
11471149

11481150
{% if sonic_asic_platform == "nvidia-bluefield" %}
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
3+
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
4+
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
45
# Apache-2.0
56
#
67
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,24 +22,18 @@ if [ $# -eq 0 ]; then
2122
exit 1
2223
fi
2324

24-
dpu_id=$1
25+
rshim_name="rshim$1"
26+
pcie=$(dpumap.sh rshim2pcie $rshim_name)
2527

26-
declare -A dpu2pcie
27-
dpu2pcie[0]="08:00.0"
28-
dpu2pcie[1]="07:00.0"
29-
dpu2pcie[2]="01:00.0"
30-
dpu2pcie[3]="02:00.0"
31-
32-
if [ -z "${dpu2pcie[$dpu_id]}" ]; then
33-
echo "Error: Invalid dpu index $dpu_id"
28+
if [ $? -ne 0 ]; then
29+
echo "Error: Invalid rshim index $1"
3430
exit 1
3531
fi
3632

37-
pcie=${dpu2pcie[$dpu_id]}
3833

39-
if ! lspci | grep $pcie > /dev/null; then
34+
if ! lspci -D | grep $pcie > /dev/null; then
4035
echo "PCIE device $pcie is not available"
4136
exit 1
4237
fi
4338

44-
/usr/sbin/rshim -i $dpu_id -d pcie-0000:$pcie
39+
/usr/sbin/rshim -i $1 -d pcie-$pcie
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
#
3+
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
4+
# Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5+
# Apache-2.0
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# read SONiC immutable variables
21+
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
22+
PLATFORM=${PLATFORM:-`sonic-db-cli CONFIG_DB HGET 'DEVICE_METADATA|localhost' platform`}
23+
PLATFORM_JSON=/usr/share/sonic/device/$PLATFORM/platform.json
24+
25+
usage(){
26+
echo "Usage: $0 {dpu2pcie|dpu2rshim|rshim2dpu|pcie2dpu|rshim2pcie|pcie2rshim} name"
27+
}
28+
29+
validate_platform(){
30+
if [[ ! -f $PLATFORM_JSON ]]; then
31+
echo "platform.json file not found. Exiting script"
32+
exit 1
33+
fi
34+
}
35+
36+
37+
validate_platform
38+
case $1 in
39+
"dpu2rshim")
40+
jq_query='.DPUS[$dpu].rshim_info'
41+
var="dpu"
42+
;;
43+
"dpu2pcie")
44+
jq_query='.DPUS[$dpu].bus_info'
45+
var="dpu"
46+
;;
47+
"pcie2dpu")
48+
jq_query='.DPUS | to_entries[] | select(.value.bus_info == $bus) | .key'
49+
var="bus"
50+
;;
51+
"pcie2rshim")
52+
jq_query='.DPUS | to_entries[] | select(.value.bus_info == $bus) | .value.rshim_info'
53+
var="bus"
54+
;;
55+
"rshim2dpu")
56+
jq_query='.DPUS | to_entries[] | select(.value.rshim_info == $rshim) | .key'
57+
var="rshim"
58+
;;
59+
"rshim2pcie")
60+
jq_query='.DPUS | to_entries[] | select(.value.rshim_info == $rshim) | .value.bus_info'
61+
var="rshim"
62+
;;
63+
*)
64+
echo "Invalid usage of script!"
65+
usage
66+
exit 1
67+
esac
68+
69+
IFS=',' read -r -a identifier_array <<< "$2"
70+
for identifier in "${identifier_array[@]}"; do
71+
op=$(jq -r --arg "$var" "$identifier" "$jq_query" "$PLATFORM_JSON")
72+
if [[ "$op" != "null" ]]; then
73+
echo "$op"
74+
else
75+
echo "Invald entry! $identifier"
76+
exit 1
77+
fi
78+
done
79+

platform/mellanox/sonic-bfb-installer.sh

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
#
33
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
4-
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
55
# Apache-2.0
66
#
77
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,12 +16,7 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818
#
19-
2019
declare -A rshim2dpu
21-
rshim2dpu["rshim0"]="dpu0"
22-
rshim2dpu["rshim1"]="dpu1"
23-
rshim2dpu["rshim2"]="dpu2"
24-
rshim2dpu["rshim3"]="dpu3"
2520

2621
command_name="sonic-bfb-installer.sh"
2722
usage(){
@@ -37,15 +32,17 @@ WORK_DIR=`mktemp -d -p "$DIR"`
3732

3833
bfb_install_call(){
3934
#Example:sudo bfb-install -b <full path to image> -r rshim<id>
40-
local appendix=$3
35+
local appendix=$4
4136
local -r rshim=$1
37+
local dpu=$2
38+
local bfb=$3
4239
local result_file=$(mktemp "${WORK_DIR}/result_file.XXXXX")
4340
if [ -z "$appendix" ]; then
44-
local cmd="timeout 600s bfb-install -b $2 -r $1"
41+
local cmd="timeout 600s bfb-install -b $bfb -r $1"
4542
else
46-
local cmd="timeout 600s bfb-install -b $2 -r $1 -c $appendix"
43+
local cmd="timeout 600s bfb-install -b $bfb -r $1 -c $appendix"
4744
fi
48-
echo "Installing bfb image on DPU connected to $1 using $cmd"
45+
echo "Installing bfb image on DPU connected to $rshim using $cmd"
4946
local indicator="$rshim:"
5047
trap 'kill_ch_procs' SIGINT SIGTERM SIGHUP
5148
eval "$cmd" > >(while IFS= read -r line; do echo "$indicator $line"; done >> "$result_file") 2>&1 &
@@ -72,14 +69,11 @@ bfb_install_call(){
7269
if [ $exit_status -ne 0 ] ||[ $verbose = true ]; then
7370
cat "$result_file"
7471
fi
75-
76-
dpu=${rshim2dpu[$rshim]}
7772
echo "$rshim: Resetting DPU $dpu"
7873
cmd="dpuctl dpu-reset --force $dpu"
7974
if [[ $verbose == true ]]; then
8075
cmd="$cmd -v"
8176
fi
82-
8377
eval $cmd
8478
}
8579

@@ -120,6 +114,31 @@ validate_rshim(){
120114
done
121115
}
122116

117+
get_mapping(){
118+
local provided_list=("$@")
119+
120+
for item1 in "${provided_list[@]}"; do
121+
var=$(dpumap.sh rshim2dpu $item1)
122+
if [ $? -ne 0 ]; then
123+
echo "$item1 does not have a valid dpu mapping!"
124+
exit 1
125+
fi
126+
rshim2dpu["$item1"]="$var"
127+
done
128+
}
129+
130+
validate_dpus(){
131+
local provided_list=("$@")
132+
for item1 in "${provided_list[@]}"; do
133+
var=$(dpumap.sh dpu2rshim $item1)
134+
if [ $? -ne 0 ]; then
135+
echo "$item1 does not have a valid rshim mapping!"
136+
exit 1
137+
fi
138+
rshim2dpu["$var"]="$item1"
139+
dev_names+=("$var")
140+
done
141+
}
123142
check_for_root(){
124143
if [ "$EUID" -ne 0 ]
125144
then echo "Please run the script in sudo mode"
@@ -144,6 +163,10 @@ main(){
144163
shift;
145164
rshim_dev=$1
146165
;;
166+
--dpu|-d)
167+
shift;
168+
dpus=$1
169+
;;
147170
--config|-c)
148171
shift;
149172
config=$1
@@ -170,18 +193,33 @@ main(){
170193
exit 1
171194
fi
172195
if [ -z "$rshim_dev" ]; then
173-
echo "No rshim interfaces provided!"
174-
usage
175-
exit 1
176-
else
177-
if [ "$rshim_dev" = "all" ]; then
196+
if [ -z "$dpus" ]; then
197+
echo "No rshim interfaces provided!"
198+
usage
199+
exit 1
200+
fi
201+
if [ "$dpus" = "all" ]; then
202+
rshim_dev="$dpus"
203+
else
204+
IFS=',' read -ra dpu_names <<< "$dpus"
205+
validate_dpus ${dpu_names[@]}
206+
fi
207+
fi
208+
209+
210+
if [ "$rshim_dev" = "all" ]; then
178211
dev_names=("${dev_names_det[@]}")
179212
echo "${#dev_names_det[@]} rshim interfaces detected:"
180213
echo "${dev_names_det[@]}"
181214
else
182-
IFS=',' read -ra dev_names <<< "$rshim_dev"
215+
if [ ${#dev_names[@]} -eq 0 ]; then
216+
# If the list is not empty, the list is obtained from the DPUs
217+
IFS=',' read -ra dev_names <<< "$rshim_dev"
218+
fi
183219
validate_rshim ${dev_names[@]}
184-
fi
220+
fi
221+
if [ ${#rshim2dpu[@]} -eq 0 ]; then
222+
get_mapping ${dev_names[@]}
185223
fi
186224
# Sort list of rshim interfaces so that config is applied in a known order
187225
sorted_devs=($(for i in "${dev_names[@]}"; do echo $i; done | sort))
@@ -211,7 +249,9 @@ main(){
211249
for i in "${!sorted_devs[@]}"
212250
do
213251
:
214-
bfb_install_call ${sorted_devs[$i]} $bfb ${arr[$i]} &
252+
rshim_name=${sorted_devs[$i]}
253+
dpu_name=${rshim2dpu[$rshim_name]}
254+
bfb_install_call ${rshim_name} ${dpu_name} $bfb ${arr[$i]} &
215255
done
216256
wait
217257
}
@@ -238,3 +278,4 @@ kill_ch_procs(){
238278
appendix=
239279
verbose=false
240280
main "$@"
281+

0 commit comments

Comments
 (0)