Use a struct for setting priority and handle in SchedClassfier attach

Use a struct called TcOptions for setting priority and handle in SchedClassifier attach

struct TcOptions implements the Default trait, so for the simple use
case in which the defaults are acceptable, we can call attach as
follows:
attach(“eth0”, TcAttachType::Ingress, TcOptions::default())

To specify all options:
attach(“eth0”, TcAttachType::Ingress, TcOptions { priority: (50), handle: (3) })

Or, some options:
attach(“eth0”, TcAttachType::Ingress, TcOptions { priority: (50), ..Default::default() })

Signed-off-by: Andre Fredette <afredette@redhat.com>
pull/418/head
Andre Fredette 2 years ago
parent ac07608b79
commit af3de84b08

@ -55,6 +55,7 @@ pub enum TcAttachType {
/// # Bpf(#[from] aya::BpfError)
/// # }
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::programs::tc::TcOptions;
/// use aya::programs::{tc, SchedClassifier, TcAttachType};
///
/// // the clsact qdisc needs to be added before SchedClassifier programs can be
@ -63,7 +64,7 @@ pub enum TcAttachType {
///
/// let prog: &mut SchedClassifier = bpf.program_mut("redirect_ingress").unwrap().try_into()?;
/// prog.load()?;
/// prog.attach("eth0", TcAttachType::Ingress, 0, 0)?;
/// prog.attach("eth0", TcAttachType::Ingress, TcOptions::default())?;
///
/// # Ok::<(), Error>(())
/// ```
@ -99,6 +100,17 @@ impl TcAttachType {
}
}
/// Options for SchedClassifier attach
#[derive(Default)]
pub struct TcOptions {
/// `priority`: priority assigned to tc program with lower number = higher priority.
/// If set to default (0), the system chooses the next highest priority or 49152 if no filters exist yet
pub priority: u16,
/// `handle`: used to uniquely identify a program at a given priority level.
/// If set to default (0), the system chooses a handle.
pub handle: u32,
}
impl SchedClassifier {
/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
@ -107,13 +119,6 @@ impl SchedClassifier {
/// Attaches the program to the given `interface`.
///
/// Valid priority values range from 0 - 65535 with lower number = higher priority.
/// 0 means let the system choose the next highest priority, or 49152 if no filters exist yet.
/// All other values in the range are taken as an explicit priority setting (aka "preference").
///
/// `handle` is used to uniquely identify a program at a given priority level.
/// If set to 0, the system will choose a handle.
///
/// The returned value can be used to detach, see [SchedClassifier::detach].
///
/// # Errors
@ -126,8 +131,7 @@ impl SchedClassifier {
&mut self,
interface: &str,
attach_type: TcAttachType,
priority: u16,
handle: u32,
options: TcOptions,
) -> Result<SchedClassifierLinkId, ProgramError> {
let prog_fd = self.data.fd_or_err()?;
let if_index = ifindex_from_ifname(interface)
@ -138,8 +142,8 @@ impl SchedClassifier {
&attach_type,
prog_fd,
&self.name,
priority,
handle,
options.priority,
options.handle,
)
}
.map_err(|io_error| TcError::NetlinkError { io_error })?;

Loading…
Cancel
Save