Skip to content

Instantly share code, notes, and snippets.

@networkop
Last active February 20, 2021 10:17
Show Gist options
  • Save networkop/4e04ef70b8c5f96d20cdf73ea32900d1 to your computer and use it in GitHub Desktop.
Save networkop/4e04ef70b8c5f96d20cdf73ea32900d1 to your computer and use it in GitHub Desktop.
qemu-macvtap
## Prep work
curl -O http://download.cirros-cloud.net/0.5.1/cirros-0.5.1-x86_64-disk.img
cat << EOF > Dockerfile
FROM centos:7
RUN yum -y install epel-release && \
yum makecache fast && \
yum install -y qemu-kvm bridge-utils iproute libvirt libvirt-client genisoimage virt-install telnet tcpdump && \
yum clean all
COPY cirros-0.5.1-x86_64-disk.img .
COPY entrypoint.sh /
ENTRYPOINT /entrypoint.sh
EOF
cat << EOF > entrypoint.sh
#!/bin/bash
trap : TERM INT; sleep infinity & wait
EOF
## Image build
docker build -t macvtap .
docker rm -f test
docker run -d -v /dev:/dev --name test --privileged macvtap
docker exec -it test bash
## Inside the container
ip link show dev eth0
ip link add link eth0 name macvtap0 type macvtap mode bridge
ip link set dev macvtap0 up
/usr/libexec/qemu-kvm -daemonize \
-name test \
-no-user-config -nodefaults \
-machine pc \
-enable-kvm \
-cpu host \
-display none \
-serial telnet:0.0.0.0:23,server,nowait \
-boot order=d,menu=on \
-drive file=/cirros-0.5.1-x86_64-disk.img,format=qcow2,if=ide \
-netdev tap,fd=88,id=hostnet0,vhost=on,vhostfd=99 88<>/dev/tap$(cat /sys/class/net/macvtap0/ifindex) 99<>/dev/vhost-net \
-device virtio-net-pci,netdev=hostnet0,id=net0,mac=$(cat /sys/class/net/macvtap0/address)
@networkop
Copy link
Author

hm... have you tried a different macvtap mode?
The default "bridge" mode may use the standard Linux bridge under the hood.

@hellt
Copy link

hellt commented Feb 12, 2021 via email

@networkop
Copy link
Author

@hellt have you seen this? "Using tc redirect to connect a virtual machine to a container network · GitHub" https://gist.github.com/mcastelino/7d85f4164ffdaf48242f9281bb1d0f9b

@hellt
Copy link

hellt commented Feb 20, 2021 via email

@networkop
Copy link
Author

no, haven't tried it, just stumbled across it by accident, thought you'd find it interesting

@hellt
Copy link

hellt commented Feb 20, 2021 via email

@networkop
Copy link
Author

that account's got a lot of interesting gists, like this one https://gist.github.com/mcastelino/fb841c7e7d26b08240e0a19d3b95c0a7 showing the use of trace-cmd

@networkop
Copy link
Author

I noticed 'ip' in the filter rule,

where?

@networkop
Copy link
Author

it seems like it was build for packet mirroring, link, which most likely means it'll redirect all packets

@hellt
Copy link

hellt commented Feb 20, 2021

where?

mislooked. I was reading the original post where he saw this technique for the first time, and it was using ip protocol match.
but indeed in his pasting he uses all which should be all frames. Will test it.

Thanks, that might be a groundbreaking approach if it works

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