Skip to content

Commit 09b9330

Browse files
committed
feat: add read_iovec to the tap
Add method to use readv syscall to read from the tap into iov. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 9d0b938 commit 09b9330

File tree

1 file changed

+14
-0
lines changed
  • src/vmm/src/devices/virtio/net

1 file changed

+14
-0
lines changed

src/vmm/src/devices/virtio/net/tap.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ impl Tap {
185185
Ok(())
186186
}
187187

188+
/// Read into slice of iovec from the tap
189+
pub(crate) fn read_iovec(&mut self, buffers: &[libc::iovec]) -> Result<usize, IoError> {
190+
let iovcnt = i32::try_from(buffers.len()).unwrap();
191+
let iov = buffers.as_ptr();
192+
193+
// SAFETY: `writev` is safe. Called with a valid tap fd, the iovec pointer and length
194+
// is provide by the `IoVecBuffer` implementation and we check the return value.
195+
let ret = unsafe { libc::readv(self.tap_file.as_raw_fd(), iov, iovcnt) };
196+
if ret == -1 {
197+
return Err(IoError::last_os_error());
198+
}
199+
Ok(usize::try_from(ret).unwrap())
200+
}
201+
188202
/// Write an `IoVecBuffer` to tap
189203
pub(crate) fn write_iovec(&mut self, buffer: &IoVecBuffer) -> Result<usize, IoError> {
190204
let iovcnt = i32::try_from(buffer.iovec_count()).unwrap();

0 commit comments

Comments
 (0)