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.

Revisions

  1. thaihust revised this gist Jul 5, 2022. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions create_vfio.sh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ confirmed_nic_list=()
    numvfs_list=()
    res="n"
    phy_driver=ixgbe
    base_mac=0a:bb:cc:dd

    echo
    echo
    @@ -93,9 +94,9 @@ do
    # set mac
    mac=""
    if [ "$k" -lt "10" ]; then
    mac="aa:bb:cc:dd:0$index:0$k"
    mac="$base_mac:0$index:0$k"
    else
    mac="aa:bb:cc:dd:0$index:$k"
    mac="$base_mac:0$index:$k"
    fi
    k=$((k+1))
    ip link set dev ${nic} vf ${j} mac $mac
  2. thaihust renamed this gist Jun 22, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. thaihust created this gist Jun 22, 2022.
    114 changes: 114 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    #! /bin/bash

    MAX_VFS=63

    ixgbe_nic_list=()
    pci_nic_list=()
    confirmed_nic_list=()
    numvfs_list=()
    res="n"
    phy_driver=ixgbe

    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="aa:bb:cc:dd:0$index:0$k"
    else
    mac="aa:bb:cc:dd: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"