blob: e87246c153c0f39d0d8d6e254a947cafaa81be76 [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
Hyunsun Moon81c8e232019-05-21 03:40:22 -060017grub_updated=0
18function update_grub_cmdline {
19 local param=$1
20 IFS='=' read -r key value <<< "$param"
21 if ! grep -q "$key"= /etc/default/grub; then
22 sed -i "s/^GRUB_CMDLINE_LINUX=\"/&${param} /" /etc/default/grub
23 else
24 sed -i "s/\\(${key}=\\)\\w*/\\1${value}/g" /etc/default/grub
25 fi
26 echo " Added \"${param}\" is to kernel parameters"
27 grub_updated=1
28}
29
30function check_vf {
31 local pfpci
32 local num_vfs
33
34 pfpci=$(readlink /sys/devices/pci*/*/*/net/"$SRIOV_PF"/device | awk '{print substr($1,10)}')
35 num_vfs=$(cat /sys/class/net/"$SRIOV_PF"/device/sriov_numvfs)
36 if [ "$num_vfs" = "0" ]; then
Hyunsun Moon2b69b952019-08-19 03:19:03 -050037 echo "INFO: SR-IOV VF does not exist"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060038 return 1
39 fi
Hyunsun Moon4eb165e2019-09-23 21:20:01 -050040 return 0
41}
42
43function check_vf_vfio {
44 local pfpci
45 local num_vfs
46
47 pfpci=$(readlink /sys/devices/pci*/*/*/net/"$SRIOV_PF"/device | awk '{print substr($1,10)}')
48 num_vfs=$(cat /sys/class/net/"$SRIOV_PF"/device/sriov_numvfs)
49 if [ "$num_vfs" = "0" ]; then
50 echo "INFO: SR-IOV VF does not exist"
51 return 1
52 fi
Hyunsun Moon81c8e232019-05-21 03:40:22 -060053
54 local vfpci
55 local driver
56 for ((idx = 0; idx < num_vfs; idx++)); do
57 local vfn="virtfn$idx"
58 # shellcheck disable=SC2012
59 vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}')
60 driver=$(lspci -vvv -s "$vfpci" | grep "Kernel driver in use" | awk '{print $5}')
61 if [ "$driver" != "vfio-pci" ]; then
Hyunsun Moon2b69b952019-08-19 03:19:03 -050062 echo "INFO: SR-IOV VF $idx does not exist or is not binded to vfio-pci"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060063 return 1
64 fi
65 done
66 return 0
67}
68
Hyunsun Moon4eb165e2019-09-23 21:20:01 -050069SRIOV_PF=
70VFIO_ENABLED=
71NR_HUGEPAGE=32
72
73while :; do
74 case $1 in
75 -i|--interface)
76 if [ "$2" ]; then
77 SRIOV_PF=$2
78 shift
79 else
80 echo 'FAIL: "--interface" requires a non-empty option arguments'
81 exit 1
82 fi
83 ;;
84 -v|--vfio)
85 VFIO_ENABLED="-b"
86 ;;
87 *) break
88 esac
89 shift
90done
91
92if [ "$(id -u)" != "0" ]; then
93 echo "FAIL: You should run this as root."
94 echo "HINT: sudo $0 -i [iface name]"
95 exit 1
96fi
97
98if [ -z "$SRIOV_PF" ]; then
99 echo "FAIL: Interface name is required"
100 echo "HINT: sudo $0 -i [iface name]"
101 exit 1
102fi
103
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600104# Check hardware virtualization is enabled
105# --------------------------
106virt=$(grep -E -m1 -w '^flags[[:blank:]]*:' /proc/cpuinfo | grep -E -wo '(vmx|svm)') || true
107if [ -z "$virt" ]; then
108 echo "FATAL: Your CPU does not support hardware virtualization."
109 exit 1
110fi
111
112msr="/dev/cpu/0/msr"
113if [ ! -r "$msr" ]; then
114 modprobe msr
115fi
116
117disabled=0
118if [ "$virt" = "vmx" ]; then
119 BIT=$(rdmsr --bitfield 0:0 0x3a 2>/dev/null || true)
120 if [ "$BIT" = "1" ]; then
121 BIT=$(rdmsr --bitfield 2:2 0x3a 2>/dev/null || true)
122 if [ "$BIT" = "0" ]; then
123 disabled=1
124 fi
125 fi
126elif [ "$virt" = "svm" ]; then
127 BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || true)
128 if [ "$BIT" = "1" ]; then
129 disabled=1
130 fi
131else
132 echo "FATAL: Unknown virtualization extension: $virt."
133 exit 1
134fi
135
136if [ "$disabled" -eq 1 ]; then
137 echo "FAIL: $virt is disabled by BIOS"
138 echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT),"
139 echo " and then hard poweroff/poweron your system"
140else
141 echo " OK: $virt is enabled"
142fi
143
144# Ensure IOMMU is enabled
145# --------------------------
146if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then
147 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500148 echo "INFO: IOMMU is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600149 update_grub_cmdline "intel_iommu=on"
150else
151 echo " OK: IOMMU is enabled"
152fi
153
154# Ensure hugepage is enabled
155# --------------------------
156hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true
157if [ "$hugepage" -eq "0" ]; then
158 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500159 echo "INFO: Hugepage is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600160
161 update_grub_cmdline "hugepages=$NR_HUGEPAGE"
162 update_grub_cmdline "default_hugepagesz=1G"
163 if ! grep -q "^vm.nr_hugepages" /etc/sysctl.conf; then
164 echo "vm.nr_hugepages=$NR_HUGEPAGE" >> /etc/sysctl.conf
165 fi
166else
167 echo " OK: Hugepage is enabled"
168fi
169
170# Ensure SR-IOV is enabled
171# --------------------------
172if ! lsmod | grep -q vfio-pci; then
173 echo 'vfio-pci' | tee /etc/modules-load.d/sriov.conf 1> /dev/null
174 systemctl restart systemd-modules-load.service
175fi
176
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500177check_func=check_vf
178if [ -n "$VFIO_ENABLED" ]; then
179 check_func=check_vf_vfio
180fi
181
182if ! $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600183 cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh
184 tee "/etc/systemd/system/sriov.service" > /dev/null << EOF
185[Unit]
186Description=Create VFs on $SRIOV_PF
187
188[Service]
189Type=oneshot
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500190ExecStart=/usr/bin/sriov.sh $SRIOV_PF $VFIO_ENABLED
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600191
192[Install]
193WantedBy=default.target
194EOF
195 systemctl daemon-reload
196 systemctl enable --now sriov.service &> /dev/null
197 echo " Configured VFs on $SRIOV_PF"
198fi
199
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500200if $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600201 echo " OK: SR-IOV is enabled on $SRIOV_PF"
202else
203 disabled=1
204fi
205
206if [ "$grub_updated" -eq 1 ]; then
207 update-grub &> /dev/null
208 echo "HINT: Grub was updated, reboot for changes to take effect"
209 exit 1
210fi
211
212exit $disabled