Skip to content

Commit

Permalink
Merge pull request #158 from flouthoc/macvlan-mtu
Browse files Browse the repository at this point in the history
core: add support setting for `MTU` on `macvlan` interface from netavark config
  • Loading branch information
openshift-merge-robot authored Jan 13, 2022
2 parents 39f44cf + e437a20 commit f7ecf56
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/network/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -402,6 +420,7 @@ impl Core {
&master_ifname,
&container_macvlan_name,
macvlan_mode,
mtu_config,
address_vector,
netns,
) {
Expand All @@ -428,13 +447,15 @@ impl Core {
master_ifname: &str,
container_macvlan: &str,
macvlan_mode: u32,
mtu: u32,
netns_ipaddr: Vec<ipnet::IpNet>,
netns: &str,
) -> Result<String, std::io::Error> {
let _ = match core_utils::CoreUtils::configure_macvlan_async(
master_ifname,
container_macvlan,
macvlan_mode,
mtu,
netns,
) {
Ok(_) => (),
Expand Down
12 changes: 12 additions & 0 deletions src/network/core_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down
18 changes: 18 additions & 0 deletions test/300-macvlan.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/testfiles/macvlan-mtu.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit f7ecf56

Please sign in to comment.