blob: 6b2a8e6cb6c03b067fc578f3d73f60459247b872 [file] [log] [blame]
Hyunsun Moone4848342020-02-16 04:28:55 -08001#!/bin/bash
2
3# Copyright (c) 2019 Intel Corporation
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# copied from https://github.com/clearlinux/cloud-native-setup/blob/master/clr-k8s-examples/9-multi-network/systemd/sriov.sh
18
19set -o errexit
20set -o pipefail
21set -o nounset
22set -x
23
24setup_pf() {
25 local pf=$1
26 local num_vfs
27
28 echo "Resetting PF $pf"
29 echo 0 | tee /sys/class/net/"$pf"/device/sriov_numvfs
30 num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_totalvfs)
31 echo "Enabling $num_vfs VFs for $pf"
32 echo "$num_vfs" | tee /sys/class/net/"$pf"/device/sriov_numvfs
33 ip link set "$pf" up
34 sleep 1
35}
36
37vfio_bind() {
38 local pf=$1
39 local pfpci
40 local num_vfs
41
42 pfpci=$(readlink /sys/devices/pci*/*/*/net/"$pf"/device | awk '{print substr($1,10)}')
43 num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_numvfs)
44
45 local vfpci
46 local mac
47 for ((idx = 0; idx < num_vfs; idx++)); do
48 #Some drivers does not support state change of VF
49 #ip link set dev $pf vf $idx state enable
50
51 local vfn="virtfn$idx"
52 # shellcheck disable=SC2012
53 vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}')
54 # Capture and set MAC of the VF before unbinding from linux, for later use in CNI
55 mac=$(cat /sys/bus/pci*/*/"$vfpci"/net/*/address)
56 ip link set dev "$pf" vf $idx mac "$mac"
57 # Bind VF to vfio-pci
58 echo "$vfpci" >/sys/bus/pci*/*/"$vfpci"/driver/unbind
59 echo "vfio-pci" >/sys/devices/pci*/*/"$vfpci"/driver_override
60 echo "$vfpci" >/sys/bus/pci/drivers/vfio-pci/bind
61 done
62}
63
64for pf in "$@"; do
65 setup_pf "$pf"
66 vfio_bind "$pf"
67done