Skip to content

Commit 6896bae

Browse files
little-dudewllenyj
authored andcommitted
update to netlink-packet-route 0.20
the main change is the switch to `bitflags` for manipulating fields that contain flags. Some integers have also been replaced by enums: - `BondMode` replaced the raw `u8` - `MacVlanMode` replaced the raw u32` - `MacVtapMode` replaced the raw u32`
1 parent 6863102 commit 6896bae

File tree

11 files changed

+56
-62
lines changed

11 files changed

+56
-62
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.19" }
26+
netlink-packet-route = { version = "0.20" }
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_bond.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: MIT
22

3+
use netlink_packet_route::link::BondMode;
34
use rtnetlink::new_connection;
45
use std::net::{Ipv4Addr, Ipv6Addr};
56

@@ -11,7 +12,7 @@ async fn main() -> Result<(), String> {
1112
.link()
1213
.add()
1314
.bond("my-bond".into())
14-
.mode(1)
15+
.mode(BondMode::ActiveBackup)
1516
.miimon(100)
1617
.updelay(100)
1718
.downdelay(100)

examples/create_macvlan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use macaddr::MacAddr;
55
use rtnetlink::{new_connection, Error, Handle};
66
use std::{env, str::FromStr};
77

8-
use netlink_packet_route::link::LinkAttribute;
8+
use netlink_packet_route::link::{LinkAttribute, MacVlanMode};
99

1010
#[tokio::main]
1111
async fn main() -> Result<(), String> {
@@ -42,7 +42,7 @@ async fn create_macvlan(
4242
let mut request = handle.link().add().macvlan(
4343
"test_macvlan".into(),
4444
link.header.index,
45-
4u32, // bridge mode
45+
MacVlanMode::Bridge,
4646
);
4747
if let Some(mac) = mac_address {
4848
request

examples/create_macvtap.rs

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

33
use futures::stream::TryStreamExt;
4+
use netlink_packet_route::link::MacVtapMode;
45
use rtnetlink::{new_connection, Error, Handle};
56
use std::env;
67

@@ -27,11 +28,10 @@ async fn create_macvtap(
2728
) -> Result<(), Error> {
2829
let mut links = handle.link().get().match_name(veth_name.clone()).execute();
2930
if let Some(link) = links.try_next().await? {
30-
// hard code mode: 4u32 i.e bridge mode
3131
let request = handle.link().add().macvtap(
3232
"test_macvtap".into(),
3333
link.header.index,
34-
4u32,
34+
MacVtapMode::Bridge,
3535
);
3636
request.execute().await?
3737
} else {

src/link/add.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use netlink_packet_core::{
1313

1414
use netlink_packet_route::{
1515
link::{
16-
InfoBond, InfoData, InfoKind, InfoMacVlan, InfoMacVtap, InfoVeth,
17-
InfoVlan, InfoVrf, InfoVxlan, InfoXfrm, LinkAttribute, LinkFlag,
18-
LinkInfo, LinkMessage, VlanQosMapping,
16+
BondMode, InfoBond, InfoData, InfoKind, InfoMacVlan, InfoMacVtap,
17+
InfoVeth, InfoVlan, InfoVrf, InfoVxlan, InfoXfrm, LinkAttribute,
18+
LinkFlags, LinkInfo, LinkMessage, MacVlanMode, MacVtapMode,
19+
VlanQosMapping,
1920
},
2021
RouteNetlinkMessage,
2122
};
@@ -45,7 +46,7 @@ impl BondAddRequest {
4546

4647
/// Adds the `mode` attribute to the bond
4748
/// This is equivalent to `ip link add name NAME type bond mode MODE`.
48-
pub fn mode(mut self, mode: u8) -> Self {
49+
pub fn mode(mut self, mode: BondMode) -> Self {
4950
self.info_data.push(InfoBond::Mode(mode));
5051
self
5152
}
@@ -442,7 +443,7 @@ impl VxlanAddRequest {
442443

443444
/// Adds the `learning` attribute to the VXLAN
444445
/// This is equivalent to `ip link add name NAME type vxlan id VNI
445-
/// [no]learning`. [no]learning - specifies if unknown source link layer
446+
/// \[no\]learning`. \[no\]learning - specifies if unknown source link layer
446447
/// addresses and IP addresses are entered into the VXLAN
447448
/// device forwarding database.
448449
pub fn learning(mut self, learning: bool) -> Self {
@@ -480,23 +481,23 @@ impl VxlanAddRequest {
480481

481482
/// Adds the `proxy` attribute to the VXLAN
482483
/// This is equivalent to `ip link add name NAME type vxlan id VNI
483-
/// [no]proxy`. [no]proxy - specifies ARP proxy is turned on.
484+
/// [no]proxy`. \[no\]proxy - specifies ARP proxy is turned on.
484485
pub fn proxy(mut self, proxy: bool) -> Self {
485486
self.info_data.push(InfoVxlan::Proxy(proxy));
486487
self
487488
}
488489

489-
/// Adds the `rsc` attribute to the VXLAN
490-
/// This is equivalent to `ip link add name NAME type vxlan id VNI [no]rsc`.
491-
/// [no]rsc - specifies if route short circuit is turned on.
490+
/// Adds the `rsc` attribute to the VXLAN This is equivalent to
491+
/// `ip link add name NAME type vxlan id VNI [no]rsc`.
492+
/// \[no\]rsc - specifies if route short circuit is turned on.
492493
pub fn rsc(mut self, rsc: bool) -> Self {
493494
self.info_data.push(InfoVxlan::Rsc(rsc));
494495
self
495496
}
496497

497498
// Adds the `l2miss` attribute to the VXLAN
498499
/// This is equivalent to `ip link add name NAME type vxlan id VNI
499-
/// [no]l2miss`. [no]l2miss - specifies if netlink LLADDR miss
500+
/// [no]l2miss`. \[no\]l2miss - specifies if netlink LLADDR miss
500501
/// notifications are generated.
501502
pub fn l2miss(mut self, l2miss: bool) -> Self {
502503
self.info_data.push(InfoVxlan::L2Miss(l2miss));
@@ -505,8 +506,8 @@ impl VxlanAddRequest {
505506

506507
// Adds the `l3miss` attribute to the VXLAN
507508
/// This is equivalent to `ip link add name NAME type vxlan id VNI
508-
/// [no]l3miss`. [no]l3miss - specifies if netlink IP ADDR miss
509-
/// notifications are generated.
509+
/// [no]l3miss`. \[no\]l3miss - specifies if netlink IP ADDR
510+
/// miss notifications are generated.
510511
pub fn l3miss(mut self, l3miss: bool) -> Self {
511512
self.info_data.push(InfoVxlan::L3Miss(l3miss));
512513
self
@@ -520,8 +521,8 @@ impl VxlanAddRequest {
520521

521522
// Adds the `udp_csum` attribute to the VXLAN
522523
/// This is equivalent to `ip link add name NAME type vxlan id VNI
523-
/// [no]udp_csum`. [no]udpcsum - specifies if UDP checksum is calculated
524-
/// for transmitted packets over IPv4.
524+
/// [no]udp_csum`. \[no\]udpcsum - specifies if UDP checksum is
525+
/// calculated for transmitted packets over IPv4.
525526
pub fn udp_csum(mut self, udp_csum: bool) -> Self {
526527
self.info_data.push(InfoVxlan::UDPCsum(udp_csum));
527528
self
@@ -588,23 +589,22 @@ impl LinkAddRequest {
588589
///
589590
/// Let's say we want to create a vlan interface on a link with id 6. By
590591
/// default, the [`vlan()`](#method.vlan) method would create a request
591-
/// with the `LinkFlag::Up` link set, so that the interface is up after
592+
/// with the `LinkFlags::Up` link set, so that the interface is up after
592593
/// creation. If we want to create a interface that is down by default we
593594
/// could do:
594595
///
595596
/// ```rust,no_run
596597
/// use futures::Future;
597-
/// use netlink_packet_route::link::LinkFlag;
598+
/// use netlink_packet_route::link::LinkFlags;
598599
/// use rtnetlink::{Handle, new_connection};
599600
///
600601
/// async fn run(handle: Handle) -> Result<(), String> {
601602
/// let vlan_id = 100;
602603
/// let link_id = 6;
603604
/// let mut request = handle.link().add().vlan("my-vlan-itf".into(),
604605
/// link_id, vlan_id);
605-
/// request.message_mut().header.flags.push(LinkFlag::Up);
606-
/// request.message_mut().header.change_mask.retain(
607-
/// |f| *f != LinkFlag::Up);
606+
/// request.message_mut().header.flags.remove(LinkFlags::Up);
607+
/// request.message_mut().header.change_mask.remove(LinkFlags::Up);
608608
/// // send the request
609609
/// request.execute().await.map_err(|e| format!("{}", e))
610610
/// }
@@ -628,8 +628,8 @@ impl LinkAddRequest {
628628

629629
let mut peer = LinkMessage::default();
630630
// FIXME: we get a -107 (ENOTCONN) (???) when trying to set `name` up.
631-
// peer.header.flags.push(LinkFlag::Up);
632-
// peer.header.change_mask.push(LinkFlag::Up);
631+
// peer.header.flags |= LinkFlags::Up;
632+
// peer.header.change_mask |= LinkFlag::Up;
633633
peer.attributes.push(LinkAttribute::IfName(name));
634634
let link_info_data = InfoData::Veth(InfoVeth::Peer(peer));
635635
self.name(peer_name)
@@ -687,7 +687,7 @@ impl LinkAddRequest {
687687
/// flags from MACVLAN_MODE (netlink-packet-route/src/rtnl/constants.rs)
688688
/// being: _PRIVATE, _VEPA, _BRIDGE, _PASSTHRU, _SOURCE, which can be
689689
/// *combined*.
690-
pub fn macvlan(self, name: String, index: u32, mode: u32) -> Self {
690+
pub fn macvlan(self, name: String, index: u32, mode: MacVlanMode) -> Self {
691691
self.name(name)
692692
.link_info(
693693
InfoKind::MacVlan,
@@ -704,7 +704,7 @@ impl LinkAddRequest {
704704
/// flags from MACVTAP_MODE (netlink-packet-route/src/rtnl/constants.rs)
705705
/// being: _PRIVATE, _VEPA, _BRIDGE, _PASSTHRU, _SOURCE, which can be
706706
/// *combined*.
707-
pub fn macvtap(self, name: String, index: u32, mode: u32) -> Self {
707+
pub fn macvtap(self, name: String, index: u32, mode: MacVtapMode) -> Self {
708708
self.name(name)
709709
.link_info(
710710
InfoKind::MacVtap,
@@ -779,11 +779,7 @@ impl LinkAddRequest {
779779
/// This is equivalent to `ip link add NAME type wireguard`.
780780
pub fn wireguard(self, name: String) -> Self {
781781
let mut request = self.name(name).link_info(InfoKind::Wireguard, None);
782-
request
783-
.message_mut()
784-
.header
785-
.flags
786-
.retain(|f| *f != LinkFlag::Up);
782+
request.message_mut().header.flags.remove(LinkFlags::Up);
787783
request
788784
}
789785

@@ -805,8 +801,8 @@ impl LinkAddRequest {
805801
}
806802

807803
fn up(mut self) -> Self {
808-
self.message.header.flags.push(LinkFlag::Up);
809-
self.message.header.change_mask.push(LinkFlag::Up);
804+
self.message.header.flags |= LinkFlags::Up;
805+
self.message.header.change_mask |= LinkFlags::Up;
810806
self
811807
}
812808

src/link/set.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use netlink_packet_core::{
77
NetlinkMessage, NLM_F_ACK, NLM_F_CREATE, NLM_F_EXCL, NLM_F_REQUEST,
88
};
99
use netlink_packet_route::{
10-
link::{LinkAttribute, LinkFlag, LinkMessage},
10+
link::{LinkAttribute, LinkFlags, LinkMessage},
1111
RouteNetlinkMessage,
1212
};
1313

@@ -105,43 +105,40 @@ impl LinkSetRequest {
105105
/// Set the link with the given index up (equivalent to `ip link set dev DEV
106106
/// up`)
107107
pub fn up(mut self) -> Self {
108-
self.message.header.flags.push(LinkFlag::Up);
109-
self.message.header.change_mask.push(LinkFlag::Up);
108+
self.message.header.flags |= LinkFlags::Up;
109+
self.message.header.change_mask |= LinkFlags::Up;
110110
self
111111
}
112112

113113
/// Set the link with the given index down (equivalent to `ip link set dev
114114
/// DEV down`)
115115
pub fn down(mut self) -> Self {
116-
self.message.header.flags.retain(|f| *f != LinkFlag::Up);
117-
self.message.header.change_mask.push(LinkFlag::Up);
116+
self.message.header.flags.remove(LinkFlags::Up);
117+
self.message.header.change_mask |= LinkFlags::Up;
118118
self
119119
}
120120

121121
/// Enable or disable promiscious mode of the link with the given index
122122
/// (equivalent to `ip link set dev DEV promisc on/off`)
123123
pub fn promiscuous(mut self, enable: bool) -> Self {
124124
if enable {
125-
self.message.header.flags.push(LinkFlag::Promisc);
125+
self.message.header.flags |= LinkFlags::Promisc;
126126
} else {
127-
self.message
128-
.header
129-
.flags
130-
.retain(|f| *f != LinkFlag::Promisc);
127+
self.message.header.flags.remove(LinkFlags::Promisc);
131128
}
132-
self.message.header.change_mask.push(LinkFlag::Promisc);
129+
self.message.header.change_mask |= LinkFlags::Promisc;
133130
self
134131
}
135132

136133
/// Enable or disable the ARP protocol of the link with the given index
137134
/// (equivalent to `ip link set dev DEV arp on/off`)
138135
pub fn arp(mut self, enable: bool) -> Self {
139136
if enable {
140-
self.message.header.flags.retain(|f| *f != LinkFlag::Noarp);
137+
self.message.header.flags.remove(LinkFlags::Noarp);
141138
} else {
142-
self.message.header.flags.push(LinkFlag::Noarp);
139+
self.message.header.flags |= LinkFlags::Noarp;
143140
}
144-
self.message.header.change_mask.push(LinkFlag::Noarp);
141+
self.message.header.change_mask |= LinkFlags::Noarp;
145142
self
146143
}
147144

src/link/set_bond_port.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl BondPortSetRequest {
5353
Ok(())
5454
}
5555

56-
/// Return a mutable reference to the Vec<InfoBondPort>
56+
/// Return a mutable reference to the `Vec<InfoBondPort>`
5757
pub fn info_port_nlas_mut(&mut self) -> &mut Vec<InfoBondPort> {
5858
&mut self.port_nlas
5959
}

src/link/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use futures::stream::TryStreamExt;
44
use netlink_packet_route::link::{
55
InfoData, InfoKind, InfoMacVlan, InfoVrf, LinkAttribute, LinkInfo,
6-
LinkMessage,
6+
LinkMessage, MacVlanMode,
77
};
88
use tokio::runtime::Runtime;
99

@@ -32,7 +32,7 @@ fn create_get_delete_wg() {
3232
fn create_get_delete_macvlan() {
3333
const MACVLAN_IFACE_NAME: &str = "mvlan1";
3434
const LOWER_DEVICE_IDX: u32 = 2;
35-
const MACVLAN_MODE: u32 = 4; // bridge
35+
const MACVLAN_MODE: MacVlanMode = MacVlanMode::Bridge;
3636
let mac_address = [2u8, 0, 0, 0, 0, 1];
3737

3838
let rt = Runtime::new().unwrap();
@@ -142,7 +142,7 @@ async fn _del_iface(handle: &mut LinkHandle, index: u32) -> Result<(), Error> {
142142
async fn _create_macvlan(
143143
name: &String,
144144
lower_device_index: u32,
145-
mode: u32,
145+
mode: MacVlanMode,
146146
mac: Vec<u8>,
147147
) -> Result<LinkHandle, Error> {
148148
let (conn, handle, _) = new_connection().unwrap();

src/neighbour/add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use netlink_packet_core::{
88
};
99
use netlink_packet_route::{
1010
neighbour::{
11-
NeighbourAddress, NeighbourAttribute, NeighbourFlag, NeighbourMessage,
11+
NeighbourAddress, NeighbourAttribute, NeighbourFlags, NeighbourMessage,
1212
NeighbourState,
1313
},
1414
route::RouteType,
@@ -80,7 +80,7 @@ impl NeighbourAddRequest {
8080

8181
/// Set flags for the neighbor cache entry.
8282
/// It should be a combination of `NTF_*` constants.
83-
pub fn flags(mut self, flags: Vec<NeighbourFlag>) -> Self {
83+
pub fn flags(mut self, flags: NeighbourFlags) -> Self {
8484
self.message.header.flags = flags;
8585
self
8686
}

src/neighbour/get.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use netlink_packet_core::{
99
NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
1010
};
1111
use netlink_packet_route::{
12-
neighbour::{NeighbourFlag, NeighbourMessage},
12+
neighbour::{NeighbourFlags, NeighbourMessage},
1313
RouteNetlinkMessage,
1414
};
1515

@@ -29,7 +29,7 @@ impl NeighbourGetRequest {
2929
/// List neighbor proxies in the system (equivalent to: `ip neighbor show
3030
/// proxy`).
3131
pub fn proxies(mut self) -> Self {
32-
self.message.header.flags.push(NeighbourFlag::Proxy);
32+
self.message.header.flags |= NeighbourFlags::Proxy;
3333
self
3434
}
3535

0 commit comments

Comments
 (0)