Skip to contentSkip to conversation

splice helpers + tests

PATCH In review Pavel Begunkov 2 participants 10 messages Feb 24, 2020 io_uring

lore
Share & tools
lore
#0 lore raw

v5 cover splice helpers + tests

Add splice prep helpers and some basic tests.

P.S. I haven't tried the skip part with splice-less kernel

v2: minor: fix comment, add inline, remove newline

v3: type changes (Stefan Metzmacher)
update io_uring.h

v4: rebase

v5: skip if not supported (Jens)
print into stderr (Jens)

Pavel Begunkov (2):
splice: add splice(2) helpers
test/splice: add basic splice tests

 src/include/liburing.h          |  12 +++
 src/include/liburing/io_uring.h |  14 ++-
 test/Makefile                   |   5 +-
 test/splice.c                   | 148 ++++++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 3 deletions(-)
 create mode 100644 test/splice.c
Signature · 1 line
2.24.0
#1 lore raw

PATCH v5 1/2 splice: add splice(2) helpers

Add splice helpers and update io_uring.h

---
 src/include/liburing.h          | 12 ++++++++++++
 src/include/liburing/io_uring.h | 14 +++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
src/include/liburing.h +12 -0
index b8bcdf5..7edd4f1 100644
@@ -191,6 +191,18 @@ static inline void io_uring_prep_rw(int op, struct io_uring_sqe *sqe, int fd,
 	sqe->__pad2[0] = sqe->__pad2[1] = sqe->__pad2[2] = 0;
 }
 
+static inline void io_uring_prep_splice(struct io_uring_sqe *sqe,
+					int fd_in, loff_t off_in,
+					int fd_out, loff_t off_out,
+					unsigned int nbytes,
+					unsigned int splice_flags)
+{
+	io_uring_prep_rw(IORING_OP_SPLICE, sqe, fd_out, (void *)off_in,
+			 nbytes, off_out);
+	sqe->splice_fd_in = fd_in;
+	sqe->splice_flags = splice_flags;
+}
+
 static inline void io_uring_prep_readv(struct io_uring_sqe *sqe, int fd,
 				       const struct iovec *iovecs,
 				       unsigned nr_vecs, off_t offset)
src/include/liburing/io_uring.h +13 -1
index 7e7c2d9..a3d5e25 100644
@@ -23,7 +23,10 @@ struct io_uring_sqe {
 		__u64	off;	/* offset into file */
 		__u64	addr2;
 	};
-	__u64	addr;		/* pointer to buffer or iovecs */
+	union {
+		__u64	addr;	/* pointer to buffer or iovecs */
+		__u64	splice_off_in;
+	};
 	__u32	len;		/* buffer size or number of iovecs */
 	union {
 		__kernel_rwf_t	rw_flags;
@@ -37,6 +40,7 @@ struct io_uring_sqe {
 		__u32		open_flags;
 		__u32		statx_flags;
 		__u32		fadvise_advice;
+		__u32		splice_flags;
 	};
 	__u64	user_data;	/* data to be passed back at completion time */
 	union {
@@ -45,6 +49,7 @@ struct io_uring_sqe {
 			__u16	buf_index;
 			/* personality to use, if used */
 			__u16	personality;
+			__s32	splice_fd_in;
 		};
 		__u64	__pad2[3];
 	};
@@ -113,6 +118,7 @@ enum {
 	IORING_OP_RECV,
 	IORING_OP_OPENAT2,
 	IORING_OP_EPOLL_CTL,
+	IORING_OP_SPLICE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
@@ -128,6 +134,12 @@ enum {
  */
 #define IORING_TIMEOUT_ABS	(1U << 0)
 
+/*
+ * sqe->splice_flags
+ * extends splice(2) flags
+ */
+#define SPLICE_F_FD_IN_FIXED	(1U << 31) /* the last bit of __u32 */
+
 /*
  * IO completion data structure (Completion Queue Entry)
  */
Signature · 1 line
2.24.0
#2 lore raw

PATCH v5 2/2 test/splice: add basic splice tests

In reply to Pavel Begunkov
---
 test/Makefile |   5 +-
 test/splice.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+), 2 deletions(-)
 create mode 100644 test/splice.c
test/Makefile +3 -2
index 09c7aa2..8437f31 100644
@@ -20,7 +20,7 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register
 		connect 7ad0e4b2f83c-test submit-reuse fallocate open-close \
 		file-update statx accept-reuse poll-v-poll fadvise madvise \
 		short-read openat2 probe shared-wq personality eventfd \
-		send_recv eventfd-ring across-fork sq-poll-kthread
+		send_recv eventfd-ring across-fork sq-poll-kthread splice
 
 include ../Makefile.quiet
 
@@ -47,7 +47,8 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \
 	7ad0e4b2f83c-test.c submit-reuse.c fallocate.c open-close.c \
 	file-update.c statx.c accept-reuse.c poll-v-poll.c fadvise.c \
 	madvise.c short-read.c openat2.c probe.c shared-wq.c \
-	personality.c eventfd.c eventfd-ring.c across-fork.c sq-poll-kthread.c
+	personality.c eventfd.c eventfd-ring.c across-fork.c sq-poll-kthread.c \
+	splice.c
 
 test_objs := $(patsubst %.c,%.ol,$(test_srcs))
 
test/splice.c new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
2.24.0
maintainer
#3 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 23 lines · Pavel Begunkov
test/splice.c new file +23 -0
+static int copy_single(struct io_uring *ring,
+			int fd_in, loff_t off_in,
+			int fd_out, loff_t off_out,
+			int pipe_fds[2],
+			unsigned int len,
+			unsigned flags1, unsigned flags2)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int i, ret = -1;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, fd_in, off_in, pipe_fds[1], -1,
+			     len, flags1);
+	sqe->flags = IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");

test/splice.c line 45

Quoted 10 lines
test/splice.c new file +10 -0
+		return -1;
+	}
+	io_uring_prep_splice(sqe, pipe_fds[0], -1, fd_out, off_out,
+			     len, flags2);
+
+	ret = io_uring_submit(ring);
+	if (ret <= 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}

