Skip to content

Commit 4d1acbf

Browse files
committed
dbs-cli: support virtio-balloon feature
We can use dbs-cli to simply reuse unused virtual machine on host. An empty virtio-balloon device should be inserted first before we ask guest to balloon some free memory. It should use update action, and only parameter is size in mib. Example: First time: ./dbs-cli --api-sock-path /tmp/api-sock --balloon-memory 0 update Next: ./dbs-cli --api-sock-path /tmp/api-sock --balloon-memory 512 update Fixes: #23 Signed-off-by: Helin Guo <helinguo@linux.alibaba.com>
1 parent 22a50b6 commit 4d1acbf

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dragonball = { git = "https://github.com/kata-containers/kata-containers", branc
1111
"virtio-vsock",
1212
"virtio-net",
1313
"virtio-mem",
14+
"virtio-balloon",
1415
"hotplug",
1516
"dbs-upcall",
1617
"atomic-guest-memory"

src/api_client.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ pub fn run_api_client(args: DBSArgs) -> Result<()> {
2727
send_request(request, &args.api_sock_path)?;
2828
}
2929

30+
if let Some(size_mib) = args.update_args.balloon_memory {
31+
let request = request_balloon_memory(size_mib);
32+
send_request(request, &args.api_sock_path)?;
33+
}
34+
3035
Ok(())
3136
}
3237

@@ -60,6 +65,13 @@ fn request_hotplug_memory(size_mib: usize) -> Value {
6065
})
6166
}
6267

68+
fn request_balloon_memory(size_mib: usize) -> Value {
69+
json!({
70+
"action": "balloon_memory",
71+
"size_mib": size_mib,
72+
})
73+
}
74+
6375
fn send_request(request: Value, api_sock_path: &str) -> Result<()> {
6476
let mut unix_stream = UnixStream::connect(api_sock_path).context("Could not create stream")?;
6577

src/api_server.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::os::unix::net::{UnixListener, UnixStream};
1010

1111
use anyhow::{anyhow, Context, Result};
1212
use dragonball::device_manager::blk_dev_mgr::BlockDeviceConfigInfo;
13+
use dragonball::device_manager::balloon_dev_mgr::BalloonDeviceConfigInfo;
1314
use dragonball::device_manager::mem_dev_mgr::MemDeviceConfigInfo;
1415
use dragonball::device_manager::virtio_net_dev_mgr::VirtioNetDeviceConfigInfo;
1516

@@ -121,6 +122,20 @@ impl ApiServer {
121122
};
122123
return self.insert_mem_device(mem_cfg);
123124
}
125+
Some("balloon_memory") => {
126+
let balloon_cfg = BalloonDeviceConfigInfo {
127+
balloon_id: "virtio-balloon0".to_string(),
128+
size_mib: v["size_mib"]
129+
.as_u64()
130+
.context("Invalid virtio-balloon size input")?,
131+
use_generic_irq: None,
132+
use_shared_irq: None,
133+
f_deflate_on_oom: false,
134+
// TODO: enable free page reporting
135+
f_reporting: false,
136+
};
137+
return self.insert_balloon_device(balloon_cfg);
138+
}
124139
_ => {
125140
println!("Unknown Actions");
126141
}

src/parser/args.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,12 @@ The type of it is an array of BlockDeviceConfigInfo, e.g.
305305
display_order = 2
306306
)]
307307
pub hotplug_memory: Option<usize>,
308+
309+
#[clap(
310+
long,
311+
value_parser,
312+
help = "Balloon memory (MiB) through connection with dbs-cli api server",
313+
display_order = 2
314+
)]
315+
pub balloon_memory: Option<usize>,
308316
}

src/vmm_comm_trait.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use dragonball::{
99
BlockDeviceConfigInfo, BootSourceConfig, VmmAction, VmmActionError, VmmData, VmmRequest,
1010
VmmResponse, VsockDeviceConfigInfo,
1111
},
12+
device_manager::balloon_dev_mgr::BalloonDeviceConfigInfo,
1213
device_manager::mem_dev_mgr::MemDeviceConfigInfo,
1314
device_manager::virtio_net_dev_mgr::VirtioNetDeviceConfigInfo,
1415
vcpu::VcpuResizeInfo,
@@ -146,10 +147,18 @@ pub trait VMMComm {
146147
.with_context(|| format!("Failed to insert virtio-blk device {:?}", blk_cfg))?;
147148
Ok(())
148149
}
149-
150+
150151
fn insert_mem_device(&self, mem_cfg: MemDeviceConfigInfo) -> Result<()> {
151152
self.handle_request_with_retry(Request::Sync(VmmAction::InsertMemDevice(mem_cfg.clone())))
152153
.with_context(|| format!("Failed to insert mem device {:?}", mem_cfg))?;
153154
Ok(())
154155
}
156+
157+
fn insert_balloon_device(&self, balloon_cfg: BalloonDeviceConfigInfo) -> Result<()> {
158+
self.handle_request_with_retry(Request::Sync(VmmAction::InsertBalloonDevice(
159+
balloon_cfg.clone(),
160+
)))
161+
.with_context(|| format!("Failed to insert balloon device {:?}", balloon_cfg))?;
162+
Ok(())
163+
}
155164
}

0 commit comments

Comments
 (0)