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 Config::disable and Ring::enable #108

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{libc, AtomicBitMap, CompletionQueue, Ring, SharedSubmissionQueue, Su
pub struct Config<'r> {
submission_entries: u32,
completion_entries: Option<u32>,
disabled: bool,
clamp: bool,
kernel_thread: bool,
cpu_affinity: Option<u32>,
Expand Down Expand Up @@ -56,6 +57,7 @@ impl<'r> Config<'r> {
Config {
submission_entries: entries,
completion_entries: None,
disabled: false,
clamp: false,
kernel_thread: true,
cpu_affinity: None,
Expand All @@ -64,6 +66,16 @@ impl<'r> Config<'r> {
}
}

/// Start the ring in a disabled state.
///
/// While the ring is disabled submissions are not allowed. To enable the
/// ring use [`Ring::enable`].
#[doc(alias = "IORING_SETUP_R_DISABLED")]
pub const fn disable(mut self) -> Config<'r> {
self.disabled = true;
self
}

/// Set the size of the completion queue.
///
/// By default the kernel will use a completion queue twice as large as the
Expand Down Expand Up @@ -151,6 +163,7 @@ impl<'r> Config<'r> {
}

/// Same as [`Config::attach`], but accepts a [`SubmissionQueue`].
#[doc(alias = "IORING_SETUP_ATTACH_WQ")]
pub const fn attach_queue(mut self, other_ring: &'r SubmissionQueue) -> Self {
self.attach = Some(other_ring);
self
Expand All @@ -170,6 +183,9 @@ impl<'r> Config<'r> {
// Don't interrupt userspace, the user must call `Ring::poll` any way.
parameters.flags |= libc::IORING_SETUP_COOP_TASKRUN;
}
if self.disabled {
parameters.flags |= libc::IORING_SETUP_R_DISABLED;
}
if let Some(completion_entries) = self.completion_entries {
parameters.cq_entries = completion_entries;
parameters.flags |= libc::IORING_SETUP_CQSIZE;
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ impl Ring {
&self.sq
}

/// Enable the ring.
///
/// This only required when starting the ring in disabled mode, see
/// [`Config::disable`].
pub fn enable(&mut self) -> io::Result<()> {
libc::syscall!(io_uring_register(
self.sq.shared.ring_fd.as_raw_fd(),
libc::IORING_REGISTER_ENABLE_RINGS,
ptr::null(),
0,
))?;
Ok(())
}

/// Poll the ring for completions.
///
/// This will wake all completed [`Future`]s with the result of their
Expand Down
14 changes: 14 additions & 0 deletions tests/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ fn submission_queue_full_is_handle_internally() {
}
}

#[test]
fn config_disabled() {
init();
let mut ring = Ring::config(1).disable().build().unwrap();

// In a disabled state, so we expect an error.
let err = ring.poll(None).unwrap_err();
assert_eq!(err.raw_os_error(), Some(libc::EBADFD));

// Enabling it should allow us to poll.
ring.enable().unwrap();
ring.poll(Some(Duration::from_millis(1))).unwrap();
}

#[test]
fn wake_ring() {
init();
Expand Down
Loading