Skip to content

Instantly share code, notes, and snippets.

@thaihust
Last active July 5, 2022 13:38
Show Gist options
  • Save thaihust/f55e72e61d6123caab6549a32c51643f to your computer and use it in GitHub Desktop.
Save thaihust/f55e72e61d6123caab6549a32c51643f to your computer and use it in GitHub Desktop.
#! /bin/bash
MAX_VFS=63
ixgbe_nic_list=()
pci_nic_list=()
confirmed_nic_list=()
numvfs_list=()
res="n"
phy_driver=ixgbe
base_mac=0a:bb:cc:dd
echo
echo
echo "!!! IMPORTANT !!!"
echo "BIND ALL CURRENT VFs TO IXGBEVF BEFORE PROCEEDING"
echo "!!! IMPORTANT !!!"
echo
echo
# get all ixgbe-driven interfaces
interfaces=`ip -o -a link show | awk -F': ' '{print $2}' | grep -v '^lo' | sort`
read -d '' -r -a iface_array <<< "$interfaces"
for iface in "${iface_array[@]}"
do
driver=`ethtool -i "${iface}" | grep "driver:" | awk '{print $2}'`
if [ "$driver" = "$phy_driver" ]; then
ixgbe_nic_list+=("$iface")
fi
done
echo "Detected devices:"
echo " ${ixgbe_nic_list[@]}"
echo "-------"
# confirm change for each interface
for index in "${!ixgbe_nic_list[@]}"
do
echo -n "Confirm changing number of VFs for ${ixgbe_nic_list[index]} (y/n)? "
read res
if [ "$res" = "y" ]; then
confirmed_nic_list+=("${index}")
fi
done
echo "-------"
# get number of VFs for each confirmed interface
for index in "${confirmed_nic_list[@]}"
do
echo -n "Enter no. of VFs for ${ixgbe_nic_list[index]}: "
read numvfs_list[index]
while [[ "${numvfs_list[index]}" -gt "${MAX_VFS}" || "${numvfs_list[index]}" -lt 1 ]]
do
echo -n "Number of VFs must be between 1 and ${MAX_VFS}. Try again: "
read numvfs_list[index]
done
done
# confirm again cause why not
echo "-------"
echo "Summary"
for index in "${confirmed_nic_list[@]}"
do
echo "NIC ${ixgbe_nic_list[index]}: ${numvfs_list[index]} VFs"
done
echo -n "confirm (y/n)? "
read res
if [ "${res}" != "y" ]; then
echo "exiting..."
exit
fi
echo "-------"
# setup VFs
echo "Setting up VFs"
for index in "${confirmed_nic_list[@]}"
do
nic="${ixgbe_nic_list[index]}"
numvfs="${numvfs_list[index]}"
echo
echo "NIC: ${nic}"
echo " Changing number of VFs to ${numvfs}"
echo 0 > /sys/class/net/${nic}/device/sriov_numvfs
echo ${numvfs} > /sys/class/net/${nic}/device/sriov_numvfs
echo " Setting mac address and spoofchk off for VFs"
k=0
for j in `seq 0 $((numvfs-1))`
do
# set mac
mac=""
if [ "$k" -lt "10" ]; then
mac="$base_mac:0$index:0$k"
else
mac="$base_mac:0$index:$k"
fi
k=$((k+1))
ip link set dev ${nic} vf ${j} mac $mac
# turn off spoof checking
ip link set dev ${nic} vf ${j} spoofchk off
done
# reload nic
echo " Reloading"
ip link set dev ${nic} down
ip link set dev ${nic} up
done
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment