Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2019-present Open Networking Foundation |
| 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 | |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 17 | set -o pipefail |
| 18 | |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 19 | grub_updated=0 |
| 20 | function update_grub_cmdline { |
| 21 | local param=$1 |
| 22 | IFS='=' read -r key value <<< "$param" |
| 23 | if ! grep -q "$key"= /etc/default/grub; then |
| 24 | sed -i "s/^GRUB_CMDLINE_LINUX=\"/&${param} /" /etc/default/grub |
| 25 | else |
| 26 | sed -i "s/\\(${key}=\\)\\w*/\\1${value}/g" /etc/default/grub |
| 27 | fi |
| 28 | echo " Added \"${param}\" is to kernel parameters" |
| 29 | grub_updated=1 |
| 30 | } |
| 31 | |
| 32 | function check_vf { |
| 33 | local pfpci |
| 34 | local num_vfs |
| 35 | |
| 36 | pfpci=$(readlink /sys/devices/pci*/*/*/net/"$SRIOV_PF"/device | awk '{print substr($1,10)}') |
| 37 | num_vfs=$(cat /sys/class/net/"$SRIOV_PF"/device/sriov_numvfs) |
| 38 | if [ "$num_vfs" = "0" ]; then |
Hyunsun Moon | 2b69b95 | 2019-08-19 03:19:03 -0500 | [diff] [blame] | 39 | echo "INFO: SR-IOV VF does not exist" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 40 | return 1 |
| 41 | fi |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 42 | return 0 |
| 43 | } |
| 44 | |
| 45 | function check_vf_vfio { |
| 46 | local pfpci |
| 47 | local num_vfs |
| 48 | |
| 49 | pfpci=$(readlink /sys/devices/pci*/*/*/net/"$SRIOV_PF"/device | awk '{print substr($1,10)}') |
| 50 | num_vfs=$(cat /sys/class/net/"$SRIOV_PF"/device/sriov_numvfs) |
| 51 | if [ "$num_vfs" = "0" ]; then |
| 52 | echo "INFO: SR-IOV VF does not exist" |
| 53 | return 1 |
| 54 | fi |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 55 | |
| 56 | local vfpci |
| 57 | local driver |
| 58 | for ((idx = 0; idx < num_vfs; idx++)); do |
| 59 | local vfn="virtfn$idx" |
| 60 | # shellcheck disable=SC2012 |
| 61 | vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}') |
| 62 | driver=$(lspci -vvv -s "$vfpci" | grep "Kernel driver in use" | awk '{print $5}') |
| 63 | if [ "$driver" != "vfio-pci" ]; then |
Hyunsun Moon | 2b69b95 | 2019-08-19 03:19:03 -0500 | [diff] [blame] | 64 | echo "INFO: SR-IOV VF $idx does not exist or is not binded to vfio-pci" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 65 | return 1 |
| 66 | fi |
| 67 | done |
| 68 | return 0 |
| 69 | } |
| 70 | |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 71 | SRIOV_PF= |
| 72 | VFIO_ENABLED= |
| 73 | NR_HUGEPAGE=32 |
| 74 | |
| 75 | while :; do |
| 76 | case $1 in |
| 77 | -i|--interface) |
| 78 | if [ "$2" ]; then |
| 79 | SRIOV_PF=$2 |
| 80 | shift |
| 81 | else |
| 82 | echo 'FAIL: "--interface" requires a non-empty option arguments' |
| 83 | exit 1 |
| 84 | fi |
| 85 | ;; |
| 86 | -v|--vfio) |
| 87 | VFIO_ENABLED="-b" |
| 88 | ;; |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 89 | -h|--help) |
| 90 | echo "Usage:" |
| 91 | echo " sudo $0 -i [iface name] Create VF from [iface name]." |
| 92 | echo " sudo $0 -i [iface name] --vfio Create VF from [iface name] and bind it to VFIO driver." |
| 93 | echo " sudo $0 -h Display this help message." |
| 94 | exit 0 |
| 95 | ;; |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 96 | *) break |
| 97 | esac |
| 98 | shift |
| 99 | done |
| 100 | |
| 101 | if [ "$(id -u)" != "0" ]; then |
| 102 | echo "FAIL: You should run this as root." |
| 103 | echo "HINT: sudo $0 -i [iface name]" |
| 104 | exit 1 |
| 105 | fi |
| 106 | |
| 107 | if [ -z "$SRIOV_PF" ]; then |
| 108 | echo "FAIL: Interface name is required" |
| 109 | echo "HINT: sudo $0 -i [iface name]" |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
Hyunsun Moon | 2234f66 | 2020-02-11 18:52:13 -0800 | [diff] [blame] | 113 | # Check VT-d is enabled in BIOS |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 114 | # -------------------------- |
Hyunsun Moon | 2234f66 | 2020-02-11 18:52:13 -0800 | [diff] [blame] | 115 | if ! dmesg | grep DMAR > /dev/null 2>&1; then |
| 116 | echo "FAIL: Intel VT-d is not enabled in BIOS" |
| 117 | echo "HINT: Enter your BIOS setup and enable VT-d and then poweroff/poweron your system" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 118 | else |
Hyunsun Moon | 2234f66 | 2020-02-11 18:52:13 -0800 | [diff] [blame] | 119 | echo " OK: Intel VT-d is enabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 120 | fi |
| 121 | |
| 122 | # Ensure IOMMU is enabled |
| 123 | # -------------------------- |
| 124 | if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then |
| 125 | disabled=1 |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 126 | echo "INFO: IOMMU is not enabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 127 | update_grub_cmdline "intel_iommu=on" |
| 128 | else |
| 129 | echo " OK: IOMMU is enabled" |
| 130 | fi |
| 131 | |
| 132 | # Ensure hugepage is enabled |
| 133 | # -------------------------- |
| 134 | hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true |
| 135 | if [ "$hugepage" -eq "0" ]; then |
| 136 | disabled=1 |
Hyunsun Moon | 2b69b95 | 2019-08-19 03:19:03 -0500 | [diff] [blame] | 137 | echo "INFO: Hugepage is disabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 138 | |
| 139 | update_grub_cmdline "hugepages=$NR_HUGEPAGE" |
| 140 | update_grub_cmdline "default_hugepagesz=1G" |
| 141 | if ! grep -q "^vm.nr_hugepages" /etc/sysctl.conf; then |
| 142 | echo "vm.nr_hugepages=$NR_HUGEPAGE" >> /etc/sysctl.conf |
| 143 | fi |
| 144 | else |
| 145 | echo " OK: Hugepage is enabled" |
| 146 | fi |
| 147 | |
| 148 | # Ensure SR-IOV is enabled |
| 149 | # -------------------------- |
| 150 | if ! lsmod | grep -q vfio-pci; then |
| 151 | echo 'vfio-pci' | tee /etc/modules-load.d/sriov.conf 1> /dev/null |
| 152 | systemctl restart systemd-modules-load.service |
| 153 | fi |
| 154 | |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 155 | check_func=check_vf |
| 156 | if [ -n "$VFIO_ENABLED" ]; then |
| 157 | check_func=check_vf_vfio |
| 158 | fi |
| 159 | |
| 160 | if ! $check_func; then |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 161 | cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh |
| 162 | tee "/etc/systemd/system/sriov.service" > /dev/null << EOF |
| 163 | [Unit] |
| 164 | Description=Create VFs on $SRIOV_PF |
| 165 | |
| 166 | [Service] |
| 167 | Type=oneshot |
Hyunsun Moon | 2234f66 | 2020-02-11 18:52:13 -0800 | [diff] [blame] | 168 | ExecStart=/usr/bin/sriov.sh $VFIO_ENABLED $SRIOV_PF |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 169 | |
| 170 | [Install] |
| 171 | WantedBy=default.target |
| 172 | EOF |
| 173 | systemctl daemon-reload |
| 174 | systemctl enable --now sriov.service &> /dev/null |
| 175 | echo " Configured VFs on $SRIOV_PF" |
| 176 | fi |
| 177 | |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 178 | if $check_func; then |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 179 | echo " OK: SR-IOV is enabled on $SRIOV_PF" |
| 180 | else |
| 181 | disabled=1 |
| 182 | fi |
| 183 | |
| 184 | if [ "$grub_updated" -eq 1 ]; then |
| 185 | update-grub &> /dev/null |
| 186 | echo "HINT: Grub was updated, reboot for changes to take effect" |
| 187 | exit 1 |
| 188 | fi |
| 189 | |
| 190 | exit $disabled |