Skip to content

arm64: enable virtio-net and add network scripts #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ make check

## Usage

### Start Emulator

```
build/kvm-host -k bzImage [-i initrd] [-d disk-image]
```
Expand All @@ -42,8 +44,63 @@ containing concatenated `bootsect.o + setup.o + misc.o + piggy.o`. `initrd` is t
initial RAM disk image, which is an optional argument.
`disk-image` is the path to disk image which can be mounted as a block device via virtio. For the reference Linux guest, ext4 filesystem is used for disk image.

### Stop Emulator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is meant to "stop" the emulation. Instead, the operation is dedicated to "exit" from the execution loop.


To exit kvm-host, press "Ctrl-A", release both keys, and then press "x".

### Enable Static Route to Test the Guest VirtIO-Net Interface

1. Start the kvm-host emulator. Once initialized, the TUN/TAP interface (for example, `tap0`) is visible in the output of `ip a`. The following is sample output from the host:

```shell
❯ ip a
1: lo: ...
2: eth0: ...
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
altname xxxxxxxxxxxxxxx
inet 192.168.x.x/24 brd 192.168.x.255 scope global dynamic noprefixroute wlan0
valid_lft xxxxxxsec preferred_lft xxxxxxsec
inet6 ...
11: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 5a:1d:bd:2d:7c:1f brd ff:ff:ff:ff:ff:ff
```

2. Execute the shell script `./scripts/set-host-bridge.sh`, which configures a bridge by assigning the default route to 10.0.0.1, integrating the TUN/TAP interface into the bridge, and activating the network interfaces. The following is sample output of `ip a` from the host:

```shell
❯ ip a
...
11: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 5a:1d:bd:2d:7c:1f brd ff:ff:ff:ff:ff:ff
12: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether d6:92:97:85:e8:7c brd ff:ff:ff:ff:ff:ff
inet 10.0.0.1/24 scope global br0
valid_lft forever preferred_lft forever
```

3. Copy the commands from `scripts/set-guest-route.sh` into the guest environment and execute them. Below is a sample `ip a` output from the guest:

```shell
~ # ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether c2:5a:92:87:97:42 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.2/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::c05a:92ff:fe87:9742/64 scope link
valid_lft forever preferred_lft forever
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
```

4. Test guest network connectivity by pinging the configured default gateway.

```bash
ping 10.0.0.1
```

## License

`kvm-host` is released under the BSD 2 clause license. Use of this source code is governed by
Expand All @@ -52,7 +109,7 @@ a BSD-style license that can be found in the LICENSE file.
## References
* [kvmtool](https://github.com/kvmtool/kvmtool)
* [KVM (Kernel-based Virtual Machine) API](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt)
* [The Linux/x86 Boot Protocol](https://www.kernel.org/doc/html/latest/x86/boot.html)
* [The Linux/x86 Boot Protocol](https://www.kernel.org/doc/html/latest/arch/x86/boot.html)
* [Using the KVM API](https://lwn.net/Articles/658511/)
* [gokvm](https://github.com/bobuhiro11/gokvm)
* [KVM Host in a few lines of code](https://zserge.com/posts/kvm/)
Expand Down
14 changes: 13 additions & 1 deletion configs/linux-arm64.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CONFIG_CC_OPTIMIZE_FOR_SIZE=y
# CONFIG_MULTIUSER is not set
# CONFIG_SYSFS_SYSCALL is not set
# CONFIG_FHANDLE is not set
# CONFIG_POSIX_TIMERS is not set
CONFIG_POSIX_TIMERS=y
# CONFIG_BUG is not set
# CONFIG_BASE_FULL is not set
# CONFIG_FUTEX is not set
Expand Down Expand Up @@ -66,3 +66,15 @@ CONFIG_EXT4_FS=y
# CONFIG_DEBUG_MISC is not set
# CONFIG_FTRACE is not set
# CONFIG_STRICT_DEVMEM is not set
CONFIG_NET=y
CONFIG_INET=y
CONFIG_IP_MULTICAST=y
CONFIG_IP_PNP=y
CONFIG_IP_PNP_DHCP=y
CONFIG_NETDEVICES=y
CONFIG_VIRTIO=y
CONFIG_VIRTIO_NET=y
CONFIG_VIRTIO_RING=y
CONFIG_UNIX=y
CONFIG_UNIX_SCM=y
CONFIG_AF_UNIX_OOB=y
Comment on lines +78 to +80
Copy link
Contributor

@jserv jserv Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm if they are needed or not.

3 changes: 3 additions & 0 deletions scripts/set-guest-route.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ip addr add 10.0.0.2/24 dev eth0
ip link set eth0 up
ip route add default via 10.0.0.1
9 changes: 9 additions & 0 deletions scripts/set-host-bridge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

sudo ip link delete br0 || true
sudo brctl addbr br0
sudo ip addr add 10.0.0.1/24 dev br0
sudo ip route add default via 10.0.0.1 dev br0
sudo ip link set br0 up
sudo ip link set tap0 master br0
sudo ip link set tap0 up
1 change: 1 addition & 0 deletions src/arch/arm64/desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#define RAM_BASE (1UL << 31)
#define SERIAL_IRQ 0
#define VIRTIO_BLK_IRQ 1
#define VIRTIO_NET_IRQ 2
#define KERNEL_OPTS "console=ttyS0"
35 changes: 25 additions & 10 deletions src/arch/arm64/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,12 @@ static int generate_fdt(vm_t *v)
__FDT(property, "ranges", &pci_ranges, sizeof(pci_ranges));
/* interrupt-map contains the interrupt mapping between the PCI device and
* the IRQ number of interrupt controller.
* virtio-blk is the only PCI device.
* interrupt-map: map each PCI device’s INTA#: GIC SPI
*/
struct virtio_blk_dev *virtio_blk = &v->virtio_blk_dev;
struct pci_dev *virtio_blk_pci = (struct pci_dev *) virtio_blk;
struct virtio_net_dev *virtio_net = &v->virtio_net_dev;
struct pci_dev *virtio_net_pci = (struct pci_dev *) virtio_net;
struct {
uint32_t pci_hi;
uint64_t pci_addr;
Expand All @@ -390,15 +392,28 @@ static int generate_fdt(vm_t *v)
uint32_t gic_type;
uint32_t gic_irqn;
uint32_t gic_irq_type;
} __attribute__((packed)) pci_irq_map[] = {{
cpu_to_fdt32(virtio_blk_pci->config_dev.base & ~(1UL << 31)),
0,
cpu_to_fdt32(1),
cpu_to_fdt32(FDT_PHANDLE_GIC),
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
cpu_to_fdt32(VIRTIO_BLK_IRQ),
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
}};
} __attribute__((packed)) pci_irq_map[] = {
/* virtio-blk: SPI VIRTIO_BLK_IRQ */
{
cpu_to_fdt32(virtio_blk_pci->config_dev.base & ~(1UL << 31)),
0,
cpu_to_fdt32(1),
cpu_to_fdt32(FDT_PHANDLE_GIC),
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
cpu_to_fdt32(VIRTIO_BLK_IRQ),
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
},
/* virtio-net: SPI VIRTIO_NET_IRQ */
{
cpu_to_fdt32(virtio_net_pci->config_dev.base & ~(1UL << 31)),
0,
cpu_to_fdt32(1),
cpu_to_fdt32(FDT_PHANDLE_GIC),
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
cpu_to_fdt32(VIRTIO_NET_IRQ),
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
},
};
__FDT(property, "interrupt-map", &pci_irq_map, sizeof(pci_irq_map));
__FDT(end_node); /* End of /pci node */

Expand Down