Skip to content

Commit

Permalink
feat: create bridge only if not exist
Browse files Browse the repository at this point in the history
Signed-off-by: sylvain-pierrot <sylvain.pierrot@etu.umontpellier.fr>
  • Loading branch information
sylvain-pierrot committed May 2, 2024
1 parent bca5559 commit a760840
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/vmm/src/core/devices/virtio/net/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(())
}

0 comments on commit a760840

Please sign in to comment.