This seems wrong, you prep one and submit, the right return value would be 1. This check should be < 1, not <= 1. I'll make the change, rest looks good to me. Thanks!

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 30 lines · Jens Axboe
Quoted 29 lines · Pavel Begunkov
test/splice.c new file +29 -0
+static int copy_single(struct io_uring *ring,
+			int fd_in, loff_t off_in,
+			int fd_out, loff_t off_out,
+			int pipe_fds[2],
+			unsigned int len,
+			unsigned flags1, unsigned flags2)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int i, ret = -1;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, fd_in, off_in, pipe_fds[1], -1,
+			     len, flags1);
+	sqe->flags = IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, pipe_fds[0], -1, fd_out, off_out,
+			     len, flags2);
+
+	ret = io_uring_submit(ring);
Quoted 9 lines

test/splice.c line 45

Quoted 4 lines
test/splice.c new file +4 -0
+	if (ret <= 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}

This seems wrong, you prep one and submit, the right return value would be 1. This check should be < 1, not <= 1. I'll make the change, rest looks good to me. Thanks!

There are 2 sqes, "fd_in -> pipe" and "pipe -> fd_out".

Signature · 1 line
Pavel Begunkov
maintainer
#5 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 41 lines · Pavel Begunkov
Quoted 30 lines · Jens Axboe
Quoted 29 lines · Pavel Begunkov
test/splice.c new file +29 -0
+static int copy_single(struct io_uring *ring,
+			int fd_in, loff_t off_in,
+			int fd_out, loff_t off_out,
+			int pipe_fds[2],
+			unsigned int len,
+			unsigned flags1, unsigned flags2)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int i, ret = -1;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, fd_in, off_in, pipe_fds[1], -1,
+			     len, flags1);
+	sqe->flags = IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, pipe_fds[0], -1, fd_out, off_out,
+			     len, flags2);
+
+	ret = io_uring_submit(ring);
Quoted 9 lines

test/splice.c line 45

Quoted 4 lines
test/splice.c new file +4 -0
+	if (ret <= 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}

This seems wrong, you prep one and submit, the right return value would be 1. This check should be < 1, not <= 1. I'll make the change, rest looks good to me. Thanks!

Quoted 1 line

There are 2 sqes, "fd_in -> pipe" and "pipe -> fd_out".

I must be blind... I guess the failure case is that it doesn't work so well on the kernels not supporting splice, as we'll return 1 there as the first submit fails. I'll clean up that bit.

Signature · 1 line
Jens Axboe
#6 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 42 lines · Jens Axboe
Quoted 41 lines · Pavel Begunkov
Quoted 39 lines · Jens Axboe
Quoted 23 lines · Pavel Begunkov
test/splice.c new file +23 -0
+static int copy_single(struct io_uring *ring,
+			int fd_in, loff_t off_in,
+			int fd_out, loff_t off_out,
+			int pipe_fds[2],
+			unsigned int len,
+			unsigned flags1, unsigned flags2)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int i, ret = -1;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, fd_in, off_in, pipe_fds[1], -1,
+			     len, flags1);
+	sqe->flags = IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");

test/splice.c line 45

