Networking with QEMU and KVM speedup


Networking in qemu

I've recently had a need to test UEFI booting for a disk image.  I stumbled upon OVMF, a bios that will handle UEFI booting.  It works pretty well but doesn't remember its config sometimes.  But that's not why I'm writing this.

In my disk image I need dual network interfaces that are on specific networks.  I don't need tap interfaces.  I can live with the slower user-mode interfaces.  But I needed to put them on separate networks.  So I did this.

-netdev user,id=user0,dhcpstart=192.168.20.20,hostfwd=tcp::5555-:22 -device e1000,netdev=user0
-netdev user,id=user1,dhcpstart=192.168.50.50,hostfwd=tcp::5556-:22 -device e1000,netdev=user1

I googled till I was blue to figure out how to do this.  You can set the dhcpstart address but qemu_system_x86-64 spews the following message:

device 'user' could not be initialized

It doesn't say why, but after fiddling with it for a long time it became obvious that this was a configuration error.  There just doesn't seem to be any explanation as to what configuration is wrong.

Finally I found a site with an example.  The key appears to be that you have to include the host's network address on the network that interface will be in.  Without this specification there is no mapping of the guest interface to a host interface.  Here is what the correct configuration should look like.

-netdev user,id=user0,net=192.168.20.0/24,dhcpstart=192.168.20.20,hostfwd=tcp::5555-:22 -device e1000,netdev=user0
-netdev user,id=user1,net=192.168.50.0/24,dhcpstart=192.168.50.50,hostfwd=tcp::5556-:22 -device e1000,netdev=user1

Now both interfaces will come up on the assigned networks.  The hostfwd option is used to map host ports to guest ports, with the larger number (the first one) mapping to the guest's port.  Here I'm mapping ports to the ssh port on the guest.  Since I'm using user-mode networking this is required otherwise the default firewall in the guest prevents me from accessing it from the host.

kvm Speedup

Another thing I discovered when running qemu_system_x86_64 is that it was slow on my fedora 21 box.  The reason is simple:  you can't run this command manually with KVM speedups (which are significant) if you're already running libvirtd.  You can go through the hoops of tying the two together, but for a quick solution to run a qemu session with full virtualization to get much better performance without dealing with libvirtd just shutoff libvirtd.

sudo service libvirtd stop

That's using archaic service format instead of the ugly sysctl interface (that you very little, systemd) but it works.  Again, this is for Fedora.  Your distro mileage will vary.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.