Skip to content

Commit

Permalink
Support adding vhost-net network devices
Browse files Browse the repository at this point in the history
Argument `virnets` receives a JSON string of `NetworkInterfaceConfig`,
rather than `VirtioNetDeviceConfigInfo`. It allows user to add any network
devices according to item's backend type.

Signed-off-by: Xuewei Niu <[email protected]>
  • Loading branch information
justxuewei committed Nov 15, 2023
1 parent baa00ce commit 17f0651
Show file tree
Hide file tree
Showing 8 changed files with 1,016 additions and 302 deletions.
1,208 changes: 951 additions & 257 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dragonball = { git = "https://github.com/kata-containers/kata-containers", branc
"virtio-fs",
"virtio-vsock",
"virtio-net",
"vhost-net",
"hotplug",
"dbs-upcall",
] }
Expand Down
66 changes: 44 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
>
> [[简体中文版]](README_zh.md)
# 1. Examples:
## Examples

See all options:

```
./dbs-cli --help
```

A simple example:
### Basic examples

```bash
./dbs-cli \
Expand Down Expand Up @@ -68,9 +68,9 @@ Create a virtio-vsock tunnel for Guest-to-Host communication.
--vsock /tmp/vsock.sock create;
```

Create virtio-net devices.
Create virtio-blk devices.

> The type of the `--virnets` receives an array of VirtioNetDeviceConfigInfo in the
> The type of the `--virblks` receives an array of BlockDeviceConfigInfo in the
> format of JSON.
```
Expand All @@ -79,28 +79,48 @@ Create virtio-net devices.
--kernel-path ~/path/to/kernel/vmlinux.bin \
--rootfs ~/path/to/rootfs/bionic.rootfs.ext4 \
--boot-args "console=ttyS0 console=ttyS1 earlyprintk=ttyS1 tty0 reboot=k debug panic=1 pci=off root=/dev/vda1" \
--virnets "[{\"iface_id\":\"eth0\",\"host_dev_name\":\"tap0\",\"num_queues\":2,\"queue_size\":0,\"guest_mac\":\"43:2D:9C:13:71:48\",\"allow_duplicate_mac\":true}]" \
--virblks '[{"drive_id":"testblk","device_type":"RawBlock","path_on_host":"/path/to/test.img","is_root_device":false,"is_read_only":false,"is_direct":false,"no_drop":false,"num_queues":1,"queue_size":1024}]' \
create;
```

Create virtio-blk devices.
### Networking

> The type of the `--virblks` receives an array of BlockDeviceConfigInfo in the
> format of JSON.
Launch a Dragonball VMM with a virtio-based device. `--virnets` receives an
array of `NetworkInterfaceConfig` in the format of JSON.

```
./dbs-cli \
--log-file dbs-cli.log --log-level ERROR \
--kernel-path ~/path/to/kernel/vmlinux.bin \
--rootfs ~/path/to/rootfs/bionic.rootfs.ext4 \
--boot-args "console=ttyS0 console=ttyS1 earlyprintk=ttyS1 tty0 reboot=k debug panic=1 pci=off root=/dev/vda1" \
--virblks '[{"drive_id":"testblk","device_type":"RawBlock","path_on_host":"/path/to/test.img","is_root_device":false,"is_read_only":false,"is_direct":false,"no_drop":false,"num_queues":1,"queue_size":1024}]' \
create;
--virnets '[{"guest_mac":"43:2D:9C:13:71:48","backend":{"type":"vhost","iface_id":"eth0","host_dev_name":"tap0","allow_duplicate_mac":true}}]'
```

# 2. Usage
The supported network devices include:

## 1. Create API Server and Update VM
```
// Virtio-net
{
"guest_mac": "43:2D:9C:13:71:48",
"backend": {
"type": "virtio",
"iface_id": "eth0",
"host_dev_name": "tap0",
"allow_duplicate_mac": true
}
}
// Vhost-net
{
"guest_mac": "43:2D:9C:13:71:48",
"backend": {
"type": "vhost",
"iface_id": "eth0",
"host_dev_name": "tap0",
"allow_duplicate_mac": true
}
}
```

## Advanced Usage

### Create API Server and Update VM

An API Server could be created by adding `--api-sock-path [socket path]` into dbs-cli creation command.

Expand All @@ -112,6 +132,8 @@ Cpu Hotplug via API Server:

Create hot-plug virtio-net devices via API Server:

**TODO: Needs to be updated**

> The type of the `--hotplug-virnets` receives an array of
> VirtioNetDeviceConfigInfo in the format of JSON.
Expand All @@ -134,11 +156,11 @@ sudo ./dbs-cli \
update
```

## 2. Exit vm
### Exit VM

> If you want to exit vm, just input `reboot` in vm's console.
If you want to exit vm, just input `reboot` in vm's console.

## 3. For developers
### For developers

If you wish to modify some details or debug to figure out the fault of codes, you can do as follow to see whether the program act expectedly or not.

Expand All @@ -154,7 +176,7 @@ To see some help:
cargo run -- --help
```

## 3. Some off-topic remarks
### Some off-topic remarks

Regarding the dependency issue of the upstream library, it is recommended to build the `build` target of Makefile to avoid it temporally.

Expand All @@ -164,6 +186,6 @@ make build

If the self-defined `dragonball` dependency is supposed to be used, please refer to [dependency document](docs/dependency.md)

# License
## License