Quoted 10 lines
test/splice.c new file +10 -0
+		return -1;
+	}
+	io_uring_prep_splice(sqe, pipe_fds[0], -1, fd_out, off_out,
+			     len, flags2);
+
+	ret = io_uring_submit(ring);
+	if (ret <= 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}

This seems wrong, you prep one and submit, the right return value would be 1. This check should be < 1, not <= 1. I'll make the change, rest looks good to me. Thanks!

Quoted 4 lines
Quoted 1 line

There are 2 sqes, "fd_in -> pipe" and "pipe -> fd_out".

I must be blind... I guess the failure case is that it doesn't work so well on the kernels not supporting splice, as we'll return 1 there as the first

I've wanted for long to kill this weird behaviour, it should consume the whole link. Can't imagine any userspace app handling all edge-case errors right...

Quoted 1 line

submit fails. I'll clean up that bit.

...I should have tested better. Thanks!

Signature · 1 line
Pavel Begunkov
maintainer
#7 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 43 lines · Pavel Begunkov
Quoted 42 lines · Jens Axboe
Quoted 41 lines · Pavel Begunkov
Quoted 39 lines · Jens Axboe
Quoted 23 lines · Pavel Begunkov
test/splice.c new file +23 -0
+static int copy_single(struct io_uring *ring,
+			int fd_in, loff_t off_in,
+			int fd_out, loff_t off_out,
+			int pipe_fds[2],
+			unsigned int len,
+			unsigned flags1, unsigned flags2)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	int i, ret = -1;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");
+		return -1;
+	}
+	io_uring_prep_splice(sqe, fd_in, off_in, pipe_fds[1], -1,
+			     len, flags1);
+	sqe->flags = IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(ring);
+	if (!sqe) {
+		fprintf(stderr, "get sqe failed\n");

test/splice.c line 45

Quoted 10 lines
test/splice.c new file +10 -0
+		return -1;
+	}
+	io_uring_prep_splice(sqe, pipe_fds[0], -1, fd_out, off_out,
+			     len, flags2);
+
+	ret = io_uring_submit(ring);
+	if (ret <= 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}

This seems wrong, you prep one and submit, the right return value would be 1. This check should be < 1, not <= 1. I'll make the change, rest looks good to me. Thanks!

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

There are 2 sqes, "fd_in -> pipe" and "pipe -> fd_out".

I must be blind... I guess the failure case is that it doesn't work so well on the kernels not supporting splice, as we'll return 1 there as the first

I've wanted for long to kill this weird behaviour, it should consume the whole link. Can't imagine any userspace app handling all edge-case errors right...

Yeah, for links it makes sense to error the chain, which would consume the whole chain too.

Quoted 3 lines
Quoted 1 line

submit fails. I'll clean up that bit.

...I should have tested better. Thanks!

No worries, just trying to do better than we have in the best so we can have some vague hope of having the test suite pass on older stable kernels.

Signature · 1 line
Jens Axboe
#8 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 6 lines · Jens Axboe
Quoted 2 lines

I've wanted for long to kill this weird behaviour, it should consume the whole link. Can't imagine any userspace app handling all edge-case errors right...

Yeah, for links it makes sense to error the chain, which would consume the whole chain too.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

submit fails. I'll clean up that bit.

...I should have tested better. Thanks!

No worries, just trying to do better than we have in the best so we can have some vague hope of having the test suite pass on older stable kernels.

Have you gave a thought to using C++ for testing? It would be really nice to have some flexible test generator removing boilerplate and allowing automatically try different flags combinations. It sounds like a lot of pain doing this in old plain C.

Signature · 1 line
Pavel Begunkov
maintainer
#9 lore raw

Re: [PATCH liburing v5 2/2] test/splice: add basic splice tests

Quoted 11 lines · Pavel Begunkov
Quoted 10 lines · Jens Axboe
Quoted 2 lines

I've wanted for long to kill this weird behaviour, it should consume the whole link. Can't imagine any userspace app handling all edge-case errors right...

Yeah, for links it makes sense to error the chain, which would consume the whole chain too.

Quoted 3 lines
Quoted 1 line

submit fails. I'll clean up that bit.

...I should have tested better. Thanks!

Quoted 8 lines
Quoted 3 lines

No worries, just trying to do better than we have in the best so we can have some vague hope of having the test suite pass on older stable kernels.

Have you gave a thought to using C++ for testing? It would be really nice to have some flexible test generator removing boilerplate and allowing automatically try different flags combinations. It sounds like a lot of pain doing this in old plain C.

I'm not a c++ guy, but if it's useful, then I'm open to anything.

Signature · 1 line
Jens Axboe