-
Notifications
You must be signed in to change notification settings - Fork 41
/
sriov.sh
executable file
·58 lines (51 loc) · 1.41 KB
/
sriov.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
OPTIND=1
bind="false"
while getopts ":b" opt; do
case ${opt} in
b)
bind="true"
;;
\?)
echo "Usage: sriov.sh [-b] ens785f0 ens785f1 ..."
echo "-b Bind to vfio-pci"
exit
;;
esac
done
shift $((OPTIND - 1))
setup_pf() {
local pf=$1
echo "Resetting PF $pf"
echo 0 | tee /sys/class/net/$pf/device/sriov_numvfs
local NUM_VFS=$(cat /sys/class/net/$pf/device/sriov_totalvfs)
echo "Enabling $NUM_VFS VFs for $pf"
echo $NUM_VFS | tee /sys/class/net/$pf/device/sriov_numvfs
ip link set $pf up
sleep 1
}
setup_vfs() {
local pf=$1
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
setup_pf $pf
setup_vfs $pf
done