Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mtu information for interfaces #1367

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/common/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ impl NetworkData {
pub fn ip_networks(&self) -> &[IpNetwork] {
self.inner.ip_networks()
}

/// Returns the Maximum Transfer Unit (MTU) of the interface.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("mtu: {}", network.mtu());
/// }
/// ```
pub fn mtu(&self) -> u64 {
self.inner.mtu()
}
}

/// MAC address for network interface.
Expand Down
9 changes: 9 additions & 0 deletions src/unix/apple/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl NetworksInner {
}
name.set_len(libc::strlen(pname));
let name = String::from_utf8_unchecked(name);
let mtu = (*if2m).ifm_data.ifi_mtu as u64;
match self.interfaces.entry(name) {
hash_map::Entry::Occupied(mut e) => {
let interface = e.get_mut();
Expand Down Expand Up @@ -137,6 +138,7 @@ impl NetworksInner {
old_errors_out,
(*if2m).ifm_data.ifi_oerrors
);
if interface.mtu != mtu { interface.mtu = mtu }
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
Expand Down Expand Up @@ -167,6 +169,7 @@ impl NetworksInner {
updated: true,
mac_addr: MacAddr::UNSPECIFIED,
ip_networks: vec![],
mtu,
},
});
}
Expand Down Expand Up @@ -196,6 +199,8 @@ pub(crate) struct NetworkDataInner {
pub(crate) mac_addr: MacAddr,
/// IP networks
pub(crate) ip_networks: Vec<IpNetwork>,
/// Interface Maximum Transfer Unit (MTU)
mtu: u64,
}

