| #!/bin/bash |
| |
| {# |
| SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org> |
| SPDX-FileCopyrightText: © 2019 Intel Corporation |
| SPDX-License-Identifier: Apache-2.0 |
| |
| # copied from https://github.com/clearlinux/cloud-native-setup/blob/master/clr-k8s-examples/9-multi-network/systemd/sriov.sh |
| #} |
| # {{ ansible_managed }} |
| |
| set -o errexit |
| set -o pipefail |
| set -o nounset |
| set -x |
| |
| setup_pf() { |
| local pf=$1 |
| local num_vfs |
| |
| echo "Resetting PF $pf" |
| echo 0 | tee /sys/class/net/"$pf"/device/sriov_numvfs |
| num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_totalvfs) |
| echo "Enabling $num_vfs VFs for $pf" |
| echo "$num_vfs" | tee /sys/class/net/"$pf"/device/sriov_numvfs |
| ip link set "$pf" up |
| sleep 1 |
| } |
| |
| vfio_bind() { |
| local pf=$1 |
| local pfpci |
| local num_vfs |
| |
| pfpci=$(readlink /sys/devices/pci*/*/*/net/"$pf"/device | awk '{print substr($1,10)}') |
| num_vfs=$(cat /sys/class/net/"$pf"/device/sriov_numvfs)/2 |
| |
| local vfpci |
| local mac |
| for ((idx = 0; idx < num_vfs; idx++)); do |
| local vfn="virtfn$idx" |
| |
| # shellcheck disable=SC2012 |
| vfpci=$(ls -l /sys/devices/pci*/*/"$pfpci" | awk -v vfn=$vfn 'vfn==$9 {print substr($11,4)}') |
| |
| # Capture and set MAC of the VF before unbinding from linux, for later use in CNI |
| mac=$(cat /sys/bus/pci*/*/"$vfpci"/net/*/address) |
| ip link set dev "$pf" vf $idx mac "$mac" |
| |
| # Bind VF to vfio-pci |
| echo "$vfpci" >/sys/bus/pci*/*/"$vfpci"/driver/unbind |
| echo "vfio-pci" >/sys/devices/pci*/*/"$vfpci"/driver_override |
| echo "$vfpci" >/sys/bus/pci/drivers/vfio-pci/bind |
| done |
| } |
| |
| for pf in "$@"; do |
| setup_pf "$pf" |
| vfio_bind "$pf" |
| done |