From dcbc3cc37518de4cc4555ef1fc96f2ac2fe56536 Mon Sep 17 00:00:00 2001 From: jiewang-group Date: Thu, 23 Feb 2023 17:15:50 +0800 Subject: [PATCH] 11 --- tests/mod_test/src/libtest.rs | 3 + tests/mod_test/tests/balloon_test.rs | 279 +++++++++++++++++++++++++-- 2 files changed, 267 insertions(+), 15 deletions(-) diff --git a/tests/mod_test/src/libtest.rs b/tests/mod_test/src/libtest.rs index 5560915d..8d3a75b4 100644 --- a/tests/mod_test/src/libtest.rs +++ b/tests/mod_test/src/libtest.rs @@ -101,6 +101,9 @@ impl TestState { serde_json::from_slice(self.qmp_sock.read_line().as_bytes()).unwrap() } + pub fn qmp_read(&self) -> Value { + serde_json::from_slice(self.qmp_sock.read_line().as_bytes()).unwrap() + } fn send_test_cmd(&self, cmd: &str) -> String { self.test_sock.write_line(cmd); self.test_sock.read_line() diff --git a/tests/mod_test/tests/balloon_test.rs b/tests/mod_test/tests/balloon_test.rs index 6c7da8a1..694b51b4 100644 --- a/tests/mod_test/tests/balloon_test.rs +++ b/tests/mod_test/tests/balloon_test.rs @@ -14,11 +14,15 @@ use mod_test::libtest::{test_init, TestState}; use mod_test::machine::TestStdMachine; use mod_test::malloc::GuestAllocator; use mod_test::virtio::{TestVirtQueue, VirtioDeviceOps}; -use mod_test::virtio_pci_modern::TestVirtioPciDev; +use mod_test::virtio_pci_modern::{TestVirtioPciDev, VirtioPciCommonCfg}; use serde_json::json; use std::cell::RefCell; +use std::process::Command; use std::rc::Rc; +use std::{thread, time}; use util::offset_of; + +const BALLOON_F_DEFLATE_ON_OOM_TEST: u32 = 2; const BALLOON_F_PRPORTING_TEST: u32 = 5; const BALLOON_F_VERSION1_TEST: u64 = 32; const PAGE_SIZE_UNIT: u64 = 4096; @@ -34,11 +38,12 @@ pub struct VirtioBalloonTest { } impl VirtioBalloonTest { - pub fn new(memsize: u64, page_size: u64, shared: bool, fpr: bool) -> Self { + pub fn new(memsize: u64, page_size: u64, shared: bool, fpr: bool, huge: bool) -> Self { let pci_slot: u8 = 0x4; let pci_fn: u8 = 0x0; let mut extra_args: Vec<&str> = Vec::new(); let mut fpr_switch = String::from("false"); + let mem_path = format!("-mem-path /tmp/stratovirt/hugepages"); let mut args: Vec<&str> = "-machine".split(' ').collect(); if shared { @@ -52,6 +57,11 @@ impl VirtioBalloonTest { args = mem_args[..].split(' ').collect(); extra_args.append(&mut args); + if huge { + args = mem_path[..].split(' ').collect(); + extra_args.append(&mut args); + } + if fpr { fpr_switch = String::from("true"); } @@ -114,7 +124,7 @@ impl VirtioBalloonTest { fn inflate_fun(shared: bool) { let page_num = 255_u32; let mut idx = 0_u32; - let balloon = VirtioBalloonTest::new(1024, 4096, shared, false); + let balloon = VirtioBalloonTest::new(1024, 4096, shared, false, false); let free_page = balloon .allocer @@ -202,10 +212,10 @@ fn inflate_fun(shared: bool) { balloon.state.borrow_mut().kill_process(); } -fn balloon_fun(shared: bool) { +fn balloon_fun(shared: bool, huge: bool) { let page_num = 255_u32; let mut idx = 0_u32; - let balloon = VirtioBalloonTest::new(1024, 4096, shared, false); + let balloon = VirtioBalloonTest::new(1024, 4096, shared, false, huge); let free_page = balloon .allocer @@ -344,26 +354,124 @@ fn balloon_fun(shared: bool) { balloon.state.borrow_mut().kill_process(); } +/// balloon device inflate test +/// TestStep: +/// 1.Init device +/// 2.Populate the inflate queue with illegal addresses +/// 3.Populate the inflate queue with legal addresses +/// Expect: +/// 1.sucess +/// 2.There are no exceptions in the process +/// 3.memory need by addr #[test] fn balloon_inflate_001() { inflate_fun(false); } - +/// balloon device inflate test +/// TestStep: +/// 1.Init device +/// 2.Populate the inflate queue with illegal addresses +/// 3.Populate the inflate queue with legal addresses +/// Expect: +/// 1.sucess +/// 2.There are no exceptions in the process +/// 3.memory released by addr #[test] fn balloon_inflate_002() { inflate_fun(true); } +fn create_huge_mem_path() { + let _output = Command::new("rm") + .arg("-rf") + .arg("/tmp/stratovirt/hugepages") + .output() + .expect("Failed to rm dir"); + + let _output = Command::new("mkdir") + .arg("-p") + .arg("/tmp/stratovirt/hugepages") + .output() + .expect("Failed to mkdir dir"); + + let _output = Command::new("mount") + .arg("-t") + .arg("hugetlbfs") + .arg("hugetlbfs") + .arg("/tmp/stratovirt/hugepages") + .output() + .expect("Failed to mount dir"); + + let _output = Command::new("sysctl") + .arg("vm.nr_hugepages=1024") + .output() + .expect("Failed to set count hugepages"); +} + +fn clean_huge_mem_path() { + let _output = Command::new("umount") + .arg("/tmp/stratovirt/hugepages") + .output() + .expect("Failed to mount dir"); +} + +/// balloon device deflate and inflate test +/// TestStep: +/// 1.Init device +/// 2.Populate the inflate queue with illegal addresses +/// 3.Populate the inflate queue with legal addresses +/// 4.Populate the deflate queue with legal addresses +/// Expect: +/// 1.Sucess +/// 2.There are no exceptions in the process +/// 3.Memory re by addr +/// 4.Free memory #[test] fn balloon_fun_001() { - balloon_fun(false); + balloon_fun(false, false); } +/// balloon device deflate and inflate test +/// TestStep: +/// 1.Init device +/// 2.Populate the inflate queue with illegal addresses +/// 3.Populate the inflate queue with legal addresses +/// 4.Populate the deflate queue with legal addresses +/// Expect: +/// 1.Sucess +/// 2.There are no exceptions in the process +/// 3.Memory re by addr +/// 4.Free memory #[test] fn balloon_fun_002() { - balloon_fun(true); + balloon_fun(true, false); } +/// TestStep: +/// 1.Init device +/// 2.Populate the inflate queue with illegal addresses +/// 3.Populate the inflate queue with legal addresses +/// 4.Populate the deflate queue with legal addresses +/// Expect: +/// 1.Sucess +/// 2.There are no exceptions in the process +/// 3.Memory re by addr +/// 4.Free memory +#[test] +fn balloon_huge_fun_001() { + create_huge_mem_path(); + balloon_fun(false, true); + balloon_fun(true, true); + clean_huge_mem_path(); +} + +/// balloon device features config test +/// TestStep: +/// 1.Init device +/// 2.set guest feature 0xFFFFFFFFFFFFFFFF +/// Expect: +/// 1.Sucess +/// 2.guest feature equel device feature #[test] fn balloon_feature_001() { let pci_slot: u8 = 0x4; @@ -404,6 +512,13 @@ fn balloon_feature_001() { test_state.borrow_mut().kill_process(); } +/// balloon device features config test +/// TestStep: +/// 1.Init device +/// 2.get device feature +/// Expect: +/// 1.Sucess +/// 2.feature OK #[test] fn balloon_feature_002() { let pci_slot: u8 = 0x4; @@ -417,11 +532,11 @@ fn balloon_feature_002() { args = mem_args[..].split(' ').collect(); extra_args.append(&mut args); - let blk_pci_args = format!( - "-device {},id=drv0,bus=pcie.{},addr={}.0,free-page-reporting=true", + let pci_args = format!( + "-device {},id=drv0,bus=pcie.{},addr={}.0,deflate-on-oom=true,free-page-reporting=true", "virtio-balloon-pci", pci_fn, pci_slot ); - args = blk_pci_args[..].split(' ').collect(); + args = pci_args[..].split(' ').collect(); extra_args.append(&mut args); let test_state = Rc::new(RefCell::new(test_init(extra_args))); @@ -439,7 +554,9 @@ fn balloon_feature_002() { assert_eq!( features, - 1u64 << BALLOON_F_VERSION1_TEST | 1u64 << BALLOON_F_PRPORTING_TEST + 1u64 << BALLOON_F_VERSION1_TEST + | 1u64 << BALLOON_F_PRPORTING_TEST + | 1u64 << BALLOON_F_DEFLATE_ON_OOM_TEST ); dev.borrow_mut() @@ -453,7 +570,7 @@ fn balloon_feature_002() { fn balloon_fpr_fun(shared: bool) { let page_num = 255_u32; let mut idx = 0_u32; - let balloon = VirtioBalloonTest::new(1024, 4096, shared, true); + let balloon = VirtioBalloonTest::new(1024, 4096, shared, true, false); let free_page = balloon .allocer @@ -538,11 +655,29 @@ fn balloon_fpr_fun(shared: bool) { balloon.state.borrow_mut().kill_process(); } +/// balloon device fpr features test +/// TestStep: +/// 1.Init device +/// 2.Populate the fpr queue with illegal addresses +/// 3.Populate the fpr queue with legal addresses +/// Expect: +/// 1.Sucess +/// 2.There are no exceptions in the process +/// 3.Free memory #[test] fn balloon_fpr_001() { balloon_fpr_fun(true); } +/// balloon device fpr features test +/// TestStep: +/// 1.Init device +/// 2.Populate the fpr queue with illegal addresses +/// 3.Populate the fpr queue with legal addresses +/// Expect: +/// 1.Sucess +/// 2.There are no exceptions in the process +/// 3.Free memory #[test] fn balloon_fpr_002() { balloon_fpr_fun(false); @@ -557,7 +692,7 @@ struct VirtioBalloonConfig { #[test] fn query() { - let balloon = VirtioBalloonTest::new(2048, 4096, false, false); + let balloon = VirtioBalloonTest::new(2048, 4096, false, false, false); let ret = balloon .state .borrow_mut() @@ -571,14 +706,84 @@ fn query() { balloon.state.borrow_mut().kill_process(); } +/// balloon device qmp config test +/// TestStep: +/// 1.Init device +/// 2.qmp config page 512M +/// 3.qmp query result 512M +/// Expect: +/// 1/2/3.Sucess #[test] fn balloon_config_001() { - let balloon = VirtioBalloonTest::new(1024, 4096, false, false); + let balloon = VirtioBalloonTest::new(1024, 4096, false, false, false); + + balloon + .state + .borrow_mut() + .qmp("{\"execute\": \"balloon\", \"arguments\": {\"value\": 536870912}}"); + let ret = balloon.state.borrow_mut().qmp_read(); + assert_eq!(*ret.get("return").unwrap(), json!({})); + + let num_pages = balloon + .device + .borrow_mut() + .config_readl(offset_of!(VirtioBalloonConfig, num_pages) as u64); + assert_eq!(num_pages, 131072); + let actual = balloon + .device + .borrow_mut() + .config_readl(offset_of!(VirtioBalloonConfig, actual) as u64); + assert_eq!(actual, 0); + balloon + .device + .borrow_mut() + .config_writel(offset_of!(VirtioBalloonConfig, actual) as u64, 131072); + let actual = balloon + .device + .borrow_mut() + .config_readl(offset_of!(VirtioBalloonConfig, actual) as u64); + assert_eq!(actual, 131072); + let _actual = balloon + .device + .borrow_mut() + .config_readl((offset_of!(VirtioBalloonConfig, actual) + 8) as u64); + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + let ret = balloon.state.borrow_mut().qmp_read(); + assert_eq!( + *ret.get("data").unwrap(), + json!({"actual": 536870912 as u64}) + ); + + balloon + .state + .borrow_mut() + .qmp("{\"execute\": \"balloon\", \"arguments\": {\"value\": 1610612736}}"); + let num_pages = balloon + .device + .borrow_mut() + .config_readl(offset_of!(VirtioBalloonConfig, num_pages) as u64); + assert_eq!(num_pages, 0); + balloon.state.borrow_mut().kill_process(); +} + +/// balloon device qmp config test +/// TestStep: +/// 1.Init device +/// 2.qmp config page 512M +/// 3.qmp query result 512M +/// Expect: +/// 1/2/3.Sucess +#[test] +fn balloon_config_002() { + let balloon = VirtioBalloonTest::new(1024, 4096, false, false, true); balloon .state .borrow_mut() .qmp("{\"execute\": \"balloon\", \"arguments\": {\"value\": 536870912}}"); + let ret = balloon.state.borrow_mut().qmp_read(); + assert_eq!(*ret.get("return").unwrap(), json!({})); let num_pages = balloon .device @@ -599,6 +804,17 @@ fn balloon_config_001() { .borrow_mut() .config_readl(offset_of!(VirtioBalloonConfig, actual) as u64); assert_eq!(actual, 131072); + let _actual = balloon + .device + .borrow_mut() + .config_readl((offset_of!(VirtioBalloonConfig, actual) + 8) as u64); + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + let ret = balloon.state.borrow_mut().qmp_read(); + assert_eq!( + *ret.get("data").unwrap(), + json!({"actual": 536870912 as u64}) + ); balloon .state @@ -611,3 +827,36 @@ fn balloon_config_001() { assert_eq!(num_pages, 0); balloon.state.borrow_mut().kill_process(); } + +/// balloon device deactive config test +/// TestStep: +/// 1.Init device +/// 2.guest write queue disable +/// Expect: +/// 1/2.Sucess +#[test] +fn balloon_deactive_001() { + let balloon = VirtioBalloonTest::new(1024, 4096, false, false, false); + + let bar = balloon.device.borrow().bar; + let common_base = balloon.device.borrow().common_base as u64; + + balloon.device.borrow().pci_dev.io_writel( + bar, + common_base + offset_of!(VirtioPciCommonCfg, queue_enable) as u64, + 0, + ); + + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + + let ret = balloon + .state + .borrow_mut() + .qmp("{\"execute\": \"query-balloon\"}"); + assert_eq!( + *ret.get("return").unwrap(), + json!({"actual": 1073741824 as u64}) + ); + balloon.state.borrow_mut().kill_process(); +} -- Gitee