-
Notifications
You must be signed in to change notification settings - Fork 71
add nexthop support #225
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
base: main
Are you sure you want to change the base?
add nexthop support #225
Conversation
Summary of ChangesHello @nanamicat, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the library's Netlink capabilities by adding full support for next-hop management. It introduces new data structures and parsing/emitting logic for next-hop messages and their attributes, as well as integrating these new message types into the existing Netlink message handling framework. This allows for more granular control and querying of next-hop configurations in the Linux kernel. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request adds support for nexthop objects, which is a great addition. The implementation is solid and follows the existing patterns in the crate. I've identified a few areas for improvement, mainly around making the parsing logic more robust and idiomatic. These changes will improve code clarity and maintainability.
| if payload.len() % 8 != 0 { | ||
| return Err(DecodeError::from("Invalid NHA_GROUP length")); | ||
| } | ||
| let mut entries = Vec::new(); | ||
| for chunk in payload.chunks(8) { | ||
| if let Ok(entry) = NexthopGroupEntry::parse(&chunk) { | ||
| entries.push(entry); | ||
| } else { | ||
| return Err(DecodeError::from( | ||
| "Failed to parse group entry", | ||
| )); | ||
| } | ||
| } | ||
| NexthopAttribute::Group(entries) |
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.
The current implementation for parsing NHA_GROUP contains an unreachable else block. The length check payload.len() % 8 != 0 ensures that payload.chunks(8) only yields 8-byte slices, for which NexthopGroupEntry::parse will not fail. This can be refactored to be more concise and idiomatic using chunks_exact and iterator collect, which also makes the code's intent clearer and removes the dead code.
if payload.len() % 8 != 0 {
return Err(DecodeError::from("Invalid NHA_GROUP length"));
}
NexthopAttribute::Group(
payload
.chunks_exact(8)
.map(NexthopGroupEntry::parse)
.collect::<Result<Vec<_, _>>>()
.map_err(|_|
// This should be unreachable given the length check above.
DecodeError::from("Failed to parse group entry"))?,
)| if payload.len() != 4 { | ||
| return Err(DecodeError::from("Invalid NHA_ID length")); | ||
| } | ||
| NexthopAttribute::Id(u32::from_ne_bytes( | ||
| payload.try_into().map_err(|_| { | ||
| DecodeError::from("Invalid NHA_ID length") | ||
| })?, | ||
| )) |
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.
The parsing logic for fixed-size attributes like NHA_ID can be simplified. Instead of an explicit length check followed by try_into(), you can rely on try_into() alone to handle the length validation. This makes the code more concise and idiomatic Rust. This same feedback applies to the parsing of NHA_GROUP_TYPE, NHA_OIF, and NHA_ENCAP_TYPE in this file.
NexthopAttribute::Id(u32::from_ne_bytes(
payload.try_into().map_err(|_| {
DecodeError::from("Invalid NHA_ID length")
})?,
))| if payload.len() != 2 { | ||
| return Err(DecodeError::from( | ||
| "Invalid NHA_GROUP_TYPE length", | ||
| )); | ||
| } | ||
| NexthopAttribute::GroupType(u16::from_ne_bytes( | ||
| payload.try_into().map_err(|_| { | ||
| DecodeError::from("Invalid NHA_GROUP_TYPE length") | ||
| })?, | ||
| )) |
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.
| if payload.len() != 4 { | ||
| return Err(DecodeError::from("Invalid NHA_OIF length")); | ||
| } | ||
| NexthopAttribute::Oif(u32::from_ne_bytes( | ||
| payload.try_into().map_err(|_| { | ||
| DecodeError::from("Invalid NHA_OIF length") | ||
| })?, | ||
| )) |
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.
| if payload.len() != 2 { | ||
| return Err(DecodeError::from( | ||
| "Invalid NHA_ENCAP_TYPE length", | ||
| )); | ||
| } | ||
| NexthopAttribute::EncapType(u16::from_ne_bytes( | ||
| payload.try_into().map_err(|_| { | ||
| DecodeError::from("Invalid NHA_ENCAP_TYPE length") | ||
| })?, | ||
| )) |
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.
| impl<'a, T: AsRef<[u8]> + 'a> Parseable<NexthopMessageBuffer<&'a T>> | ||
| for Vec<NexthopAttribute> | ||
| { | ||
| fn parse(buf: &NexthopMessageBuffer<&'a T>) -> Result<Self, DecodeError> { | ||
| let mut nlas = vec![]; | ||
| for nla_buf in buf.attributes() { | ||
| let nla = nla_buf?; | ||
| nlas.push(NexthopAttribute::parse_with_param( | ||
| &(nla.value(), nla.kind()), | ||
| (), | ||
| )?); | ||
| } | ||
| Ok(nlas) | ||
| } | ||
| } |
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.
The parse implementation for Vec<NexthopAttribute> can be written more idiomatically using a functional style with map and collect. This makes the code more concise and easier to read.
| impl<'a, T: AsRef<[u8]> + 'a> Parseable<NexthopMessageBuffer<&'a T>> | |
| for Vec<NexthopAttribute> | |
| { | |
| fn parse(buf: &NexthopMessageBuffer<&'a T>) -> Result<Self, DecodeError> { | |
| let mut nlas = vec![]; | |
| for nla_buf in buf.attributes() { | |
| let nla = nla_buf?; | |
| nlas.push(NexthopAttribute::parse_with_param( | |
| &(nla.value(), nla.kind()), | |
| (), | |
| )?); | |
| } | |
| Ok(nlas) | |
| } | |
| } | |
| impl<'a, T: AsRef<[u8]> + 'a> Parseable<NexthopMessageBuffer<&'a T>> | |
| for Vec<NexthopAttribute> | |
| { | |
| fn parse(buf: &NexthopMessageBuffer<&'a T>) -> Result<Self, DecodeError> { | |
| buf.attributes() | |
| .map(|nla_buf| { | |
| let nla = nla_buf?; | |
| NexthopAttribute::parse_with_param( | |
| &(nla.value(), nla.kind()), | |
| (), | |
| ) | |
| }) | |
| .collect() | |
| } | |
| } |
https://www.man7.org/linux/man-pages/man8/ip-nexthop.8.html