Browse Source

async_perf_event_array: access inner through async

Avoid holding onto raw file descriptors.

Remove some implied bounds (BorrowMut implies Borrow).
Tamir Duberstein 1 năm trước cách đây
mục cha
commit
8b0c7f1204

+ 22 - 30
aya/src/maps/perf/async_perf_event_array.rs

@@ -1,8 +1,5 @@
 use bytes::BytesMut;
-use std::{
-    borrow::{Borrow, BorrowMut},
-    os::fd::{AsRawFd as _, RawFd},
-};
+use std::borrow::{Borrow, BorrowMut};
 
 // See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features.
 //
@@ -92,7 +89,7 @@ pub struct AsyncPerfEventArray<T> {
     perf_map: PerfEventArray<T>,
 }
 
-impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> {
+impl<T: BorrowMut<MapData>> AsyncPerfEventArray<T> {
     /// Opens the perf buffer at the given index.
     ///
     /// The returned buffer will receive all the events eBPF programs send at the given index.
@@ -103,16 +100,11 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> {
     ) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError> {
         let Self { perf_map } = self;
         let buf = perf_map.open(index, page_count)?;
-        let fd = buf.as_raw_fd();
-        Ok(AsyncPerfEventArrayBuffer {
-            buf,
-
-            #[cfg(feature = "async_tokio")]
-            async_tokio_fd: AsyncFd::new(fd)?,
-
-            #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
-            async_std_fd: Async::new(fd)?,
-        })
+        #[cfg(feature = "async_tokio")]
+        let buf = AsyncFd::new(buf)?;
+        #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
+        let buf = Async::new(buf)?;
+        Ok(AsyncPerfEventArrayBuffer { buf })
     }
 }
 
@@ -131,17 +123,18 @@ impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
 ///
 /// See the [`AsyncPerfEventArray` documentation](AsyncPerfEventArray) for an overview of how to
 /// use perf buffers.
-pub struct AsyncPerfEventArrayBuffer<T> {
+pub struct AsyncPerfEventArrayBuffer<T: BorrowMut<MapData>> {
+    #[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
     buf: PerfEventArrayBuffer<T>,
 
     #[cfg(feature = "async_tokio")]
-    async_tokio_fd: AsyncFd<RawFd>,
+    buf: AsyncFd<PerfEventArrayBuffer<T>>,
 
     #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
-    async_std_fd: Async<RawFd>,
+    buf: Async<PerfEventArrayBuffer<T>>,
 }
 
-impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
+impl<T: BorrowMut<MapData>> AsyncPerfEventArrayBuffer<T> {
     /// Reads events from the buffer.
     ///
     /// This method reads events into the provided slice of buffers, filling
@@ -155,21 +148,20 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
         &mut self,
         buffers: &mut [BytesMut],
     ) -> Result<Events, PerfBufferError> {
-        let Self {
-            buf,
-            #[cfg(feature = "async_tokio")]
-            async_tokio_fd,
-            #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
-            async_std_fd,
-        } = self;
+        let Self { buf } = self;
         loop {
             #[cfg(feature = "async_tokio")]
-            let mut guard = async_tokio_fd.readable_mut().await?;
+            let mut guard = buf.readable_mut().await?;
+            #[cfg(feature = "async_tokio")]
+            let buf = guard.get_inner_mut();
 
             #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
-            if !buf.readable() {
-                async_std_fd.readable().await?;
-            }
+            let buf = {
+                if !buf.get_ref().readable() {
+                    buf.readable().await?;
+                }
+                buf.get_mut()
+            };
 
             let events = buf.read_events(buffers)?;
             const EMPTY: Events = Events { read: 0, lost: 0 };

+ 3 - 4
aya/src/maps/perf/perf_event_array.rs

@@ -31,7 +31,7 @@ pub struct PerfEventArrayBuffer<T> {
     buf: PerfBuffer,
 }
 
-impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArrayBuffer<T> {
+impl<T: BorrowMut<MapData>> PerfEventArrayBuffer<T> {
     /// Returns true if the buffer contains events that haven't been read.
     pub fn readable(&self) -> bool {
         self.buf.readable()
@@ -55,7 +55,7 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArrayBuffer<T> {
     }
 }
 
-impl<T: BorrowMut<MapData> + Borrow<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
+impl<T: BorrowMut<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
     fn as_raw_fd(&self) -> RawFd {
         self.buf.as_raw_fd()
     }
@@ -169,7 +169,7 @@ impl<T: Borrow<MapData>> PerfEventArray<T> {
     }
 }
 
-impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArray<T> {
+impl<T: BorrowMut<MapData>> PerfEventArray<T> {
     /// Opens the perf buffer at the given index.
     ///
     /// The returned buffer will receive all the events eBPF programs send at the given index.
@@ -180,7 +180,6 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArray<T> {
     ) -> Result<PerfEventArrayBuffer<T>, PerfBufferError> {
         // FIXME: keep track of open buffers
 
-        // this cannot fail as new() checks that the fd is open
         let map_data: &MapData = self.map.deref().borrow();
         let map_fd = map_data.fd;
         let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;

+ 8 - 8
xtask/public-api/aya.txt

@@ -372,7 +372,7 @@ pub fn aya::maps::perf::PerfBufferError::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::perf::PerfBufferError
 pub fn aya::maps::perf::PerfBufferError::from(t: T) -> T
 pub struct aya::maps::perf::AsyncPerfEventArray<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
 pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
 impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>
 pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@@ -404,8 +404,8 @@ impl<T> core::borrow::BorrowMut<T> for aya::maps::perf::AsyncPerfEventArray<T> w
 pub fn aya::maps::perf::AsyncPerfEventArray<T>::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::perf::AsyncPerfEventArray<T>
 pub fn aya::maps::perf::AsyncPerfEventArray<T>::from(t: T) -> T
-pub struct aya::maps::perf::AsyncPerfEventArrayBuffer<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArrayBuffer<T>
+pub struct aya::maps::perf::AsyncPerfEventArrayBuffer<T: core::borrow::BorrowMut<aya::maps::MapData>>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArrayBuffer<T>
 pub async fn aya::maps::perf::AsyncPerfEventArrayBuffer<T>::read_events(&mut self, buffers: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError>
 impl<T> core::marker::Send for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
 impl<T> core::marker::Sync for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
@@ -460,7 +460,7 @@ pub fn aya::maps::perf::Events::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::perf::Events
 pub fn aya::maps::perf::Events::from(t: T) -> T
 pub struct aya::maps::perf::PerfEventArray<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
 pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
 impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData>
 pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@@ -493,10 +493,10 @@ pub fn aya::maps::perf::PerfEventArray<T>::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::perf::PerfEventArray<T>
 pub fn aya::maps::perf::PerfEventArray<T>::from(t: T) -> T
 pub struct aya::maps::perf::PerfEventArrayBuffer<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArrayBuffer<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArrayBuffer<T>
 pub fn aya::maps::perf::PerfEventArrayBuffer<T>::read_events(&mut self, out_bufs: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError>
 pub fn aya::maps::perf::PerfEventArrayBuffer<T>::readable(&self) -> bool
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer<T>
 pub fn aya::maps::perf::PerfEventArrayBuffer<T>::as_raw_fd(&self) -> std::os::fd::raw::RawFd
 impl<T> core::marker::Send for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
 impl<T> core::marker::Sync for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
@@ -1067,7 +1067,7 @@ pub fn aya::maps::array::Array<T, V>::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::array::Array<T, V>
 pub fn aya::maps::array::Array<T, V>::from(t: T) -> T
 pub struct aya::maps::AsyncPerfEventArray<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
 pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
 impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>
 pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@@ -1460,7 +1460,7 @@ pub fn aya::maps::PerCpuValues<T>::borrow_mut(&mut self) -> &mut T
 impl<T> core::convert::From<T> for aya::maps::PerCpuValues<T>
 pub fn aya::maps::PerCpuValues<T>::from(t: T) -> T
 pub struct aya::maps::PerfEventArray<T>
-impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
+impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
 pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
 impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData>
 pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError