From 4ff62e68d31ab8ff8a78a6c7f71f4a604a50d43a Mon Sep 17 00:00:00 2001 From: Aditya Rajan Date: Wed, 12 Jan 2022 10:55:08 +0530 Subject: [PATCH 1/2] core, macvlan: add support setting for MTU from netavark config Netavark config accepts `MTU` as network option so allow users to configure the same for `macvlan` interface. See https://github.com/containernetworking/plugins/blob/master/plugins/main/macvlan/macvlan.go#L181 Follow up PR to: https://github.com/containers/netavark/pull/111 Signed-off-by: Aditya Rajan --- src/network/core.rs | 21 +++++++++++++++++++++ src/network/core_utils.rs | 12 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/network/core.rs b/src/network/core.rs index fdeb5ce7e..e8374edb3 100644 --- a/src/network/core.rs +++ b/src/network/core.rs @@ -322,6 +322,24 @@ impl Core { } } + // mtu to configure, 0 means it was not set do nothing. + let mut mtu_config: u32 = 0; + if let Some(options_map) = network.options.as_ref() { + if let Some(mtu) = options_map.get("mtu") { + match mtu.parse() { + Ok(mtu) => { + mtu_config = mtu; + } + Err(err) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("unable to parse mtu: {}", err), + )) + } + } + } + } + // get master interface name let mut master_ifname: String = match network.network_interface.as_ref() { None => { @@ -402,6 +420,7 @@ impl Core { &master_ifname, &container_macvlan_name, macvlan_mode, + mtu_config, address_vector, netns, ) { @@ -428,6 +447,7 @@ impl Core { master_ifname: &str, container_macvlan: &str, macvlan_mode: u32, + mtu: u32, netns_ipaddr: Vec, netns: &str, ) -> Result { @@ -435,6 +455,7 @@ impl Core { master_ifname, container_macvlan, macvlan_mode, + mtu, netns, ) { Ok(_) => (), diff --git a/src/network/core_utils.rs b/src/network/core_utils.rs index ae7228510..68aab31a8 100644 --- a/src/network/core_utils.rs +++ b/src/network/core_utils.rs @@ -650,6 +650,7 @@ impl CoreUtils { master_ifname: &str, macvlan_ifname: &str, macvlan_mode: u32, + mtu: u32, netns_path: &str, ) -> Result<(), Error> { let (_connection, handle, _) = match rtnetlink::new_connection() { @@ -705,6 +706,17 @@ impl CoreUtils { } } + // configure mtu of macvlan interface + // before moving it to network namespace + // See: https://github.com/containernetworking/plugins/blob/master/plugins/main/macvlan/macvlan.go#L181 + if mtu != 0 { + if let Err(err) = + CoreUtils::set_link_mtu(&handle, &macvlan_tmp_name.to_owned(), mtu).await + { + return Err(err); + } + } + // change network namespace of macvlan interface match File::open(netns_path) { Ok(netns_file) => { From e437a2068ba17cb7b7e68329935dec2e1fb43a9a Mon Sep 17 00:00:00 2001 From: Aditya Rajan Date: Thu, 13 Jan 2022 14:05:26 +0530 Subject: [PATCH 2/2] test: add test for macvlan with mtu Signed-off-by: Aditya Rajan --- test/300-macvlan.bats | 18 +++++++++++++++++ test/testfiles/macvlan-mtu.json | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/testfiles/macvlan-mtu.json diff --git a/test/300-macvlan.bats b/test/300-macvlan.bats index f0a0ae118..354bfcdfd 100644 --- a/test/300-macvlan.bats +++ b/test/300-macvlan.bats @@ -24,6 +24,24 @@ function setup() { assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up" assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device" + ipaddr="10.88.0.2/16" + run_in_container_netns ip addr show eth0 + assert "$output" "=~" "$ipaddr" "IP address matches container address" + assert_json "$result" ".podman.interfaces.eth0.subnets[0].ipnet" "==" "$ipaddr" "Result contains correct IP address" +} + +@test "macvlan setup with mtu" { + run_netavark --file ${TESTSDIR}/testfiles/macvlan-mtu.json setup $(get_container_netns_path) + result="$output" + + mac=$(jq -r '.podman.interfaces.eth0.mac_address' <<< "$result" ) + # check that interface exists + run_in_container_netns ip -j --details link show eth0 + link_info="$output" + assert_json "$link_info" ".[].mtu" "==" "1400" "MTU matches configured MTU" + assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac" + assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up" + assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device" ipaddr="10.88.0.2/16" run_in_container_netns ip addr show eth0 diff --git a/test/testfiles/macvlan-mtu.json b/test/testfiles/macvlan-mtu.json new file mode 100644 index 000000000..ab3150d51 --- /dev/null +++ b/test/testfiles/macvlan-mtu.json @@ -0,0 +1,35 @@ +{ + "container_id": "someID", + "container_name": "someName", + "networks": { + "podman": { + "static_ips": [ + "10.88.0.2" + ], + "interface_name": "eth0" + } + }, + "network_info": { + "podman": { + "name": "podman", + "id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9", + "driver": "macvlan", + "network_interface": "dummy0", + "subnets": [ + { + "subnet": "10.88.0.0/16", + "gateway": "10.88.0.1" + } + ], + "ipv6_enabled": false, + "internal": false, + "dns_enabled": false, + "ipam_options": { + "driver": "host-local" + }, + "options": { + "mtu": "1400" + } + } + } + }