Skip to content

Commit

Permalink
refactor: clean code
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 1cbd327 commit 4a60c7a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/vmm/src/core/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ impl Net {
tap.set_vnet_hdr_size(VIRTIO_NET_HDR_SIZE as i32)
.map_err(Error::Tap)?;

let bridge = Bridge::new("br0".to_string());
let bridge_name = "br0".to_string();
let bridge = Bridge::new(bridge_name.clone());
bridge.set_addr(iface_host_addr, netmask);
bridge.attach_link(tap.get_name().map_err(Error::Tap)?);
bridge.set_up();

// Get internet access
iptables_ip_masq(network, netmask);
iptables_ip_masq(network, netmask, bridge_name);

let net = Arc::new(Mutex::new(Net {
mem,
Expand Down
4 changes: 2 additions & 2 deletions src/vmm/src/core/devices/virtio/net/iptables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::net::Ipv4Addr;

use super::xx_netmask_width;

pub fn iptables_ip_masq(network: Ipv4Addr, netmask: Ipv4Addr) {
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 br0 -j MASQUERADE", source);
let rule = format!("-s {} ! -o {} -j MASQUERADE", source, link_name);

let exists = ipt.exists("nat", "POSTROUTING", rule.as_str()).unwrap();
if !exists {
Expand Down

0 comments on commit 4a60c7a

Please sign in to comment.