From 89c3ad733315f12d26df5517b9f949e612e407d7 Mon Sep 17 00:00:00 2001 From: raffaelespazzoli Date: Sat, 5 Nov 2022 19:07:59 -0400 Subject: [PATCH] made veth functions asynchronous fixed some comment spelling Signed-off-by: raffaelespazzoli --- test/integration-test/src/tests/load.rs | 80 ++++++++++++------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/test/integration-test/src/tests/load.rs b/test/integration-test/src/tests/load.rs index 4063addc..6d8dc524 100644 --- a/test/integration-test/src/tests/load.rs +++ b/test/integration-test/src/tests/load.rs @@ -19,7 +19,7 @@ use rtnetlink::Error::RequestFailed; const MAX_RETRIES: u32 = 100; const RETRY_DURATION_MS: u64 = 10; -// veth names can be mac 15 char long +// veth names can be max 15 char long const TEST_VETH_NAME: &str = "aya-veth1"; #[integration_test] @@ -34,7 +34,7 @@ fn long_name() { name_prog.load().unwrap(); // Create veth interface pair - create_veth_pair(TEST_VETH_NAME).unwrap(); + block_on(create_veth_pair(TEST_VETH_NAME)).unwrap(); name_prog .attach(TEST_VETH_NAME, XdpFlags::default()) @@ -44,8 +44,8 @@ fn long_name() { // It seem though that it now uses the name from the ELF symbol table instead. // Therefore, as long as we were able to load the program, this is good enough. - // Delete veth interface pait - delete_veth_pair(TEST_VETH_NAME).unwrap(); + // Delete veth interface pair + block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap(); } #[integration_test] @@ -104,7 +104,7 @@ fn unload() { prog.load().unwrap(); // Create veth interface pair - create_veth_pair(TEST_VETH_NAME).unwrap(); + block_on(create_veth_pair(TEST_VETH_NAME)).unwrap(); let link = prog.attach(TEST_VETH_NAME, XdpFlags::default()).unwrap(); { @@ -124,8 +124,8 @@ fn unload() { assert_loaded!("test_unload", false); - // Delete veth interface pait - delete_veth_pair(TEST_VETH_NAME).unwrap(); + // Delete veth interface pair + block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap(); } #[integration_test] @@ -135,7 +135,7 @@ fn pin_link() { let prog: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap(); prog.load().unwrap(); // Create veth interface pair - create_veth_pair(TEST_VETH_NAME).unwrap(); + block_on(create_veth_pair(TEST_VETH_NAME)).unwrap(); let link_id = prog.attach(TEST_VETH_NAME, XdpFlags::default()).unwrap(); let link = prog.take_link(link_id).unwrap(); @@ -156,8 +156,8 @@ fn pin_link() { drop(new_link); assert_loaded!("test_unload", false); - // Delete veth interface pait - delete_veth_pair(TEST_VETH_NAME).unwrap(); + // Delete veth interface pair + block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap(); } #[integration_test] @@ -165,7 +165,7 @@ fn pin_lifecycle() { let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/pass"); // create veth interface pair - create_veth_pair(TEST_VETH_NAME).unwrap(); + block_on(create_veth_pair(TEST_VETH_NAME)).unwrap(); // 1. Load Program and Pin { @@ -199,44 +199,38 @@ fn pin_lifecycle() { // program should be unloaded assert_loaded!("pass", false); - // Delete veth interface pait - delete_veth_pair(TEST_VETH_NAME).unwrap(); + // Delete veth interface pair + block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap(); } -fn create_veth_pair(veth_name: &str) -> Result<(), rtnetlink::Error> { +async fn create_veth_pair(veth_name: &str) -> Result<(), rtnetlink::Error> { let (connection, handle, _) = new_connection().unwrap(); tokio::spawn(connection); - block_on(async { - handle - .link() - .add() - .veth(veth_name.to_string(), veth_name.to_string() + "-p") - // Execute the request, and wait for it to finish - .execute() - .await?; - Ok::<(), rtnetlink::Error>(()) - }) + return handle + .link() + .add() + .veth(veth_name.to_string(), veth_name.to_string() + "-p") + // Execute the request, and wait for it to finish + .execute() + .await; } -fn delete_veth_pair(veth_name: &str) -> Result<(), rtnetlink::Error> { +async fn delete_veth_pair(veth_name: &str) -> Result<(), rtnetlink::Error> { let (connection, handle, _) = new_connection().unwrap(); tokio::spawn(connection); - block_on(async { - let index = match handle - .link() - .get() - .match_name(veth_name.to_string()) - .execute() - // should return only one result - .try_next() - .await? - { - Some(v) => v.header.index, - None => return Err(RequestFailed), - }; - - // it should be enough to delete one the pairs - handle.link().del(index).execute().await?; - Ok::<(), rtnetlink::Error>(()) - }) + let index = match handle + .link() + .get() + .match_name(veth_name.to_string()) + .execute() + // should return only one result + .try_next() + .await? + { + Some(v) => v.header.index, + None => return Err(RequestFailed), + }; + + // it should be enough to delete one the pairs + return handle.link().del(index).execute().await; }