From f46fd17cc31f7f900f01d8e97baebe6445c468d4 Mon Sep 17 00:00:00 2001 From: Andre Fredette Date: Sun, 23 Oct 2022 18:33:59 -0400 Subject: [PATCH] Support reconstruction of `SchedClassifierLink` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the proposed solution for Step 2 of issue #414 “For cases where you have done program.take_link() to manage ownership of TcLink we need an API similar to PinnedLink::from_pin that can reconstruct a TcLink” As long as a user application continues to run after executing `take_link()`, the `SchedClassifierLink` returned can be used to detach the program. However, if we want to handle cases where the application exits or crashes, we need a way to save and reconstruct the link, and to do that, we also need to know the information required for the reconstruction -- namely, the `interface`, `attach_type`, `priority`, and `handle`. The user knows the first two because they are required to execute `attach()` in the first place; however, the user will not know the others if they let the system choose them. This pr solves the problems by adding an `impl` for `SchedClassifierLink` with an accessor for `tc_options` and a `new()` function. Signed-off-by: Andre Fredette --- aya/src/programs/tc.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index d1e90be1..6ff044dc 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -228,6 +228,33 @@ define_link_wrapper!( TcLinkId ); +impl SchedClassifierLink { + /// Creates a new SchedClassifierLink instance + pub fn new( + interface: &str, + attach_type: TcAttachType, + priority: u16, + handle: u32, + ) -> Result { + let if_index = ifindex_from_ifname(interface) + .map_err(|io_error| TcError::NetlinkError { io_error })?; + Ok(SchedClassifierLink(TcLink { + if_index: if_index as i32, + attach_type, + priority, + handle, + })) + } + + /// Returns options for a SchedClassifierLink + pub fn tc_options(&self) -> TcOptions { + TcOptions { + priority: self.0.priority, + handle: self.0.handle, + } + } +} + /// Add the `clasct` qdisc to the given interface. /// /// The `clsact` qdisc must be added to an interface before [`SchedClassifier`]