Skip to contentSkip to conversation

runqemu: support different net modes

PATCH New Adrian Freihofer 1 participant 4 messages Jun 22, 2015

lore
Share & tools
lore
#0 lore raw

cover runqemu: support different net modes

runqemu runs qemu with one emulated NIC. The host is configured to work as a NAT router for this emulated NIC. The virtual device is connected to the Internet by default. This network mode is fine if a standalone device needs to be developed and tested.
Today's M2M/IoT applications demand for devices with extended network capabilities. To develop connectivity related features a bridged network setup is much better suited than the supported NAT setup. A bridged setup allows to virtualize the device under test/development as well as a reference implementation of a cloud server. If the cloud server is part of the virtual test setup ptests and image tests for connectivity verification are possible.

Tutorial to run a M2M test and development setup:
1. Prepare the host for the 192.168.7.x connection. The "tap0" device is for debugging purpose only. It is created by: "runquem-gen-tapdevs ... noinet"
2. Prepare the host for a second interface emulated by qemu. Setup a bridge and a tap device e.g. tap-inet0 which is connected to the bridge.
3. Run the virtual device with two emulated NICs:
runqemu noinet ... qemuparams="-device virtio-net-pci,netdev=tap-inet0 ..."
4. Run another qemu instance and tap device for the reference cloud server.
5. Run a ptest to verify the connection between device and cloud server

This patch-set provides minimal changes to poky which are required to run a setup as described above. These patches do not provide extended implementations of runquem-gen-tapdevs, runqemu or a script to run a virtual reference server. This still needs to be implemented by the user. However, with the patch-set the user is enabled to do so without hacking in the poky folder.

Adrian Freihofer (3):
connman: fix qemu config
runqemu: add support for multiple NICs
runqemu: support different net modes

 meta/recipes-connectivity/connman/connman-conf.bb  |  2 +-
 .../connman/connman-conf/qemuall/wired-setup       |  6 ++--
 scripts/runqemu                                    |  5 +++
 scripts/runqemu-gen-tapdevs                        | 11 ++++---
 scripts/runqemu-ifdown                             | 33 +++++++++++--------
 scripts/runqemu-ifup                               | 38 +++++++++++++---------
 scripts/runqemu-internal                           | 13 +++++---
 7 files changed, 68 insertions(+), 40 deletions(-)
Signature · 1 line
2.4.3
#1 lore raw

PATCH 1/3 connman: fix qemu config

If a network interface is configured by kernel boot parameters and the image is booted in qemu, connman takes the IP settings from /proc/cmdline for this NIC.
The regex for parsing /proc/cmdline is invalid.

---
 meta/recipes-connectivity/connman/connman-conf.bb                  | 2 +-
 meta/recipes-connectivity/connman/connman-conf/qemuall/wired-setup | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
meta/recipes-connectivity/connman/connman-conf.bb +1 -1
index 2358d9d..4271a37 100644
@@ -7,7 +7,7 @@ LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/GPL-2.0;md5=80
 SRC_URI_append_qemuall = "file://wired.config \
                           file://wired-setup \
                          "
-PR = "r2"
+PR = "r3"
 
 S = "${WORKDIR}"
 
meta/recipes-connectivity/connman/connman-conf/qemuall/wired-setup +4 -2
index c46899e..4a61513 100644
@@ -2,8 +2,10 @@
 
 CONFIGF=/var/lib/connman/wired.config
 
-# Extract wired network config from /proc/cmdline
-NET_CONF=`cat /proc/cmdline |sed -ne 's/^.*ip=\([^ ]*\):\([^ ]*\):\([^ ]*\):\([^ ]*\).*$/\1\/\4\/\3/p'`
+# Extract wired network config (IPv4 = <client-ip>/<netmask>/<gw-ip>) from /proc/cmdline
+# "ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip> "
+NET_CONF=`cat /proc/cmdline |sed -ne 's/^.*ip=\([0-9.]*\):\([0-9.]*\):\([0-9.]*\):\([0-9.]*\).*$/\1\/\4\/\3/p'`
+NET_CONF=${NET_CONF=%/}
 
 # Check if eth0 is already set via kernel cmdline
 if [ "x$NET_CONF" = "x" ]; then
