made veth functions asynchronous

fixed some comment spelling

Signed-off-by: raffaelespazzoli <raffaele.spazzoli@gmail.com>
pull/434/head
raffaelespazzoli 3 years ago
parent efc8b9989c
commit 89c3ad7333

@ -19,7 +19,7 @@ use rtnetlink::Error::RequestFailed;
const MAX_RETRIES: u32 = 100; const MAX_RETRIES: u32 = 100;
const RETRY_DURATION_MS: u64 = 10; 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"; const TEST_VETH_NAME: &str = "aya-veth1";
#[integration_test] #[integration_test]
@ -34,7 +34,7 @@ fn long_name() {
name_prog.load().unwrap(); name_prog.load().unwrap();
// Create veth interface pair // Create veth interface pair
create_veth_pair(TEST_VETH_NAME).unwrap(); block_on(create_veth_pair(TEST_VETH_NAME)).unwrap();
name_prog name_prog
.attach(TEST_VETH_NAME, XdpFlags::default()) .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. // 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. // Therefore, as long as we were able to load the program, this is good enough.
// Delete veth interface pait // Delete veth interface pair
delete_veth_pair(TEST_VETH_NAME).unwrap(); block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap();
} }
#[integration_test] #[integration_test]
@ -104,7 +104,7 @@ fn unload() {
prog.load().unwrap(); prog.load().unwrap();
// Create veth interface pair // 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(); let link = prog.attach(TEST_VETH_NAME, XdpFlags::default()).unwrap();
{ {
@ -124,8 +124,8 @@ fn unload() {
assert_loaded!("test_unload", false); assert_loaded!("test_unload", false);
// Delete veth interface pait // Delete veth interface pair
delete_veth_pair(TEST_VETH_NAME).unwrap(); block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap();
} }
#[integration_test] #[integration_test]
@ -135,7 +135,7 @@ fn pin_link() {
let prog: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap(); let prog: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap();
prog.load().unwrap(); prog.load().unwrap();
// Create veth interface pair // 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_id = prog.attach(TEST_VETH_NAME, XdpFlags::default()).unwrap();
let link = prog.take_link(link_id).unwrap(); let link = prog.take_link(link_id).unwrap();
@ -156,8 +156,8 @@ fn pin_link() {
drop(new_link); drop(new_link);
assert_loaded!("test_unload", false); assert_loaded!("test_unload", false);
// Delete veth interface pait // Delete veth interface pair
delete_veth_pair(TEST_VETH_NAME).unwrap(); block_on(delete_veth_pair(TEST_VETH_NAME)).unwrap();
} }
#[integration_test] #[integration_test]
@ -165,7 +165,7 @@ fn pin_lifecycle() {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/pass"); let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/pass");
// create veth interface pair // 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 // 1. Load Program and Pin
{ {
@ -199,29 +199,25 @@ fn pin_lifecycle() {
// program should be unloaded // program should be unloaded
assert_loaded!("pass", false); assert_loaded!("pass", false);
// Delete veth interface pait // Delete veth interface pair
delete_veth_pair(TEST_VETH_NAME).unwrap(); 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(); let (connection, handle, _) = new_connection().unwrap();
tokio::spawn(connection); tokio::spawn(connection);
block_on(async { return handle
handle
.link() .link()
.add() .add()
.veth(veth_name.to_string(), veth_name.to_string() + "-p") .veth(veth_name.to_string(), veth_name.to_string() + "-p")
// Execute the request, and wait for it to finish // Execute the request, and wait for it to finish
.execute() .execute()
.await?; .await;
Ok::<(), rtnetlink::Error>(())
})
} }
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(); let (connection, handle, _) = new_connection().unwrap();
tokio::spawn(connection); tokio::spawn(connection);
block_on(async {
let index = match handle let index = match handle
.link() .link()
.get() .get()
@ -236,7 +232,5 @@ fn delete_veth_pair(veth_name: &str) -> Result<(), rtnetlink::Error> {
}; };
// it should be enough to delete one the pairs // it should be enough to delete one the pairs
handle.link().del(index).execute().await?; return handle.link().del(index).execute().await;
Ok::<(), rtnetlink::Error>(())
})
} }

Loading…
Cancel
Save