impl NetworkDataInner {
Expand Down Expand Up @@ -254,4 +259,8 @@ impl NetworkDataInner {
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&self.ip_networks
}

pub(crate) fn mtu(&self) -> u64 {
self.mtu
}
}
9 changes: 9 additions & 0 deletions src/unix/freebsd/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl NetworksInner {
}
if let Some(name) = utils::c_buf_to_utf8_string(&data.ifmd_name) {
let data = &data.ifmd_data;
let mtu = data.ifi_mtu as u64;
match self.interfaces.entry(name) {
hash_map::Entry::Occupied(mut e) => {
let interface = e.get_mut();
Expand All @@ -91,6 +92,7 @@ impl NetworksInner {
old_and_new!(interface, ifi_opackets, old_ifi_opackets, data);
old_and_new!(interface, ifi_ierrors, old_ifi_ierrors, data);
old_and_new!(interface, ifi_oerrors, old_ifi_oerrors, data);
if interface.mtu != mtu { interface.mtu = mtu }
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
Expand All @@ -115,6 +117,7 @@ impl NetworksInner {
updated: true,
mac_addr: MacAddr::UNSPECIFIED,
ip_networks: vec![],
mtu,
},
});
}
Expand Down Expand Up @@ -151,6 +154,8 @@ pub(crate) struct NetworkDataInner {
pub(crate) mac_addr: MacAddr,
/// IP networks
pub(crate) ip_networks: Vec<IpNetwork>,
/// Interface Maximum Transfer Unit (MTU)
mtu: u64,
}

impl NetworkDataInner {
Expand Down Expand Up @@ -209,4 +214,8 @@ impl NetworkDataInner {
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&self.ip_networks
}

pub(crate) fn mtu(&self) -> u64 {
self.mtu
}
}
15 changes: 15 additions & 0 deletions src/unix/linux/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn refresh_networks_list_from_sysfs(

for entry in dir.flatten() {
let parent = &entry.path().join("statistics");
let entry_path = &entry.path();
let entry = match entry.file_name().into_string() {
Ok(entry) => entry,
Err(_) => continue,
Expand All @@ -63,6 +64,8 @@ fn refresh_networks_list_from_sysfs(
let tx_errors = read(parent, "tx_errors", &mut data);
// let rx_compressed = read(parent, "rx_compressed", &mut data);
// let tx_compressed = read(parent, "tx_compressed", &mut data);
let mtu = read(entry_path, "mtu", &mut data);

match interfaces.entry(entry) {
hash_map::Entry::Occupied(mut e) => {
let interface = e.get_mut();
Expand All @@ -76,6 +79,7 @@ fn refresh_networks_list_from_sysfs(
old_and_new!(interface, tx_errors, old_tx_errors);
// old_and_new!(e, rx_compressed, old_rx_compressed);
// old_and_new!(e, tx_compressed, old_tx_compressed);
if interface.mtu != mtu { interface.mtu = mtu }
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
Expand All @@ -99,6 +103,7 @@ fn refresh_networks_list_from_sysfs(
// old_rx_compressed: rx_compressed,
// tx_compressed,
// old_tx_compressed: tx_compressed,
mtu,
updated: true,
},
});
Expand Down Expand Up @@ -164,6 +169,8 @@ pub(crate) struct NetworkDataInner {
/// MAC address
pub(crate) mac_addr: MacAddr,
pub(crate) ip_networks: Vec<IpNetwork>,
/// Interface Maximum Transfer Unit (MTU)
mtu: u64,
// /// Indicates the number of compressed packets received by this
// /// network device. This value might only be relevant for interfaces
// /// that support packet compression (e.g: PPP).
Expand Down Expand Up @@ -219,6 +226,9 @@ impl NetworkDataInner {
// old_tx_compressed,
// read(path, "tx_compressed", data)
// );
let mtu_path = &Path::new("/sys/class/net/").join(path);
let mtu = read(mtu_path, "mtu", data);
if self.mtu != mtu { self.mtu = mtu }
}

pub(crate) fn received(&self) -> u64 {
Expand Down Expand Up @@ -276,6 +286,11 @@ impl NetworkDataInner {
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&self.ip_networks
}

pub(crate) fn mtu(&self) -> u64 {
self.mtu
}

}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/unknown/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ impl NetworkDataInner {
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&[]
}

pub(crate) fn mtu(&self) -> u64 {
0
}
}
11 changes: 11 additions & 0 deletions src/windows/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ impl NetworksInner {
Ok(s) => s,
_ => continue,
};

let mtu = ptr.Mtu as u64;
match self.interfaces.entry(interface_name) {
hash_map::Entry::Occupied(mut e) => {
let interface = e.get_mut();
Expand All @@ -114,6 +116,7 @@ impl NetworksInner {
);
old_and_new!(interface, errors_in, old_errors_in, ptr.InErrors);
old_and_new!(interface, errors_out, old_errors_out, ptr.OutErrors);
if interface.mtu != mtu { interface.mtu = mtu }
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
Expand All @@ -137,6 +140,7 @@ impl NetworksInner {
old_errors_out: ptr.OutErrors,
mac_addr: MacAddr::UNSPECIFIED,
ip_networks: vec![],
mtu,
updated: true,
},
});
Expand Down Expand Up @@ -179,6 +183,7 @@ impl NetworksInner {
);
old_and_new!(interface, errors_in, old_errors_in, entry.InErrors);
old_and_new!(interface, errors_out, old_errors_out, entry.OutErrors);
if interface.mtu != entry.Mtu as u64 { interface.mtu = entry.Mtu as u64 }
}
}
}
Expand All @@ -201,6 +206,8 @@ pub(crate) struct NetworkDataInner {
updated: bool,
pub(crate) mac_addr: MacAddr,
pub(crate) ip_networks: Vec<IpNetwork>,
/// Interface Maximum Transfer Unit (MTU)
mtu: u64,
}

impl NetworkDataInner {
Expand Down Expand Up @@ -259,4 +266,8 @@ impl NetworkDataInner {
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&self.ip_networks
}

pub(crate) fn mtu(&self) -> u64 {
self.mtu
}
}