diff mbox series

[2/2] runqemu-ifupdown/get-tapdevs: Add support for ip tuntap

Message ID 91fca441bb3b2040a2c25ad6d6dc14526c6a6fe0.1686300665.git.joerg.sommer@navimatix.de
State New
Headers show
Series [1/2] runqemu-gen-tapdevs: Refactoring | expand

Commit Message

Jörg Sommer June 9, 2023, 8:51 a.m. UTC
The *ip* command supports the creation and destruction of TAP devices since
2009 and might be more likely installed on systems then *tunctl*. Therefore
it should be tried to setup or teardown the TAP interface with *ip* before
falling back to *tunctl*.

https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=580fbd88f75cc9eea0d28a48c025b090eb9419a7

Signed-off-by: Jörg Sommer <joerg.sommer@navimatix.de>
---
 scripts/runqemu-gen-tapdevs | 26 +++++++++++++++++---------
 scripts/runqemu-ifdown      | 14 ++++++++------
 scripts/runqemu-ifup        | 31 +++++++++++++++++++------------
 3 files changed, 44 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/scripts/runqemu-gen-tapdevs b/scripts/runqemu-gen-tapdevs
index f2d6cc39c2..ffb82adce6 100755
--- a/scripts/runqemu-gen-tapdevs
+++ b/scripts/runqemu-gen-tapdevs
@@ -50,12 +50,6 @@  if ! [ $COUNT -ge 0 ]; then
 	exit 1
 fi
 
-TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
-if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
-	echo "Error: $TUNCTL is not an executable"
-	usage
-fi
-
 if [ $EUID -ne 0 ]; then
 	echo "Error: This script must be run with root privileges"
 	exit
@@ -68,15 +62,29 @@  if [ ! -x "$RUNQEMU_IFUP" ]; then
 	exit 1
 fi
 
-if ! interfaces=`ip link` 2>/dev/null; then
+TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
+ip_supports_tuntap=false
+if interfaces=`ip tuntap list` 2>/dev/null; then
+	ip_supports_tuntap=true
+	interfaces=`echo "$interfaces |cut -f1 -d:`
+elif [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
+	echo "Error: $TUNCTL is not an executable"
+	usage
+elif interfaces=`ip link` 2>/dev/null; then
+	interfaces=`echo "$interfaces" | sed '/^[0-9]\+: \(docker[0-9]\+\):.*/!d; s//\1/'`
+else
 	echo "Failed to call 'ip link'" >&2
 	exit 1
 fi
 
 # Ensure we start with a clean slate
-for tap in `echo "$interfaces" | sed '/^[0-9]\+: \(docker[0-9]\+\):.*/!d; s//\1/'`; do
+for tap in $interfaces; do
 	echo "Note: Destroying pre-existing tap interface $tap..."
-	$TUNCTL -d $tap
+	if $ip_supports_tuntap; then
+		ip tuntap del $tap mode tap
+	else
+		$TUNCTL -d $tap
+	fi
 done
 rm -f /etc/runqemu-nosudo
 
diff --git a/scripts/runqemu-ifdown b/scripts/runqemu-ifdown
index e0eb5344c6..f72166b32b 100755
--- a/scripts/runqemu-ifdown
+++ b/scripts/runqemu-ifdown
@@ -33,13 +33,15 @@  fi
 TAP=$1
 STAGING_BINDIR_NATIVE=$2
 
-TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
-if [ ! -e "$TUNCTL" ]; then
-	echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
-	exit 1
-fi
+if !ip tuntap del $TAP mode tap 2>/dev/null; then
+	TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
+	if [ ! -e "$TUNCTL" ]; then
+		echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
+		exit 1
+	fi
 
-$TUNCTL -d $TAP
+	$TUNCTL -d $TAP
+fi
 
 IFCONFIG=`which ip 2> /dev/null`
 if [ "x$IFCONFIG" = "x" ]; then
diff --git a/scripts/runqemu-ifup b/scripts/runqemu-ifup
index bb661740c5..5fdcddeeda 100755
--- a/scripts/runqemu-ifup
+++ b/scripts/runqemu-ifup
@@ -41,22 +41,29 @@  USERID="-u $1"
 GROUP="-g $2"
 STAGING_BINDIR_NATIVE=$3
 
-TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
-if [ ! -x "$TUNCTL" ]; then
-       echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
-	exit 1
+if taps=$(ip tuntap list 2>/dev/null); then
+	tap_no=$(( $(echo "$taps" |sort -r |sed 's/^tap//; s/:.*//; q') + 1 ))
+	ip tuntap add tap$tap_no mode tap group $2 && TAP=tap$tap_no
 fi
 
-TAP=`$TUNCTL -b $GROUP 2>&1`
-STATUS=$?
-if [ $STATUS -ne 0 ]; then
-# If tunctl -g fails, try using tunctl -u, for older host kernels 
-# which do not support the TUNSETGROUP ioctl
-	TAP=`$TUNCTL -b $USERID 2>&1`
+if [ -z $TAP ]; then
+	TUNCTL=$STAGING_BINDIR_NATIVE/tunctl
+	if [ ! -x "$TUNCTL" ]; then
+		echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native"
+		exit 1
+	fi
+
+	TAP=`$TUNCTL -b $GROUP 2>&1`
 	STATUS=$?
 	if [ $STATUS -ne 0 ]; then
-		echo "tunctl failed:"
-		exit 1
+	# If tunctl -g fails, try using tunctl -u, for older host kernels
+	# which do not support the TUNSETGROUP ioctl
+		TAP=`$TUNCTL -b $USERID 2>&1`
+		STATUS=$?
+		if [ $STATUS -ne 0 ]; then
+			echo "tunctl failed:"
+			exit 1
+		fi
 	fi
 fi