Skip to content

Commit 3b6bb9a

Browse files
committed
feat: add VirtIO demo
Signed-off-by: João Peixoto <joaopeixotooficial@gmail.com>
1 parent cbe4a41 commit 3b6bb9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3492
-51
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ systems and targeting several supported platforms. The available demos are:
88
* [Dual-guest Linux+FreeRTOS](demos/linux+freertos/README.md)
99
* [Dual-Guest Linux+Zephyr](demos/linux+zephyr/README.md)
1010
* [Dual-Guest Zephyr+Baremetal](demos/zephyr+baremetal/README.md)
11-
11+
* [VirtIO Demo](demos/virtio/README.md)
1212

1313
---
1414

@@ -262,23 +262,25 @@ Build the firmware and deploy the system according to the target platform:
262262
| Linux+FreeRTOS | linux+freertos |
263263
| Linux+Zephyr | linux+zephyr |
264264
| Zephyr+Baremetal | zephyr+baremetal |
265+
| VirtIO | virtio |
265266

266267
## Appendix II
267268

268-
| | baremetal | linux+freertos | linux+zephyr | zephyr+baremetal |
269-
| ----------------- | --------- | -------------- | ------------ | ---------------- |
270-
| zcu102 | x | x | | |
271-
| zcu104 | x | x | | |
272-
| imx8qm | x | x | | |
273-
| tx2 | x | x | | |
274-
| rpi4 | x | x | x | |
275-
| qemu-aarch64-virt | x | x | x | |
276-
| fvp-a-aarch64 | x | x | x | x |
277-
| fvp-a-aarch32 | x | x | x | x |
278-
| fvp-r-aarch64 | x | x | x | x |
279-
| fvp-r-aarch32 | x | | | x |
280-
| qemu-riscv64-virt | x | x | | |
281-
| qemu-riscv32-virt | x | x | | |
269+
| | baremetal | linux+freertos | linux+zephyr | zephyr+baremetal | virtio |
270+
| ----------------- | --------- | -------------- | ------------ | ---------------- | ---------------- |
271+
| zcu102 | x | x | | | x |
272+
| zcu104 | x | x | | | x |
273+
| imx8qm | x | x | | | |
274+
| tx2 | x | x | | | |
275+
| rpi4 | x | x | x | | x |
276+
| qemu-aarch64-virt | x | x | x | | x |
277+
278+
| fvp-a-aarch64 | x | x | x | x | |
279+
| fvp-a-aarch32 | x | x | x | x | |
280+
| fvp-r-aarch64 | x | x | x | x | |
281+
| fvp-r-aarch32 | x | | | x | |
282+
| qemu-riscv64-virt | x | x | | | x |
283+
| qemu-riscv32-virt | x | x | | | |
282284

283285

284286
---

