无法编译 io_uring

Trouble compiling io_uring

我一直在阅读 https://kernel.dk/io_uring.pdf,我想尝试实际的系统调用(io_uring_setup、io_uring_enter)来检查我的理解,但我无法编译以下简单程序:

#include <kernel/io_uring.h>
#include <stdint.h>

int main() {
    struct io_uring_params p;
    int ring = io_uring_setup((__u32) 512, &p);
    return 0;
}

我收到 io_uring_setup 函数的隐式声明错误。手册页 https://manpages.debian.org/unstable/liburing-dev/io_uring_setup.2.en.html 建议唯一要包含的文件是 linux/io_uring.h,但是当我查看源代码时,我没有看到 io_uring_setup.[=14= 的定义]

(2021 年中)作为 @oakad stated in the comments the io_uring syscalls are not currently wrapped by libc. If a user wants to invoke raw io_uring syscalls (e.g. as described in io_uring_setup(2)) it is up to them to provide the additional boilerplate to do so and ensure they obey all the expected rules... Rather than doing everything by hand it looks easier to use liburingio_uring 包装器库)。

我不清楚你为什么选择使用

#include <kernel/io_uring.h>

-- 这看起来不对。我系统上的 header 被

找到
#include <linux/io_uring.h>

以下在我的系统上编译没有错误:

/* Needed for memset */
#include <stdio.h>
/* Needed for perror */
#include <string.h>
/* Needed to invoke the syscall */
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
/* Needed for io_uring data structures. Compilation will error with a "No such
 * file or directory" on the following include line if your kernel headers are
 * too old/don't know about io_uring. If this happens you have to manually 
 * declare all the io_uring structures/defines yourself. */
#include <linux/io_uring.h>
/* C libraries don't (yet) provide a pretty wrapper for the io_uring syscalls 
 * so create an io_uring_setup syscall wrapper ourselves */
int io_uring_setup(unsigned int entries, struct io_uring_params *p) {
    return syscall(__NR_io_uring_setup, entries, p);
}

int main() {
    int fd;
    struct io_uring_params p;

    memset(&p, 0, sizeof(p));
    fd = io_uring_setup(512, &p);

    if (fd < 0)
        perror("io_uring_setup");

    return 0;
}

然而,正如 Efficient IO with io_uring PDF this is just the tip of the iceberg when using io_uring via direct syscalls. The Lord of the io_uring tutorial has a section titled The Low-level io_uring Interface 中提到的,它更详细地描述了用法,但使用 io_uring 看起来既简单又安全。