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 | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 113 | # Check VT is enabled in BIOS |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 114 | # -------------------------- |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 115 | if ! grep -q -E -wo 'vmx' /proc/cpuinfo; then |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 116 | echo "FATAL: Your CPU does not support hardware virtualization." |
| 117 | exit 1 |
| 118 | fi |
| 119 | |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 120 | apt update &>/dev/null |
| 121 | apt install msr-tools -y &>/dev/null |
| 122 | modprobe msr |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 123 | |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 124 | if [[ "$(rdmsr 0x3a)" = "1" ]]; then |
| 125 | echo "FAIL: Intel VT is not enabled in BIOS" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 126 | echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT)," |
| 127 | echo " and then hard poweroff/poweron your system" |
| 128 | else |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 129 | echo " OK: Intel VT is enabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 130 | fi |
| 131 | |
| 132 | # Ensure IOMMU is enabled |
| 133 | # -------------------------- |
| 134 | if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then |
| 135 | disabled=1 |
Hyunsun Moon | 4d00b3a | 2019-09-25 15:30:45 -0500 | [diff] [blame] | 136 | echo "INFO: IOMMU is not enabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 137 | update_grub_cmdline "intel_iommu=on" |
| 138 | else |
| 139 | echo " OK: IOMMU is enabled" |
| 140 | fi |
| 141 | |
| 142 | # Ensure hugepage is enabled |
| 143 | # -------------------------- |
| 144 | hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true |
| 145 | if [ "$hugepage" -eq "0" ]; then |
| 146 | disabled=1 |
Hyunsun Moon | 2b69b95 | 2019-08-19 03:19:03 -0500 | [diff] [blame] | 147 | echo "INFO: Hugepage is disabled" |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 148 | |
| 149 | update_grub_cmdline "hugepages=$NR_HUGEPAGE" |
| 150 | update_grub_cmdline "default_hugepagesz=1G" |
| 151 | if ! grep -q "^vm.nr_hugepages" /etc/sysctl.conf; then |
| 152 | echo "vm.nr_hugepages=$NR_HUGEPAGE" >> /etc/sysctl.conf |
| 153 | fi |
| 154 | else |
| 155 | echo " OK: Hugepage is enabled" |
| 156 | fi |
| 157 | |
| 158 | # Ensure SR-IOV is enabled |
| 159 | # -------------------------- |
| 160 | if ! lsmod | grep -q vfio-pci; then |
| 161 | echo 'vfio-pci' | tee /etc/modules-load.d/sriov.conf 1> /dev/null |
| 162 | systemctl restart systemd-modules-load.service |
| 163 | fi |
| 164 | |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 165 | check_func=check_vf |
| 166 | if [ -n "$VFIO_ENABLED" ]; then |
| 167 | check_func=check_vf_vfio |
| 168 | fi |
| 169 | |
| 170 | if ! $check_func; then |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 171 | cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh |
| 172 | tee "/etc/systemd/system/sriov.service" > /dev/null << EOF |
| 173 | [Unit] |
| 174 | Description=Create VFs on $SRIOV_PF |
| 175 | |
| 176 | [Service] |
| 177 | Type=oneshot |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 178 | ExecStart=/usr/bin/sriov.sh $SRIOV_PF $VFIO_ENABLED |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 179 | |
| 180 | [Install] |
| 181 | WantedBy=default.target |
| 182 | EOF |
| 183 | systemctl daemon-reload |
| 184 | systemctl enable --now sriov.service &> /dev/null |
| 185 | echo " Configured VFs on $SRIOV_PF" |
| 186 | fi |
| 187 | |
Hyunsun Moon | 4eb165e | 2019-09-23 21:20:01 -0500 | [diff] [blame] | 188 | if $check_func; then |
Hyunsun Moon | 81c8e23 | 2019-05-21 03:40:22 -0600 | [diff] [blame] | 189 | echo " OK: SR-IOV is enabled on $SRIOV_PF" |
| 190 | else |
| 191 | disabled=1 |
| 192 | fi |
| 193 | |
| 194 | if [ "$grub_updated" -eq 1 ]; then |
| 195 | update-grub &> /dev/null |
| 196 | echo "HINT: Grub was updated, reboot for changes to take effect" |
| 197 | exit 1 |
| 198 | fi |
| 199 | |
| 200 | exit $disabled |