-
Notifications
You must be signed in to change notification settings - Fork 76
stats: Add support for RTM_NEWSTATS/RTM_GETSTATS #283
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
Draft
cathay4t
wants to merge
1
commit into
rust-netlink:main
Choose a base branch
from
cathay4t:to_upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ pub mod nsid; | |
| pub mod prefix; | ||
| pub mod route; | ||
| pub mod rule; | ||
| pub mod stats; | ||
| pub mod tc; | ||
|
|
||
| mod message; | ||
|
|
||
This file contains hidden or 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 hidden or 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,106 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{ | ||
| DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer, Parseable, | ||
| }; | ||
|
|
||
| use super::xstats::{ | ||
| parse_offload_xstats_inner, AfSpecStats, LinkXstatGroup, OffloadXstat, | ||
| VecLinkXstats, | ||
| }; | ||
| use crate::link::{Stats64, Stats64Buffer}; | ||
|
|
||
| const IFLA_STATS_LINK_64: u16 = 1; | ||
| const IFLA_STATS_LINK_XSTATS: u16 = 2; | ||
| const IFLA_STATS_LINK_XSTATS_SLAVE: u16 = 3; | ||
| const IFLA_STATS_LINK_OFFLOAD_XSTATS: u16 = 4; | ||
| const IFLA_STATS_AF_SPEC: u16 = 5; | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone)] | ||
| #[non_exhaustive] | ||
| pub enum StatsAttribute { | ||
| Link64(Stats64), | ||
| LinkXstats(Vec<LinkXstatGroup>), | ||
| LinkXstatsPort(Vec<LinkXstatGroup>), | ||
| LinkOffloadXstats(Vec<OffloadXstat>), | ||
| AfSpec(AfSpecStats), | ||
| Other(DefaultNla), | ||
| } | ||
|
|
||
| impl Nla for StatsAttribute { | ||
| fn value_len(&self) -> usize { | ||
| match self { | ||
| Self::Link64(v) => v.buffer_len(), | ||
| Self::LinkXstats(v) | Self::LinkXstatsPort(v) => { | ||
| v.as_slice().buffer_len() | ||
| } | ||
| Self::LinkOffloadXstats(v) => v.as_slice().buffer_len(), | ||
| Self::AfSpec(v) => v.buffer_len(), | ||
| Self::Other(attr) => attr.value_len(), | ||
| } | ||
| } | ||
|
|
||
| fn emit_value(&self, buffer: &mut [u8]) { | ||
| match self { | ||
| Self::Link64(v) => v.emit(buffer), | ||
| Self::LinkXstats(v) | Self::LinkXstatsPort(v) => { | ||
| v.as_slice().emit(buffer) | ||
| } | ||
| Self::LinkOffloadXstats(v) => v.as_slice().emit(buffer), | ||
| Self::AfSpec(v) => v.emit(buffer), | ||
| Self::Other(attr) => attr.emit_value(buffer), | ||
| } | ||
| } | ||
|
|
||
| fn kind(&self) -> u16 { | ||
| match self { | ||
| Self::Link64(_) => IFLA_STATS_LINK_64, | ||
| Self::LinkXstats(_) => IFLA_STATS_LINK_XSTATS, | ||
| Self::LinkXstatsPort(_) => IFLA_STATS_LINK_XSTATS_SLAVE, | ||
| Self::LinkOffloadXstats(_) => IFLA_STATS_LINK_OFFLOAD_XSTATS, | ||
| Self::AfSpec(_) => IFLA_STATS_AF_SPEC, | ||
| Self::Other(attr) => attr.kind(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> | ||
| for StatsAttribute | ||
| { | ||
| fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> { | ||
| let payload = buf.value(); | ||
| Ok(match buf.kind() { | ||
| IFLA_STATS_LINK_64 => Self::Link64( | ||
| Stats64::parse(&Stats64Buffer::new(payload)) | ||
| .context("invalid IFLA_STATS_LINK_64 value")?, | ||
| ), | ||
| IFLA_STATS_LINK_XSTATS => { | ||
| let err = "invalid IFLA_STATS_LINK_XSTATS value"; | ||
| Self::LinkXstats( | ||
| VecLinkXstats::parse(&NlaBuffer::new(payload)) | ||
| .context(err)? | ||
| .0, | ||
| ) | ||
| } | ||
| IFLA_STATS_LINK_XSTATS_SLAVE => { | ||
| let err = "invalid IFLA_STATS_LINK_XSTATS_SLAVE value"; | ||
| Self::LinkXstatsPort( | ||
| VecLinkXstats::parse(&NlaBuffer::new(payload)) | ||
| .context(err)? | ||
| .0, | ||
| ) | ||
| } | ||
| IFLA_STATS_LINK_OFFLOAD_XSTATS => { | ||
| let err = "invalid IFLA_STATS_LINK_OFFLOAD_XSTATS value"; | ||
| Self::LinkOffloadXstats( | ||
| parse_offload_xstats_inner(payload).context(err)?, | ||
| ) | ||
| } | ||
| IFLA_STATS_AF_SPEC => Self::AfSpec(AfSpecStats::parse(payload)), | ||
| kind => Self::Other( | ||
| DefaultNla::parse(buf) | ||
| .context(format!("unknown IFLA_STATS type {kind}"))?, | ||
| ), | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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,58 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{ | ||
| DecodeError, Emitable, NlaBuffer, NlasIterator, Parseable, | ||
| }; | ||
|
|
||
| use crate::AddressFamily; | ||
|
|
||
| const STATS_HEADER_LEN: usize = 12; | ||
|
|
||
| buffer!(StatsMessageBuffer(STATS_HEADER_LEN) { | ||
| family: (u8, 0), | ||
| pad1: (u8, 1), | ||
| pad2: (u16, 2..4), | ||
| ifindex: (u32, 4..8), | ||
| filter_mask: (u32, 8..STATS_HEADER_LEN), | ||
| payload: (slice, STATS_HEADER_LEN..), | ||
| }); | ||
|
|
||
| impl<'a, T: AsRef<[u8]> + ?Sized> StatsMessageBuffer<&'a T> { | ||
| pub fn attributes( | ||
| &self, | ||
| ) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> { | ||
| NlasIterator::new(self.payload()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone, Default)] | ||
| pub struct StatsHeader { | ||
| pub family: AddressFamily, | ||
| pub ifindex: u32, | ||
| pub filter_mask: u32, | ||
| } | ||
|
|
||
| impl Emitable for StatsHeader { | ||
| fn buffer_len(&self) -> usize { | ||
| STATS_HEADER_LEN | ||
| } | ||
|
|
||
| fn emit(&self, buffer: &mut [u8]) { | ||
| let mut packet = StatsMessageBuffer::new(buffer); | ||
| packet.set_family(self.family.into()); | ||
| packet.set_pad1(0); | ||
| packet.set_pad2(0); | ||
| packet.set_ifindex(self.ifindex); | ||
| packet.set_filter_mask(self.filter_mask); | ||
| } | ||
| } | ||
|
|
||
| impl<T: AsRef<[u8]>> Parseable<StatsMessageBuffer<T>> for StatsHeader { | ||
| fn parse(buf: &StatsMessageBuffer<T>) -> Result<Self, DecodeError> { | ||
| Ok(StatsHeader { | ||
| family: buf.family().into(), | ||
| ifindex: buf.ifindex(), | ||
| filter_mask: buf.filter_mask(), | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,50 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{DecodeError, Emitable, ErrorContext, Parseable}; | ||
|
|
||
| use crate::stats::{StatsAttribute, StatsHeader, StatsMessageBuffer}; | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone, Default)] | ||
| #[non_exhaustive] | ||
| pub struct StatsMessage { | ||
| pub header: StatsHeader, | ||
| pub attributes: Vec<StatsAttribute>, | ||
| } | ||
|
|
||
| impl<'a, T: AsRef<[u8]> + 'a> Parseable<StatsMessageBuffer<&'a T>> | ||
| for StatsMessage | ||
| { | ||
| fn parse(buf: &StatsMessageBuffer<&'a T>) -> Result<Self, DecodeError> { | ||
| Ok(Self { | ||
| header: StatsHeader::parse(buf) | ||
| .context("failed to parse stats message header")?, | ||
| attributes: Vec::<StatsAttribute>::parse(buf) | ||
| .context("failed to parse stats message NLAs")?, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: AsRef<[u8]> + 'a> Parseable<StatsMessageBuffer<&'a T>> | ||
| for Vec<StatsAttribute> | ||
| { | ||
| fn parse(buf: &StatsMessageBuffer<&'a T>) -> Result<Self, DecodeError> { | ||
| let mut attributes = vec![]; | ||
| for nla_buf in buf.attributes() { | ||
| attributes.push(StatsAttribute::parse(&nla_buf?)?); | ||
| } | ||
| Ok(attributes) | ||
| } | ||
| } | ||
|
|
||
| impl Emitable for StatsMessage { | ||
| fn buffer_len(&self) -> usize { | ||
| self.header.buffer_len() + self.attributes.as_slice().buffer_len() | ||
| } | ||
|
|
||
| fn emit(&self, buffer: &mut [u8]) { | ||
| self.header.emit(buffer); | ||
| self.attributes | ||
| .as_slice() | ||
| .emit(&mut buffer[self.header.buffer_len()..]); | ||
| } | ||
| } |
This file contains hidden or 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,20 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| mod attribute; | ||
| mod header; | ||
| mod message; | ||
| mod xstats; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| pub use self::{ | ||
| attribute::StatsAttribute, | ||
| header::{StatsHeader, StatsMessageBuffer}, | ||
| message::StatsMessage, | ||
| xstats::{ | ||
| AfSpecStatEntry, AfSpecStats, Bond3adStats, BondXstat, | ||
| BridgeMcastStats, BridgeStpXstats, BridgeVlanXstats, BridgeXstat, | ||
| HwSInfo, HwStats64, LinkXstatGroup, OffloadXstat, | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Stats64Buffer::new(payload)directly without checking the payload length can lead to out-of-bounds reads or panics if the kernel sends a malformed or shorter payload thanLINK_STATS64_LEN(200 bytes). UseStats64Buffer::new_checked(payload)instead to safely validate the payload length before parsing.