Skip to content

Commit

Permalink
Merge pull request #1023 from l2dy/fdlink/sockops
Browse files Browse the repository at this point in the history
aya: use FdLink in SockOps programs
  • Loading branch information
alessandrod authored and Billy99 committed Sep 11, 2024
2 parents 02d1db5 + c44f8b0 commit c33a061
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
65 changes: 59 additions & 6 deletions aya/src/programs/sock_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use std::os::fd::AsFd;
use crate::{
generated::{bpf_attach_type::BPF_CGROUP_SOCK_OPS, bpf_prog_type::BPF_PROG_TYPE_SOCK_OPS},
programs::{
define_link_wrapper, load_program, CgroupAttachMode, ProgAttachLink, ProgAttachLinkId,
define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink,
ProgramData, ProgramError,
},
sys::{bpf_link_create, LinkTarget, SyscallError},
util::KernelVersion,
};

/// A program used to work with sockets.
Expand Down Expand Up @@ -63,10 +65,31 @@ impl SockOps {
mode: CgroupAttachMode,
) -> Result<SockOpsLinkId, ProgramError> {
let prog_fd = self.fd()?;
let prog_fd = prog_fd.as_fd();
let cgroup_fd = cgroup.as_fd();
let attach_type = BPF_CGROUP_SOCK_OPS;
if KernelVersion::current().unwrap() >= KernelVersion::new(5, 7, 0) {
let link_fd = bpf_link_create(
prog_fd,
LinkTarget::Fd(cgroup_fd),
attach_type,
None,
mode.into(),
)
.map_err(|(_, io_error)| SyscallError {
call: "bpf_link_create",
io_error,
})?;
self.data
.links
.insert(SockOpsLink::new(SockOpsLinkInner::Fd(FdLink::new(link_fd))))
} else {
let link = ProgAttachLink::attach(prog_fd, cgroup_fd, attach_type, mode)?;

let link =
ProgAttachLink::attach(prog_fd.as_fd(), cgroup.as_fd(), BPF_CGROUP_SOCK_OPS, mode)?;
self.data.links.insert(SockOpsLink::new(link))
self.data
.links
.insert(SockOpsLink::new(SockOpsLinkInner::ProgAttach(link)))
}
}

/// Detaches the program.
Expand All @@ -85,11 +108,41 @@ impl SockOps {
}
}

#[derive(Debug, Hash, Eq, PartialEq)]
enum SockOpsLinkIdInner {
Fd(<FdLink as Link>::Id),
ProgAttach(<ProgAttachLink as Link>::Id),
}

#[derive(Debug)]
enum SockOpsLinkInner {
Fd(FdLink),
ProgAttach(ProgAttachLink),
}

impl Link for SockOpsLinkInner {
type Id = SockOpsLinkIdInner;

fn id(&self) -> Self::Id {
match self {
Self::Fd(fd) => SockOpsLinkIdInner::Fd(fd.id()),
Self::ProgAttach(p) => SockOpsLinkIdInner::ProgAttach(p.id()),
}
}

fn detach(self) -> Result<(), ProgramError> {
match self {
Self::Fd(fd) => fd.detach(),
Self::ProgAttach(p) => p.detach(),
}
}
}

define_link_wrapper!(
/// The link used by [SockOps] programs.
SockOpsLink,
/// The type returned by [SockOps::attach]. Can be passed to [SockOps::detach].
SockOpsLinkId,
ProgAttachLink,
ProgAttachLinkId
SockOpsLinkInner,
SockOpsLinkIdInner
);
8 changes: 0 additions & 8 deletions xtask/public-api/aya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4048,14 +4048,10 @@ impl core::convert::From<aya::programs::links::ProgAttachLink> for aya::programs
pub fn aya::programs::sk_msg::SkMsgLink::from(b: aya::programs::links::ProgAttachLink) -> aya::programs::sk_msg::SkMsgLink
impl core::convert::From<aya::programs::links::ProgAttachLink> for aya::programs::sk_skb::SkSkbLink
pub fn aya::programs::sk_skb::SkSkbLink::from(b: aya::programs::links::ProgAttachLink) -> aya::programs::sk_skb::SkSkbLink
impl core::convert::From<aya::programs::links::ProgAttachLink> for aya::programs::sock_ops::SockOpsLink
pub fn aya::programs::sock_ops::SockOpsLink::from(b: aya::programs::links::ProgAttachLink) -> aya::programs::sock_ops::SockOpsLink
impl core::convert::From<aya::programs::sk_msg::SkMsgLink> for aya::programs::links::ProgAttachLink
pub fn aya::programs::links::ProgAttachLink::from(w: aya::programs::sk_msg::SkMsgLink) -> aya::programs::links::ProgAttachLink
impl core::convert::From<aya::programs::sk_skb::SkSkbLink> for aya::programs::links::ProgAttachLink
pub fn aya::programs::links::ProgAttachLink::from(w: aya::programs::sk_skb::SkSkbLink) -> aya::programs::links::ProgAttachLink
impl core::convert::From<aya::programs::sock_ops::SockOpsLink> for aya::programs::links::ProgAttachLink
pub fn aya::programs::links::ProgAttachLink::from(w: aya::programs::sock_ops::SockOpsLink) -> aya::programs::links::ProgAttachLink
impl core::fmt::Debug for aya::programs::links::ProgAttachLink
pub fn aya::programs::links::ProgAttachLink::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::marker::Freeze for aya::programs::links::ProgAttachLink
Expand Down Expand Up @@ -5343,10 +5339,6 @@ impl aya::programs::links::Link for aya::programs::sock_ops::SockOpsLink
pub type aya::programs::sock_ops::SockOpsLink::Id = aya::programs::sock_ops::SockOpsLinkId
pub fn aya::programs::sock_ops::SockOpsLink::detach(self) -> core::result::Result<(), aya::programs::ProgramError>
pub fn aya::programs::sock_ops::SockOpsLink::id(&self) -> Self::Id
impl core::convert::From<aya::programs::links::ProgAttachLink> for aya::programs::sock_ops::SockOpsLink
pub fn aya::programs::sock_ops::SockOpsLink::from(b: aya::programs::links::ProgAttachLink) -> aya::programs::sock_ops::SockOpsLink
impl core::convert::From<aya::programs::sock_ops::SockOpsLink> for aya::programs::links::ProgAttachLink
pub fn aya::programs::links::ProgAttachLink::from(w: aya::programs::sock_ops::SockOpsLink) -> aya::programs::links::ProgAttachLink
impl core::fmt::Debug for aya::programs::sock_ops::SockOpsLink
pub fn aya::programs::sock_ops::SockOpsLink::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::ops::drop::Drop for aya::programs::sock_ops::SockOpsLink
Expand Down

0 comments on commit c33a061

Please sign in to comment.