Skip to content

Commit

Permalink
Simplify sriov.sh, do not generate random MAC
Browse files Browse the repository at this point in the history
Signed-off-by: Saikrishna Edupuganti <[email protected]>
  • Loading branch information
krsna1729 committed Jan 26, 2019
1 parent 88c73a8 commit 0a00800
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions clr-k8s-examples/9-multi-network/systemd/sriov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,38 @@ while getopts ":b" opt; do
done
shift $((OPTIND - 1))

reset_pf() {
setup_pf() {
local pf=$1
echo "Resetting $pf"
echo "Resetting PF $pf"
echo 0 | tee /sys/class/net/$pf/device/sriov_numvfs
}

set_pf() {
local pf=$1
local NUM_VFS=$(cat /sys/class/net/$pf/device/sriov_totalvfs)
echo "Enabling $NUM_VFS for $pf"
echo "Enabling $NUM_VFS VFs for $pf"
echo $NUM_VFS | tee /sys/class/net/$pf/device/sriov_numvfs
ip link set $pf up
}

bind_vfs_vfio() {
if [ $bind != "true" ]; then return; fi
local pf=$1
local pci=$(readlink /sys/devices/pci*/*/*/net/$pf/device | awk '{print substr($1,10)}')
echo "Binding VFs of PF $pf ($pci) to vfio-pci"
for i in $(ls -l /sys/devices/pci*/*/$pci | awk '"virtfn"==substr($9,1,6) {print substr($11,4)}'); do
echo $i | tee /sys/bus/pci*/*/$i/driver/unbind
echo vfio-pci | tee /sys/devices/pci*/*/$i/driver_override
echo $i | tee /sys/bus/pci/drivers/vfio-pci/bind
done
sleep 1
}

setup_vfs() {
local pf=$1
local NUM_VFS=$(cat /sys/class/net/$pf/device/sriov_totalvfs)
echo "Setting up VFs of PF $pf"
for ((i = 0; i < ${NUM_VFS}; i++)); do
ip link set dev $pf vf $i state enable
ip link set dev $pf vf $i mac \
$(printf '00:80:86:%02X:%02X:%02X\n' $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)))
# ip link set $pf vf $i spoofchk off
local pfpci=$(readlink /sys/devices/pci*/*/*/net/$pf/device | awk '{print substr($1,10)}')
local NUM_VFS=$(cat /sys/class/net/$pf/device/sriov_numvfs)
for ((idx = 0; idx < NUM_VFS; idx++)); do
ip link set dev $pf vf $idx state enable
if [ $bind != "true" ]; then continue; fi

local vfn="virtfn$idx"
local vfpci=$(ls -l /sys/devices/pci*/*/$pfpci | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}')
# Capture and set MAC of the VF before unbinding from linux, for later use in CNI
local mac=$(cat /sys/bus/pci*/*/$vfpci/net/*/address)
ip link set dev $pf vf $idx mac $mac
# Bind VF to vfio-pci
echo $vfpci >/sys/bus/pci*/*/$vfpci/driver/unbind
echo "vfio-pci" >/sys/devices/pci*/*/$vfpci/driver_override
echo $vfpci >/sys/bus/pci/drivers/vfio-pci/bind
done
}

for pf in "$@"; do
reset_pf $pf
set_pf $pf
bind_vfs_vfio $pf
setup_pf $pf
setup_vfs $pf
done

0 comments on commit 0a00800

Please sign in to comment.