Skip to content

Commit

Permalink
aya: move mmap from perf_buffer.rs to sys/mod.rs
Browse files Browse the repository at this point in the history
mmap() is needed for the ring buffer implementation, so move it to a common module
  • Loading branch information
willfindlay authored and tamird committed Sep 27, 2023
1 parent f04f2d0 commit ead7066
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions aya/src/maps/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ impl<T: BorrowMut<MapData>, V: Pod> BloomFilter<T, V> {

#[cfg(test)]
mod tests {
use std::{ffi::c_long, io};
use std::io;

use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT};
use libc::{c_long, EFAULT, ENOENT};

use super::*;
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V>

#[cfg(test)]
mod tests {
use std::{ffi::c_long, io};
use std::io;

use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT};
use libc::{c_long, EFAULT, ENOENT};

use super::{
super::test_utils::{self, new_map},
Expand Down
4 changes: 2 additions & 2 deletions aya/src/maps/lpm_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K

#[cfg(test)]
mod tests {
use std::{ffi::c_long, io, mem, net::Ipv4Addr};
use std::{io, mem, net::Ipv4Addr};

use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT};
use libc::{c_long, EFAULT, ENOENT};

use super::*;
use crate::{
Expand Down
24 changes: 2 additions & 22 deletions aya/src/maps/perf/perf_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use std::{
ffi::c_void,
io, mem,
os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd},
ptr, slice,
sync::atomic::{self, AtomicPtr, Ordering},
};

use bytes::BytesMut;
use libc::{c_int, munmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
use libc::{c_void, munmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
use thiserror::Error;

use crate::{
generated::{
perf_event_header, perf_event_mmap_page,
perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE},
},
sys::{perf_event_ioctl, perf_event_open_bpf, SysResult},
sys::{mmap, perf_event_ioctl, perf_event_open_bpf, SysResult},
PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE,
};

Expand Down Expand Up @@ -282,25 +281,6 @@ impl Drop for PerfBuffer {
}
}

#[cfg_attr(test, allow(unused_variables))]
unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(not(test))]
return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset);

#[cfg(test)]
use crate::sys::TEST_MMAP_RET;

#[cfg(test)]
TEST_MMAP_RET.with(|ret| *ret.borrow())
}

#[derive(Debug)]
#[repr(C)]
struct Sample {
Expand Down
4 changes: 2 additions & 2 deletions aya/src/sys/fake.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, ffi::c_long, io, ptr};
use std::{cell::RefCell, io, ptr};

use libc::c_void;
use libc::{c_long, c_void};

use super::{SysResult, Syscall};

Expand Down
18 changes: 17 additions & 1 deletion aya/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
pub(crate) use bpf::*;
#[cfg(test)]
pub(crate) use fake::*;
use libc::{c_int, c_long, pid_t, SYS_bpf, SYS_perf_event_open};
use libc::{c_int, c_long, c_void, pid_t, SYS_bpf, SYS_perf_event_open};
#[doc(hidden)]
pub use netlink::netlink_set_link_up;
pub(crate) use netlink::*;
Expand Down Expand Up @@ -114,3 +114,19 @@ fn syscall(call: Syscall<'_>) -> SysResult<c_long> {
ret => Err((ret, io::Error::last_os_error())),
}
}

#[cfg_attr(test, allow(unused_variables))]
pub(crate) unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(not(test))]
return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset);

#[cfg(test)]
TEST_MMAP_RET.with(|ret| *ret.borrow())
}
4 changes: 2 additions & 2 deletions aya/src/sys/perf_event.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
ffi::{c_long, CString, OsStr},
ffi::{CString, OsStr},
io, mem,
os::fd::{BorrowedFd, FromRawFd as _, OwnedFd},
};

use libc::{c_int, pid_t};
use libc::{c_int, c_long, pid_t};

use super::{syscall, SysResult, Syscall};
use crate::generated::{
Expand Down

0 comments on commit ead7066

Please sign in to comment.