From a760840a59d1c4f8b39776d40ae5bc265d707a7b Mon Sep 17 00:00:00 2001 From: sylvain-pierrot Date: Thu, 2 May 2024 10:50:11 +0200 Subject: [PATCH] feat: create bridge only if not exist Signed-off-by: sylvain-pierrot --- src/vmm/src/core/devices/virtio/net/bridge.rs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/vmm/src/core/devices/virtio/net/bridge.rs b/src/vmm/src/core/devices/virtio/net/bridge.rs index 58b731f..30c0c78 100644 --- a/src/vmm/src/core/devices/virtio/net/bridge.rs +++ b/src/vmm/src/core/devices/virtio/net/bridge.rs @@ -6,19 +6,24 @@ pub fn host_bridge(tap_name: String, bridge_name: String) { tokio::spawn(connection); futures::executor::block_on(async { - let _ = create_bridge(handle.clone(), bridge_name.clone()).await; + let _ = create_bridge_if_not_exist(handle.clone(), bridge_name.clone()).await; let _ = attach_link_to_bridge(handle, tap_name, bridge_name).await; }) } -async fn create_bridge(handle: Handle, name: String) -> Result<(), Error> { - handle - .link() - .add() - .bridge(name) - .execute() - .await - .map_err(|_| Error::RequestFailed) +async fn create_bridge_if_not_exist(handle: Handle, name: String) -> Result<(), Error> { + let mut bridge_names = handle.link().get().match_name(name.clone()).execute(); + + match bridge_names.try_next().await? { + Some(_) => Ok(()), + None => handle + .link() + .add() + .bridge(name) + .execute() + .await + .map_err(|_| Error::RequestFailed), + } } async fn attach_link_to_bridge( @@ -42,13 +47,13 @@ async fn attach_link_to_bridge( None => panic!(), }; - handle + let _ = handle .link() .set(link_index) .controller(master_index) .execute() .await - .unwrap(); + .map_err(|_| Error::RequestFailed); Ok(()) }