Skip to content

Commit 608ba28

Browse files
committed
arm64: enable virtio-net and add network scripts
Enable POSIX timers to support ping’s POSIX timer syscalls: - CONFIG_POSIX_TIMERS=y in configs/linux-arm64.config Enable network stack and virtio-net in kernel config: - CONFIG_NET, CONFIG_INET, CONFIG_IP_MULTICAST, CONFIG_IP_PNP - CONFIG_IP_PNP_DHCP, CONFIG_NETDEVICES, CONFIG_VIRTIO - CONFIG_VIRTIO_NET, CONFIG_VIRTIO_RING, CONFIG_UNIX - CONFIG_UNIX_SCM, CONFIG_AF_UNIX_OOB Add virtio_net_dev to FDT generation: - src/arch/arm64/vm.c: include virtio_net_dev in interrupt-map Define VIRTIO_NET_IRQ=2 (SPI 34) in src/arch/arm64/desc.h Add scripts for host and guest network setup: - scripts/set-host-bridge.sh to configure TAP bridge and default route - scripts/set-guest-route.sh to configure guest static IP and route Update README.md with new scripts and build instructions
1 parent 93f1fee commit 608ba28

File tree

6 files changed

+108
-11
lines changed

6 files changed

+108
-11
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ make check
3333

3434
## Usage
3535

