Skip to content

Commit

Permalink
aya: Add BPF_PROG_TYPE_CGROUP_SOCK_ADDR
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
  • Loading branch information
dave-tucker committed May 19, 2022
1 parent 41a27e3 commit ea331d4
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 34 deletions.
14 changes: 10 additions & 4 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use crate::{
MapKind, Object, ParseError, ProgramSection,
},
programs::{
BtfTracePoint, CgroupSkb, CgroupSkbAttachType, CgroupSysctl, Extension, FEntry, FExit,
KProbe, LircMode2, Lsm, PerfEvent, ProbeKind, Program, ProgramData, ProgramError,
RawTracePoint, SchedClassifier, SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint,
UProbe, Xdp,
BtfTracePoint, CgroupSkb, CgroupSkbAttachType, CgroupSockAddr, CgroupSysctl, Extension,
FEntry, FExit, KProbe, LircMode2, Lsm, PerfEvent, ProbeKind, Program, ProgramData,
ProgramError, RawTracePoint, SchedClassifier, SkMsg, SkSkb, SkSkbKind, SockOps,
SocketFilter, TracePoint, UProbe, Xdp,
},
sys::{
bpf_load_btf, bpf_map_freeze, bpf_map_update_elem_ptr, is_btf_datasec_supported,
Expand Down Expand Up @@ -481,6 +481,12 @@ impl<'a> BpfLoader<'a> {
data: ProgramData::new(prog_name, obj, btf_fd),
expected_attach_type: Some(CgroupSkbAttachType::Egress),
}),
ProgramSection::CgroupSockAddr { attach_type, .. } => {
Program::CgroupSockAddr(CgroupSockAddr {
data: ProgramData::new(prog_name, obj, btf_fd),
attach_type: *attach_type,
})
}
ProgramSection::LircMode2 { .. } => Program::LircMode2(LircMode2 {
data: ProgramData::new(prog_name, obj, btf_fd),
}),
Expand Down
222 changes: 192 additions & 30 deletions aya/src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
bpf_map_def,
generated::{bpf_insn, bpf_map_type::BPF_MAP_TYPE_ARRAY, BPF_F_RDONLY_PROG},
obj::btf::{Btf, BtfError, BtfExt},
programs::CgroupSockAddrAttachType,
BpfError,
};
use std::slice::from_raw_parts_mut;
Expand Down Expand Up @@ -104,30 +105,82 @@ pub(crate) struct Function {

#[derive(Debug, Clone)]
pub enum ProgramSection {
KRetProbe { name: String },
KProbe { name: String },
UProbe { name: String },
URetProbe { name: String },
TracePoint { name: String },
SocketFilter { name: String },
Xdp { name: String },
SkMsg { name: String },
SkSkbStreamParser { name: String },
SkSkbStreamVerdict { name: String },
SockOps { name: String },
SchedClassifier { name: String },
CgroupSkb { name: String },
CgroupSkbIngress { name: String },
CgroupSkbEgress { name: String },
CgroupSysctl { name: String },
LircMode2 { name: String },
PerfEvent { name: String },
RawTracePoint { name: String },
Lsm { name: String },
BtfTracePoint { name: String },
FEntry { name: String },
FExit { name: String },
Extension { name: String },
KRetProbe {
name: String,
},
KProbe {
name: String,
},
UProbe {
name: String,
},
URetProbe {
name: String,
},
TracePoint {
name: String,
},
SocketFilter {
name: String,
},
Xdp {
name: String,
},
SkMsg {
name: String,
},
SkSkbStreamParser {
name: String,
},
SkSkbStreamVerdict {
name: String,
},
SockOps {
name: String,
},
SchedClassifier {
name: String,
},
CgroupSkb {
name: String,
},
CgroupSkbIngress {
name: String,
},
CgroupSkbEgress {
name: String,
},
CgroupSockAddr {
name: String,
attach_type: CgroupSockAddrAttachType,
},
CgroupSysctl {
name: String,
},
LircMode2 {
name: String,
},
PerfEvent {
name: String,
},
RawTracePoint {
name: String,
},
Lsm {
name: String,
},
BtfTracePoint {
name: String,
},
FEntry {
name: String,
},
FExit {
name: String,
},
Extension {
name: String,
},
}

impl ProgramSection {
Expand All @@ -145,9 +198,10 @@ impl ProgramSection {
ProgramSection::SkSkbStreamVerdict { name } => name,
ProgramSection::SockOps { name } => name,
ProgramSection::SchedClassifier { name } => name,
ProgramSection::CgroupSkb { name } => name,
ProgramSection::CgroupSkbIngress { name } => name,
ProgramSection::CgroupSkbEgress { name } => name,
ProgramSection::CgroupSkb { name, .. } => name,
ProgramSection::CgroupSkbIngress { name, .. } => name,
ProgramSection::CgroupSkbEgress { name, .. } => name,
ProgramSection::CgroupSockAddr { name, .. } => name,
ProgramSection::CgroupSysctl { name } => name,
ProgramSection::LircMode2 { name } => name,
ProgramSection::PerfEvent { name } => name,
Expand Down Expand Up @@ -221,11 +275,63 @@ impl FromStr for ProgramSection {
"skb" => CgroupSkb { name },
"sysctl" => CgroupSysctl { name },
_ => {
return Err(ParseError::InvalidProgramSection {
section: section.to_owned(),
})
if let Ok(attach_type) = CgroupSockAddrAttachType::try_from(name.as_str()) {
CgroupSockAddr { name, attach_type }
} else {
return Err(ParseError::InvalidProgramSection {
section: section.to_owned(),
});
}
}
},
"cgroup/bind4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::Bind4,
},
"cgroup/bind6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::Bind6,
},
"cgroup/connect4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::Connect4,
},
"cgroup/connect6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::Connect6,
},
"cgroup/getpeername4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::GetPeerName4,
},
"cgroup/getpeername6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::GetPeerName6,
},
"cgroup/getsockname4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::GetSockName4,
},
"cgroup/getsockname6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::GetSockName6,
},
"cgroup/sendmsg4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::UDPSendMsg4,
},
"cgroup/sendmsg6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::UDPSendMsg6,
},
"cgroup/recvmsg4" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::UDPRecvMsg4,
},
"cgroup/recvmsg6" => CgroupSockAddr {
name,
attach_type: CgroupSockAddrAttachType::UDPRecvMsg6,
},
"lirc_mode2" => LircMode2 { name },
"perf_event" => PerfEvent { name },
"raw_tp" | "raw_tracepoint" => RawTracePoint { name },
Expand Down Expand Up @@ -677,6 +783,14 @@ pub enum ParseError {

#[error("no symbols found for the maps included in the maps section")]
NoSymbolsInMapSection {},

#[error("the attach type {attach_type} is not valid for {prog_type}")]
InvalidAttachType {
/// the attach type
attach_type: String,
/// the program type
prog_type: String,
},
}

#[derive(Debug)]
Expand Down Expand Up @@ -1641,6 +1755,54 @@ mod tests {
);
}

#[test]
fn test_parse_section_sock_addr_named() {
let mut obj = fake_obj();

assert_matches!(
obj.parse_section(fake_section(
BpfSectionKind::Program,
"cgroup/connect4/foo",
bytes_of(&fake_ins())
)),
Ok(())
);
assert_matches!(
obj.programs.get("foo"),
Some(Program {
section: ProgramSection::CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect4,
..
},
..
})
);
}

#[test]
fn test_parse_section_sock_addr_unnamed() {
let mut obj = fake_obj();

assert_matches!(
obj.parse_section(fake_section(
BpfSectionKind::Program,
"cgroup/connect4",
bytes_of(&fake_ins())
)),
Ok(())
);
assert_matches!(
obj.programs.get("connect4"),
Some(Program {
section: ProgramSection::CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect4,
..
},
..
})
);
}

#[test]
fn test_patch_map_data() {
let mut obj = fake_obj();
Expand Down

0 comments on commit ea331d4

Please sign in to comment.