Skip to content
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

Add constructor of IoUring from raw FD #288

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

HeroicKatora
Copy link
Contributor

The user must communicate the parameters utilized with its construction out-of-band but can otherwise use an existing file descriptor. The library will then map the existing ring's queues. This permits using a pre-opened file descriptor such as one shared by a parent process, or one stashed away to be kept open in systemd's file descriptor store.

Keeping a ring open can allow its operations to complete despite the writer's process dying unexpectedly. Additionally, another process might register a personality (IORING_REGISTER_PERSONALITY) to be used as means of effective access controls for operations that must avoid confused deputy scenarios. A ring opened by a parent might also have pre-existing trusted restrictions (IORING_REGISTER_RESTRICTIONS) applied to it.

Note that this is not an implementation of IORING_SETUP_ATTACH_WQ or of a multi-issuer submission queue.

The user must communicate the parameters utilized with its construction
out-of-band but can otherwise use an existing file descriptor. The
library will then map the existing ring's queues. This permits using a
pre-opened file descriptor such as one shared by a parent process, or
one stashed away to be kept open in systemd's file descriptor store.

Keeping a ring open can allow its operations to complete despite the
writer's process dying unexpectedly. Additionally, another process might
register a personality (IORING_REGISTER_PERSONALITY) to be used as means
of effective access controls for operations that must avoid confused
deputy scenarios. A ring opened by a parent might also have pre-existing
trusted restrictions (IORING_REGISTER_RESTRICTIONS) applied to it.

Note that this is _not_ an implementation of `IORING_SETUP_ATTACH_WQ` or
of a multi-issuer submission queue.
@@ -81,6 +81,11 @@ impl IoUring<squeue::Entry, cqueue::Entry> {
pub fn new(entries: u32) -> io::Result<Self> {
Self::builder().build(entries)
}

/// Create an `IoUring` instance from a pre-opened file descriptor.
pub unsafe fn from_fd(fd: RawFd, params: Parameters) -> io::Result<Self> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if user uses an existing fd, how does he construct Parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In my use case testing I've been a little naughty and used the internal representation with a byte transmute (relying on io_uring_params). Definitely not the route I'd want to promote.

Given that the kernel's interface struct written in io_uring_setup is essentially fixed forever, would it make sense to define a public version of the parameter type with Rust mappings of the attributes? Depending on where we want to handle the unsafety of validating / trusting its attributes, the interface here could take such a new struct or Parameters could be publicly convertible from it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good idea, but I'm willing to add a #[repr(transparent)] to Parameters, and user needs to call unsafe code to convert it.

@quininer quininer merged commit a4b65d0 into tokio-rs:master Jun 4, 2024
12 checks passed
@quininer
Copy link
Member

quininer commented Jun 4, 2024

Thank you!

daniel-levin pushed a commit to daniel-levin/io-uring that referenced this pull request Jul 10, 2024
* Add constructor of IoUring from raw FD

The user must communicate the parameters utilized with its construction
out-of-band but can otherwise use an existing file descriptor. The
library will then map the existing ring's queues. This permits using a
pre-opened file descriptor such as one shared by a parent process, or
one stashed away to be kept open in systemd's file descriptor store.

Keeping a ring open can allow its operations to complete despite the
writer's process dying unexpectedly. Additionally, another process might
register a personality (IORING_REGISTER_PERSONALITY) to be used as means
of effective access controls for operations that must avoid confused
deputy scenarios. A ring opened by a parent might also have pre-existing
trusted restrictions (IORING_REGISTER_RESTRICTIONS) applied to it.

Note that this is _not_ an implementation of `IORING_SETUP_ATTACH_WQ` or
of a multi-issuer submission queue.

* Make parameters repr-transparent, add safety note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants