Skip to content

Commit 13729d5

Browse files
committed
Use netlink-packet-route 0.18.0
Signed-off-by: Gris Ge <[email protected]>
1 parent dabef43 commit 13729d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+588
-432
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rtnetlink"
3-
version = "0.13.1"
3+
version = "0.14.0"
44
authors = ["Corentin Henry <[email protected]>"]
55
edition = "2018"
66
homepage = "https://github.com/rust-netlink/rtnetlink"
@@ -23,7 +23,7 @@ log = "0.4.8"
2323
thiserror = "1"
2424
netlink-sys = { version = "0.8" }
2525
netlink-packet-utils = { version = "0.5" }
26-
netlink-packet-route = { version = "0.17" }
26+
netlink-packet-route = { version = "0.18" }
2727
netlink-packet-core = { version = "0.7" }
2828
netlink-proto = { default-features = false, version = "0.11" }
2929
nix = { version = "0.27.1", default-features = false, features = ["fs", "mount", "sched", "signal"] }

examples/create_vlan.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ use rtnetlink::{new_connection, QosMapping};
77
fn parse_mapping(parameter: &str) -> Result<QosMapping, Box<dyn StdError>> {
88
let (from, to) = parameter
99
.split_once(':')
10-
.ok_or_else(|| "Failed to parse mapping..")?;
10+
.ok_or("Failed to parse mapping..")?;
1111

1212
Ok(QosMapping {
1313
from: u32::from_str(from)?,
1414
to: u32::from_str(to)?,
1515
})
1616
}
1717

18-
const ARG_BASE: &'static str = "--base";
19-
const ARG_NAME: &'static str = "--name";
20-
const ARG_ID: &'static str = "--id";
21-
const ARG_INGRESS_QOS: &'static str = "--ingress-qos-mapping";
22-
const ARG_EGRESS_QOS: &'static str = "--egress-qos-mapping";
18+
const ARG_BASE: &str = "--base";
19+
const ARG_NAME: &str = "--name";
20+
const ARG_ID: &str = "--id";
21+
const ARG_INGRESS_QOS: &str = "--ingress-qos-mapping";
22+
const ARG_EGRESS_QOS: &str = "--egress-qos-mapping";
2323

2424
enum ParsingMode {
2525
None,
@@ -51,7 +51,7 @@ async fn main() -> Result<(), String> {
5151
ARG_EGRESS_QOS => Ok(ParsingMode::Egress),
5252
other => {
5353
usage();
54-
return Err(format!("Unexpected argument: {other}"));
54+
Err(format!("Unexpected argument: {other}"))
5555
}
5656
}
5757
}

examples/get_links.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use futures::stream::TryStreamExt;
44
use netlink_packet_route::{
5-
constants::{AF_BRIDGE, RTEXT_FILTER_BRVLAN},
6-
link::nlas::Nla,
5+
link::{LinkAttribute, LinkExtentMask},
6+
AddressFamily,
77
};
88
use rtnetlink::{new_connection, Error, Handle};
99

@@ -52,8 +52,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
5252
// We should have received only one message
5353
assert!(links.try_next().await?.is_none());
5454