`DBS-CLI` is licensed under [Apache License](http://www.apache.org/licenses/LICENSE-2.0), Version 2.0.
6 changes: 3 additions & 3 deletions src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ fn request_cpu_resize(vcpu_resize_num: usize) -> Value {
})
}

/// Insert virtio-net devices
fn request_virtio_net(virtio_net_config: &str) -> Value {
/// Insert virtio network devices
fn request_virtio_net(net_config: &str) -> Value {
json!({
"action": "insert_virnets",
"config": virtio_net_config,
"config": net_config,
})
}

Expand Down
9 changes: 4 additions & 5 deletions src/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use std::os::unix::net::{UnixListener, UnixStream};
use anyhow::{anyhow, Context, Result};
use dragonball::device_manager::blk_dev_mgr::BlockDeviceConfigInfo;
use dragonball::device_manager::fs_dev_mgr::FsMountConfigInfo;
use dragonball::device_manager::virtio_net_dev_mgr::VirtioNetDeviceConfigInfo;

use crate::vmm_comm_trait::VMMComm;
use dragonball::api::v1::{VmmRequest, VmmResponse};
use dragonball::api::v1::{NetworkInterfaceConfig, VmmRequest, VmmResponse};
use dragonball::vcpu::VcpuResizeInfo;
use serde_json::Value;

Expand Down Expand Up @@ -87,11 +86,11 @@ impl ApiServer {
Some(config_json) => config_json,
None => return Err(anyhow!("The config of virtio-net device is required")),
};
let configs: Vec<VirtioNetDeviceConfigInfo> = serde_json::from_str(config_json)
.context("Parse virtio-net device config from json")?;
let configs: Vec<NetworkInterfaceConfig> = serde_json::from_str(config_json)
.context("Parse NetworkInterfaceConfig from JSON")?;
for config in configs.iter() {
self.insert_virnet(config.clone())
.context("Insert a virtio-net device to the Dragonball")?;
.context("Insert a virtio device to the Dragonball")?;
}
}
Some("insert_virblks") => {
Expand Down
15 changes: 8 additions & 7 deletions src/cli_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ use vmm_sys_util::eventfd::EventFd;

use dragonball::{
api::v1::{
BlockDeviceConfigInfo, BootSourceConfig, InstanceInfo, VmmRequest, VmmResponse,
VsockDeviceConfigInfo,
BlockDeviceConfigInfo, BootSourceConfig, InstanceInfo, NetworkInterfaceConfig, VmmRequest,
VmmResponse, VsockDeviceConfigInfo,
},
device_manager::fs_dev_mgr::FsDeviceConfigInfo,
device_manager::virtio_net_dev_mgr::VirtioNetDeviceConfigInfo,
vm::{CpuTopology, VmConfigInfo},
};

Expand Down Expand Up @@ -156,13 +155,15 @@ impl CliInstance {
.expect("failed to set vsock socket path");
}

// Virtio devices
if !args.create_args.virnets.is_empty() {
let configs: Vec<VirtioNetDeviceConfigInfo> =
serde_json::from_str(&args.create_args.virnets)
.expect("failed to parse virtio-net devices from JSON");
let configs: Vec<NetworkInterfaceConfig> =
serde_json::from_str(&args.create_args.virnets).unwrap_or_else(|err| {
panic!("Failed to parse NetworkInterfaceConfig from JSON: {}", err)
});
for config in configs.into_iter() {
self.insert_virnet(config)
.expect("failed to insert a virtio-net device");
.expect("Failed to insert a virtio device");
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/parser/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ pub struct CreateArgs {
long,
value_parser,
default_value = "",
help = r#"Insert virtio-net devices into the Dragonball.
The type of it is an array of VirtioNetDeviceConfigInfo, e.g.
--virnets '[{"iface_id":"eth0","host_dev_name":"tap0","num_queues":2,"queue_size":0,"allow_duplicate_mac":true}]'"#,
help = r#"Insert virtio devices into the Dragonball before launched. The supported devices are virtio-net and vhost-net."#,
display_order = 2
)]
pub virnets: String,
Expand Down
9 changes: 4 additions & 5 deletions src/vmm_comm_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use std::sync::{Arc, Mutex};

use dragonball::{
api::v1::{
BlockDeviceConfigInfo, BootSourceConfig, VmmAction, VmmActionError, VmmData, VmmRequest,
VmmResponse, VsockDeviceConfigInfo,
BlockDeviceConfigInfo, BootSourceConfig, NetworkInterfaceConfig, VmmAction, VmmActionError,
VmmData, VmmRequest, VmmResponse, VsockDeviceConfigInfo,
},
device_manager::fs_dev_mgr::{FsDeviceConfigInfo, FsMountConfigInfo},
device_manager::virtio_net_dev_mgr::VirtioNetDeviceConfigInfo,
vcpu::VcpuResizeInfo,
vm::VmConfigInfo,
};
Expand Down Expand Up @@ -135,9 +134,9 @@ pub trait VMMComm {
Ok(())
}

fn insert_virnet(&self, config: VirtioNetDeviceConfigInfo) -> Result<()> {
fn insert_virnet(&self, config: NetworkInterfaceConfig) -> Result<()> {
self.handle_request(Request::Sync(VmmAction::InsertNetworkDevice(config)))
.context("Request to insert a virtio-net device")?;
.context("Request to insert a virtio device")?;
Ok(())
}

Expand Down

0 comments on commit 17f0651

Please sign in to comment.