Skip to content

Commit f7733cd

Browse files
committed
Add infrastructure to generate io_uring type defs
Basically the code for creating src/sys.rs.
1 parent 27dd269 commit f7733cd

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/target
1+
target/
22
Cargo.lock

sys/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "a10-sys"
3+
description = "Generate the type definitions for A10"
4+
version = "0.0.0"
5+
publish = false
6+
authors = ["Thomas de Zeeuw <thomasdezeeuw@gmail.com>"]
7+
edition = "2021"
8+
9+
[dependencies]
10+
libc = { version = "0.2.132", default-features = false }
11+
12+
[build-dependencies]
13+
bindgen = "0.63.0"

sys/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Generate `sys.rs`
2+
3+
First get a copy of liburing, which holds the function and type definitions,
4+
e.g. from <https://github.com/axboe/liburing>.
5+
6+
Then run `make` and copy all files from `liburing/src/include` into `include`.

sys/build.rs

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
fn main() {
2+
println!("cargo:rerun-if-changed=include/liburing.h");
3+
4+
let bindings = bindgen::Builder::default()
5+
// From `liburing/src/include/liburing.h`.
6+
.header("include/liburing.h")
7+
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
8+
.impl_debug(false)
9+
.impl_partialeq(false)
10+
.derive_copy(true)
11+
.derive_debug(false)
12+
.derive_default(false)
13+
.derive_hash(false)
14+
.derive_partialord(false)
15+
.derive_ord(false)
16+
.derive_partialeq(false)
17+
.derive_eq(false)
18+
.merge_extern_blocks(true)
19+
.default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop)
20+
.prepend_enum_name(false)
21+
.rustfmt_bindings(true)
22+
.sort_semantically(true)
23+
// Limit to io_uring types and constants.
24+
.allowlist_type("io_uring.*")
25+
.allowlist_var("IORING.*")
26+
// We define the function ourselves, since no definitions exist in libc
27+
// yet (otherwise this wasn't needed at all!).
28+
.ignore_functions()
29+
// We'll use the libc definition.
30+
.blocklist_item("sigset_t")
31+
// Add our header with the `syscall!` macro and module docs.
32+
.raw_line(HEADER.trim())
33+
.disable_header_comment()
34+
.generate()
35+
.expect("failed to generate bindings");
36+
37+
bindings
38+
.write_to_file("src/sys.rs")
39+
.expect("failed to write generated bindings");
40+
}
41+
42+
/// Code added at the top of the generated file.
43+
const HEADER: &str = "
44+
//! Code that should be moved to libc once C libraries have a wrapper.
45+
46+
#![allow(dead_code, non_camel_case_types)]
47+
#![allow(clippy::unreadable_literal, clippy::missing_safety_doc)]
48+
49+
/// Helper macro to execute a system call that returns an `io::Result`.
50+
macro_rules! syscall {
51+
($fn: ident ( $($arg: expr),* $(,)? ) ) => {{
52+
let res = unsafe { libc::$fn($( $arg, )*) };
53+
if res == -1 {
54+
Err(std::io::Error::last_os_error())
55+
} else {
56+
Ok(res)
57+
}
58+
}};
59+
}
60+
61+
pub use syscall;
62+
pub use libc::*;
63+
64+
pub unsafe fn io_uring_setup(entries: c_uint, p: *mut io_uring_params) -> c_int {
65+
syscall(SYS_io_uring_setup, entries as c_long, p as c_long) as _
66+
}
67+
68+
pub unsafe fn io_uring_register(
69+
fd: c_int,
70+
opcode: c_uint,
71+
arg: *const c_void,
72+
nr_args: c_uint,
73+
) -> c_int {
74+
syscall(
75+
SYS_io_uring_register,
76+
fd as c_long,
77+
opcode as c_long,
78+
arg as c_long,
79+
nr_args as c_long,
80+
) as _
81+
}
82+
83+
pub unsafe fn io_uring_enter2(
84+
fd: c_int,
85+
to_submit: c_uint,
86+
min_complete: c_uint,
87+
flags: c_uint,
88+
arg: *const libc::c_void,
89+
size: usize,
90+
) -> c_int {
91+
syscall(
92+
SYS_io_uring_enter,
93+
fd as c_long,
94+
to_submit as c_long,
95+
min_complete as c_long,
96+
flags as c_long,
97+
arg as c_long,
98+
size as c_long,
99+
) as _
100+
}
101+
";

sys/include/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Put the header files here, see the README.
2+
*

sys/src/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Moved to `/src/sys.rs`.
2+
sys.rs

sys/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod sys;
2+
pub use sys::*;

0 commit comments

Comments
 (0)