From: Jens Axboe <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCHSET RFC for-next 0/2] Add direct descriptor ring passing
Date: Fri, 17 Jun 2022 08:07:37 -0600 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
[-- Attachment #1: Type: text/plain, Size: 377 bytes --]
On 6/17/22 7:45 AM, Jens Axboe wrote:
> Hi,
>
> One of the things we currently cannot do with direct descriptors is pass
> it to another application or ring. This adds support for doing so, through
> the IORING_OP_MSG_RING ring-to-ring messaging opcode.
>
> Some unresolved questions in patch 2 that need debating.
Here's the basic test case I wrote for it.
--
Jens Axboe
[-- Attachment #2: fd-pass.c --]
[-- Type: text/x-csrc, Size: 3798 bytes --]
/* SPDX-License-Identifier: MIT */
/*
* Description: run various fixed file fd passing tests
*
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "liburing.h"
#include "helpers.h"
#define FSIZE 128
#define PAT 0x9a
static int verify_fixed_read(struct io_uring *ring, int fixed_fd, int fail)
{
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
char buf[FSIZE];
int i;
sqe = io_uring_get_sqe(ring);
io_uring_prep_read(sqe, fixed_fd, buf, FSIZE, 0);
sqe->flags |= IOSQE_FIXED_FILE;
io_uring_submit(ring);
io_uring_wait_cqe(ring, &cqe);
if (cqe->res != FSIZE) {
if (fail && cqe->res == -EBADF)
return 0;
fprintf(stderr, "Read: %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(ring, cqe);
for (i = 0; i < FSIZE; i++) {
if (buf[i] != PAT) {
fprintf(stderr, "got %x, wanted %x\n", buf[i], PAT);
return 1;
}
}
return 0;
}
static int test(const char *filename)
{
struct io_uring sring, dring;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
int ret;
ret = io_uring_queue_init(8, &sring, 0);
if (ret) {
fprintf(stderr, "ring setup failed: %d\n", ret);
return 1;
}
ret = io_uring_queue_init(8, &dring, 0);
if (ret) {
fprintf(stderr, "ring setup failed: %d\n", ret);
return 1;
}
ret = io_uring_register_files_sparse(&sring, 8);
if (ret) {
fprintf(stderr, "register files failed %d\n", ret);
return 1;
}
ret = io_uring_register_files_sparse(&dring, 8);
if (ret) {
fprintf(stderr, "register files failed %d\n", ret);
return 1;
}
/* open direct descriptor */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_openat_direct(sqe, AT_FDCWD, filename, 0, 0644, 0);
io_uring_submit(&sring);
ret = io_uring_wait_cqe(&sring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe failed %d\n", ret);
return 1;
}
if (cqe->res) {
fprintf(stderr, "cqe res %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&sring, cqe);
/* verify data is sane for source ring */
if (verify_fixed_read(&sring, 0, 0))
return 1;
/* send direct descriptor to destination ring */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_msg_ring(sqe, dring.ring_fd, 0, 0x89, 0);
sqe->addr = 1;
sqe->addr3 = 0;
sqe->file_index = 1;
io_uring_submit(&sring);
ret = io_uring_wait_cqe(&sring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe failed %d\n", ret);
return 1;
}
if (cqe->res) {
fprintf(stderr, "msg_ring failed %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&sring, cqe);
/* get posted completion for the passing */
ret = io_uring_wait_cqe(&dring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe failed %d\n", ret);
return 1;
}
if (cqe->user_data != 0x89) {
fprintf(stderr, "bad user_data %ld\n", (long) cqe->res);
return 1;
}
io_uring_cqe_seen(&dring, cqe);
/* now verify we can read the sane data from the destination ring */
if (verify_fixed_read(&dring, 0, 0))
return 1;
/* close descriptor in source ring */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_close_direct(sqe, 0);
io_uring_submit(&sring);
ret = io_uring_wait_cqe(&sring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe failed %d\n", ret);
return 1;
}
if (cqe->res) {
fprintf(stderr, "direct close failed %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&sring, cqe);
/* check that source ring fails after close */
if (verify_fixed_read(&sring, 0, 1))
return 1;
/* check we can still read from destination ring */
if (verify_fixed_read(&dring, 0, 0))
return 1;
return 0;
}
int main(int argc, char *argv[])
{
char fname[80];
int ret;
if (argc > 1)
return 0;
sprintf(fname, ".fd-pass.%d", getpid());
t_create_file_pattern(fname, FSIZE, PAT);
ret = test(fname);
if (ret) {
fprintf(stderr, "test failed\n");
return ret;
}
return 0;
}
prev parent reply other threads:[~2022-06-17 14:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-17 13:45 [PATCHSET RFC for-next 0/2] Add direct descriptor ring passing Jens Axboe
2022-06-17 13:45 ` [PATCH 1/2] io_uring: split out fixed file installation and removal Jens Axboe
2022-06-17 13:45 ` [PATCH 2/2] io_uring: add support for passing fixed file descriptors Jens Axboe
2022-06-18 11:02 ` Hao Xu
2022-06-18 11:34 ` Jens Axboe
2022-06-18 12:47 ` Hao Xu
2022-06-18 12:50 ` Jens Axboe
2022-06-18 13:09 ` Hao Xu
2022-06-18 13:16 ` Jens Axboe
2022-06-18 13:27 ` Hao Xu
2022-06-17 14:07 ` Jens Axboe [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox