Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save digitalsignalperson/ab68794576b4475f3b26166aa5ae266c to your computer and use it in GitHub Desktop.
Save digitalsignalperson/ab68794576b4475f3b26166aa5ae266c to your computer and use it in GitHub Desktop.
QEMU usermode virtual vlan using -netdev socket

Goal

How to launch multiple QEMU based VM's that can communicate with the outside world and between each other other without any setup on the host.

This uses two features available in qemu

  • User Mode Networking stack - SLIRP
  • Socket networking backend allows you to create a network of guests that can see each other

This allows us to have

  • SLIRP to communicate to the outside world
  • multicast to communicate between the VMs
  • And it uses the multicast to allow the host to communicate with the guests

Virtual Machine launch

VMN=1
qemu-system-x86_64 \
    -machine pc,accel=kvm,kernel_irqchip \
    -enable-kvm \
    -bios OVMF.fd \
    -smp sockets=1,cpus=4,cores=2 -cpu host \
    -m 1024 \
    -vga none -nographic \
    -drive file="$IMAGE",if=virtio,aio=threads,format=raw \
    -netdev user,id=mynet0,hostfwd=tcp::${VMN}0022-:22,hostfwd=tcp::${VMN}2375-:2375 \
    -device virtio-net-pci,netdev=mynet0 \
    -netdev socket,id=vlan,mcast=230.0.0.1:1234 \
    -device virtio-net-pci,netdev=vlan \

You can add any number of VM's to this socket network, choosing a different value for VMN

Connecting to this network from the host

You can use socat to create a tap interface on the host with an IP in the same subnet as you want to VLAN to be on and

sudo socat UDP4-DATAGRAM:230.0.0.1:1234,sourceport=1234,reuseaddr,ip-add-membership=230.0.0.1:127.0.0.1 TUN:10.0.3.1/24,tun-type=tap,iff-no-pi,iff-up,tun-name=vmvlan0

This will create a tap interface vmvlan0 on the host with IP: 10.0.3.1

We can run a DHCP server on this tap interface to allow for seamless configuration of the VLAN driven by the host

DHCP based VLAN configuration

To assign IP addresss to these VM's from the host you can use dnsmasq serving DHCP on the vmvlan0 interface.

sudo -E dnsmasq --conf-file=dnsmasq.conf --leasefile-ro

dnsmasq.conf

strict-order
pid-file=default.pid
except-interface=lo
bind-dynamic
interface=vmvlan0
dhcp-range=10.0.3.100,10.0.3.200
dhcp-no-override
dhcp-authoritative
dhcp-lease-max=253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment