From 0072ed8224c9fa4d1353abf138c6ef6241c9bcd7 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 17 May 2021 16:06:31 -0400 Subject: [PATCH] linux::io_uring: implement io_uring_register --- io_uring/register.ha | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ io_uring/sqe.ha | 2 -- io_uring/uring.ha | 10 +++++----- diff --git a/io_uring/register.ha b/io_uring/register.ha new file mode 100644 index 0000000000000000000000000000000000000000..a7b5a97c0df2f5b76af1a7e6dcb4b7d0ded3fc07 --- /dev/null +++ b/io_uring/register.ha @@ -0,0 +1,144 @@ +use errors; +use rt; +use types; + +// Registers a set of fixed buffers with an [[io_uring]]. Note that you must +// call [[unregister_buffers]] before registering a new set of buffers (even if +// some of them have similar addresses to the old set). +export fn register_buffers(ring: *io_uring, iov: []rt::iovec) (void | error) = { + assert(len(iov) <= types::UINT_MAX); + return match (rt::io_uring_register(ring.fd, regop::REGISTER_BUFFERS, + iov: *[*]rt::iovec, len(iov): uint)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Unregisters all fixed buffers associated with an [[io_uring]]. +export fn unregister_buffers(ring: *io_uring) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::UNREGISTER_BUFFERS, null, 0)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Registers a set of file descriptors with an [[io_uring]]. The set of files +// may be sparse, meaning that some are set to -1, to be updated later using +// [[register_files_update]]. +export fn register_files(ring: *io_uring, files: []int) (void | error) = { + assert(len(files) <= types::UINT_MAX); + return match (rt::io_uring_register(ring.fd, regop::REGISTER_FILES, + files: *[*]int, len(files): uint)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Applies a set of [[files_update]]s to the set of files registered with an +// [[io_uring]]. +export fn register_files_update( + ring: *io_uring, + updates: []files_update, +) (void | error) = { + assert(len(updates) <= types::UINT_MAX); + return match (rt::io_uring_register(ring.fd, regop::REGISTER_FILES_UPDATE, + updates: *[*]files_update, len(updates): uint)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Unregisters all files associated with an [[io_uring]]. +export fn unregister_files(ring: *io_uring) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::UNREGISTER_FILES, null, 0)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Registers an eventfd(2) with this [[io_uring]] to be notified of completion +// events. +export fn register_eventfd(ring: *io_uring, fd: int) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::REGISTER_EVENTFD, &fd, 1)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Similar to [[register_eventfd]], but only notifies of events which complet +// asyncronously. +export fn register_eventfd_async(ring: *io_uring, fd: int) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::REGISTER_EVENTFD_ASYNC, &fd, 1)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Unregisters the eventfd(2) associated with this [[io_uring]]. +export fn unregister_eventfd(ring: *io_uring) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::UNREGISTER_EVENTFD, null, 0)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// XXX: This interface is pretty bad. It would be nice to improve on it +// a bit before making it part of the API. +//export fn register_probe(ring: *io_uring, out: *probe, nops: size) void = { +// assert(nops * size(probe_op) <= types::UINT_MAX); +// match (rt::io_uring_register(ring.fd, regop::REGISTER_PROBE, +// out, (nops * size(probe)): uint)) { +// rt::errno => abort("Unexpected io_uring REGISTER_PROBE error"), +// void => void, +// }; +//}; + +// Registers the current process's credentials as a personality with an +// [[io_uring]], returning an ID. Set the personality field of an [[sqe]] to use +// that personality for an I/O submission. +export fn register_personality(ring: *io_uring) int = { + return match (rt::io_uring_register(ring.fd, + regop::REGISTER_PERSONALITY, null, 0)) { + rt::errno => abort("Unexpected io_uring REGISTER_PERSONALITY error"), + i: int => i, + }; +}; + +// Unregisters a personality previously configured with +// [[register_personality]]. +export fn unregister_personality(ring: *io_uring, id: int) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::UNREGISTER_PERSONALITY, null, id: uint)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Enables submissions for an [[io_uring]] which was started in the disabled +// state via [[setup_flags::R_DISABLED]]. Future access to this io_uring is +// subject to any configured restrictions. +export fn register_enable_rings(ring: *io_uring) (void | error) = { + return match (rt::io_uring_register(ring.fd, + regop::REGISTER_ENABLE_RINGS, null, 0)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; + +// Registers a restriction for this [[io_uring]], limiting the kinds of future +// registrations and I/O submissions which are permitted for it. This is only +// accepted if the [[io_uring]] was set up in a disabled state via +// [[setup_flags::R_DISABLED]]. +export fn register_restrictions(ring: *io_uring, res: []restriction) (void | error) = { + assert(len(res) < types::UINT_MAX); + return match (rt::io_uring_register(ring.fd, regop::REGISTER_RESTRICTIONS, + res: *[*]restriction, len(res): uint)) { + err: rt::errno => errors::errno(err), + int => void, + }; +}; diff --git a/io_uring/sqe.ha b/io_uring/sqe.ha index a0ca560b4ff8bdf986a8dab36943ae704f70720f..0977b54ffc3b867828f649acabfa1cf2bad1f183 100644 --- a/io_uring/sqe.ha +++ b/io_uring/sqe.ha @@ -1,5 +1,3 @@ -// TODO: -// - Interface for buffer registration use rt; use types; diff --git a/io_uring/uring.ha b/io_uring/uring.ha index e7bcdf6e383d245dfdaffdfc1d9e04215fc30bd4..de8601e552e5a29c2bb32e474bb6c1de248d9737 100644 --- a/io_uring/uring.ha +++ b/io_uring/uring.ha @@ -276,18 +276,18 @@ }; // Details for a REGISTER_RESTRICTIONS operation. export type restriction = struct { - opcode: restriction_op, + opcode: resop, union { - register_op: u8, - sqe_op: u8, - sqe_flags: u8, + register_op: regop, + sqe_op: op, + sqe_flags: sqe_flags, }, resv: u8, resv2: [3]u32, }; // Opcode for a [[restriction]]. -export type restriction_op = enum u16 { +export type resop = enum u16 { // Allow an io_uring_register(2) opcode REGISTER_OP = 0, // Allow an sqe opcode -- 2.48.1