Signature · 1 line
2.4.3
#2 lore raw

PATCH 2/3 runqemu: add support for multiple NICs

In reply to Adrian Freihofer

Fixes [YOCTO #6356]

Current runqemu script does not support qemu configurations with more than one emulated NIC. If parameters for one or more additional NICs are appended qemu ends up with an invalid configuration.

Example to reproduce:
runqemu ... qemuparams="-net nic,model=virtio... -net tap, ..."

---
 scripts/runqemu-internal | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/runqemu-internal +1 -1
index bc2a355..ea2badc 100755
@@ -256,7 +256,7 @@ else
         n1=$(($n0 * 2 + 1))
         n2=$(($n1 + 1))
 
-        KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0"
+        KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0::eth0:off"
         QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
         if [ "$KVM_ACTIVE" = "yes" ]; then
             QEMU_NETWORK_CMD="-net nic,model=virtio $QEMU_TAP_CMD,vhost=on"
Signature · 1 line
2.4.3
#3 lore raw

PATCH 3/3 runqemu: support different net modes

In reply to Adrian Freihofer

This patch adds a "noinet" command line parameter to runqemu. If the noinet parameter is passed to runqemu, runquemu does not pass the default gateway to the kernel parameters of the target device and it does not configure NAT routing on the host. In noinet network mode the connection with IP address 192.168.7.x can be considered as a dedicated debug interface.

To emulate Internet connectivity in a test or development setup an additional NIC can be emulated by qemu. The second NIC might be connected to a bridge on the host. This is more than helpful if connected applications are developed. E.g. for testing and development of M2M/IoT applications a reference implementation of a cloud server can be connected to the bridge as well. Such a setup allows writing ptests to verify the communication between the target device and the cloud server.

---
 scripts/runqemu             |  5 +++++
 scripts/runqemu-gen-tapdevs | 11 +++++++----
 scripts/runqemu-ifdown      | 33 +++++++++++++++++++--------------
 scripts/runqemu-ifup        | 38 +++++++++++++++++++++++---------------
 scripts/runqemu-internal    | 13 +++++++++----
 5 files changed, 63 insertions(+), 37 deletions(-)
scripts/runqemu +5 -0
index 84ece4d..f354c21 100755
@@ -31,6 +31,7 @@ usage() {
     echo "  VM - boot a vmdk image"
     echo "  Simplified QEMU command-line options can be passed with:"
     echo "    nographic - disables video console"
+    echo "    noinet - disables route to internet on eth0"
     echo "    serial - enables a serial console on /dev/ttyS0"
     echo "    kvm - enables KVM when running qemux86/qemux86-64 (VT-capable CPU required)"
     echo "    publicvnc - enable a VNC server open to all hosts"
@@ -70,6 +71,7 @@ SCRIPT_KERNEL_OPT=""
 SERIALSTDIO=""
 KVM_ENABLED="no"
 KVM_ACTIVE="no"
+NETMODE="nat"
 
 # Determine whether the file is a kernel or QEMU image, and set the
 # appropriate variables
@@ -188,6 +190,9 @@ while true; do
         "publicvnc")
             SCRIPT_QEMU_OPT="$SCRIPT_QEMU_OPT -vnc 0.0.0.0:0"
             ;;
+        "noinet")
+            NETMODE="noinet"
+            ;;
         "") break ;;
         *)
             # A directory name is an nfs rootfs
scripts/runqemu-gen-tapdevs +7 -4
index d3b27be..f1bb5ca 100755
@@ -23,11 +23,13 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 usage() {
-	echo "Usage: sudo $0 <uid> <gid> <num> <native-sysroot-basedir>"
+	echo "Usage: sudo $0 <uid> <gid> <num> <native-sysroot-basedir> <netmode>"
         echo "Where <uid> is the numeric user id the tap devices will be owned by"
 	echo "Where <gid> is the numeric group id the tap devices will be owned by"
 	echo "<num> is the number of tap devices to create (0 to remove all)"
 	echo "<native-sysroot-basedir> is the path to the build system's native sysroot"
+	echo "<netmode> is optional and defaults to nat. If netmode is set to noinet,"
+	echo "          qemu is started without internet gateway."
 	exit 1
 }
 
