Page 1 of 1
Network test under qemu
Posted: Sun Apr 15, 2012 10:03 am
by fiveayem
Hello,
I am currently developping a network stack for my OS and I need a bit of your help.
For the moment, I am at the very beginning, as I have only developped an ethernet card driver for the RTL8139. I configured qemu so that it emulates a PC with this card. Card detection works well. However, I would like to do further tests with it. For instance, it would be nice if I could send packet to the network card in the virtual machine, and see if it receives them well.
How could I do such a thing with qemu ?
Thanks for your help.
Re: Network test under qemu
Posted: Sun Apr 15, 2012 12:32 pm
by bluemoon
I'm on the same status with you (just started working on RTL8139)
For qemu, the easy one is "user mode", or host-only adapter in VMware's language.
However, the manual stated that you cannot actively send packet from host to the guest. I guess the VM's nat would block those packet, although you may try doing UDP hole punching.
The official way should be create a tap device on the host(aka bridged network), and map it the qemu.
You may find more information here:
http://wiki.qemu.org/Documentation/Networking
Re: Network test under qemu
Posted: Sun Apr 15, 2012 12:37 pm
by fiveayem
Ok, thanks, I'm going to try it. I'll let you know the result...
Re: Network test under qemu
Posted: Sun Apr 15, 2012 12:48 pm
by Nable
Did you try reading QEmu/docs/qemu-doc.html ?
You can create TUN/TAP pseudo-interface in your host, then connect it to VLAN of your virtual machine and run tcpdump/wireshark on TUN interface.
Here are some quotes:
3.7.2 Using TAP network interfaces
This is the standard way to connect QEMU to a real network. QEMU adds a virtual network device on your host (called tapN), and you can then configure it as if it was a real ethernet card.
3.7.2.1 Linux host
As an example, you can download the linux-test-xxx.tar.gz archive and copy the script qemu-ifup in /etc and configure properly sudo so that the command ifconfig contained in qemu-ifup can be executed as root. You must verify that your host kernel supports the TAP network interfaces: the device /dev/net/tun must be present.
See sec_invocation to have examples of command lines using the TAP network interfaces.
3.7.2.2 Windows host
There is a virtual ethernet driver for Windows 2000/XP systems, called TAP-Win32. But it is not included in standard QEMU for Windows, so you will need to get it separately. It is part of OpenVPN package, so download OpenVPN from :
http://openvpn.net/.
-net nic[,vlan=n][,macaddr=mac][,model=type] [,name=name][,addr=addr][,vectors=v]
Create a new Network Interface Card and connect it to VLAN n (n = 0 is the default). The NIC is an e1000 by default on the PC target. Optionally, the MAC address can be changed to mac, the device address set to addr (PCI cards only), and a name can be assigned for use in monitor commands. Optionally, for PCI cards, you can specify the number v of MSI-X vectors that the card should have; this option currently only affects virtio cards; set v = 0 to disable MSI-X. If no -net option is specified, a single NIC is created. Qemu can emulate several different models of network card. Valid values for type are virtio, i82551, i82557b, i82559er, ne2k_pci, ne2k_isa, pcnet, rtl8139, e1000, smc91c111, lance and mcf_fec. Not all devices are supported on all targets. Use -net nic,model=? for a list of available devices for your target.
-net tap[,vlan=n][,name=name][,fd=h][,ifname=name] [,script=file][,downscript=dfile]
Connect the host TAP network interface name to VLAN n, use the network script file to configure it and the network script dfile to deconfigure it. If name is not provided, the OS automatically provides one. fd=h can be used to specify the handle of an already opened host TAP interface. The default network configure script is /etc/qemu-ifup and the default network deconfigure script is /etc/qemu-ifdown. Use script=no or downscript=no to disable script execution. Example:
qemu linux.img -net nic -net tap
More complicated example (two NICs, each one connected to a TAP device)
qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
-net nic,vlan=1 -net tap,vlan=1,ifname=tap1
Here is the cmd line that i use:
Code: Select all
qemu.exe -L Bios -m 192 -vga std -display none -soundhw pcspk -localtime -M pc -net nic,vlan=0,model=rtl8139 -net tap,vlan=0,ifname=tap0 -hda harddisk.img -cdrom cdrom.iso -boot order=dc,menu=on
But the better way is to connect two instances of QEmu with your OS and start listener in one instance, then sender in second, so you'll test both receiving and transmitting in your OS.
Upd: oh, my efforts were useless..
Re: Network test under qemu
Posted: Mon Apr 16, 2012 9:36 am
by bluemoon
I found this link very helpful:
https://help.ubuntu.com/community/KVM/Networking
Basically, I'm doing this on Linux Mint 12(kubuntu):
Install the capability tool and grant to qemu:
sudo apt-get install libcap2-bin
sudo setcap cap_net_admin=ei /usr/bin/qemu-system-x86_64
sudo setcap cap_net_admin=ei /usr/bin/qemu
Stop network:
sudo invoke-rc.d networking stop
Add the bridge:
sudo nano /etc/network/interface
Code: Select all
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
Restart network:
sudo /etc/init.d/networking restart
Then, I'm able to start qemu with:
Code: Select all
sudo qemu-system-i386 -m 32 -vga std -serial stdio -netdev tap,id=br0 -device rtl8139,netdev=br0 bluemoon.img
And I see tap0 created. I have not complete my 8139 driver yet but I suppose I can send packets to/from this tap interface to the host and my physical network.
Hope this helps.
PS: To Nable, -net option is obsoleted according to qemu document, although it still exists for compatibility.
PS. Should I put this on wiki or link related wiki page to this thread? The document of qemu + tap is scatters across qemu project, kvm project and linux administration guide, each project redirect to each other and there is really no single place of a walk thought for it.
Re: Network test under qemu
Posted: Tue Apr 17, 2012 10:21 am
by fiveayem
For me, it does not work so well...
Indeed, /etc/init.d/networking restart freezes.
Re: Network test under qemu
Posted: Thu Apr 19, 2012 2:21 pm
by bluemoon
Are you using Mint or what distribution? The setup may differ.
Anyway, I finally managed to got an IRQ from the 8139
With the line:
Code: Select all
// Enable receive
outb( nic->io_addr + 0x37, 1<<3 );
I'm able to get IRQ0B randomly...So if you still need help on getting this work, we can discuss the detail setup.
Re: Network test under qemu
Posted: Sat Dec 15, 2012 10:05 am
by razer
I'm trying the same. I'm wring a network driver for NE2000 for my OS. Therefore i'm trying to setup a bridge to be able to send data (example a ping) to my OS.
Here is my call of qemu:
sudo qemu-system-i386 -m 8M -cpu qemu32 -hda sweb.cmdk -debugcon /dev/stdout -netdev tap,id=br0 -device ne2k_pci,netdev=br0
Additionally I addded to /etc/network/interfaces
Code: Select all
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
How can I now verify that the configuration is right? I setup my NIC and enabled IRQs but how can I send some data to my OS?
Re: Network test under qemu
Posted: Sat Dec 15, 2012 10:13 am
by bluemoon
try sending multicast packets.