Skip to content

Commit c8fcaa1

Browse files
committed
Provide option to bind VFs to vfio driver
1 parent b0ecec6 commit c8fcaa1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

hack/prepare.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ IS_X86_WITH_BLUEFIELD=false
1919
IS_ARM_WITH_BLUEFIELD=false
2020
IS_X86_WITH_MLX=false
2121
CONFIG_ONLY=false
22+
OPT_VFIO_AUTO_BIND=false
23+
VFIO_BIND_ONLY=false
2224

2325
function log() {
2426
echo "$(date +"%Y-%m-%d_%H-%M-%S-%3N") $1"
@@ -139,6 +141,45 @@ process_multiport_eswitch_mode() {
139141
udevadm settle
140142
}
141143

144+
function vfio_bind() {
145+
local pf0="${devs[0]}"
146+
147+
lsmod | grep -q '^vfio_pci' || {
148+
log "vfio-pci module not loaded, loading it"
149+
modprobe vfio-pci
150+
}
151+
152+
numvfs=$(cat /sys/bus/pci/devices/$pf0/sriov_numvfs)
153+
vf_offset=$(cat /sys/bus/pci/devices/$pf0/sriov_offset)
154+
155+
vf_in_block_count=0
156+
vf_block_count=0
157+
vf_block_hex_index=0
158+
159+
modified_pci=${pf0::-3}
160+
161+
# pci address of vfs are organized in a block of 8
162+
# e.g., 3b:00.0 ... 3b:00.7, 3b:01.0 ... 3b:01.7, etc.
163+
for i in $(seq $vf_offset $((vf_offset + numvfs - 1))); do
164+
vf_in_block_count=$(($i%8))
165+
if [ $vf_in_block_count -eq 0 ]; then
166+
vf_block_count=$[$vf_block_count + 1]
167+
vf_block_hex_index=$(printf '%x' $vf_block_count)
168+
fi
169+
digits=${#vf_block_hex_index}
170+
if [ $digits -eq 2 ]; then
171+
modified_pci=${pf0::-4}
172+
fi
173+
174+
vf_pci_addr=$modified_pci$vf_block_hex_index"."$vf_in_block_count
175+
176+
echo 'vfio-pci' > /sys/bus/pci/devices/$vf_pci_addr/driver_override
177+
echo $vf_pci_addr > /sys/bus/pci/drivers/vfio-pci/bind
178+
log "Bound VF $vf_pci_addr to vfio-pci driver"
179+
sleep 0.2
180+
done
181+
}
182+
142183
function create_vf() {
143184
local pf0="${devs[0]}"
144185
local pf1="${devs[1]}"
@@ -161,6 +202,13 @@ function create_vf() {
161202
return
162203
fi
163204

205+
if [[ "$VFIO_BIND_ONLY" == "true" ]]; then
206+
log "Skipping VF creation as requested, binding VFs to vfio-pci driver"
207+
vfio_bind
208+
actualvfs=$(cat /sys/bus/pci/devices/$pf0/sriov_numvfs)
209+
return
210+
fi
211+
164212
# we disable automatic binding so that VFs don't get created, saves a lot of time
165213
# plus we don't need to unbind them before enabling switchdev mode
166214
log "disabling automatic binding of VFs on pf0 '$pf0'"
@@ -188,8 +236,16 @@ function create_vf() {
188236
actualvfs=$((NUMVFS_DESIRED<totalvfs ? NUMVFS_DESIRED : totalvfs))
189237
log "creating $actualvfs virtual functions"
190238
echo $actualvfs > /sys/bus/pci/devices/$pf0/sriov_numvfs
239+
240+
if [[ "$OPT_VFIO_AUTO_BIND" == "true" ]]; then
241+
log "Binding VFs to vfio-pci driver"
242+
vfio_bind
243+
else
244+
log "Skipping VF binding to vfio-pci driver"
245+
fi
191246
}
192247

248+
193249
function get_pattern() {
194250
local dev=$1
195251
pattern=$(devlink port | grep pci/$dev/ | grep "virtual\|pcivf" | awk '{print $5}' | sed -rn 's/(.*[a-z_])[0-9]{1,3}$/\1/p' | uniq)
@@ -258,6 +314,9 @@ fi
258314

259315
while [[ $# -gt 0 ]]; do
260316
case $1 in
317+
--vfio-auto-bind)
318+
OPT_VFIO_AUTO_BIND=true
319+
;;
261320
--multiport-eswitch)
262321
OPT_MULTIPORT=true
263322
;;
@@ -267,6 +326,9 @@ while [[ $# -gt 0 ]]; do
267326
--config-only)
268327
CONFIG_ONLY=true
269328
;;
329+
--vfio-bind-only)
330+
VFIO_BIND_ONLY=true
331+
;;
270332
*)
271333
err "Invalid argument $1"
272334
esac

0 commit comments

Comments
 (0)