Skip to content

Bpf_link_create support issue in the Android kernel 5.7.0 and later #1244

Open
@SajjadPourali

Description

@SajjadPourali

It appears that Android with kernel 5.7.0 and later still does not support bpf_link_create properly, which causes attaching cgroup programs using Aya to be impossible.

Image

I use Android 16 and 15 in the Android emulator. A potential solution is to add a fallback method that switches ProgAttachLink::attach in case bpf_link_create is unsupported, which can resolve the issue on Android.

aya/aya/src/programs/cgroup_sock_addr.rs

    pub fn attach<T: AsFd>(
        &mut self,
        cgroup: T,
        mode: CgroupAttachMode,
    ) -> Result<CgroupSockAddrLinkId, ProgramError> {
        let prog_fd = self.fd()?;
        let prog_fd = prog_fd.as_fd();
        let cgroup_fd = cgroup.as_fd();
        let attach_type = self.data.expected_attach_type.unwrap();
        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(),
                None,
            );
            match link_fd {
                Ok(lfd) => {
                    return self.data.links.insert(CgroupSockAddrLink::new(
                        CgroupSockAddrLinkInner::Fd(FdLink::new(lfd)),
                    ));
                }
                Err(e) => {
                    warn!(
                        "failed to create link using bpf_link_create: {}, switching to fallback",
                        e.1.to_string()
                    );
                }
            };
        }
        let link = ProgAttachLink::attach(prog_fd, cgroup_fd, attach_type, mode)?;

        self.data.links.insert(CgroupSockAddrLink::new(
            CgroupSockAddrLinkInner::ProgAttach(link),
        ))
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions