#!/bin/sh # Only supported driver for now is r8169 ,ake it configurable in the future driver='r8169' sys_drv_name="$driver"'-vfnetdev' intf='enp4s0' usage() { echo "$0 create/destroy . Default $intf" exit 1 } [ $# -lt 1 ] && usage [ -n "$2" ] && intf="$2" echo "Checking for interface $intf" if [ ! -e "/sys/class/net/$intf/device/mdev_supported_types/$sys_drv_name/create" ]; then echo "interface $intf has no vfio-mdev support" exit 1 fi vf_create() { dev_uuid=$(uuidgen) sudo sh -c "echo $dev_uuid > /sys/class/net/$intf/device/mdev_supported_types/$sys_drv_name/create" #sudo sh -c "echo $dev_uuid > /sys/class/net/$intf/mdev_supported_types/net-vfnetdev/create" #the newly created mdev is not tied to any port of the parent deice yet echo "Bind $intf to the newly created mdevice $dev_uuid" sudo sh -c "echo $intf > /sys/bus/mdev/devices/$dev_uuid/vfnetdev/netdev" #ensure the IOMMU group is readble by non root program vfio_group=$(basename $(readlink /sys/bus/mdev/devices/$dev_uuid/iommu_group)) user=$(whoami) grp=$(id -g -n $user) sudo chown "$user":"$grp" /dev/vfio/$vfio_group echo "created $dev_uuid" echo "Run ./r8169 $vfio_group $dev_uuid" } vf_destroy() { # FIXME only one mdev per ethernet supported for now echo 1 > /sys/class/mdev_bus/$intf/*/remove > /dev/null } case "$1" in create) vf_create ;; destroy) vf_destroy ;; *) usage ;; esac