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 | |
| 17 | SRIOV_PF=${1:?"Specify SR-IOV interface name as argv[1]"} |
| 18 | NR_HUGEPAGE=32 |
| 19 | |
| 20 | if [ "$(id -u)" != "0" ]; then |
| 21 | echo "FAIL: You should run this as root." |
| 22 | echo "HINT: sudo $0 $SRIOV_PF" |
| 23 | exit 1 |
| 24 | fi |
| 25 | |
| 26 | grub_updated=0 |
| 27 | function update_grub_cmdline { |
| 28 | local param=$1 |
| 29 | IFS='=' read -r key value <<< "$param" |
| 30 | if ! grep -q "$key"= /etc/default/grub; then |
| 31 | sed -i "s/^GRUB_CMDLINE_LINUX=\"/&${param} /" /etc/default/grub |
| 32 | else |
| 33 | sed -i "s/\\(${key}=\\)\\w*/\\1${value}/g" /etc/default/grub |
| 34 | fi |
| 35 | echo " Added \"${param}\" is to kernel parameters" |
| 36 | grub_updated=1 |
| 37 | } |
| 38 | |
| 39 | function check_vf { |
| 40 | local pfpci |
| 41 | local num_vfs |
| 42 | |
| 43 | pfpci=$(readlink /sys/devices/pci*/*/*/net/"$SRIOV_PF"/device | awk '{print substr($1,10)}') |
| 44 | num_vfs=$(cat /sys/class/net/"$SRIOV_PF"/device/sriov_numvfs) |
| 45 | if [ "$num_vfs" = "0" ]; then |
| 46 | echo "FAIL: SR-IOV VFs are not created" |
| 47 | return 1 |
| 48 | fi |
| 49 | |
| 50 | local vfpci |
| 51 | local driver |
| 52 | for ((idx = 0; idx < num_vfs; idx++)); do |
| 53 | local vfn="virtfn$idx" |
| 54 | # shellcheck disable=SC2012 |
| 55 | vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}') |
| 56 | driver=$(lspci -vvv -s "$vfpci" | grep "Kernel driver in use" | awk '{print $5}') |
| 57 | if [ "$driver" != "vfio-pci" ]; then |
| 58 | echo "FAIL: SR-IOV VF $idx does not exist or bind to vfio-pci" |
| 59 | return 1 |
| 60 | fi |
| 61 | done |
| 62 | return 0 |
| 63 | } |
| 64 | |
| 65 | # Check hardware virtualization is enabled |
| 66 | # -------------------------- |
| 67 | virt=$(grep -E -m1 -w '^flags[[:blank:]]*:' /proc/cpuinfo | grep -E -wo '(vmx|svm)') || true |
| 68 | if [ -z "$virt" ]; then |
| 69 | echo "FATAL: Your CPU does not support hardware virtualization." |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | msr="/dev/cpu/0/msr" |
| 74 | if [ ! -r "$msr" ]; then |
| 75 | modprobe msr |
| 76 | fi |
| 77 | |
| 78 | disabled=0 |
| 79 | if [ "$virt" = "vmx" ]; then |
| 80 | BIT=$(rdmsr --bitfield 0:0 0x3a 2>/dev/null || true) |
| 81 | if [ "$BIT" = "1" ]; then |
| 82 | BIT=$(rdmsr --bitfield 2:2 0x3a 2>/dev/null || true) |
| 83 | if [ "$BIT" = "0" ]; then |
| 84 | disabled=1 |
| 85 | fi |
| 86 | fi |
| 87 | elif [ "$virt" = "svm" ]; then |
| 88 | BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || true) |
| 89 | if [ "$BIT" = "1" ]; then |
| 90 | disabled=1 |
| 91 | fi |
| 92 | else |
| 93 | echo "FATAL: Unknown virtualization extension: $virt." |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | if [ "$disabled" -eq 1 ]; then |
| 98 | echo "FAIL: $virt is disabled by BIOS" |
| 99 | echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT)," |
| 100 | echo " and then hard poweroff/poweron your system" |
| 101 | else |
| 102 | echo " OK: $virt is enabled" |
| 103 | fi |
| 104 | |
| 105 | # Ensure IOMMU is enabled |
| 106 | # -------------------------- |
| 107 | if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then |
| 108 | disabled=1 |
| 109 | echo "FAIL: IOMMU is disabled" |
| 110 | update_grub_cmdline "intel_iommu=on" |
| 111 | else |
| 112 | echo " OK: IOMMU is enabled" |
| 113 | fi |
| 114 | |
| 115 | # Ensure hugepage is enabled |
| 116 | # -------------------------- |
| 117 | hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true |
| 118 | if [ "$hugepage" -eq "0" ]; then |
| 119 | disabled=1 |
| 120 | echo "FAIL: Hugepage is disabled" |
| 121 | |
| 122 | update_grub_cmdline "hugepages=$NR_HUGEPAGE" |
| 123 | update_grub_cmdline "default_hugepagesz=1G" |
| 124 | if ! grep -q "^vm.nr_hugepages" /etc/sysctl.conf; then |
| 125 | echo "vm.nr_hugepages=$NR_HUGEPAGE" >> /etc/sysctl.conf |
| 126 | fi |
| 127 | else |
| 128 | echo " OK: Hugepage is enabled" |
| 129 | fi |
| 130 | |
| 131 | # Ensure SR-IOV is enabled |
| 132 | # -------------------------- |
| 133 | if ! lsmod | grep -q vfio-pci; then |
| 134 | echo 'vfio-pci' | tee /etc/modules-load.d/sriov.conf 1> /dev/null |
| 135 | systemctl restart systemd-modules-load.service |
| 136 | fi |
| 137 | |
| 138 | if ! check_vf; then |
| 139 | cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh |
| 140 | tee "/etc/systemd/system/sriov.service" > /dev/null << EOF |
| 141 | [Unit] |
| 142 | Description=Create VFs on $SRIOV_PF |
| 143 | |
| 144 | [Service] |
| 145 | Type=oneshot |
| 146 | ExecStart=/usr/bin/sriov.sh -b $SRIOV_PF |
| 147 | |
| 148 | [Install] |
| 149 | WantedBy=default.target |
| 150 | EOF |
| 151 | systemctl daemon-reload |
| 152 | systemctl enable --now sriov.service &> /dev/null |
| 153 | echo " Configured VFs on $SRIOV_PF" |
| 154 | fi |
| 155 | |
| 156 | if check_vf; then |
| 157 | echo " OK: SR-IOV is enabled on $SRIOV_PF" |
| 158 | else |
| 159 | disabled=1 |
| 160 | fi |
| 161 | |
| 162 | if [ "$grub_updated" -eq 1 ]; then |
| 163 | update-grub &> /dev/null |
| 164 | echo "HINT: Grub was updated, reboot for changes to take effect" |
| 165 | exit 1 |
| 166 | fi |
| 167 | |
| 168 | exit $disabled |