@@ -36,7 +38,7 @@ if [ $EUID -ne 0 ]; then
 	exit
 fi
 
-if [ $# -ne 4 ]; then
+if [ "$#" -lt 4 -o "$#" -gt 5 ]; then
 	echo "Error: Incorrect number of arguments"
 	usage
 fi
@@ -45,6 +47,7 @@ TUID=$1
 GID=$2
 COUNT=$3
 SYSROOT=$4
+NETMODE=${5:-nat}
 
 TUNCTL=$SYSROOT/usr/bin/tunctl
 if [[ ! -x "$TUNCTL" || -d "$TUNCTL" ]]; then
@@ -75,10 +78,10 @@ for tap in `$IFCONFIG link | grep tap | awk '{ print \$2 }' | sed s/://`; do
 	$TUNCTL -d $tap
 done
 
-echo "Creating $COUNT tap devices for UID: $TUID GID: $GID..."
+echo "Creating $COUNT tap devices for UID: $TUID GID: $GID netmode: $NETMODE..."
 for ((index=0; index < $COUNT; index++)); do
 	echo "Creating tap$index"
-	ifup=`$RUNQEMU_IFUP $TUID $GID $SYSROOT 2>&1`
+	ifup=`$RUNQEMU_IFUP $TUID $GID $SYSROOT $NETMODE 2>&1`
 	if [ $? -ne 0 ]; then
 		echo "Error running tunctl: $ifup"
 		exit 1
scripts/runqemu-ifdown +19 -14
index 8f66cfa..9d12391 100755
@@ -27,7 +27,8 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 usage() {
-	echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir>"
+	echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir> <netmode>"
+	echo "netmode is optional. Supported are nat (default) or noinet."
 }
 
 if [ $EUID -ne 0 ]; then
@@ -35,13 +36,14 @@ if [ $EUID -ne 0 ]; then
 	exit 1
 fi
 
-if [ $# -ne 2 ]; then
+if [ "$#" -lt 2 -o "$#" -gt 3 ]; then
 	usage
 	exit 1
 fi
 
 TAP=$1
 NATIVE_SYSROOT_DIR=$2
+NETMODE=${3:-nat}
 
 TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl
 if [ ! -e "$TUNCTL" ]; then
@@ -51,16 +53,19 @@ fi
 
 $TUNCTL -d $TAP
 
-# cleanup the remaining iptables rules
-IPTABLES=`which iptables 2> /dev/null`
-if [ "x$IPTABLES" = "x" ]; then
-	IPTABLES=/sbin/iptables
-fi
-if [ ! -x "$IPTABLES" ]; then
-	echo "$IPTABLES cannot be executed"
-	exit 1
+
+if [ "$NETMODE" == "nat" ]; then
+	# cleanup the remaining iptables rules
+	IPTABLES=`which iptables 2> /dev/null`
+	if [ "x$IPTABLES" = "x" ]; then
+		IPTABLES=/sbin/iptables
+	fi
+	if [ ! -x "$IPTABLES" ]; then
+		echo "$IPTABLES cannot be executed"
+		exit 1
+	fi
+	n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
+	dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
+	$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
+	$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
 fi
-n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
-dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
-$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
-$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
scripts/runqemu-ifup +23 -15
index b5a3db9..eef0a7f 100755
@@ -34,7 +34,8 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 usage() {
-	echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir>"
+	echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir> <netmode>"
+	echo "netmode is optional. Supported are nat (default) or noinet."
 }
 
 if [ $EUID -ne 0 ]; then
@@ -42,7 +43,7 @@ if [ $EUID -ne 0 ]; then
 	exit 1
 fi
 
-if [ $# -ne 3 ]; then
+if [ "$#" -lt 3 -o "$#" -gt 4 ]; then
 	usage
 	exit 1
 fi
@@ -50,6 +51,7 @@ fi
 USERID="-u $1"
 GROUP="-g $2"
 NATIVE_SYSROOT_DIR=$3
+NETMODE=${4:-nat}
 
 TUNCTL=$NATIVE_SYSROOT_DIR/usr/bin/tunctl
 if [ ! -x "$TUNCTL" ]; then
@@ -80,13 +82,16 @@ if [ ! -x "$IFCONFIG" ]; then
 	exit 1
 fi
 
-IPTABLES=`which iptables 2> /dev/null`
-if [ "x$IPTABLES" = "x" ]; then
-	IPTABLES=/sbin/iptables
-fi
-if [ ! -x "$IPTABLES" ]; then
-	echo "$IPTABLES cannot be executed"
-	exit 1
+
+if [ "$NETMODE" == "nat" ]; then
+	IPTABLES=`which iptables 2> /dev/null`
+	if [ "x$IPTABLES" = "x" ]; then
+		IPTABLES=/sbin/iptables
+	fi
+	if [ ! -x "$IPTABLES" ]; then
+		echo "$IPTABLES cannot be executed"
+		exit 1
+	fi
 fi
 
 n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
@@ -96,11 +101,14 @@ $IFCONFIG link set dev $TAP up
 dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
 $IFCONFIG route add to 192.168.7.$dest dev $TAP
 
-# setup NAT for tap0 interface to have internet access in QEMU
-$IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
-$IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
-echo 1 > /proc/sys/net/ipv4/ip_forward
-echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
-$IPTABLES -P FORWARD ACCEPT
+
+if [ "$NETMODE" == "nat" ]; then
+	# setup NAT for tap0 interface to have internet access in QEMU
+	$IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
+	$IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
+	echo 1 > /proc/sys/net/ipv4/ip_forward
+	echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp
+	$IPTABLES -P FORWARD ACCEPT
+fi
 
 echo $TAP
scripts/runqemu-internal +9 -4
index ea2badc..ed4af09 100755
@@ -211,10 +211,10 @@ else
             echo "Setting up tap interface under sudo"
             # Redirect stderr since we could see a LD_PRELOAD warning here if pseudo is loaded
             # but inactive. This looks scary but is harmless
-            tap=`sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT 2> /dev/null`
+            tap=`sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT $NETMODE 2> /dev/null`
             if [ $? -ne 0 ]; then
                 # Re-run standalone to see verbose errors
-                sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT
+                sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT $NETMODE
                 return 1
             fi
             LOCKFILE="$LOCKDIR/$tap"
@@ -232,7 +232,7 @@ else
             if [ ! -e "$NOSUDO_FLAG" -a "$USE_PRECONF_TAP" = "no" ]; then
                 # Redirect stderr since we could see a LD_PRELOAD warning here if pseudo is loaded
                 # but inactive. This looks scary but is harmless
-                sudo $QEMUIFDOWN $TAP $OECORE_NATIVE_SYSROOT 2> /dev/null
+                sudo $QEMUIFDOWN $TAP $OECORE_NATIVE_SYSROOT $NETMODE 2> /dev/null
             fi
             echo "Releasing lockfile of preconfigured tap device '$TAP'"
             release_lock $LOCKFILE
@@ -256,7 +256,12 @@ else
         n1=$(($n0 * 2 + 1))
         n2=$(($n1 + 1))
 
-        KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0::eth0:off"
+        #ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip>
+        if [ "$NETMODE" == "noinet" ]; then  # Do not define the gateway
+            KERNEL_NETWORK_CMD="ip=192.168.7.$n2:::255.255.255.0::eth0:off"
+        else  # Default to nat
+            KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0::eth0:off"
+        fi
         QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
         if [ "$KVM_ACTIVE" = "yes" ]; then
             QEMU_NETWORK_CMD="-net nic,model=virtio $QEMU_TAP_CMD,vhost=on"
Signature · 1 line
2.4.3