This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
netlink-packet-route/src/rtnl/link: Implement new commands property_a…
…dd and property_del for ip link
- Loading branch information
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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,65 @@ | ||
use crate::{ | ||
packet::{ | ||
nlas::link::{Nla, Prop}, | ||
LinkMessage, | ||
NetlinkMessage, | ||
NetlinkPayload, | ||
RtnlMessage, | ||
NLM_F_ACK, | ||
NLM_F_APPEND, | ||
NLM_F_CREATE, | ||
NLM_F_EXCL, | ||
NLM_F_REQUEST, | ||
}, | ||
Error, | ||
Handle, | ||
}; | ||
use futures::stream::StreamExt; | ||
|
||
pub struct LinkNewPropRequest { | ||
handle: Handle, | ||
message: LinkMessage, | ||
} | ||
|
||
impl LinkNewPropRequest { | ||
pub(crate) fn new(handle: Handle, index: u32) -> Self { | ||
let mut message = LinkMessage::default(); | ||
message.header.index = index; | ||
LinkNewPropRequest { handle, message } | ||
} | ||
|
||
/// Execute the request | ||
pub async fn execute(self) -> Result<(), Error> { | ||
let LinkNewPropRequest { | ||
mut handle, | ||
message, | ||
} = self; | ||
let mut req = NetlinkMessage::from(RtnlMessage::NewLinkProp(message)); | ||
req.header.flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE | NLM_F_APPEND; | ||
|
||
let mut response = handle.request(req)?; | ||
while let Some(message) = response.next().await { | ||
if let NetlinkPayload::Error(err) = message.payload { | ||
return Err(Error::NetlinkError(err)); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Return a mutable reference to the request | ||
pub fn message_mut(&mut self) -> &mut LinkMessage { | ||
&mut self.message | ||
} | ||
|
||
/// Add alternative name to the link. This is equivalent to `ip link property add altname | ||
/// ALT_IFNAME dev LINK`. | ||
pub fn alt_ifname(mut self, alt_ifnames: &[&str]) -> Self { | ||
let mut props = Vec::new(); | ||
for alt_ifname in alt_ifnames { | ||
props.push(Prop::AltIfName(alt_ifname.to_string())); | ||
} | ||
|
||
self.message.nlas.push(Nla::PropList(props)); | ||
self | ||
} | ||
} |
This file contains 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,63 @@ | ||
use crate::{ | ||
packet::{ | ||
nlas::link::{Nla, Prop}, | ||
LinkMessage, | ||
NetlinkMessage, | ||
NetlinkPayload, | ||
RtnlMessage, | ||
NLM_F_ACK, | ||
NLM_F_EXCL, | ||
NLM_F_REQUEST, | ||
}, | ||
Error, | ||
Handle, | ||
}; | ||
use futures::stream::StreamExt; | ||
|
||
pub struct LinkDelPropRequest { | ||
handle: Handle, | ||
message: LinkMessage, | ||
} | ||
|
||
impl LinkDelPropRequest { | ||
pub(crate) fn new(handle: Handle, index: u32) -> Self { | ||
let mut message = LinkMessage::default(); | ||
message.header.index = index; | ||
LinkDelPropRequest { handle, message } | ||
} | ||
|
||
/// Execute the request | ||
pub async fn execute(self) -> Result<(), Error> { | ||
let LinkDelPropRequest { | ||
mut handle, | ||
message, | ||
} = self; | ||
let mut req = NetlinkMessage::from(RtnlMessage::DelLinkProp(message)); | ||
req.header.flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL; | ||
|
||
let mut response = handle.request(req)?; | ||
while let Some(message) = response.next().await { | ||
if let NetlinkPayload::Error(err) = message.payload { | ||
return Err(Error::NetlinkError(err)); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Return a mutable reference to the request | ||
pub fn message_mut(&mut self) -> &mut LinkMessage { | ||
&mut self.message | ||
} | ||
|
||
/// Remove alternative name to the link. This is equivalent to `ip link property del altname | ||
/// ALT_IFNAME dev LINK`. | ||
pub fn alt_ifname(mut self, alt_ifnames: &[&str]) -> Self { | ||
let mut props = Vec::new(); | ||
for alt_ifname in alt_ifnames { | ||
props.push(Prop::AltIfName(alt_ifname.to_string())); | ||
} | ||
|
||
self.message.nlas.push(Nla::PropList(props)); | ||
self | ||
} | ||
} |