1. Preface
This paper mainly introduces the steps of setting up the environment for communication with the host computer network under qemu. The host is Ubuntu 18 04, the target machine is qemu 5.0, running kernel 5.10. This paper mainly integrates the contents of several articles in the reference documents.
The main steps include:
- Configure host
- Configure qemu kernel to support network card
- Configure qemu to create front and back ends
- Configure the IP of qemu
2. Configure host
We use the bridge method to connect the host with the network card of qemu.
First, we create a bridge on the host side in the following way:
#!/bin/sh #sudo ifconfig eth0 down # First, close the host network card interface sudo brctl addbr br0 # Add a bridge named br0 sudo brctl addif br0 eth0 # Add an interface in br0 sudo brctl stp br0 off # If there is only one bridge, turn off the spanning tree protocol sudo brctl setfd br0 1 # Set forwarding delay for br0 sudo brctl sethello br0 1 # Set the hello time for br0 sudo ifconfig br0 0.0.0.0 promisc up # Enable br0 interface sudo ifconfig eth0 0.0.0.0 promisc up # Enable network card interface sudo dhclient br0 # Get br0's IP address from dhcp server sudo brctl show br0 # View the list of virtual bridges sudo brctl showstp br0 # View the interface information of br0
At this time, the bridge has obtained the IP, and the network card eth0 that the host can connect to the network has also joined the bridge
Run the following script to create a TAP device as the interface of QEMU:
#!/bin/sh sudo tunctl -t tap0 -u root # Create a tap0 interface that only allows root access sudo brctl addif br0 tap0 # Add a tap0 interface in the virtual bridge sudo ifconfig tap0 0.0.0.0 promisc up # Enable tap0 interface sudo brctl showstp br0
At this time, the network card at one end of the host and the network card at one end of the qemu are connected to the network bridge.
Here is a brief description. qemu mainly supports four network communication modes, among which we use TAP/bridge communication mode. TAP is a virtual network device supported by LInux kernel. It is completely realized by software simulation. TAP works at the data link layer and is commonly used for network bridging.
After the above, the effects on the host are as follows:
ubuntu@VM-0-9-ubuntu:~$ ifconfig br0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500 inet 172.17.0.9 netmask 255.255.240.0 broadcast 172.17.15.255 inet6 fe80::5054:ff:fec6:1dac prefixlen 64 scopeid 0x20<link> ether 52:54:00:c6:1d:ac txqueuelen 1000 (Ethernet) RX packets 97163 bytes 8327257 (8.3 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 100929 bytes 17786837 (17.7 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500 inet6 fe80::5054:ff:fec6:1dac prefixlen 64 scopeid 0x20<link> ether 52:54:00:c6:1d:ac txqueuelen 1000 (Ethernet) RX packets 102458 bytes 13522649 (13.5 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 103875 bytes 18965827 (18.9 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 3871 bytes 328826 (328.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3871 bytes 328826 (328.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::84d9:66ff:fe1b:ddb6 prefixlen 64 scopeid 0x20<link> ether 86:d9:66:1b:dd:b6 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 13 bytes 966 (966.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3. Configure the target kernel to support the network card
You can view the network cards supported by qemu through the following command:
ubuntu@VM-0-9-ubuntu$ qemu-system-aarch64 -M virt -nic model=help Supported NIC models: e1000 e1000-82544gc e1000-82545em e1000e i82550 i82551 i82557a i82557b i82557c i82558a i82558b i82559a i82559b i82559c i82559er i82562 i82801 ne2k_pci pcnet rocker rtl8139 tulip virtio-net-pci virtio-net-pci-non-transitional virtio-net-pci-transitional vmxnet3
qemu supports e1000 network card by default. The following configuration items need to be enabled in the kernel to support e1000:
Device Drivers ---> [*] Network device support ---> [*] Ethernet driver support ---> [*] Intel devices <*> Intel(R) PRO/1000 Gigabit Ethernet support
This turns on CONFIG_E1000=y configuration item
4. Create front end and back end for qemu
The network interface of QEMU is divided into the following two parts:
- The simulated hardware seen by guest is also called NIC. Common are e1000 network card, rt8139 network card, and virtio net device. These are collectively referred to as network front ends.
- Back end of network card on host. QEMU is used to exchange data with the external network on the host. The most common backend is "user", which is used to provide NAT host network access. The tap backend allows guest s to directly access the host's network. There is also a socket type backend, which is used to connect multiple QEMU instances to simulate a shared network.
reference resources New - nic option for QEMU In this article, the - nic option configures the front-end and back-end of the qemu network in a very simple way. You only need to add the following to qemu's startup parameters:
-nic tap,model=e1000
My qemu parameters are configured as follows:
#!/bin/bash IMAGE_PATH=~/qemu/kernel/linux/arch/arm64/boot ROOTFS_PATH=~/qemu/rootfs/linux_rootfs ROOTFS=rootfs.ext4 ./qemu \ -smp 2 \ -M virt \ -m 1024 \ -cpu cortex-a57 \ -kernel $IMAGE_PATH/Image \ -append 'root=/dev/vda rw console=ttyAMA0 cma=48M' \ -drive if=none,file=$ROOTFS_PATH/$ROOTFS,id=hd0 \ -device virtio-blk-device,drive=hd0 \ -nographic \ -nic tap,model=e1000 \ $1 $2
5. Configure the IP address of qemu
Add the following contents to the startup script rcS:
ifconfig eth0 172.17.0.2 netmask 255.255.240.0
The effects are as follows:
/ # ifconfig -a eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:56 inet addr:172.17.0.2 Bcast:172.17.15.255 Mask:255.255.240.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:60 (60.0 B) TX bytes:0 (0.0 B) lo Link encap:Local Loopback LOOPBACK MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
6. Experiment
ping the host in qemu:
/ # ping 172.17.0.9 PING 172.17.0.9 (172.17.0.9): 56 data bytes 64 bytes from 172.17.0.9: seq=0 ttl=64 time=16.348 ms 64 bytes from 172.17.0.9: seq=1 ttl=64 time=1.905 ms 64 bytes from 172.17.0.9: seq=2 ttl=64 time=1.090 ms 64 bytes from 172.17.0.9: seq=3 ttl=64 time=0.980 ms 64 bytes from 172.17.0.9: seq=4 ttl=64 time=0.978 ms
Unfortunately ping www.baidu.com Com No:<