forked from virt-do/cloudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get internet access (virt-do#39)
* feat: create bridge interface Signed-off-by: sylvain-pierrot <[email protected]> * feat: create bridge only if not exist Signed-off-by: sylvain-pierrot <[email protected]> * fix: cargo clippy Signed-off-by: sylvain-pierrot <[email protected]> * feat: send code though gRPC to the agent (virt-do#37) * feat(agent/proto): add agent configuration in execute request Signed-off-by: Martin Moreira de Jesus <[email protected]> * feat: send code through cli and api to vm Signed-off-by: Mauran <[email protected]> --------- Signed-off-by: Martin Moreira de Jesus <[email protected]> Signed-off-by: Mauran <[email protected]> Co-authored-by: Martin Moreira de Jesus <[email protected]> * Feat: add initramfs implementation for vmm (virt-do#34) * feat(vmm): implemented automatic generation of rootfs with initramfs Signed-off-by: Muriel Paraire <[email protected]> * feat: image generation based off language Signed-off-by: Muriel Paraire <[email protected]> * feat(vmm): implemented automatic generation of rootfs with initramfs Signed-off-by: Muriel Paraire <[email protected]> * fix(vmm): fix logging & language order Signed-off-by: Muriel Paraire <[email protected]> * feat(vmm): one image per language Signed-off-by: Muriel Paraire <[email protected]> * feat(vmm): implemented initramfs Signed-off-by: Muriel Paraire <[email protected]> * fix(vmm): code cleanup Signed-off-by: Muriel Paraire <[email protected]> * fix(vmm): code cleanup Signed-off-by: Muriel Paraire <[email protected]> * fix(vmm): code cleanup Signed-off-by: Muriel Paraire <[email protected]> * fix: rust export for cargo agent and increase MMIO_GAP_END Signed-off-by: Mauran <[email protected]> * chore: lint Signed-off-by: Mauran <[email protected]> * fix: add back tracing Signed-off-by: Mauran <[email protected]> --------- Signed-off-by: Muriel Paraire <[email protected]> Signed-off-by: Mauran <[email protected]> Co-authored-by: Mauran <[email protected]> * feat: internet works Signed-off-by: sylvain-pierrot <[email protected]> * fix: cargo clippy Signed-off-by: sylvain-pierrot <[email protected]> --------- Signed-off-by: sylvain-pierrot <[email protected]> Signed-off-by: Martin Moreira de Jesus <[email protected]> Signed-off-by: Mauran <[email protected]> Signed-off-by: Muriel Paraire <[email protected]> Co-authored-by: Thomas Mauran <[email protected]> Co-authored-by: Martin Moreira de Jesus <[email protected]> Co-authored-by: Muriel Paraire <[email protected]> Co-authored-by: Mauran <[email protected]> Signed-off-by: Muriel Paraire <[email protected]>
- Loading branch information
1 parent
4d62c5c
commit d9badf8
Showing
9 changed files
with
198 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::net::{IpAddr, Ipv4Addr}; | ||
|
||
use futures::stream::TryStreamExt; | ||
use rtnetlink::{new_connection, Error, Handle}; | ||
|
||
use super::xx_netmask_width; | ||
|
||
#[derive(Clone)] | ||
pub struct Bridge { | ||
name: String, | ||
handle: Handle, | ||
} | ||
|
||
impl Bridge { | ||
pub fn new(name: String) -> Self { | ||
let (connection, handle, _) = new_connection().unwrap(); | ||
tokio::spawn(connection); | ||
|
||
let br = Self { name, handle }; | ||
br.create_bridge_if_not_exist(); | ||
|
||
br | ||
} | ||
|
||
fn create_bridge_if_not_exist(&self) { | ||
futures::executor::block_on(async { | ||
let mut bridge_names = self | ||
.handle | ||
.link() | ||
.get() | ||
.match_name(self.name.clone()) | ||
.execute(); | ||
|
||
let _ = match bridge_names.try_next().await { | ||
Ok(_) => Ok(()), | ||
Err(_) => self | ||
.handle | ||
.link() | ||
.add() | ||
.bridge(self.name.clone()) | ||
.execute() | ||
.await | ||
.map_err(|_| Error::RequestFailed), | ||
}; | ||
}); | ||
} | ||
|
||
pub fn set_addr(&self, addr: Ipv4Addr, netmask: Ipv4Addr) { | ||
futures::executor::block_on(async { | ||
let mut bridge_names = self | ||
.handle | ||
.link() | ||
.get() | ||
.match_name(self.name.clone()) | ||
.execute(); | ||
|
||
let bridge_index = match bridge_names.try_next().await { | ||
Ok(Some(link)) => link.header.index, | ||
Ok(None) => panic!(), | ||
Err(_) => panic!(), | ||
}; | ||
|
||
let prefix_len = xx_netmask_width(netmask.octets()); | ||
|
||
let _ = self | ||
.handle | ||
.address() | ||
.add(bridge_index, IpAddr::V4(addr), prefix_len) | ||
.execute() | ||
.await | ||
.map_err(|_| Error::RequestFailed); | ||
}); | ||
} | ||
|
||
pub fn set_up(&self) { | ||
futures::executor::block_on(async { | ||
let mut bridge_names = self | ||
.handle | ||
.link() | ||
.get() | ||
.match_name(self.name.clone()) | ||
.execute(); | ||
|
||
let bridge_index = match bridge_names.try_next().await { | ||
Ok(Some(link)) => link.header.index, | ||
Ok(None) => panic!(), | ||
Err(_) => panic!(), | ||
}; | ||
|
||
let _ = self | ||
.handle | ||
.link() | ||
.set(bridge_index) | ||
.up() | ||
.execute() | ||
.await | ||
.map_err(|_| Error::RequestFailed); | ||
}); | ||
} | ||
|
||
pub fn attach_link(&self, link_name: String) { | ||
futures::executor::block_on(async { | ||
let mut link_names = self | ||
.handle | ||
.link() | ||
.get() | ||
.match_name(link_name.clone()) | ||
.execute(); | ||
let mut master_names = self | ||
.handle | ||
.link() | ||
.get() | ||
.match_name(self.name.clone()) | ||
.execute(); | ||
|
||
let link_index = match link_names.try_next().await { | ||
Ok(Some(link)) => link.header.index, | ||
Ok(None) => panic!(), | ||
Err(_) => panic!(), | ||
}; | ||
let master_index = match master_names.try_next().await { | ||
Ok(Some(link)) => link.header.index, | ||
Ok(None) => panic!(), | ||
Err(_) => panic!(), | ||
}; | ||
|
||
let _ = self | ||
.handle | ||
.link() | ||
.set(link_index) | ||
.controller(master_index) | ||
.execute() | ||
.await | ||
.map_err(|_| Error::RequestFailed); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::net::Ipv4Addr; | ||
|
||
use super::xx_netmask_width; | ||
|
||
pub fn iptables_ip_masq(network: Ipv4Addr, netmask: Ipv4Addr, link_name: String) { | ||
let prefix_len = xx_netmask_width(netmask.octets()); | ||
let source = format!("{}/{}", network, prefix_len); | ||
|
||
let ipt = iptables::new(false).unwrap(); | ||
let rule = format!("-s {} ! -o {} -j MASQUERADE", source, link_name); | ||
|
||
let exists = ipt.exists("nat", "POSTROUTING", rule.as_str()).unwrap(); | ||
if !exists { | ||
let _ = ipt.insert_unique("nat", "POSTROUTING", rule.as_str(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters