Skip to content

Commit cf2ff5c

Browse files
alpireanakryiko
authored andcommitted
libbpf-rs: add attach_netns method to Program
BPF sk_lookup program type (BPF_PROG_TYPE_SK_LOOKUP) introduces programmability into the socket lookup performed by the transport layer when a packet is to be delivered locally. When invoked, the BPF sk_lookup program can select a socket that will receive the incoming packet by calling the bpf_sk_assign() BPF helper function. See more details at https://www.kernel.org/doc/html/latest/bpf/prog_sk_lookup.html Signed-off-by: Alex Rebert <[email protected]>
1 parent 2aeaa1e commit cf2ff5c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

libbpf-rs/src/program.rs

+27
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub enum ProgramType {
7373
StructOps,
7474
Ext,
7575
Lsm,
76+
SkLookup,
77+
Syscall,
7678
/// See [`MapType::Unknown`]
7779
Unknown = u32::MAX,
7880
}
@@ -110,6 +112,20 @@ pub enum ProgramAttachType {
110112
TraceFexit,
111113
ModifyReturn,
112114
LsmMac,
115+
TraceIter,
116+
CgroupInet4Getpeername,
117+
CgroupInet6Getpeername,
118+
CgroupInet4Getsockname,
119+
CgroupInet6Getsockname,
120+
XdpDevmap,
121+
CgroupInetSockRelease,
122+
XdpCpumap,
123+
SkLookup,
124+
Xdp,
125+
SkSkbVerdict,
126+
SkReuseportSelect,
127+
SkReuseportSelectOrMigrate,
128+
PerfEvent,
113129
/// See [`MapType::Unknown`]
114130
Unknown = u32::MAX,
115131
}
@@ -344,4 +360,15 @@ impl Program {
344360
Ok(Link::new(ptr))
345361
}
346362
}
363+
364+
/// Attach this program to [netns-based programs](https://lwn.net/Articles/819618/)
365+
pub fn attach_netns(&mut self, netns_fd: i32) -> Result<Link> {
366+
let ptr = unsafe { libbpf_sys::bpf_program__attach_netns(self.ptr, netns_fd) };
367+
let err = unsafe { libbpf_sys::libbpf_get_error(ptr as *const _) };
368+
if err != 0 {
369+
Err(Error::System(err as i32))
370+
} else {
371+
Ok(Link::new(ptr))
372+
}
373+
}
347374
}

0 commit comments

Comments
 (0)