Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2019 Intel Corporation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # copied from https://github.com/clearlinux/cloud-native-setup/blob/master/clr-k8s-examples/9-multi-network/systemd/sriov.sh |
| 18 | |
| 19 | set -o errexit |
| 20 | set -o pipefail |
| 21 | set -o nounset |
| 22 | set -x |
| 23 | |
| 24 | OPTIND=1 |
| 25 | bind="false" |
| 26 | |
| 27 | while getopts ":b" opt; do |
| 28 | case ${opt} in |
| 29 | b) |
| 30 | bind="true" |
| 31 | ;; |
| 32 | \?) |
| 33 | echo "Usage: sriov.sh [-b] ens785f0 ens785f1 ..." |
| 34 | echo "-b Bind to vfio-pci" |
| 35 | exit |
| 36 | ;; |
| 37 | esac |
| 38 | done |
| 39 | shift $((OPTIND - 1)) |
| 40 | |
| 41 | setup_pf() { |
| 42 | local pf=$1 |
| 43 | local num_vfs |
| 44 | |
| 45 | echo "Resetting PF $pf" |
| 46 | echo 0 | tee /sys/class/net/"$pf"/device/sriov_numvfs |
| 47 | num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_totalvfs) |
| 48 | echo "Enabling $num_vfs VFs for $pf" |
| 49 | echo "$num_vfs" | tee /sys/class/net/"$pf"/device/sriov_numvfs |
| 50 | ip link set "$pf" up |
| 51 | sleep 1 |
| 52 | } |
| 53 | |
| 54 | vfio_bind() { |
| 55 | local pf=$1 |
| 56 | local pfpci |
| 57 | local num_vfs |
| 58 | |
| 59 | pfpci=$(readlink /sys/devices/pci*/*/*/net/"$pf"/device | awk '{print substr($1,10)}') |
| 60 | num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_numvfs) |
| 61 | |
| 62 | local vfpci |
| 63 | local mac |
| 64 | for ((idx = 0; idx < num_vfs; idx++)); do |
| 65 | #Some drivers does not support state change of VF |
| 66 | #ip link set dev $pf vf $idx state enable |
| 67 | |
| 68 | local vfn="virtfn$idx" |
| 69 | # shellcheck disable=SC2012 |
| 70 | vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}') |
| 71 | # Capture and set MAC of the VF before unbinding from linux, for later use in CNI |
| 72 | mac=$(cat /sys/bus/pci*/*/"$vfpci"/net/*/address) |
| 73 | ip link set dev "$pf" vf $idx mac "$mac" |
| 74 | # Bind VF to vfio-pci |
| 75 | echo "$vfpci" >/sys/bus/pci*/*/"$vfpci"/driver/unbind |
| 76 | echo "vfio-pci" >/sys/devices/pci*/*/"$vfpci"/driver_override |
| 77 | echo "$vfpci" >/sys/bus/pci/drivers/vfio-pci/bind |
| 78 | done |
| 79 | } |
| 80 | |
| 81 | for pf in "$@"; do |
| 82 | setup_pf "$pf" |
| 83 | if [ $bind ]; then vfio_bind "$pf"; fi |
| 84 | done |