blob: a39560d5ae53775256fcee84950fc0f8fcc61d5a [file] [log] [blame]
Hyunsun Moon81c8e232019-05-21 03:40:22 -06001#!/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
17SRIOV_PF=${1:?"Specify SR-IOV interface name as argv[1]"}
18NR_HUGEPAGE=32
19
20if [ "$(id -u)" != "0" ]; then
21 echo "FAIL: You should run this as root."
22 echo "HINT: sudo $0 $SRIOV_PF"
23 exit 1
24fi
25
26grub_updated=0
27function 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
39function 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
Hyunsun Moon2b69b952019-08-19 03:19:03 -050046 echo "INFO: SR-IOV VF does not exist"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060047 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
Hyunsun Moon2b69b952019-08-19 03:19:03 -050058 echo "INFO: SR-IOV VF $idx does not exist or is not binded to vfio-pci"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060059 return 1
60 fi
61 done
62 return 0
63}
64
65# Check hardware virtualization is enabled
66# --------------------------
67virt=$(grep -E -m1 -w '^flags[[:blank:]]*:' /proc/cpuinfo | grep -E -wo '(vmx|svm)') || true
68if [ -z "$virt" ]; then
69 echo "FATAL: Your CPU does not support hardware virtualization."
70 exit 1
71fi
72
73msr="/dev/cpu/0/msr"
74if [ ! -r "$msr" ]; then
75 modprobe msr
76fi
77
78disabled=0
79if [ "$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
87elif [ "$virt" = "svm" ]; then
88 BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || true)
89 if [ "$BIT" = "1" ]; then
90 disabled=1
91 fi
92else
93 echo "FATAL: Unknown virtualization extension: $virt."
94 exit 1
95fi
96
97if [ "$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"
101else
102 echo " OK: $virt is enabled"
103fi
104
105# Ensure IOMMU is enabled
106# --------------------------
107if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then
108 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500109 echo "INFO: IOMMU is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600110 update_grub_cmdline "intel_iommu=on"
111else
112 echo " OK: IOMMU is enabled"
113fi
114
115# Ensure hugepage is enabled
116# --------------------------
117hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true
118if [ "$hugepage" -eq "0" ]; then
119 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500120 echo "INFO: Hugepage is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600121
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
127else
128 echo " OK: Hugepage is enabled"
129fi
130
131# Ensure SR-IOV is enabled
132# --------------------------
133if ! 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
136fi
137
138if ! 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]
142Description=Create VFs on $SRIOV_PF
143
144[Service]
145Type=oneshot
146ExecStart=/usr/bin/sriov.sh -b $SRIOV_PF
147
148[Install]
149WantedBy=default.target
150EOF
151 systemctl daemon-reload
152 systemctl enable --now sriov.service &> /dev/null
153 echo " Configured VFs on $SRIOV_PF"
154fi
155
156if check_vf; then
157 echo " OK: SR-IOV is enabled on $SRIOV_PF"
158else
159 disabled=1
160fi
161
162if [ "$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
166fi
167
168exit $disabled