blob: 29a77e789eb1f7fd67d6a6fc7cb8f5867488ab1b [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 Moon2234f662020-02-11 18:52:13 -0800113# Check VT-d is enabled in BIOS
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600114# --------------------------
Hyunsun Moon2234f662020-02-11 18:52:13 -0800115if ! dmesg | grep DMAR > /dev/null 2>&1; then
116 echo "FAIL: Intel VT-d is not enabled in BIOS"
117 echo "HINT: Enter your BIOS setup and enable VT-d and then poweroff/poweron your system"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600118else
Hyunsun Moon2234f662020-02-11 18:52:13 -0800119 echo " OK: Intel VT-d is enabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600120fi
121
122# Ensure IOMMU is enabled
123# --------------------------
124if ! compgen -G "/sys/class/iommu/*/devices" > /dev/null; then
125 disabled=1
Hyunsun Moon4d00b3a2019-09-25 15:30:45 -0500126 echo "INFO: IOMMU is not enabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600127 update_grub_cmdline "intel_iommu=on"
128else
129 echo " OK: IOMMU is enabled"
130fi
131
132# Ensure hugepage is enabled
133# --------------------------
134hugepage=$(grep -i HugePages_Total /proc/meminfo | awk '{print $2}') || true
135if [ "$hugepage" -eq "0" ]; then
136 disabled=1
Hyunsun Moon2b69b952019-08-19 03:19:03 -0500137 echo "INFO: Hugepage is disabled"
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600138
139 update_grub_cmdline "hugepages=$NR_HUGEPAGE"
140 update_grub_cmdline "default_hugepagesz=1G"
141 if ! grep -q "^vm.nr_hugepages" /etc/sysctl.conf; then
142 echo "vm.nr_hugepages=$NR_HUGEPAGE" >> /etc/sysctl.conf
143 fi
144else
145 echo " OK: Hugepage is enabled"
146fi
147
148# Ensure SR-IOV is enabled
149# --------------------------
150if ! lsmod | grep -q vfio-pci; then
151 echo 'vfio-pci' | tee /etc/modules-load.d/sriov.conf 1> /dev/null
152 systemctl restart systemd-modules-load.service
153fi
154
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500155check_func=check_vf
156if [ -n "$VFIO_ENABLED" ]; then
157 check_func=check_vf_vfio
158fi
159
160if ! $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600161 cp "$(cd "$(dirname "$0")" && pwd)/sriov.sh" /usr/bin/sriov.sh
162 tee "/etc/systemd/system/sriov.service" > /dev/null << EOF
163[Unit]
164Description=Create VFs on $SRIOV_PF
165
166[Service]
167Type=oneshot
Hyunsun Moon2234f662020-02-11 18:52:13 -0800168ExecStart=/usr/bin/sriov.sh $VFIO_ENABLED $SRIOV_PF
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600169
170[Install]
171WantedBy=default.target
172EOF
173 systemctl daemon-reload
174 systemctl enable --now sriov.service &> /dev/null
175 echo " Configured VFs on $SRIOV_PF"
176fi
177
Hyunsun Moon4eb165e2019-09-23 21:20:01 -0500178if $check_func; then
Hyunsun Moon81c8e232019-05-21 03:40:22 -0600179 echo " OK: SR-IOV is enabled on $SRIOV_PF"
180else
181 disabled=1
182fi
183
184if [ "$grub_updated" -eq 1 ]; then
185 update-grub &> /dev/null
186 echo "HINT: Grub was updated, reboot for changes to take effect"
187 exit 1
188fi
189
190exit $disabled