blob: 3f357e56fa60888294f6824c0d7eaa0a0fb83b42 [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 Moon4d00b3a2019-09-25 15:30:45 -050017set -o pipefail
18
Hyunsun Moon81c8e232019-05-21 03:40:22 -060019grub_updated=0
20function 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
32function 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 Moon2b69b952019-08-19 03:19:03 -050039 echo "INFO: SR-IOV VF does not exist"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060040 return 1
41 fi
Hyunsun Moon4eb165e2019-09-23 21:20:01 -050042 return 0
43}
44
45function 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 Moon81c8e232019-05-21 03:40:22 -060055
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 Moon2b69b952019-08-19 03:19:03 -050064 echo "INFO: SR-IOV VF $idx does not exist or is not binded to vfio-pci"
Hyunsun Moon81c8e232019-05-21 03:40:22 -060065 return 1
66 fi
67 done
68 return 0
69}
70
Hyunsun Moon4eb165e2019-09-23 21:20:01 -050071SRIOV_PF=
72VFIO_ENABLED=
73NR_HUGEPAGE=32
74
75while :; 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 Moon4d00b3a2019-09-25 15:30:45 -050089 -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 Moon4eb165e2019-09-23 21:20:01 -050096 *) break
97 esac
98 shift
99done
100
101if [ "$(id -u)" != "0" ]; then
102 echo "FAIL: You should run this as root."
103 echo "HINT: sudo $0 -i [iface name]"
104 exit 1
105fi
106
107if [ -z "$SRIOV_PF" ]; then
108 echo "FAIL: Interface name is required"
109 echo "HINT: sudo $0 -i [iface name]"
110 exit 1
111fi
112
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500113# Check VT is enabled in BIOS
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600114# --------------------------
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500115if ! grep -q -E -wo 'vmx' /proc/cpuinfo; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600116 echo "FATAL: Your CPU does not support hardware virtualization."
117 exit 1
118fi
119
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500120apt update &>/dev/null
121apt install msr-tools -y &>/dev/null
122modprobe msr
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600123
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500124if [[ "$(rdmsr 0x3a)" = "1" ]]; then
125 echo "FAIL: Intel VT is not enabled in BIOS"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600126 echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT),"
127 echo " and then hard poweroff/poweron your system"
128else
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500129 echo " OK: Intel VT is enabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600130fi
131
132# Ensure IOMMU is enabled
133# --------------------------
134if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then
135 disabled=1
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500136 echo "INFO: IOMMU is not enabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600137 update_grub_cmdline "intel_iommu=on"
138else
139 echo " OK: IOMMU is enabled"
140fi
141
142# Ensure hugepage is enabled
143# --------------------------
144hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true
145if [ "$hugepage" -eq "0" ]; then
146 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500147 echo "INFO: Hugepage is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600148
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
154else
155 echo " OK: Hugepage is enabled"
156fi
157
158# Ensure SR-IOV is enabled
159# --------------------------
160if ! 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
163fi
164
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500165check_func=check_vf
166if [ -n "$VFIO_ENABLED" ]; then
167 check_func=check_vf_vfio
168fi
169
170if ! $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600171 cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh
172 tee "/etc/systemd/system/sriov.service" > /dev/null << EOF
173[Unit]
174Description=Create VFs on $SRIOV_PF
175
176[Service]
177Type=oneshot
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500178ExecStart=/usr/bin/sriov.sh $SRIOV_PF $VFIO_ENABLED
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600179
180[Install]
181WantedBy=default.target
182EOF
183 systemctl daemon-reload
184 systemctl enable --now sriov.service &> /dev/null
185 echo " Configured VFs on $SRIOV_PF"
186fi
187
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500188if $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600189 echo " OK: SR-IOV is enabled on $SRIOV_PF"
190else
191 disabled=1
192fi
193
194if [ "$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
198fi
199
200exit $disabled