aya: move mmap from perf_buffer.rs to sys/mod.rs

mmap() is needed for the ring buffer implementation, so move it to a common module
reviewable/pr629/r74
William Findlay 4 years ago committed by Andrew Werner
parent 79971ccc53
commit 02aa54c9f1

@ -79,10 +79,10 @@ impl<T: BorrowMut<MapData>, V: Pod> BloomFilter<T, V> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ffi::c_long, io}; use std::io;
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{c_long, EFAULT, ENOENT};
use super::*; use super::*;
use crate::{ use crate::{

@ -103,10 +103,10 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V>
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ffi::c_long, io}; use std::io;
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{c_long, EFAULT, ENOENT};
use super::{ use super::{
super::test_utils::{self, new_map}, super::test_utils::{self, new_map},

@ -196,10 +196,10 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ffi::c_long, io, mem, net::Ipv4Addr}; use std::{io, mem, net::Ipv4Addr};
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{c_long, EFAULT, ENOENT};
use super::*; use super::*;
use crate::{ use crate::{

@ -1,5 +1,4 @@
use std::{ use std::{
ffi::c_void,
io, mem, io, mem,
os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd}, os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd},
ptr, slice, ptr, slice,
@ -7,7 +6,7 @@ use std::{
}; };
use bytes::BytesMut; 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 thiserror::Error;
use crate::{ use crate::{
@ -15,7 +14,7 @@ use crate::{
perf_event_header, perf_event_mmap_page, perf_event_header, perf_event_mmap_page,
perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE}, 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, PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE,
}; };
@ -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)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
struct Sample { struct Sample {

@ -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}; use super::{SysResult, Syscall};

@ -13,7 +13,7 @@ use std::{
pub(crate) use bpf::*; pub(crate) use bpf::*;
#[cfg(test)] #[cfg(test)]
pub(crate) use fake::*; 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)] #[doc(hidden)]
pub use netlink::netlink_set_link_up; pub use netlink::netlink_set_link_up;
pub(crate) use netlink::*; pub(crate) use netlink::*;
@ -114,3 +114,19 @@ fn syscall(call: Syscall<'_>) -> SysResult<c_long> {
ret => Err((ret, io::Error::last_os_error())), 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())
}

@ -1,10 +1,10 @@
use std::{ use std::{
ffi::{c_long, CString, OsStr}, ffi::{CString, OsStr},
io, mem, io, mem,
os::fd::{BorrowedFd, FromRawFd as _, OwnedFd}, 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 super::{syscall, SysResult, Syscall};
use crate::generated::{ use crate::generated::{

Loading…
Cancel
Save