demos/virtio/README.md

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# VirtIO Demo
2+
3+
This demo showcases a four-guest configuration, comprising three Linux VMs and one FreeRTOS VM.
4+
One Linux guest operates as the VirtIO backend, while the remaining three guests act as VirtIO frontends.
5+
In this setup:
6+
- Two Linux VirtIO frontend guests share access to the same network and storage interfaces.
7+
- The FreeRTOS VirtIO frontend guest is equipped with a VirtIO console.
8+
9+
Access details:
10+
- The backend VM can be accessed via SSH at the static IP address **192.168.42.15**.
11+
- Once the VirtIO backend device is initialized in the backend VM, the Linux frontend VMs
12+
will become accessible via SSH at static IP addresses **192.168.42.16** and **192.168.42.17**, respectively.
13+
14+
To build FreeRTOS, set:
15+
16+
```
17+
export FREERTOS_PARAMS="STD_ADDR_SPACE=y"
18+
```
19+
20+
And follow the instructions in [FreeRTOS](../../guests/freertos/README.md).
21+
To build linux follow [Linux](../../guests/linux/README.md).
22+
23+
Additionally, you must build the I/O dispatcher system and the VirtIO device model.
24+
25+
## 1) Build the I/O dispatcher system
26+
27+
Setup environment variables:
28+
```
29+
export BAO_DEMOS_LINUX_DRIVERS=$BAO_DEMOS_WRKDIR_SRC/bao-linux-drivers
30+
export BAO_DEMOS_LINUX_DRIVERS_REPO=https://github.com/bao-project/bao-linux-drivers.git
31+
export BAO_DEMOS_LINUX_DRIVERS_BRANCH=linux-$BAO_DEMOS_LINUX_VERSION
32+
export BAO_DEMOS_BUILDROOT_LINUX_BUILD=$(BAO_DEMOS_BUILDROOT)/output/build/linux-custom
33+
export BAO_DEMOS_BUILDROOT_TOOLCHAIN_DIR=$(BAO_DEMOS_BUILDROOT)/output/host/bin
34+
```
35+
36+
Clone Bao's Linux drivers repo into your working directory:
37+
```
38+
git clone --depth 1 --branch $BAO_DEMOS_LINUX_DRIVERS_BRANCH $BAO_DEMOS_LINUX_DRIVERS_REPO $BAO_DEMOS_LINUX_DRIVERS
39+
```
40+
41+
To compile the I/O dispatcher kernel module, set up the target architecture and cross-compilers based on your target platform.
42+
Use the appropriate configuration below:
43+
44+
For aarch64:
45+
```
46+
export BAO_DEMOS_LINUX_DRIVERS_ARCH=arm64
47+
export BAO_DEMOS_LINUX_DRIVERS_CC=$BAO_DEMOS_BUILDROOT_TOOLCHAIN_DIR/aarch64-buildroot-linux-gnu-
48+
```
49+
50+
For riscv64:
51+
```
52+
export BAO_DEMOS_LINUX_DRIVERS_ARCH=riscv
53+
export BAO_DEMOS_LINUX_DRIVERS_CC=$BAO_DEMOS_BUILDROOT_TOOLCHAIN_DIR/riscv64-buildroot-linux-gnu-
54+
```
55+
56+
For arm:
57+
```
58+
export BAO_DEMOS_LINUX_DRIVERS_ARCH=arm32
59+
export BAO_DEMOS_LINUX_DRIVERS_CC=$BAO_DEMOS_BUILDROOT_TOOLCHAIN_DIR/arm-buildroot-linux-gnueabi-
60+
```
61+
62+
Run the following command to compile the I/O dispatcher kernel module:
63+
```
64+
make -C $BAO_DEMOS_BUILDROOT_LINUX_BUILD \
65+
M=$BAO_DEMOS_LINUX_DRIVERS/iodispatcher \
66+
CROSS_COMPILE=$BAO_DEMOS_LINUX_DRIVERS_CC \
67+
ARCH=$BAO_DEMOS_LINUX_DRIVERS_ARCH \
68+
modules
69+
```
70+
71+
After successful compilation, copy the `iodispatcher.ko` file to the images directory:
72+
```
73+
cp $BAO_DEMOS_LINUX_DRIVERS/iodispatcher/iodispatcher.ko $BAO_DEMOS_WRKDIR_IMGS
74+
```
75+
76+
## 2) Build the Bao VirtIO device model
77+
78+
Setup environment variables:
79+
```
80+
export BAO_DEMOS_BAO_VIRTIO_DM_URL=https://github.com/bao-project/bao-virtio-dm.git
81+
export BAO_DEMOS_BAO_VIRTIO_DM_DIR=$BAO_DEMOS_WRKDIR/bao-virtio-dm
82+
```
83+
84+
If your target is aarch64:
85+
```
86+
export BAO_DEMOS_BAO_VIRTIO_DM_TARGET=aarch64-unknown-linux-gnu
87+
```
88+
89+
If your target is riscv64:
90+
```
91+
export BAO_DEMOS_BAO_VIRTIO_DM_TARGET=riscv64gc-unknown-linux-gnu
92+
```
93+
94+
If your target is arm:
95+
```
96+
export BAO_DEMOS_BAO_VIRTIO_DM_TARGET=arm-unknown-linux-gnueabi
97+
```
98+
99+
Export a variable pointing to the final binary:
100+
```
101+
export BAO_DEMOS_BAO_VIRTIO_DM_BIN=$BAO_DEMOS_BAO_VIRTIO_DM_DIR/target/$BAO_DEMOS_BAO_VIRTIO_DM_TARGET/release
102+
```
103+
104+
Clone Bao's VirtIO device model repo:
105+
```
106+
git clone --depth 1 --branch main $BAO_DEMOS_BAO_VIRTIO_DM_URL $BAO_DEMOS_BAO_VIRTIO_DM_DIR
107+
```
108+
109+
Build the device model:
110+
```
111+
cd $BAO_DEMOS_BAO_VIRTIO_DM_DIR && cargo build --target=$BAO_DEMOS_BAO_VIRTIO_DM_TARGET --release --locked
112+
```
113+
114+
After successful compilation, copy the `bao-virtio-dm` binary to the images directory:
115+
```
116+
cp $BAO_DEMOS_BAO_VIRTIO_DM_BIN/bao-virtio-dm $BAO_DEMOS_WRKDIR_IMGS
117+
```
118+
119+
<!--- instruction#1 -->
120+
## 3) Setup the storage images for the two frontend Linux guests
121+
122+
Setup environment variables:
123+
```
124+
export BAO_DEMOS_FRONTEND1_IMG=$BAO_DEMOS_WRKDIR_IMGS/ext4_frontend_vm1_image.img
125+
export BAO_DEMOS_FRONTEND2_IMG=$BAO_DEMOS_WRKDIR_IMGS/ext4_frontend_vm2_image.img
126+
export BAO_DEMOS_FRONTEND1_MP=$BAO_DEMOS_WRKDIR_SRC/ext4_frontend_vm1
127+
export BAO_DEMOS_FRONTEND2_MP=$BAO_DEMOS_WRKDIR_SRC/ext4_frontend_vm2
128+
```
129+
130+
Begin by creating two mount points on your host system:
131+
```
132+
mkdir -p $BAO_DEMOS_FRONTEND1_MP
133+
mkdir -p $BAO_DEMOS_FRONTEND2_MP
134+
```
135+
136+
Create two empty disk images, one for each Linux guest VM:
137+
```
138+
dd if=/dev/zero of=$BAO_DEMOS_FRONTEND1_IMG bs=1M count=1
139+
dd if=/dev/zero of=$BAO_DEMOS_FRONTEND2_IMG bs=1M count=1
140+
```
141+
142+
**Note**: The example above creates disk images with a size of 1MB.
143+
Adjust the count value to set a different size, considering the memory allocation for your VMs.
144+
145+
Format the disk images using the ext4 filesystem:
146+
```
147+
sudo mkfs.ext4 $BAO_DEMOS_FRONTEND1_IMG
148+
sudo mkfs.ext4 $BAO_DEMOS_FRONTEND2_IMG
149+
```
150+
151+
Mount the disk images to their respective mount points:
152+
```
153+
sudo mount -o loop $BAO_DEMOS_FRONTEND1_IMG $BAO_DEMOS_FRONTEND1_MP
154+
sudo mount -o loop $BAO_DEMOS_FRONTEND2_IMG $BAO_DEMOS_FRONTEND2_MP
155+
```
156+
157+
At this stage, you can customize the filesystems. Create directories, add files, or perform other modifications as required.
158+
159+
After making the necessary changes, unmount the images:
160+
```
161+
sudo umount $BAO_DEMOS_FRONTEND1_MP
162+
sudo umount $BAO_DEMOS_FRONTEND2_MP
163+
```
164+
165+
<!--- instruction#2 -->
166+
## 4) Transfer files to the backend VM
167+
168+
All necessary files, such as storage images, binaries, and configuration files, can be efficiently transferred using a file system overlay.
169+
170+
Export the defined global variable:
171+
```
172+
export BAO_DEMOS_BUILDROOT_OVERLAY=$BAO_DEMOS/guests/linux/buildroot/overlay
173+
```
174+
175+
Create the subdirectories in the buildroot overlay directory:
176+
```
177+
mkdir -p $BAO_DEMOS_BUILDROOT_OVERLAY/bin $BAO_DEMOS_BUILDROOT_OVERLAY/etc
178+
```
179+
180+
Copy all the files to the respective overlay directory:
181+
```
182+
cp $BAO_DEMOS_WRKDIR_IMGS/ext4_frontend_vm1_image.img $BAO_DEMOS_BUILDROOT_OVERLAY/etc
183+
cp $BAO_DEMOS_WRKDIR_IMGS/ext4_frontend_vm2_image.img $BAO_DEMOS_BUILDROOT_OVERLAY/etc
184+
cp -r $BAO_DEMOS/demos/virtio/setup/config* $BAO_DEMOS_BUILDROOT_OVERLAY/etc
185+
cp -r $BAO_DEMOS/demos/virtio/setup/setup* $BAO_DEMOS_BUILDROOT_OVERLAY/etc
186+
```
187+
188+
**Note**:
189+
The scripts and configuration files provide the necessary instructions to:
190+
- Set up the virtual network
191+
- Load the I/O dispatcher kernel module
192+
- Configure and launch the VirtIO backends
193+
194+
To insert the I/O dispatcher kernel module automatically when the system starts and run the demo, you can set up and export the following variable:
195+
```
196+
export BAO_DEMOS_BUILDROOT_POST_BUILD_SCRIPT=$BAO_DEMOS/demos/virtio/setup/post-build.sh
197+
```
198+
199+
This will introduce a new entry into the system initialization routine within the `/etc/inittab` file,
200+
ensuring the automatic insertion of the I/O dispatcher kernel module and the execution of all setup scripts required to configure and run the VirtIO demo.
201+
202+
Lastly, rebuild the backend kernel with:
203+
```
204+
cd $BAO_DEMOS_BUILDROOT
205+
make linux-reconfigure all
206+
```
207+
208+
<!--- instruction#3 -->
209+
### 5.1) Run the Demo (VirtIO Console)
210+
211+
Once the VirtIO console backend is launched, check the log file:
212+
```
213+
cat /etc/bao-vm1.log
214+
```
215+
216+
You should see output similar to the following:
217+
```
218+
virtio-console device id 0 at /dev/pts/0
219+
```
220+
221+
To interact with the FreeRTOS guest, connect to the VirtIO console using `picocom`:
222+
```
223+
picocom -b 115200 /dev/pts/0
224+
```
225+
226+
This allows you to view FreeRTOS guest messages.
227+
Notwithstanding, any input typed into the backend console will trigger corresponding messages from the FreeRTOS guest.
228+
229+
<!--- instruction#4 -->
230+
### 5.2) Run the Demo (VirtIO Network)
231+
232+
You can log into the two Linux frontend guests via `ssh` using their static IP addresses:
233+
234+
```
235+
ssh root@192.168.42.16
236+
ssh root@192.168.42.17
237+
```
238+
239+
<!--- instruction#5 -->
240+
### 5.3) Run the Demo (VirtIO Block)
241+
242+
After logging into each Linux frontend VM, mount the virtual storage filesystem by executing the following commands:
243+
```
244+
mkdir /mnt/ext4_mountpoint
245+
mount /dev/vda /mnt/ext4_mountpoint
246+
```
247+
248+
This will allow you to access the storage filesystem.
249+
You can verify changes made to the storage filesystem either from the backend VM or your laptop.
250+
Changes are persistent and will remain intact even after a reboot of the Linux frontend guests.
251+
252+
**Note**: To use a filesystem as the root file system, add the following parameters to the `bootargs` in the frontend device tree file:
253+
```
254+
root=/dev/vda rootwait
255+
```
256+
257+
This will configure the Linux frontend to boot directly from the virtual storage.
258+
259+
<!--- instruction#end -->

0 commit comments

Comments
 (0)