Skip to content

Commit eb9aa03

Browse files
committed
netlink: avoid some allocations
1 parent 9e262e0 commit eb9aa03

File tree

2 files changed

+167
-144
lines changed

2 files changed

+167
-144
lines changed

aya/src/programs/tc.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
//! Network traffic control programs.
2-
use std::{
3-
ffi::{CStr, CString},
4-
io,
5-
os::fd::AsFd as _,
6-
path::Path,
7-
};
2+
use std::{ffi::CString, io, os::fd::AsFd as _, path::Path};
83

94
use aya_obj::generated::{
105
TC_H_CLSACT, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS,
@@ -22,7 +17,7 @@ use crate::{
2217
id_as_key, impl_try_into_fdlink, load_program, query,
2318
},
2419
sys::{
25-
BpfLinkCreateArgs, LinkTarget, NetlinkError, ProgQueryTarget, SyscallError,
20+
BpfLinkCreateArgs, LinkTarget, NetlinkError, NetlinkSocket, ProgQueryTarget, SyscallError,
2621
bpf_link_create, bpf_link_get_info_by_fd, bpf_link_update, bpf_prog_get_fd_by_id,
2722
netlink_find_filter_with_name, netlink_qdisc_add_clsact, netlink_qdisc_attach,
2823
netlink_qdisc_detach,
@@ -610,30 +605,16 @@ pub fn qdisc_detach_program(
610605
name: &str,
611606
) -> Result<(), TcError> {
612607
let cstr = CString::new(name).map_err(TcError::NulError)?;
613-
qdisc_detach_program_fast(if_name, attach_type, &cstr)
614-
}
615-
616-
/// Detaches the programs with the given name as a C string.
617-
/// Unlike [`qdisc_detach_program`], this function does not allocate an additional
618-
/// [`CString`] to store the name.
619-
///
620-
/// # Errors
621-
///
622-
/// Returns [`io::ErrorKind::NotFound`] to indicate that no programs with the
623-
/// given name were found, so nothing was detached. Other error kinds indicate
624-
/// an actual failure while detaching a program.
625-
fn qdisc_detach_program_fast(
626-
if_name: &str,
627-
attach_type: TcAttachType,
628-
name: &CStr,
629-
) -> Result<(), TcError> {
630608
let if_index = ifindex_from_ifname(if_name)? as i32;
631609

632-
let filter_info = unsafe { netlink_find_filter_with_name(if_index, attach_type, name)? };
610+
let sock = NetlinkSocket::open().map_err(NetlinkError::from)?;
611+
let filter_info = netlink_find_filter_with_name(&sock, if_index, attach_type, &cstr)?;
612+
// Check for errors before detaching any programs.
613+
let filter_info: Vec<_> = filter_info.collect::<Result<_, _>>()?;
633614
if filter_info.is_empty() {
634615
return Err(TcError::IoError(io::Error::new(
635616
io::ErrorKind::NotFound,
636-
name.to_string_lossy(),
617+
name.to_owned(),
637618
)));
638619
}
639620

0 commit comments

Comments
 (0)