Skip to content

Instantly share code, notes, and snippets.

@mcastelino
Last active February 23, 2024 08:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mcastelino/43cc733e53d65ef67452ecaf78e936c2 to your computer and use it in GitHub Desktop.
Save mcastelino/43cc733e53d65ef67452ecaf78e936c2 to your computer and use it in GitHub Desktop.
QEMU Tips and Tricks

How to launch QEMU from command line without libvirt with macvtap and vhost support

This sets up a host local bridge with a macvlan interface for VM to host communication. The macvtap is setup with vhost support.

The command line options to note are

-netdev tap,fd=3,id=hostnet0,vhost=on,vhostfd=4 3<>$"$tapdev" 4<>/dev/vhost-net -device virtio-net-pci,netdev=hostnet0,id=net0,mac=$(< /sys/class/net/testtap/address)

This sets up two different fd's. The first for macvtap and the second for vhost-net.

ip link del testbr
ip link del testvlan
ip link del testtap

ip link add name testbr type bridge
ip link add link testbr name testvlan type macvlan mode bridge
ip addr add 172.20.0.1/16 dev testvlan
ip link set dev testvlan up
ip link set dev testbr up
ip link add link testbr name testtap type macvtap mode bridge
ip link set testtap up

tapindex=$(< /sys/class/net/testtap/ifindex)
tapdev=/dev/tap"$tapindex"

echo "Tap Index :=" $tapindex
echo "Tap Dev :=" $tapdev
echo "Mac Address :="
cat /sys/class/net/testtap/address

../x86_64-softmmu/qemu-system-x86_64 -trace events=/tmp/events -machine pc-lite,accel=kvm,kernel_irqchip,nvdimm -cpu host -m 256,maxmem=1G,slots=2 -smp 2 -no-user-config -nodefaults -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -kernel ./vmlinux-4.9.34-63.1.container -append "reboot=k panic=1 rw tsc=reliable no_timer_check noreplace-smp root=/dev/pmem0p1  init=/usr/lib/systemd/systemd initcall_debug rootfstype=ext4 rootflags=dax,data=ordered dhcp rcupdate.rcu_expedited=1 clocksource=kvm-clock console=hvc0 single iommu=false quiet" -device virtio-serial-pci,id=virtio-serial0 -chardev pty,id=charconsole0 -device virtconsole,chardev=charconsole0,id=console0 -nographic -object memory-backend-file,id=mem0,share,mem-path=./clear-16160-containers.img,size=235929600 -device nvdimm,memdev=mem0,id=nv0 -no-reboot -netdev tap,fd=3,id=hostnet0,vhost=on,vhostfd=4 3<>$"$tapdev" 4<>/dev/vhost-net -device virtio-net-pci,netdev=hostnet0,id=net0,mac=$(< /sys/class/net/testtap/address)


How to launch QEMU with vhost-scsi-pci

This combination results in 0 emulation for storage and networking. Console still needs emulation. But that is not performance critical.

-device vhost-scsi-pci,id=vhost1,wwpn=naa.50014053386c1931,event_idx=off

Here the the SCSI LUN is setup using Linux-IO backstore and then present it to the VMs using the vhost-scsi target using targetcli and fileio

[root@godel ~]# targetcli
targetcli shell version 2.1.fb42
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> cd backstores/fileio
/backstores/fileio> create pnfs_lio_dev_1 /pnfs_lio_dev_1 100M

/backstores/fileio> cd /vhost/
/vhost> create
Created target naa.50014053386c1931.
Created TPG 1.
/vhost> cd naa.50014053386c1931/tpg1/
/vhost> cd naa.50014053386c1931/tpg1/luns
/vhost/naa.50...931/tpg1/luns> create /backstores/fileio/pnfs_lio_dev_1

cd /
/> ls
...
/>exit

http://people.redhat.com/bcodding/pnfs/nfs/scsi/2016/07/13/pnfs_scsi_testing_vhost_setup_for_VMs/

http://linux-iscsi.org/wiki/Targetcli#Create_a_target

How to launch QEMU with virtio-blk-pci

-drive file=./myblk.img,if=none,id=drive-virtio-disk0,format=raw -device virtio-blk-pci,scsi=off,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=2

How to launch QEMU with vsock

Here we choose a CID 3 for the VM

-device vhost-vsock-pci,id=vhost-vsock-pci0,guest-cid=3

How test the vsock connection

Here the CID is set to 3 and the port 0xFFFF

In the VM

socat - SOCKET-LISTEN:40:0:x0000xFFxFFx0000x03x00000000000000

On the host

socat - SOCKET-CONNECT:40:0:x0000xFFxFFx0000x03x00000000000000

Minimal console over vsock

VM: Without auth:

socat SOCKET-LISTEN:40:0:x0000xFFxFFx0000x03x00000000000000,reuseaddr,fork EXEC:bash,pty,stderr,setsid,sigint,sane 

With auth:

socat SOCKET-LISTEN:40:0:x0000xFFxFFx0000x03x00000000000000,reuseaddr,fork EXEC:login,pty,stderr,setsid,sigint,sane 

On the host

socat - SOCKET-CONNECT:40:0:x0000xFFxFFx0000x03x00000000000000

Misc

https://www.linux-kvm.org/images/f/f9/2012-forum-virtio-blk-performance-improvement.pdf https://bugzilla.redhat.com/show_bug.cgi?id=1365823

@ardje
Copy link

ardje commented Oct 21, 2019

Why don't you directly connect the macvtap to a vlan or physical interface?
The macvlan infra is meant to not have to use bridges, as they are a simplified bridge with a fixed forwarding database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment