When you run qemu normally without network options specified, automatically user mode network is setup between host OS and guest OS. In user mode networking, QEmu manages network interface internally in the user mode emulator application. QEmu provides DHCP host which assigns a dynamic IP for the guest OS. TCP and UDP ports can be redirected from the host OS to the guest OS using QEmu command line parameters. Easy to set up but, Only TCP and UDP connections are available by default. Ping command, for example, doesn't work inside the guest OS
In TAP networking, QEmu connects the guest OS ethernet to the host OS using TAP network bridge. Linux creates a virtual network interface (tap0) which appears in ifconfig listing as other network interfaces.
First off all you need to configure a network bridge. Which requires the bridge-utils package. For the TUN/TAP you need to check your kernel config file for CONFIG_TUN=m or CONFIG_TUN=y.
$ grep CONFIG_TUN= /boot/config-`uname -r`
Also you need to make sure /dev/net/tun exists. To make it:
$ mknod /dev/net/tun c 10 200
You need to have bridge utils installed. But then its safe to install these two packages, and I have installed these two and my network is working fine.
$ sudo apt-get install uml_utilities
$ sudo apt-get install bridge-utils
Once this is done, We can start setting up the network. I assume that Windows XP is installed on QEMU and KQEMU accelerator is being already used for the speed up purpose. Please refer to my other post for doing this.
For setting up the network qemu calls /etc/qemu-ifup script when -net nic -net tap option is given, It expects you to write the qemu-ifup script to setup bridge network. So first lets do that.
$ sudo vim /etc/qemu-ifup
# script to bring up the tun device in QEMU in bridged mode
# first parameter is name of tap device (e.g. tap0)
# Prasad Shetty
# First take eth0 down, Add bridge, then bring it up with IP 0.0.0.0
ifconfig eth0 down
brctl addbr br0
ifconfig eth0 0.0.0.0 up
# Configure the bridge, create bridge between eth0 and br0
# and add the default route
ifconfig br0 $ETH0IP netmask 255.255.255.0 broadcast $BROADCAST
brctl addif br0 eth0
route add default gw $GATEWAY
# Bring the TAP bridge network up, and intereface with br0
ifconfig $1 up
brctl addif br0 $1
when qemu shuts down, it will call /etc/qemu-ifdown script, in which we can add functionality to bring down the bridge, and bring up the ethernet in normal mode.
$ sudo vim /etc/qemu-ifdown
# Script to bring down and delete bridge br0 when QEMU exits
# Prasad Shetty
# Bring down eth0 and br0
ifconfig eth0 down
ifconfig br0 down
# Delete the bridge
brctl delbr br0
# bring up eth0 in "normal" mode
ifconfig eth0 0.0.0.0 -promisc up
Once all this is done, we just need to specify which tap device to use for bridge to qemu, we can use tunctl to setup a TUN TAP device and then pass the retun value to qemu to use it with qemu-ifup and qemu-ifdown script.
I add some more functionality to this script to take care of setting up basic things before calling the qemu to run windows with network options. The comments itself explain the details of it. This is called wrapper script. All these things work with root and setting up the same for user is possible but it has lot of complexities and most possibly may not work properly.
$ sudo vim winxp.sh
# insert the tuntap module
sudo modprobe tun
# insert the bridge module
sudo modprobe bridge
# insert the kqemu module
sudo modprobe kqemu major=0
iface=`sudo tunctl -b`
sudo qemu -m 256 -boot c -hda xp.img -snapshot -kernel-kqemu -localtime
-net nic -net tap,ifname=$iface
sudo tunctl -d $iface &>/dev/null
Now you can run this wrapper script and your QEMU comes up with windows xp with network up. Enjoy browsing under windows running on QEMU.
I sometimes get bus error, which I dont think is because of any errors in the network setup, If I fix it I will update here.