Damjan Marion | 98a5d81 | 2018-06-09 15:11:52 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | function die() { |
| 4 | echo "ERROR: $*" >&2 |
| 5 | exit 1 |
| 6 | } |
| 7 | |
| 8 | function 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 | } |
| 26 | function 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 | |
| 39 | function 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 | |
| 69 | function 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 | |
| 77 | function 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} |
| 93 | done |
| 94 | |
| 95 | [ $(cat ${path}/sriov_numvfs) -gt 0 ] && show_vfs ${path} ${netdev} |
| 96 | } |
| 97 | |
| 98 | function help() { |
| 99 | |
| 100 | cat << __EOF__ |
| 101 | |
| 102 | $0 show <dev> |
| 103 | Displays information about <dev> where <dev> is PCI address |
| 104 | or linux interface name. |
| 105 | |
| 106 | $0 remove-all <dev> |
| 107 | Remove all virtual functions from device <dev>. |
| 108 | |
| 109 | $0 remove-all <dev> <num> |
| 110 | Create <num> virtual functions on device<dev>. |
| 111 | __EOF__ |
| 112 | } |
| 113 | |
| 114 | case $1 in |
| 115 | show) |
| 116 | show $2 |
| 117 | ;; |
| 118 | create) |
| 119 | create $2 $3 |
| 120 | ;; |
| 121 | remove-all) |
| 122 | remove_all $2 |
| 123 | ;; |
| 124 | help) |
| 125 | help $2 |
| 126 | ;; |
| 127 | *) |
| 128 | echo "Please specify command (show, create, remove-all)" |
| 129 | help |
| 130 | ;; |
| 131 | esac |
| 132 | |