55-
for nla in msg.nlas.into_iter() {
56-
if let Nla::IfName(name) = nla {
55+
for nla in msg.attributes.into_iter() {
56+
if let LinkAttribute::IfName(name) = nla {
5757
println!("found link with index {index} (name = {name})");
5858
return Ok(());
5959
}
@@ -79,8 +79,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
7979
async fn dump_links(handle: Handle) -> Result<(), Error> {
8080
let mut links = handle.link().get().execute();
8181
'outer: while let Some(msg) = links.try_next().await? {
82-
for nla in msg.nlas.into_iter() {
83-
if let Nla::IfName(name) = nla {
82+
for nla in msg.attributes.into_iter() {
83+
if let LinkAttribute::IfName(name) = nla {
8484
println!("found link {} ({})", msg.header.index, name);
8585
continue 'outer;
8686
}
@@ -94,11 +94,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
9494
let mut links = handle
9595
.link()
9696
.get()
97-
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
97+
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
9898
.execute();
9999
'outer: while let Some(msg) = links.try_next().await? {
100-
for nla in msg.nlas.into_iter() {
101-
if let Nla::AfSpecBridge(data) = nla {
100+
for nla in msg.attributes.into_iter() {
101+
if let LinkAttribute::AfSpecBridge(data) = nla {
102102
println!(
103103
"found interface {} with AfSpecBridge data {:?})",
104104
msg.header.index, data

examples/get_links_async.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use futures::stream::TryStreamExt;
44
use netlink_packet_route::{
5-
constants::{AF_BRIDGE, RTEXT_FILTER_BRVLAN},
6-
link::nlas::Nla,
5+
link::{LinkAttribute, LinkExtentMask},
6+
AddressFamily,
77
};
88
use rtnetlink::{new_connection, Error, Handle};
99

@@ -52,8 +52,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
5252
// We should have received only one message
5353
assert!(links.try_next().await?.is_none());
5454

55-
for nla in msg.nlas.into_iter() {
56-
if let Nla::IfName(name) = nla {
55+
for nla in msg.attributes.into_iter() {
56+
if let LinkAttribute::IfName(name) = nla {
5757
println!("found link with index {index} (name = {name})");
5858
return Ok(());
5959
}
@@ -79,8 +79,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
7979
async fn dump_links(handle: Handle) -> Result<(), Error> {
8080
let mut links = handle.link().get().execute();
8181
'outer: while let Some(msg) = links.try_next().await? {
82-
for nla in msg.nlas.into_iter() {
83-
if let Nla::IfName(name) = nla {
82+
for nla in msg.attributes.into_iter() {
83+
if let LinkAttribute::IfName(name) = nla {
8484
println!("found link {} ({})", msg.header.index, name);
8585
continue 'outer;
8686
}
@@ -94,11 +94,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
9494
let mut links = handle
9595
.link()
9696
.get()
97-
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
97+
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
9898
.execute();
9999
'outer: while let Some(msg) = links.try_next().await? {
100-
for nla in msg.nlas.into_iter() {
101-
if let Nla::AfSpecBridge(data) = nla {
100+
for nla in msg.attributes.into_iter() {
101+
if let LinkAttribute::AfSpecBridge(data) = nla {
102102
println!(
103103
"found interface {} with AfSpecBridge data {:?})",
104104
msg.header.index, data

examples/get_links_thread_builder.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// SPDX-License-Identifier: MIT
22

33
use futures::stream::TryStreamExt;
4-
use netlink_packet_route::{link::nlas::Nla, AF_BRIDGE, RTEXT_FILTER_BRVLAN};
4+
use netlink_packet_route::{
5+
link::{LinkAttribute, LinkExtentMask},
6+
AddressFamily,
7+
};
58
use rtnetlink::{new_connection, Error, Handle};
69

710
async fn do_it(rt: &tokio::runtime::Runtime) -> Result<(), ()> {
@@ -48,8 +51,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
4851
// We should have received only one message
4952
assert!(links.try_next().await?.is_none());
5053

51-
for nla in msg.nlas.into_iter() {
52-
if let Nla::IfName(name) = nla {
54+
for nla in msg.attributes.into_iter() {
55+
if let LinkAttribute::IfName(name) = nla {
5356
println!("found link with index {index} (name = {name})");
5457
return Ok(());
5558
}
@@ -75,8 +78,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
7578
async fn dump_links(handle: Handle) -> Result<(), Error> {
7679
let mut links = handle.link().get().execute();
7780
'outer: while let Some(msg) = links.try_next().await? {
78-
for nla in msg.nlas.into_iter() {
79-
if let Nla::IfName(name) = nla {
81+
for nla in msg.attributes.into_iter() {
82+
if let LinkAttribute::IfName(name) = nla {
8083
println!("found link {} ({})", msg.header.index, name);
8184
continue 'outer;
8285
}
@@ -90,11 +93,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
9093
let mut links = handle
9194
.link()
9295
.get()
93-
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
96+
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
9497
.execute();
9598
'outer: while let Some(msg) = links.try_next().await? {
96-
for nla in msg.nlas.into_iter() {
97-
if let Nla::AfSpecBridge(data) = nla {
99+
for nla in msg.attributes.into_iter() {
100+
if let LinkAttribute::AfSpecBridge(data) = nla {
98101
println!(
99102
"found interface {} with AfSpecBridge data {:?})",
100103
msg.header.index, data

examples/ip_monitor.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22

33
use futures::stream::StreamExt;
44

5-
use netlink_packet_route::constants::*;
65
use netlink_sys::{AsyncSocket, SocketAddr};
76
use rtnetlink::new_connection;
87

8+
const RTNLGRP_LINK: u32 = 1;
9+
const RTNLGRP_NEIGH: u32 = 3;
10+
const RTNLGRP_IPV4_IFADDR: u32 = 5;
11+
const RTNLGRP_IPV4_MROUTE: u32 = 6;
12+
const RTNLGRP_IPV4_ROUTE: u32 = 7;
13+
const RTNLGRP_IPV4_RULE: u32 = 8;
14+
const RTNLGRP_IPV6_IFADDR: u32 = 9;
15+
const RTNLGRP_IPV6_MROUTE: u32 = 10;
16+
const RTNLGRP_IPV6_ROUTE: u32 = 11;
17+
const RTNLGRP_IPV6_RULE: u32 = 19;
18+
const RTNLGRP_IPV4_NETCONF: u32 = 24;
19+
const RTNLGRP_IPV6_NETCONF: u32 = 25;
20+
const RTNLGRP_MPLS_ROUTE: u32 = 27;
21+
const RTNLGRP_NSID: u32 = 28;
22+
const RTNLGRP_MPLS_NETCONF: u32 = 29;
23+
924
const fn nl_mgrp(group: u32) -> u32 {
1025
if group > 31 {
1126
panic!("use netlink_sys::Socket::add_membership() for this group");

examples/property_altname.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// SPDX-License-Identifier: MIT
22

33
use futures::stream::TryStreamExt;
4-
use netlink_packet_route::{
5-
link::nlas::{Nla, Prop},
6-
LinkMessage,
7-
};
4+
use netlink_packet_route::link::{LinkAttribute, LinkMessage, Prop};
85
use rtnetlink::{new_connection, Error, Handle};
96
use std::env;
107

@@ -66,8 +63,8 @@ async fn show_property_alt_ifnames(
6663
link_name: &str,
6764
handle: Handle,
6865
) -> Result<(), Error> {
69-
for nla in get_link(link_name, handle).await?.nlas.into_iter() {
70-
if let Nla::PropList(ref prop_list) = nla {
66+
for nla in get_link(link_name, handle).await?.attributes.into_iter() {
67+
if let LinkAttribute::PropList(ref prop_list) = nla {
7168
for prop in prop_list {
7269
if let Prop::AltIfName(altname) = prop {
7370
println!("altname: {altname}");

src/addr/add.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use netlink_packet_core::{
99
};
1010

1111
use netlink_packet_route::{
12-
nlas::address::Nla, AddressMessage, RtnlMessage, AF_INET, AF_INET6,
12+
address::{AddressAttribute, AddressMessage},
13+
AddressFamily, RouteNetlinkMessage,
1314
};
1415

1516
use crate::{try_nl, Error, Handle};
@@ -34,46 +35,35 @@ impl AddressAddRequest {
3435
message.header.prefix_len = prefix_len;
3536
message.header.index = index;
3637

37-
let address_vec = match address {
38-
IpAddr::V4(ipv4) => {
39-
message.header.family = AF_INET as u8;
40-
ipv4.octets().to_vec()
41-
}
42-
IpAddr::V6(ipv6) => {
43-
message.header.family = AF_INET6 as u8;
44-
ipv6.octets().to_vec()
45-
}
38+
message.header.family = match address {
39+
IpAddr::V4(_) => AddressFamily::Inet,
40+
IpAddr::V6(_) => AddressFamily::Inet6,
4641
};
4742

4843
if address.is_multicast() {
49-
message.nlas.push(Nla::Multicast(address_vec));
50-
} else if address.is_unspecified() {
51-
message.nlas.push(Nla::Unspec(address_vec));
52-
} else if address.is_ipv6() {
53-
message.nlas.push(Nla::Address(address_vec));
44+
if let IpAddr::V6(a) = address {
45+
message.attributes.push(AddressAttribute::Multicast(a));
46+
}
5447
} else {
55-
message.nlas.push(Nla::Address(address_vec.clone()));
48+
message.attributes.push(AddressAttribute::Address(address));
5649

5750
// for IPv4 the IFA_LOCAL address can be set to the same value as
5851
// IFA_ADDRESS
59-
message.nlas.push(Nla::Local(address_vec.clone()));
52+
message.attributes.push(AddressAttribute::Local(address));
6053

6154
// set the IFA_BROADCAST address as well (IPv6 does not support
6255
// broadcast)
63-
if prefix_len == 32 {
64-
message.nlas.push(Nla::Broadcast(address_vec));
65-
} else {
66-
let ip_addr: u32 = u32::from(Ipv4Addr::new(
67-
address_vec[0],
68-
address_vec[1],
69-
address_vec[2],
70-
address_vec[3],
71-
));
72-
let brd = Ipv4Addr::from(
73-
(0xffff_ffff_u32) >> u32::from(prefix_len) | ip_addr,
74-
);
75-
message.nlas.push(Nla::Broadcast(brd.octets().to_vec()));
76-
};
56+
if let IpAddr::V4(a) = address {
57+
if prefix_len == 32 {
58+
message.attributes.push(AddressAttribute::Broadcast(a));
59+
} else {
60+
let ip_addr = u32::from(a);
61+
let brd = Ipv4Addr::from(
62+
(0xffff_ffff_u32) >> u32::from(prefix_len) | ip_addr,
63+
);
64+
message.attributes.push(AddressAttribute::Broadcast(brd));
65+
};
66+
}
7767
}
7868
AddressAddRequest {
7969
handle,
@@ -97,7 +87,8 @@ impl AddressAddRequest {
9787
message,
9888
replace,
9989
} = self;
100-
let mut req = NetlinkMessage::from(RtnlMessage::NewAddress(message));
90+
let mut req =
91+
NetlinkMessage::from(RouteNetlinkMessage::NewAddress(message));
10192
let replace = if replace { NLM_F_REPLACE } else { NLM_F_EXCL };
10293
req.header.flags = NLM_F_REQUEST | NLM_F_ACK | replace | NLM_F_CREATE;
10394

src/addr/del.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use futures::stream::StreamExt;
44
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
5-
use netlink_packet_route::{AddressMessage, RtnlMessage};
5+
use netlink_packet_route::{address::AddressMessage, RouteNetlinkMessage};
66

77
use crate::{try_nl, Error, Handle};
88

@@ -23,7 +23,8 @@ impl AddressDelRequest {
2323
message,
2424
} = self;
2525

26-
let mut req = NetlinkMessage::from(RtnlMessage::DelAddress(message));
26+
let mut req =
27+
NetlinkMessage::from(RouteNetlinkMessage::DelAddress(message));
2728
req.header.flags = NLM_F_REQUEST | NLM_F_ACK;
2829
let mut response = handle.request(req)?;
2930
while let Some(msg) = response.next().await {

0 commit comments

Comments
 (0)