pub struct TcHook { /* private fields */ }
Expand description
Represents a location where a TC-BPF filter can be attached.
The BPF TC subsystem has different control paths from other BPF programs.
As such a BPF program using a TC Hook (SEC("classifier")
or SEC("tc")
) must be operated
more independently from other Program
s.
This struct exposes operations to create, attach, query and destroy a bpf_tc_hook using the TC subsystem.
Documentation about the libbpf TC interface can be found here.
An example of using a BPF TC program can found here.
Implementations§
Source§impl TcHook
impl TcHook
Sourcepub fn new(fd: BorrowedFd<'_>) -> Self
pub fn new(fd: BorrowedFd<'_>) -> Self
Sourcepub fn create(&mut self) -> Result<Self>
pub fn create(&mut self) -> Result<Self>
Create a new TcHook
as well as the underlying qdiscs
If a TcHook
already exists with the same parameters as the hook calling
Self::create()
, this function will still succeed.
Will always fail on a TC_CUSTOM
hook
Sourcepub fn ifindex(&mut self, idx: i32) -> &mut Self
pub fn ifindex(&mut self, idx: i32) -> &mut Self
Set the interface to attach to
Interfaces can be listed by using ip link
command from the iproute2 software package
Sourcepub fn attach_point(&mut self, ap: TcAttachPoint) -> &mut Self
pub fn attach_point(&mut self, ap: TcAttachPoint) -> &mut Self
Set what type of TC point to attach onto
TC_EGRESS
, TC_INGRESS
, or TC_CUSTOM
An TC_EGRESS|TC_INGRESS
hook can be used as an attach point for calling
Self::destroy()
to remove the clsact bpf tc qdisc, but cannot be used for an
Self::attach()
operation
Sourcepub fn parent(&mut self, maj: u32, min: u32) -> &mut Self
pub fn parent(&mut self, maj: u32, min: u32) -> &mut Self
Set the parent of a hook
Will cause an EINVAL upon Self::attach()
if set upon an
TC_EGRESS/TC_INGRESS/(TC_EGRESS|TC_INGRESS)
hook
Must be set on a TC_CUSTOM
hook
Current acceptable values are TC_H_CLSACT
for maj
, and TC_H_MIN_EGRESS
or
TC_H_MIN_INGRESS
for min
Sourcepub fn replace(&mut self, replace: bool) -> &mut Self
pub fn replace(&mut self, replace: bool) -> &mut Self
Set whether this hook should replace an existing hook
If replace is not true upon attach, and a hook already exists
an EEXIST error will be returned from Self::attach()
Sourcepub fn handle(&mut self, handle: u32) -> &mut Self
pub fn handle(&mut self, handle: u32) -> &mut Self
Set the handle of a hook. If unset upon attach, the kernel will assign a handle for the hook
Sourcepub fn get_handle(&self) -> u32
pub fn get_handle(&self) -> u32
Get the handle of a hook. Only has meaning after hook is attached
Sourcepub fn priority(&mut self, priority: u32) -> &mut Self
pub fn priority(&mut self, priority: u32) -> &mut Self
Set the priority of a hook If unset upon attach, the kernel will assign a priority for the hook
Sourcepub fn get_priority(&self) -> u32
pub fn get_priority(&self) -> u32
Get the priority of a hook Only has meaning after hook is attached
Sourcepub fn query(&mut self) -> Result<u32>
pub fn query(&mut self) -> Result<u32>
Query a hook to inspect the program identifier (prog_id)
Sourcepub fn attach(&mut self) -> Result<Self>
pub fn attach(&mut self) -> Result<Self>
Attach a filter to the TcHook so that the program starts processing
Once the hook is processing, changing the values will have no effect unless the hook is
Self::attach()
’d again (replace=true
being required)
Users can create a second hook by changing the handle, the priority or the attach_point and
calling the Self::attach()
method again. Beware doing this. It might be better to
Copy the TcHook and change the values on the copied hook for easier Self::detach()
NOTE: Once a TcHook
is attached, it, and the maps it uses, will outlive the userspace
application that spawned them Make sure to detach if this is not desired
Sourcepub fn destroy(&mut self) -> Result<()>
pub fn destroy(&mut self) -> Result<()>
Destroy attached filters
If called on a hook with an attach_point of TC_EGRESS
, will detach all egress hooks
If called on a hook with an attach_point of TC_INGRESS
, will detach all ingress hooks
If called on a hook with an attach_point of TC_EGRESS|TC_INGRESS
, will destroy the clsact
tc qdisc and detach all hooks
Will error with EOPNOTSUPP if attach_point is TC_CUSTOM
It is good practice to query before destroying as the tc qdisc may be used by multiple programs
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TcHook
impl RefUnwindSafe for TcHook
impl Send for TcHook
impl Sync for TcHook
impl Unpin for TcHook
impl UnwindSafe for TcHook
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)