Selaa lähdekoodia

Support reconstruction of `SchedClassifierLink`

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 <afredette@redhat.com>
Andre Fredette 2 vuotta sitten
vanhempi
commit
f46fd17
1 muutettua tiedostoa jossa 27 lisäystä ja 0 poistoa
  1. 27 0
      aya/src/programs/tc.rs

+ 27 - 0
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<SchedClassifierLink, ProgramError> {
+        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`]