blob: 21868b021bb77ac71d02ab8a13d3d197f27bdc0e [file] [log] [blame]
Damjan Marion98a5d812018-06-09 15:11:52 +02001#!/bin/bash
2
3function die() {
4 echo "ERROR: $*" >&2
5 exit 1
6}
7
8function show_vfs() {
9 path=$1
10 netdev=$2
11 printf "\nVirtual Functions:\n%-2s %-12s %-9s %-12s %-17s %s\n" \
12 "ID" "PCI Addr" "PCI ID" "Driver" "MAC Addr" "Config"
13 for vf_path in ${path}/virtfn*; do
14 vf=$(basename $(readlink ${vf_path}))
15 vfid=$(basename ${vf_path//virtfn/})
16 line=$(ip link show dev ${netdev} | grep "vf ${vfid}")
17 driver=$(basename $(readlink ${vf_path}/driver))
18 pciid="$(cat ${vf_path}/vendor | cut -dx -f2):$(cat ${vf_path}/device | cut -dx -f2)"
19 mac=$(echo $line | sed -n -E -e 's/.*MAC ([0-9a-f:]+),.*/\1/p')
20 cfg=$(echo $line | cut -d, -f2-)
21
22 printf "%-2s %-12s %-9s %-12s %-17s%s\n" \
23 $vfid $vf $pciid $driver $mac "$cfg"
24 done
25}
26function get_pci_addr() {
27 local addr
28 if [ -d /sys/class/net/$2/device ]; then
29 addr=$(basename $(readlink /sys/class/net/${2}/device))
30 else
31 addr=$2
32 fi
33 if [ ! -d /sys/bus/pci/devices/${pci_addr} ]; then
34 die "PCI device $2 doesn't exist"
35 fi
36 eval "$1=${addr}"
37}
38
39function show () {
40 get_pci_addr pci_addr $1
41 path="/sys/bus/pci/devices/${pci_addr}"
42
43 if [ ! -f ${path}/sriov_numvfs ]; then
44 die "PCI device $1 is not SR-IOV device"
45 fi
46
47 printf "%-20s: %s\n" "PCI Address" ${pci_addr}
48 printf "%-20s: %s\n" "PCI ID" \
49 "$(cat ${path}/vendor | cut -dx -f2):$(cat ${path}/device | cut -dx -f2)"
50 printf "%-20s: %s\n" "Driver name" $(basename $(readlink ${path}/driver))
51 printf "%-20s: %s\n" "Driver Version" $(cat ${path}/driver/module/version)
52 printf "%-20s: %s\n" "PCI Link Speed (max)" "$(cat ${path}/current_link_speed) ($(cat ${path}/max_link_speed))"
53 printf "%-20s: %s\n" "PCI Link Width (max)" "$(cat ${path}/current_link_width) ($(cat ${path}/max_link_width))"
54 printf "%-20s: %s\n" "NUMA Node" $(cat ${path}/numa_node)
55 printf "%-20s: %s\n" "Number of VFs" $(cat ${path}/sriov_numvfs)
56 printf "%-20s: %s\n" "Total VFs" $(cat ${path}/sriov_totalvfs)
57 if [ -d ${path}/net/* ] ; then
58 netdev=$(basename ${path}/net/*)
59 netdev_path=${path}/net/${netdev}
60 printf "%-20s: %s\n" "Interface" ${netdev}
61 printf "%-20s: %s\n" "MAC Address" $(cat ${netdev_path}/address)
62 printf "%-20s: %s\n" "Speed" $(cat ${netdev_path}/speed)
63 printf "%-20s: %s\n" "State" $(cat ${netdev_path}/operstate)
64 fi
65
66 [ $(cat ${path}/sriov_numvfs) -gt 0 ] && show_vfs ${path} ${netdev}
67}
68
69function remove_all () {
70 get_pci_addr pci_addr $1
71 path="/sys/bus/pci/devices/${pci_addr}"
72 [ $(cat ${path}/sriov_numvfs) -gt 0 ] || die "No VFs configured on $1"
73 echo 0 | sudo tee ${path}/sriov_numvfs > /dev/null
74 echo "VFs removed..."
75}
76
77function create () {
78 get_pci_addr pci_addr $1
79 path="/sys/bus/pci/devices/${pci_addr}"
80 [ $(cat ${path}/sriov_numvfs) -gt 0 ] && die "VFs already configured on $1"
81 [ "0$2" -gt 0 ] || die "Please specify number of VFs to create"
82 echo $2 | sudo tee ${path}/sriov_numvfs > /dev/null
83 [ -d ${path}/net/* ] || die "No net device for $1"
84 netdev=$(basename ${path}/net/*)
85 netdev_path=${path}/net/${netdev}
86
87 mac_prefix=$(cat ${netdev_path}/address | cut -d: -f1,3,4,5,6 )
88 for vf_path in ${path}/virtfn*; do
89 vf=$(basename $(readlink ${vf_path}))
90 vfid=$(basename ${vf_path//virtfn/})
91 mac="${mac_prefix}:$(printf "%02x" ${vfid})"
92 sudo ip link set dev ${netdev} vf ${vfid} mac ${mac}
Damjan Marion539e6372018-10-26 13:43:43 +020093 sudo ip link set dev ${netdev} vf ${vfid} trust on
94 sudo ip link set dev ${netdev} vf ${vfid} spoofchk off
Damjan Marion98a5d812018-06-09 15:11:52 +020095 done
96
97 [ $(cat ${path}/sriov_numvfs) -gt 0 ] && show_vfs ${path} ${netdev}
98}
99
100function help() {
101
102cat << __EOF__
103
104$0 show <dev>
105 Displays information about <dev> where <dev> is PCI address
106 or linux interface name.
107
108$0 remove-all <dev>
109 Remove all virtual functions from device <dev>.
110
Damjan Marion539e6372018-10-26 13:43:43 +0200111$0 create <dev> <num>
Damjan Marion98a5d812018-06-09 15:11:52 +0200112 Create <num> virtual functions on device<dev>.
113__EOF__
114}
115
116case $1 in
117 show)
118 show $2
119 ;;
120 create)
121 create $2 $3
122 ;;
123 remove-all)
124 remove_all $2
125 ;;
126 help)
127 help $2
128 ;;
129 *)
130 echo "Please specify command (show, create, remove-all)"
131 help
132 ;;
133esac
134