36+
### Start Emulater
37+
3638
```
3739
build/kvm-host -k bzImage [-i initrd] [-d disk-image]
3840
```
@@ -42,8 +44,63 @@ containing concatenated `bootsect.o + setup.o + misc.o + piggy.o`. `initrd` is t
4244
initial RAM disk image, which is an optional argument.
4345
`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.
4446

47+
### Stop Emulator
48+
4549
To exit kvm-host, press "Ctrl-A", release both keys, and then press "x".
4650

51+
### Enable Static Route to Test the Guest VirtIO-Net Interface
52+
53+
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:
54+
55+
```shell
56+
❯ ip a
57+
1: lo: ...
58+
2: eth0: ...
59+
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
60+
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
61+
altname xxxxxxxxxxxxxxx
62+
inet 192.168.x.x/24 brd 192.168.x.255 scope global dynamic noprefixroute wlan0
63+
valid_lft xxxxxxsec preferred_lft xxxxxxsec
64+
inet6 ...
65+
11: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
66+
link/ether 5a:1d:bd:2d:7c:1f brd ff:ff:ff:ff:ff:ff
67+
```
68+
69+
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:
70+
71+
```shell
72+
❯ ip a
73+
...
74+
11: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
75+
link/ether 5a:1d:bd:2d:7c:1f brd ff:ff:ff:ff:ff:ff
76+
12: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
77+
link/ether d6:92:97:85:e8:7c brd ff:ff:ff:ff:ff:ff
78+
inet 10.0.0.1/24 scope global br0
79+
valid_lft forever preferred_lft forever
80+
```
81+
82+
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:
83+
84+
```shell
85+
~ # ip a
86+
1: lo: <LOOPBACK> mtu 65536 qdisc noop qlen 1000
87+
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
88+
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
89+
link/ether c2:5a:92:87:97:42 brd ff:ff:ff:ff:ff:ff
90+
inet 10.0.0.2/24 scope global eth0
91+
valid_lft forever preferred_lft forever
92+
inet6 fe80::c05a:92ff:fe87:9742/64 scope link
93+
valid_lft forever preferred_lft forever
94+
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000
95+
link/sit 0.0.0.0 brd 0.0.0.0
96+
```
97+
98+
4. Test guest network connectivity by pinging the configured default gateway.
99+
100+
```bash
101+
ping 10.0.0.1
102+
```
103+
47104
## License
48105

49106
`kvm-host` is released under the BSD 2 clause license. Use of this source code is governed by

configs/linux-arm64.config

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CONFIG_CC_OPTIMIZE_FOR_SIZE=y
1212
# CONFIG_MULTIUSER is not set
1313
# CONFIG_SYSFS_SYSCALL is not set
1414
# CONFIG_FHANDLE is not set
15-
# CONFIG_POSIX_TIMERS is not set
15+
CONFIG_POSIX_TIMERS=y
1616
# CONFIG_BUG is not set
1717
# CONFIG_BASE_FULL is not set
1818
# CONFIG_FUTEX is not set
@@ -66,3 +66,15 @@ CONFIG_EXT4_FS=y
6666
# CONFIG_DEBUG_MISC is not set
6767
# CONFIG_FTRACE is not set
6868
# CONFIG_STRICT_DEVMEM is not set
69+
CONFIG_NET=y
70+
CONFIG_INET=y
71+
CONFIG_IP_MULTICAST=y
72+
CONFIG_IP_PNP=y
73+
CONFIG_IP_PNP_DHCP=y
74+
CONFIG_NETDEVICES=y
75+
CONFIG_VIRTIO=y
76+
CONFIG_VIRTIO_NET=y
77+
CONFIG_VIRTIO_RING=y
78+
CONFIG_UNIX=y
79+
CONFIG_UNIX_SCM=y
80+
CONFIG_AF_UNIX_OOB=y

scripts/set-guest-route.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ip addr add 10.0.0.2/24 dev eth0
2+
ip link set eth0 up
3+
ip route add default via 10.0.0.1

scripts/set-host-bridge.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
sudo ip link delete br0 || true
4+
sudo brctl addbr br0
5+
sudo ip addr add 10.0.0.1/24 dev br0
6+
sudo ip route add default via 10.0.0.1 dev br0
7+
sudo ip link set br0 up
8+
sudo ip link set tap0 master br0
9+
sudo ip link set tap0 up

src/arch/arm64/desc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#define RAM_BASE (1UL << 31)
44
#define SERIAL_IRQ 0
55
#define VIRTIO_BLK_IRQ 1
6+
#define VIRTIO_NET_IRQ 2
67
#define KERNEL_OPTS "console=ttyS0"

src/arch/arm64/vm.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,12 @@ static int generate_fdt(vm_t *v)
378378
__FDT(property, "ranges", &pci_ranges, sizeof(pci_ranges));
379379
/* interrupt-map contains the interrupt mapping between the PCI device and
380380
* the IRQ number of interrupt controller.
381-
* virtio-blk is the only PCI device.
381+
* interrupt-map: map each PCI device’s INTA#: GIC SPI
382382
*/
383383
struct virtio_blk_dev *virtio_blk = &v->virtio_blk_dev;
384384
struct pci_dev *virtio_blk_pci = (struct pci_dev *) virtio_blk;
385+
struct virtio_net_dev *virtio_net = &v->virtio_net_dev;
386+
struct pci_dev *virtio_net_pci = (struct pci_dev *) virtio_net;
385387
struct {
386388
uint32_t pci_hi;
387389
uint64_t pci_addr;
@@ -390,15 +392,28 @@ static int generate_fdt(vm_t *v)
390392
uint32_t gic_type;
391393
uint32_t gic_irqn;
392394
uint32_t gic_irq_type;
393-
} __attribute__((packed)) pci_irq_map[] = {{
394-
cpu_to_fdt32(virtio_blk_pci->config_dev.base & ~(1UL << 31)),
395-
0,
396-
cpu_to_fdt32(1),
397-
cpu_to_fdt32(FDT_PHANDLE_GIC),
398-
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
399-
cpu_to_fdt32(VIRTIO_BLK_IRQ),
400-
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
401-
}};
395+
} __attribute__((packed)) pci_irq_map[] = {
396+
/* virtio-blk: SPI VIRTIO_BLK_IRQ */
397+
{
398+
cpu_to_fdt32(virtio_blk_pci->config_dev.base & ~(1UL << 31)),
399+
0,
400+
cpu_to_fdt32(1),
401+
cpu_to_fdt32(FDT_PHANDLE_GIC),
402+
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
403+
cpu_to_fdt32(VIRTIO_BLK_IRQ),
404+
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
405+
},
406+
/* virtio-net: SPI VIRTIO_NET_IRQ */
407+
{
408+
cpu_to_fdt32(virtio_net_pci->config_dev.base & ~(1UL << 31)),
409+
0,
410+
cpu_to_fdt32(1),
411+
cpu_to_fdt32(FDT_PHANDLE_GIC),
412+
cpu_to_fdt32(ARM_FDT_IRQ_TYPE_SPI),
413+
cpu_to_fdt32(VIRTIO_NET_IRQ),
414+
cpu_to_fdt32(ARM_FDT_IRQ_EDGE_TRIGGER),
415+
},
416+
};
402417
__FDT(property, "interrupt-map", &pci_irq_map, sizeof(pci_irq_map));
403418
__FDT(end_node); /* End of /pci node */
404419

0 commit comments

Comments
 (0)