-
Notifications
You must be signed in to change notification settings - Fork 22
Prep methods for additional io_uring features #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2894f9a
sqe poll methods
b868027
Merge branch 'master' into poll
d11ce3a
sqe "poll" methods
29c56e4
work
5e0cf19
work
4cc833d
work
09097dc
work
bc7b2ad
work
ca2762d
work
235f4b8
progress
8309ad9
progress
71715cf
progress
a9ea0ec
progress
9dd821f
resolve discussions
ca487c8
Merge branch 'master' into additional-methods
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use nix::sys::socket::InetAddr; | ||
| use std::{ | ||
| io::{self, Read, Write}, | ||
| net::{TcpListener, TcpStream}, | ||
| os::unix::io::{AsRawFd, FromRawFd}, | ||
| }; | ||
| use iou::SockAddr; | ||
|
|
||
| const MESSAGE: &'static [u8] = b"Hello World"; | ||
|
|
||
| #[test] | ||
| #[ignore] // kernel 5.5 needed for accept | ||
| fn accept() -> io::Result<()> { | ||
| let mut ring = iou::IoUring::new(1)?; | ||
|
|
||
| let listener = TcpListener::bind(("0.0.0.0", 0))?; | ||
| listener.set_nonblocking(true)?; | ||
|
|
||
| let mut stream = TcpStream::connect(listener.local_addr()?)?; | ||
| stream.write_all(MESSAGE)?; | ||
|
|
||
| let fd = listener.as_raw_fd(); | ||
| let mut sq = ring.sq(); | ||
| let mut sqe = sq.next_sqe().expect("failed to get sqe"); | ||
| unsafe { | ||
| sqe.prep_accept(fd, None, iou::SockFlag::empty()); | ||
| sq.submit()?; | ||
| } | ||
| let cqe = ring.wait_for_cqe()?; | ||
| let accept_fd = cqe.result()?; | ||
| let mut accept_buf = [0; MESSAGE.len()]; | ||
| let mut stream = unsafe { TcpStream::from_raw_fd(accept_fd as _) }; | ||
| stream.read_exact(&mut accept_buf)?; | ||
| assert_eq!(accept_buf, MESSAGE); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] // kernel 5.5 needed for accept | ||
| fn accept_with_params() -> io::Result<()> { | ||
| let mut ring = iou::IoUring::new(1)?; | ||
|
|
||
| let listener = TcpListener::bind(("0.0.0.0", 0))?; | ||
| listener.set_nonblocking(true)?; | ||
|
|
||
| let mut connection_stream = TcpStream::connect(listener.local_addr()?)?; | ||
| connection_stream.write_all(MESSAGE)?; | ||
|
|
||
| let fd = listener.as_raw_fd(); | ||
| let mut sq = ring.sq(); | ||
| let mut sqe = sq.next_sqe().expect("failed to get sqe"); | ||
| let mut accept_params = iou::SockAddrStorage::uninit(); | ||
| unsafe { | ||
| sqe.prep_accept(fd, Some(&mut accept_params), iou::SockFlag::empty()); | ||
| sq.submit()?; | ||
| } | ||
| let cqe = ring.wait_for_cqe()?; | ||
| let accept_fd = cqe.result()?; | ||
| let mut accept_buf = [0; MESSAGE.len()]; | ||
| let mut accepted_stream = unsafe { TcpStream::from_raw_fd(accept_fd as _) }; | ||
| accepted_stream.read_exact(&mut accept_buf)?; | ||
| assert_eq!(accept_buf, MESSAGE); | ||
|
|
||
| let addr = unsafe { accept_params.as_socket_addr()? }; | ||
| let connection_addr = SockAddr::Inet(InetAddr::from_std(&connection_stream.local_addr()?)); | ||
| assert_eq!(addr, connection_addr); | ||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use nix::sys::socket::{AddressFamily, SockProtocol, SockType, InetAddr, SockFlag}; | ||
| use std::{io, net::TcpListener}; | ||
|
|
||
| #[test] | ||
| #[ignore] // kernel 5.5 needed for connect | ||
| fn connect() -> io::Result<()> { | ||
| let listener = TcpListener::bind(("0.0.0.0", 0))?; | ||
| listener.set_nonblocking(true)?; | ||
| let listener_addr = iou::SockAddr::new_inet(InetAddr::from_std(&listener.local_addr()?)); | ||
|
|
||
| let socket = nix::sys::socket::socket( | ||
| AddressFamily::Inet, | ||
| SockType::Stream, | ||
| SockFlag::SOCK_NONBLOCK, | ||
| SockProtocol::Tcp, | ||
| ) | ||
| .map_err(|_| io::Error::new(io::ErrorKind::Other, "failed to create socket"))?; | ||
|
|
||
| let mut ring = iou::IoUring::new(1)?; | ||
| let mut sqe = ring.next_sqe().expect("failed to get sqe"); | ||
| unsafe { | ||
| sqe.prep_connect(socket, &listener_addr); | ||
| sqe.set_user_data(42); | ||
| ring.submit_sqes()?; | ||
| } | ||
| let cqe = ring.wait_for_cqe()?; | ||
| let _res = cqe.result()?; | ||
| assert_eq!(cqe.user_data(), 42); | ||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,66 @@ | ||
| #![feature(test)] | ||
| extern crate libc; | ||
| extern crate test; | ||
|
|
||
| use std::{io, os::unix::io::RawFd}; | ||
|
|
||
| pub fn pipe() -> io::Result<(RawFd, RawFd)> { | ||
| unsafe { | ||
| let mut fds = core::mem::MaybeUninit::<[libc::c_int; 2]>::uninit(); | ||
|
|
||
| let res = libc::pipe(fds.as_mut_ptr() as *mut libc::c_int); | ||
|
|
||
| if res < 0 { | ||
| Err(io::Error::from_raw_os_error(-res)) | ||
| } else { | ||
| Ok((fds.assume_init()[0], fds.assume_init()[1])) | ||
| } | ||
| } | ||
| } | ||
| use std::{ | ||
| io::{self, Read, Write}, | ||
| os::unix::{io::AsRawFd, net}, | ||
| }; | ||
| const MESSAGE: &'static [u8] = b"Hello World"; | ||
|
|
||
| #[test] | ||
| fn test_poll_add() -> io::Result<()> { | ||
| let mut ring = iou::IoUring::new(2)?; | ||
| let (read, write) = pipe()?; | ||
|
|
||
| let (mut read, mut write) = net::UnixStream::pair()?; | ||
| unsafe { | ||
| let mut sqe = ring.next_sqe().expect("no sqe"); | ||
| sqe.prep_poll_add(read, iou::PollMask::POLLIN); | ||
| let mut sqe = ring.next_sqe().expect("failed to get sqe"); | ||
| sqe.prep_poll_add(read.as_raw_fd(), iou::PollFlags::POLLIN); | ||
| sqe.set_user_data(0xDEADBEEF); | ||
| ring.submit_sqes()?; | ||
| } | ||
|
|
||
| let res = unsafe { | ||
| let buf = b"hello"; | ||
| libc::write( | ||
| write, | ||
| buf.as_ptr() as *const libc::c_void, | ||
| buf.len() as libc::size_t, | ||
| ) | ||
| }; | ||
|
|
||
| if res < 0 { | ||
| return Err(io::Error::from_raw_os_error(-res as _)); | ||
| } | ||
| write.write(MESSAGE)?; | ||
|
|
||
| let cqe = ring.wait_for_cqe()?; | ||
| assert_eq!(cqe.user_data(), 0xDEADBEEF); | ||
| let mask = unsafe { iou::PollMask::from_bits_unchecked(cqe.result()? as _) }; | ||
| assert!(mask.contains(iou::PollMask::POLLIN)); | ||
| unsafe { | ||
| libc::close(write); | ||
| libc::close(read); | ||
| } | ||
| let mask = unsafe { iou::PollFlags::from_bits_unchecked(cqe.result()? as _) }; | ||
| assert!(mask.contains(iou::PollFlags::POLLIN)); | ||
| let mut buf = [0; MESSAGE.len()]; | ||
| read.read(&mut buf)?; | ||
| assert_eq!(buf, MESSAGE); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_poll_remove() -> io::Result<()> { | ||
| let mut ring = iou::IoUring::new(2)?; | ||
| let (read, write) = pipe()?; | ||
|
|
||
| let (read, _write) = net::UnixStream::pair()?; | ||
| let uname = nix::sys::utsname::uname(); | ||
| let version = semver::Version::parse(uname.release()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| unsafe { | ||
| let mut sqe = ring.next_sqe().expect("no sqe"); | ||
| sqe.prep_poll_add(read, iou::PollMask::POLLIN); | ||
| let mut sqe = ring.next_sqe().expect("failed to get sqe"); | ||
| sqe.prep_poll_add(read.as_raw_fd(), iou::PollFlags::POLLIN); | ||
| sqe.set_user_data(0xDEADBEEF); | ||
| ring.submit_sqes()?; | ||
|
|
||
| let mut sqe = ring.next_sqe().expect("no sqe"); | ||
| let mut sqe = ring.next_sqe().expect("failed to get sqe"); | ||
| sqe.prep_poll_remove(0xDEADBEEF); | ||
| sqe.set_user_data(42); | ||
| ring.submit_sqes()?; | ||
| for _ in 0..2 { | ||
| let cqe = ring.wait_for_cqe()?; | ||
| let _ = cqe.result()?; | ||
| let user_data = cqe.user_data(); | ||
| if version < semver::Version::parse("5.5.0-0") { | ||
| let _ = cqe.result()?; | ||
| } else if user_data == 0xDEADBEEF { | ||
| let err = cqe | ||
| .result() | ||
| .expect_err("on kernels >=5.5 error is expected"); | ||
| let err_no = nix::errno::Errno::from_i32( | ||
| err.raw_os_error() | ||
| .expect("on kernels >=5.5 os_error is expected"), | ||
| ); | ||
| assert_eq!(err_no, nix::errno::Errno::ECANCELED); | ||
| } else { | ||
| let _ = cqe.result()?; | ||
| } | ||
| } | ||
| libc::close(write); | ||
| libc::close(read); | ||
| Ok(()) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to make sure I have a correct understanding of this API: this function is safe to call after as the
prep_acceptSQE you passed this to has completed, and calling it before it has completed would be incorrect. Is that right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely right.