-
Notifications
You must be signed in to change notification settings - Fork 81
add nexthop support #149
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
Open
nanamicat
wants to merge
1
commit into
rust-netlink:main
Choose a base branch
from
nanamicat:main
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.
Open
add nexthop support #149
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
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
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,59 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use crate::{try_nl, Error, Handle}; | ||
| use futures_util::stream::StreamExt; | ||
| use netlink_packet_core::{ | ||
| NetlinkMessage, NLM_F_ACK, NLM_F_CREATE, NLM_F_EXCL, NLM_F_REPLACE, | ||
| NLM_F_REQUEST, | ||
| }; | ||
| use netlink_packet_route::{nexthop::NexthopMessage, RouteNetlinkMessage}; | ||
|
|
||
| /// A request to create a new nexthop. This is equivalent to the `ip nexthop add` | ||
| /// commands. | ||
| #[derive(Debug)] | ||
| pub struct NexthopAddRequest { | ||
| handle: Handle, | ||
| message: NexthopMessage, | ||
| replace: bool, | ||
| } | ||
|
|
||
| impl NexthopAddRequest { | ||
| pub(crate) fn new(handle: Handle, message: NexthopMessage) -> Self { | ||
| NexthopAddRequest { | ||
| handle, | ||
| message, | ||
| replace: false, | ||
| } | ||
| } | ||
|
|
||
| /// Replace existing matching nexthop. | ||
| pub fn replace(self) -> Self { | ||
| Self { | ||
| replace: true, | ||
| ..self | ||
| } | ||
| } | ||
|
|
||
| /// Execute the request. | ||
| pub async fn execute(self) -> Result<(), Error> { | ||
| let NexthopAddRequest { | ||
| mut handle, // Need mut for handle.request | ||
| message, | ||
| replace, | ||
| } = self; | ||
| let mut req = | ||
| NetlinkMessage::from(RouteNetlinkMessage::NewNexthop(message)); | ||
| let replace = if replace { NLM_F_REPLACE } else { NLM_F_EXCL }; | ||
| req.header.flags = NLM_F_REQUEST | NLM_F_ACK | replace | NLM_F_CREATE; | ||
|
|
||
| let mut response = handle.request(req)?; | ||
| while let Some(message) = response.next().await { | ||
| try_nl!(message); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn message_mut(&mut self) -> &mut NexthopMessage { | ||
| &mut self.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_route::{ | ||
| nexthop::{ | ||
| NexthopAttribute, NexthopFlags, NexthopGroupEntry, NexthopMessage, | ||
| }, | ||
| route::{RouteProtocol, RouteScope}, | ||
| AddressFamily, | ||
| }; | ||
| use std::{ | ||
| marker::PhantomData, | ||
| net::{IpAddr, Ipv4Addr, Ipv6Addr}, | ||
| }; | ||
|
|
||
| /// A builder for [`NexthopMessage`] | ||
| #[derive(Debug)] | ||
| pub struct NexthopMessageBuilder<T = IpAddr> { | ||
| message: NexthopMessage, | ||
| _phantom: PhantomData<T>, | ||
| } | ||
|
|
||
| impl<T> NexthopMessageBuilder<T> { | ||
| /// Create a new builder without specifying address family | ||
| fn new_no_address_family() -> Self { | ||
| let mut message = NexthopMessage::default(); | ||
| message.header.protocol = u8::from(RouteProtocol::Static); | ||
| message.header.scope = u8::from(RouteScope::Universe); | ||
| Self { | ||
| message, | ||
| _phantom: PhantomData, | ||
| } | ||
| } | ||
|
|
||
| /// Set the nexthop ID | ||
| pub fn id(mut self, id: u32) -> Self { | ||
| self.message.nlas.push(NexthopAttribute::Id(id)); | ||
| self | ||
| } | ||
|
|
||
| /// Set the interface index | ||
| pub fn oif(mut self, index: u32) -> Self { | ||
| self.message.nlas.push(NexthopAttribute::Oif(index)); | ||
| self | ||
| } | ||
|
|
||
| /// Set the nexthop as blackhole | ||
| pub fn blackhole(mut self) -> Self { | ||
| self.message.nlas.push(NexthopAttribute::Blackhole); | ||
| self | ||
| } | ||
|
|
||
| /// Set the nexthop group | ||
| pub fn group(mut self, entries: Vec<(u32, u8)>) -> Self { | ||
| let group_entries = entries | ||
| .into_iter() | ||
| .map(|(id, weight)| NexthopGroupEntry { | ||
| id, | ||
| weight, | ||
| resvd1: 0, | ||
| resvd2: 0, | ||
| }) | ||
| .collect(); | ||
| self.message | ||
| .nlas | ||
| .push(NexthopAttribute::Group(group_entries)); | ||
| self | ||
| } | ||
|
|
||
| /// Set flags | ||
| pub fn flags(mut self, flags: NexthopFlags) -> Self { | ||
| self.message.header.flags = flags; | ||
| self | ||
| } | ||
|
|
||
| /// Set the nexthop protocol | ||
| pub fn protocol(mut self, protocol: RouteProtocol) -> Self { | ||
| self.message.header.protocol = u8::from(protocol); | ||
| self | ||
| } | ||
|
|
||
| /// Set the nexthop scope | ||
| pub fn scope(mut self, scope: RouteScope) -> Self { | ||
| self.message.header.scope = u8::from(scope); | ||
| self | ||
| } | ||
|
|
||
| /// Build the message | ||
| pub fn build(self) -> NexthopMessage { | ||
| self.message | ||
| } | ||
| } | ||
|
|
||
| impl Default for NexthopMessageBuilder<Ipv4Addr> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl NexthopMessageBuilder<Ipv4Addr> { | ||
| /// Create a new builder for IPv4 | ||
| pub fn new() -> Self { | ||
| let mut builder = Self::new_no_address_family(); | ||
| builder.message.header.family = AddressFamily::Inet; | ||
| builder | ||
| } | ||
|
|
||
| /// Set the gateway IP address | ||
| pub fn gateway(mut self, addr: Ipv4Addr) -> Self { | ||
| self.message | ||
| .nlas | ||
| .push(NexthopAttribute::Gateway(addr.octets().to_vec())); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Default for NexthopMessageBuilder<Ipv6Addr> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl NexthopMessageBuilder<Ipv6Addr> { | ||
| /// Create a new builder for IPv6 | ||
| pub fn new() -> Self { | ||
| let mut builder = Self::new_no_address_family(); | ||
| builder.message.header.family = AddressFamily::Inet6; | ||
| builder | ||
| } | ||
|
|
||
| /// Set the gateway IP address | ||
| pub fn gateway(mut self, addr: Ipv6Addr) -> Self { | ||
| self.message | ||
| .nlas | ||
| .push(NexthopAttribute::Gateway(addr.octets().to_vec())); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Default for NexthopMessageBuilder<IpAddr> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl NexthopMessageBuilder<IpAddr> { | ||
| /// Create a new builder for any IP address family | ||
| pub fn new() -> Self { | ||
| Self::new_no_address_family() | ||
| } | ||
|
|
||
| /// Set the gateway IP address | ||
| pub fn gateway(mut self, addr: IpAddr) -> Self { | ||
| let (family, bytes) = match addr { | ||
| IpAddr::V4(addr) => (AddressFamily::Inet, addr.octets().to_vec()), | ||
| IpAddr::V6(addr) => (AddressFamily::Inet6, addr.octets().to_vec()), | ||
| }; | ||
| // Only set family if not already set or if explicitly different (though usually we trust the caller) | ||
| if self.message.header.family == AddressFamily::Unspec { | ||
| self.message.header.family = family; | ||
| } | ||
| self.message.nlas.push(NexthopAttribute::Gateway(bytes)); | ||
| self | ||
| } | ||
| } | ||
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,46 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use crate::{try_nl, Error, Handle}; | ||
| use futures_util::stream::StreamExt; | ||
| use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST}; | ||
| use netlink_packet_route::{ | ||
| nexthop::{NexthopAttribute, NexthopMessage}, | ||
| RouteNetlinkMessage, | ||
| }; | ||
|
|
||
| /// A request to delete a nexthop. This is equivalent to the `ip nexthop del` | ||
| /// commands. | ||
| #[derive(Debug)] | ||
| pub struct NexthopDelRequest { | ||
| handle: Handle, | ||
| message: NexthopMessage, | ||
| } | ||
|
|
||
| impl NexthopDelRequest { | ||
| pub(crate) fn new(handle: Handle, id: u32) -> Self { | ||
| let mut message = NexthopMessage::default(); | ||
| message.nlas.push(NexthopAttribute::Id(id)); | ||
| NexthopDelRequest { handle, message } | ||
| } | ||
|
|
||
| /// Execute the request. | ||
| pub async fn execute(self) -> Result<(), Error> { | ||
| let NexthopDelRequest { | ||
| mut handle, | ||
| message, | ||
| } = self; | ||
| let mut req = | ||
| NetlinkMessage::from(RouteNetlinkMessage::DelNexthop(message)); | ||
| req.header.flags = NLM_F_REQUEST | NLM_F_ACK; | ||
|
|
||
| let mut response = handle.request(req)?; | ||
| while let Some(message) = response.next().await { | ||
| try_nl!(message); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn message_mut(&mut self) -> &mut NexthopMessage { | ||
| &mut self.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use futures_util::{ | ||
| future::{self, Either}, | ||
| stream::{Stream, StreamExt}, | ||
| FutureExt, | ||
| }; | ||
| use netlink_packet_core::{NetlinkMessage, NLM_F_DUMP, NLM_F_REQUEST}; | ||
| use netlink_packet_route::{nexthop::NexthopMessage, RouteNetlinkMessage}; | ||
|
|
||
| use crate::{try_rtnl, Error, Handle}; | ||
|
|
||
| /// A request to get nexthops. This is equivalent to the `ip nexthop show` commands. | ||
| #[derive(Debug)] | ||
| pub struct NexthopGetRequest { | ||
| handle: Handle, | ||
| message: NexthopMessage, | ||
| } | ||
|
|
||
| impl NexthopGetRequest { | ||
| pub(crate) fn new(handle: Handle) -> Self { | ||
| NexthopGetRequest { | ||
| handle, | ||
| message: NexthopMessage::default(), | ||
| } | ||
| } | ||
|
|
||
| /// Execute the request. | ||
| pub fn execute(self) -> impl Stream<Item = Result<NexthopMessage, Error>> { | ||
| let NexthopGetRequest { | ||
| mut handle, | ||
| message, | ||
| } = self; | ||
| let mut req = | ||
| NetlinkMessage::from(RouteNetlinkMessage::GetNexthop(message)); | ||
| req.header.flags = NLM_F_REQUEST | NLM_F_DUMP; | ||
|
|
||
| match handle.request(req) { | ||
| Ok(response) => Either::Left(response.map(move |msg| { | ||
| Ok(try_rtnl!(msg, RouteNetlinkMessage::NewNexthop)) | ||
| })), | ||
| Err(e) => Either::Right( | ||
| future::err::<NexthopMessage, Error>(e).into_stream(), | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| pub fn message_mut(&mut self) -> &mut NexthopMessage { | ||
| &mut self.message | ||
| } | ||
| } |
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.
The current implementation of
gateway()can lead to an inconsistentNexthopMessageif it's called multiple times with IP addresses of different families. The address family is set based on the first call, but this is not validated on subsequent calls. This could result in a message withfamily = Inetbut a gateway with an IPv6 address, which the kernel would likely reject.To ensure correctness, the method should validate that any subsequent calls use an IP address of the same family. Panicking on misuse is an acceptable and common approach for builders to enforce correct usage.
The comment on line 157 is also a bit confusing and can be removed as part of this change.