From fe032078b703af08a8e0e2bba6be7f46b0b6d038 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Mon, 18 Dec 2023 11:53:30 +0100 Subject: [PATCH 1/3] Do not activate network namespaces and traffic control on FreeBSD. Not supported there. Signed-off-by: Yann Dirson --- src/handle.rs | 12 +++++++++--- src/lib.rs | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/handle.rs b/src/handle.rs index 1249ee9..08de353 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -6,9 +6,11 @@ use netlink_packet_route::RouteNetlinkMessage; use netlink_proto::{sys::SocketAddr, ConnectionHandle}; use crate::{ - AddressHandle, Error, LinkHandle, NeighbourHandle, QDiscHandle, - RouteHandle, RuleHandle, TrafficChainHandle, TrafficClassHandle, - TrafficFilterHandle, + AddressHandle, Error, LinkHandle, NeighbourHandle, RouteHandle, RuleHandle, +}; +#[cfg(not(target_os = "freebsd"))] +use crate::{ + QDiscHandle, TrafficChainHandle, TrafficClassHandle, TrafficFilterHandle, }; #[derive(Clone, Debug)] @@ -71,24 +73,28 @@ impl Handle { /// Create a new handle, specifically for traffic control qdisc requests /// (equivalent to `tc qdisc show` commands) + #[cfg(not(target_os = "freebsd"))] pub fn qdisc(&self) -> QDiscHandle { QDiscHandle::new(self.clone()) } /// Create a new handle, specifically for traffic control class requests /// (equivalent to `tc class show dev ` commands) + #[cfg(not(target_os = "freebsd"))] pub fn traffic_class(&self, ifindex: i32) -> TrafficClassHandle { TrafficClassHandle::new(self.clone(), ifindex) } /// Create a new handle, specifically for traffic control filter requests /// (equivalent to `tc filter show dev ` commands) + #[cfg(not(target_os = "freebsd"))] pub fn traffic_filter(&self, ifindex: i32) -> TrafficFilterHandle { TrafficFilterHandle::new(self.clone(), ifindex) } /// Create a new handle, specifically for traffic control chain requests /// (equivalent to `tc chain show dev ` commands) + #[cfg(not(target_os = "freebsd"))] pub fn traffic_chain(&self, ifindex: i32) -> TrafficChainHandle { TrafficChainHandle::new(self.clone(), ifindex) } diff --git a/src/lib.rs b/src/lib.rs index e8370cd..c9053df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,9 @@ mod handle; pub use crate::handle::*; +#[cfg(not(target_os = "freebsd"))] mod ns; +#[cfg(not(target_os = "freebsd"))] pub use crate::ns::*; mod errors; @@ -29,7 +31,9 @@ pub use crate::rule::*; mod connection; pub use crate::connection::*; +#[cfg(not(target_os = "freebsd"))] mod traffic_control; +#[cfg(not(target_os = "freebsd"))] pub use crate::traffic_control::*; mod neighbour; From d8875637896e04e6f32c2d9ce1c2148f0914023c Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Mon, 18 Dec 2023 12:16:38 +0100 Subject: [PATCH 2/3] Do not activate AddressFamily::Bridge on FreeBSD Not supported there. Signed-off-by: Yann Dirson --- src/neighbour/add.rs | 1 + src/neighbour/handle.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/neighbour/add.rs b/src/neighbour/add.rs index 55abf22..09b06a5 100644 --- a/src/neighbour/add.rs +++ b/src/neighbour/add.rs @@ -51,6 +51,7 @@ impl NeighbourAddRequest { } } + #[cfg(not(target_os = "freebsd"))] pub(crate) fn new_bridge(handle: Handle, index: u32, lla: &[u8]) -> Self { let mut message = NeighbourMessage::default(); diff --git a/src/neighbour/handle.rs b/src/neighbour/handle.rs index 15da656..1b9b22a 100644 --- a/src/neighbour/handle.rs +++ b/src/neighbour/handle.rs @@ -23,6 +23,7 @@ impl NeighbourHandle { NeighbourAddRequest::new(self.0.clone(), index, destination) } + #[cfg(not(target_os = "freebsd"))] /// Add a new fdb entry (equivalent to `bridge fdb add`) pub fn add_bridge(&self, index: u32, lla: &[u8]) -> NeighbourAddRequest { NeighbourAddRequest::new_bridge(self.0.clone(), index, lla) From cb07811eede9cf7941aef8e4553507dbe9d16a3d Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Wed, 10 Jan 2024 15:05:06 +0100 Subject: [PATCH 3/3] Avoid build failures of unsupported examples on freebsd I'm not really happy with the readability, there ought to be a real way to mark an example as "non applicable in cfg so and so". Signed-off-by: Yann Dirson --- examples/add_netns.rs | 5 +++++ examples/add_netns_async.rs | 5 +++++ examples/add_tc_qdisc_ingress.rs | 4 ++++ examples/del_netns.rs | 5 +++++ examples/del_netns_async.rs | 5 +++++ examples/get_links.rs | 5 +++++ examples/get_links_async.rs | 5 +++++ examples/get_links_thread_builder.rs | 10 +++++++++- 8 files changed, 43 insertions(+), 1 deletion(-) diff --git a/examples/add_netns.rs b/examples/add_netns.rs index ddac000..9f4f4a2 100644 --- a/examples/add_netns.rs +++ b/examples/add_netns.rs @@ -1,8 +1,13 @@ // SPDX-License-Identifier: MIT +#[cfg(not(target_os = "freebsd"))] use rtnetlink::NetworkNamespace; use std::env; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[tokio::main] async fn main() -> Result<(), String> { env_logger::init(); diff --git a/examples/add_netns_async.rs b/examples/add_netns_async.rs index 3014e90..8dbedcd 100644 --- a/examples/add_netns_async.rs +++ b/examples/add_netns_async.rs @@ -1,8 +1,13 @@ // SPDX-License-Identifier: MIT +#[cfg(not(target_os = "freebsd"))] use rtnetlink::NetworkNamespace; use std::env; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[async_std::main] async fn main() -> Result<(), String> { env_logger::init(); diff --git a/examples/add_tc_qdisc_ingress.rs b/examples/add_tc_qdisc_ingress.rs index ebb6a75..186a3be 100644 --- a/examples/add_tc_qdisc_ingress.rs +++ b/examples/add_tc_qdisc_ingress.rs @@ -4,6 +4,10 @@ use std::env; use rtnetlink::new_connection; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[tokio::main] async fn main() -> Result<(), ()> { env_logger::init(); diff --git a/examples/del_netns.rs b/examples/del_netns.rs index 55a13df..1d147c3 100644 --- a/examples/del_netns.rs +++ b/examples/del_netns.rs @@ -1,8 +1,13 @@ // SPDX-License-Identifier: MIT +#[cfg(not(target_os = "freebsd"))] use rtnetlink::NetworkNamespace; use std::env; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[tokio::main] async fn main() -> Result<(), String> { let args: Vec = env::args().collect(); diff --git a/examples/del_netns_async.rs b/examples/del_netns_async.rs index 6f105b4..59da114 100644 --- a/examples/del_netns_async.rs +++ b/examples/del_netns_async.rs @@ -1,8 +1,13 @@ // SPDX-License-Identifier: MIT +#[cfg(not(target_os = "freebsd"))] use rtnetlink::NetworkNamespace; use std::env; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[async_std::main] async fn main() -> Result<(), String> { let args: Vec = env::args().collect(); diff --git a/examples/get_links.rs b/examples/get_links.rs index 9a33a9c..09a42a8 100644 --- a/examples/get_links.rs +++ b/examples/get_links.rs @@ -7,6 +7,10 @@ use netlink_packet_route::{ }; use rtnetlink::{new_connection, Error, Handle}; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[tokio::main] async fn main() -> Result<(), ()> { env_logger::init(); @@ -90,6 +94,7 @@ async fn dump_links(handle: Handle) -> Result<(), Error> { Ok(()) } +#[cfg(not(target_os = "freebsd"))] async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> { let mut links = handle .link() diff --git a/examples/get_links_async.rs b/examples/get_links_async.rs index 33b951a..e6986a8 100644 --- a/examples/get_links_async.rs +++ b/examples/get_links_async.rs @@ -7,6 +7,10 @@ use netlink_packet_route::{ }; use rtnetlink::{new_connection, Error, Handle}; +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] #[async_std::main] async fn main() -> Result<(), ()> { env_logger::init(); @@ -90,6 +94,7 @@ async fn dump_links(handle: Handle) -> Result<(), Error> { Ok(()) } +#[cfg(not(target_os = "freebsd"))] async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> { let mut links = handle .link() diff --git a/examples/get_links_thread_builder.rs b/examples/get_links_thread_builder.rs index 41669b1..9c82e4c 100644 --- a/examples/get_links_thread_builder.rs +++ b/examples/get_links_thread_builder.rs @@ -5,8 +5,11 @@ use netlink_packet_route::{ link::{LinkAttribute, LinkExtentMask}, AddressFamily, }; -use rtnetlink::{new_connection, Error, Handle}; +#[cfg(not(target_os = "freebsd"))] +use rtnetlink::new_connection; +use rtnetlink::{Error, Handle}; +#[cfg(not(target_os = "freebsd"))] async fn do_it(rt: &tokio::runtime::Runtime) -> Result<(), ()> { env_logger::init(); let (connection, handle, _) = new_connection().unwrap(); @@ -89,6 +92,7 @@ async fn dump_links(handle: Handle) -> Result<(), Error> { Ok(()) } +#[cfg(not(target_os = "freebsd"))] async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> { let mut links = handle .link() @@ -109,6 +113,10 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> { Ok(()) } +#[cfg(target_os = "freebsd")] +fn main() -> () {} + +#[cfg(not(target_os = "freebsd"))] fn main() -> Result<(), String> { let rt = tokio::runtime::Builder::new_multi_thread() .enable_io()