mode: 0755
overwrite: true
path: "/usr/local/bin/configure-switchdev.sh"
contents:
  inline: |
    #!/bin/bash
    set -eux
    input="/etc/sriov_config.json"
    UDEV_RULE_FILE='/etc/udev/rules.d/10-persistent-net.rules'

    if [ ! -f $input ]; then
      echo "File /etc/sriov_config.json not exist."
      exit
    fi

    append_to_file(){
      content="$1"
      file_name="$2"
      if ! test -f "$file_name"
      then
        echo "$content" > "$file_name"
      else
        if ! grep -Fxq "$content" "$file_name"
        then
          echo "$content" >> "$file_name"
        fi
      fi
    }

    add_udev_rule_for_sriov_pf(){
        pf_pci=$(grep PCI_SLOT_NAME /sys/class/net/$1/device/uevent | cut -d'=' -f2)
        udev_data_line="SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", KERNELS==\"$pf_pci\", NAME=\"$1\""
        append_to_file "$udev_data_line" "$UDEV_RULE_FILE"
    }

    jq -c '.interfaces[]' ${input} | while read iface;do
      num_vfs=$(echo $iface | jq '.numVfs' -r)
      pci_addr=$(echo $iface | jq '.pciAddress' -r)
      echo "Set $num_vfs VFs on device $pci_addr"

      # create VFs
      echo $num_vfs > /sys/bus/pci/devices/${pci_addr}/sriov_numvfs
    done
    # wait for vfs to be ready
    sleep 5

    jq -c '.interfaces[]' $input | while read iface;
    do
      eswitch_mode=$(echo $iface | jq '.eSwitchMode' -r)
      if [[ "$eswitch_mode" == "switchdev" ]]; then
        pci_addr=$(echo $iface | jq '.pciAddress' -r)
        name=$(echo $iface | jq '.name' -r)

        # Create udev rule to save PF name
        add_udev_rule_for_sriov_pf $name

        echo "unload VF driver for $pci_addr"
        VfDirs=$(ls /sys/bus/pci/devices/${pci_addr} | grep virtfn)
        for VfDir in $VfDirs
        do
          VfPciAddr=$(basename "$( readlink -f /sys/bus/pci/devices/${pci_addr}/$VfDir )")
          echo $VfPciAddr > /sys/bus/pci/drivers/mlx5_core/unbind || true
        done
        # set PF to switchdev mode
        devlink dev eswitch set pci/${pci_addr} mode switchdev
        ip link set ${name} up

        # turn hw-tc-offload on
        /usr/sbin/ethtool -K ${name} hw-tc-offload on

        # load VF driver
        for VfDir in $VfDirs
        do
          VfPciAddr=$(basename "$( readlink -f /sys/bus/pci/devices/${pci_addr}/$VfDir )")
          echo $VfPciAddr > /sys/bus/pci/drivers_probe
        done
      fi
    done
