splice.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/mman.h>
  10. #include "helpers.h"
  11. #include "liburing.h"
  12. #define BUF_SIZE (16 * 4096)
  13. struct test_ctx {
  14. int real_pipe1[2];
  15. int real_pipe2[2];
  16. int real_fd_in;
  17. int real_fd_out;
  18. /* fds or for registered files */
  19. int pipe1[2];
  20. int pipe2[2];
  21. int fd_in;
  22. int fd_out;
  23. void *buf_in;
  24. void *buf_out;
  25. };
  26. static unsigned int splice_flags = 0;
  27. static unsigned int sqe_flags = 0;
  28. static int has_splice = 0;
  29. static int has_tee = 0;
  30. static int read_buf(int fd, void *buf, int len)
  31. {
  32. int ret;
  33. while (len) {
  34. ret = read(fd, buf, len);
  35. if (ret < 0)
  36. return ret;
  37. len -= ret;
  38. buf += ret;
  39. }
  40. return 0;
  41. }
  42. static int write_buf(int fd, const void *buf, int len)
  43. {
  44. int ret;
  45. while (len) {
  46. ret = write(fd, buf, len);
  47. if (ret < 0)
  48. return ret;
  49. len -= ret;
  50. buf += ret;
  51. }
  52. return 0;
  53. }
  54. static int check_content(int fd, void *buf, int len, const void *src)
  55. {
  56. int ret;
  57. ret = read_buf(fd, buf, len);
  58. if (ret)
  59. return ret;
  60. ret = memcmp(buf, src, len);
  61. return (ret != 0) ? -1 : 0;
  62. }
  63. static int create_file(const char *filename)
  64. {
  65. int fd, save_errno;
  66. fd = open(filename, O_RDWR | O_CREAT, 0644);
  67. save_errno = errno;
  68. unlink(filename);
  69. errno = save_errno;
  70. return fd;
  71. }
  72. static int init_splice_ctx(struct test_ctx *ctx)
  73. {
  74. int ret, rnd_fd;
  75. ctx->buf_in = t_calloc(BUF_SIZE, 1);
  76. ctx->buf_out = t_calloc(BUF_SIZE, 1);
  77. ctx->fd_in = create_file(".splice-test-in");
  78. if (ctx->fd_in < 0) {
  79. perror("file open");
  80. return 1;
  81. }
  82. ctx->fd_out = create_file(".splice-test-out");
  83. if (ctx->fd_out < 0) {
  84. perror("file open");
  85. return 1;
  86. }
  87. /* get random data */
  88. rnd_fd = open("/dev/urandom", O_RDONLY);
  89. if (rnd_fd < 0)
  90. return 1;
  91. ret = read_buf(rnd_fd, ctx->buf_in, BUF_SIZE);
  92. if (ret != 0)
  93. return 1;
  94. close(rnd_fd);
  95. /* populate file */
  96. ret = write_buf(ctx->fd_in, ctx->buf_in, BUF_SIZE);
  97. if (ret)
  98. return ret;
  99. if (pipe(ctx->pipe1) < 0)
  100. return 1;
  101. if (pipe(ctx->pipe2) < 0)
  102. return 1;
  103. ctx->real_pipe1[0] = ctx->pipe1[0];
  104. ctx->real_pipe1[1] = ctx->pipe1[1];
  105. ctx->real_pipe2[0] = ctx->pipe2[0];
  106. ctx->real_pipe2[1] = ctx->pipe2[1];
  107. ctx->real_fd_in = ctx->fd_in;
  108. ctx->real_fd_out = ctx->fd_out;
  109. return 0;
  110. }
  111. static int do_splice_op(struct io_uring *ring,
  112. int fd_in, loff_t off_in,
  113. int fd_out, loff_t off_out,
  114. unsigned int len,
  115. __u8 opcode)
  116. {
  117. struct io_uring_cqe *cqe;
  118. struct io_uring_sqe *sqe;
  119. int ret = -1;
  120. do {
  121. sqe = io_uring_get_sqe(ring);
  122. if (!sqe) {
  123. fprintf(stderr, "get sqe failed\n");
  124. return -1;
  125. }
  126. io_uring_prep_splice(sqe, fd_in, off_in, fd_out, off_out,
  127. len, splice_flags);
  128. sqe->flags |= sqe_flags;
  129. sqe->user_data = 42;
  130. sqe->opcode = opcode;
  131. ret = io_uring_submit(ring);
  132. if (ret != 1) {
  133. fprintf(stderr, "sqe submit failed: %d\n", ret);
  134. return ret;
  135. }
  136. ret = io_uring_wait_cqe(ring, &cqe);
  137. if (ret < 0) {
  138. fprintf(stderr, "wait completion %d\n", cqe->res);
  139. return ret;
  140. }
  141. if (cqe->res <= 0) {
  142. io_uring_cqe_seen(ring, cqe);
  143. return cqe->res;
  144. }
  145. len -= cqe->res;
  146. if (off_in != -1)
  147. off_in += cqe->res;
  148. if (off_out != -1)
  149. off_out += cqe->res;
  150. io_uring_cqe_seen(ring, cqe);
  151. } while (len);
  152. return 0;
  153. }
  154. static int do_splice(struct io_uring *ring,
  155. int fd_in, loff_t off_in,
  156. int fd_out, loff_t off_out,
  157. unsigned int len)
  158. {
  159. return do_splice_op(ring, fd_in, off_in, fd_out, off_out, len,
  160. IORING_OP_SPLICE);
  161. }
  162. static int do_tee(struct io_uring *ring, int fd_in, int fd_out,
  163. unsigned int len)
  164. {
  165. return do_splice_op(ring, fd_in, 0, fd_out, 0, len, IORING_OP_TEE);
  166. }
  167. static void check_splice_support(struct io_uring *ring, struct test_ctx *ctx)
  168. {
  169. int ret;
  170. ret = do_splice(ring, -1, 0, -1, 0, BUF_SIZE);
  171. has_splice = (ret == -EBADF);
  172. }
  173. static void check_tee_support(struct io_uring *ring, struct test_ctx *ctx)
  174. {
  175. int ret;
  176. ret = do_tee(ring, -1, -1, BUF_SIZE);
  177. has_tee = (ret == -EBADF);
  178. }
  179. static int check_zero_splice(struct io_uring *ring, struct test_ctx *ctx)
  180. {
  181. int ret;
  182. ret = do_splice(ring, ctx->fd_in, -1, ctx->pipe1[1], -1, 0);
  183. if (ret)
  184. return ret;
  185. ret = do_splice(ring, ctx->pipe2[0], -1, ctx->pipe1[1], -1, 0);
  186. if (ret)
  187. return ret;
  188. return 0;
  189. }
  190. static int splice_to_pipe(struct io_uring *ring, struct test_ctx *ctx)
  191. {
  192. int ret;
  193. ret = lseek(ctx->real_fd_in, 0, SEEK_SET);
  194. if (ret)
  195. return ret;
  196. /* implicit file offset */
  197. ret = do_splice(ring, ctx->fd_in, -1, ctx->pipe1[1], -1, BUF_SIZE);
  198. if (ret)
  199. return ret;
  200. ret = check_content(ctx->real_pipe1[0], ctx->buf_out, BUF_SIZE,
  201. ctx->buf_in);
  202. if (ret)
  203. return ret;
  204. /* explicit file offset */
  205. ret = do_splice(ring, ctx->fd_in, 0, ctx->pipe1[1], -1, BUF_SIZE);
  206. if (ret)
  207. return ret;
  208. return check_content(ctx->real_pipe1[0], ctx->buf_out, BUF_SIZE,
  209. ctx->buf_in);
  210. }
  211. static int splice_from_pipe(struct io_uring *ring, struct test_ctx *ctx)
  212. {
  213. int ret;
  214. ret = write_buf(ctx->real_pipe1[1], ctx->buf_in, BUF_SIZE);
  215. if (ret)
  216. return ret;
  217. ret = do_splice(ring, ctx->pipe1[0], -1, ctx->fd_out, 0, BUF_SIZE);
  218. if (ret)
  219. return ret;
  220. ret = check_content(ctx->real_fd_out, ctx->buf_out, BUF_SIZE,
  221. ctx->buf_in);
  222. if (ret)
  223. return ret;
  224. ret = ftruncate(ctx->real_fd_out, 0);
  225. if (ret)
  226. return ret;
  227. return lseek(ctx->real_fd_out, 0, SEEK_SET);
  228. }
  229. static int splice_pipe_to_pipe(struct io_uring *ring, struct test_ctx *ctx)
  230. {
  231. int ret;
  232. ret = do_splice(ring, ctx->fd_in, 0, ctx->pipe1[1], -1, BUF_SIZE);
  233. if (ret)
  234. return ret;
  235. ret = do_splice(ring, ctx->pipe1[0], -1, ctx->pipe2[1], -1, BUF_SIZE);
  236. if (ret)
  237. return ret;
  238. return check_content(ctx->real_pipe2[0], ctx->buf_out, BUF_SIZE,
  239. ctx->buf_in);
  240. }
  241. static int fail_splice_pipe_offset(struct io_uring *ring, struct test_ctx *ctx)
  242. {
  243. int ret;
  244. ret = do_splice(ring, ctx->fd_in, 0, ctx->pipe1[1], 0, BUF_SIZE);
  245. if (ret != -ESPIPE && ret != -EINVAL)
  246. return ret;
  247. ret = do_splice(ring, ctx->pipe1[0], 0, ctx->fd_out, 0, BUF_SIZE);
  248. if (ret != -ESPIPE && ret != -EINVAL)
  249. return ret;
  250. return 0;
  251. }
  252. static int fail_tee_nonpipe(struct io_uring *ring, struct test_ctx *ctx)
  253. {
  254. int ret;
  255. ret = do_tee(ring, ctx->fd_in, ctx->pipe1[1], BUF_SIZE);
  256. if (ret != -ESPIPE && ret != -EINVAL)
  257. return ret;
  258. return 0;
  259. }
  260. static int fail_tee_offset(struct io_uring *ring, struct test_ctx *ctx)
  261. {
  262. int ret;
  263. ret = do_splice_op(ring, ctx->pipe2[0], -1, ctx->pipe1[1], 0,
  264. BUF_SIZE, IORING_OP_TEE);
  265. if (ret != -ESPIPE && ret != -EINVAL)
  266. return ret;
  267. ret = do_splice_op(ring, ctx->pipe2[0], 0, ctx->pipe1[1], -1,
  268. BUF_SIZE, IORING_OP_TEE);
  269. if (ret != -ESPIPE && ret != -EINVAL)
  270. return ret;
  271. return 0;
  272. }
  273. static int check_tee(struct io_uring *ring, struct test_ctx *ctx)
  274. {
  275. int ret;
  276. ret = write_buf(ctx->real_pipe1[1], ctx->buf_in, BUF_SIZE);
  277. if (ret)
  278. return ret;
  279. ret = do_tee(ring, ctx->pipe1[0], ctx->pipe2[1], BUF_SIZE);
  280. if (ret)
  281. return ret;
  282. ret = check_content(ctx->real_pipe1[0], ctx->buf_out, BUF_SIZE,
  283. ctx->buf_in);
  284. if (ret) {
  285. fprintf(stderr, "tee(), invalid src data\n");
  286. return ret;
  287. }
  288. ret = check_content(ctx->real_pipe2[0], ctx->buf_out, BUF_SIZE,
  289. ctx->buf_in);
  290. if (ret) {
  291. fprintf(stderr, "tee(), invalid dst data\n");
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. static int check_zero_tee(struct io_uring *ring, struct test_ctx *ctx)
  297. {
  298. return do_tee(ring, ctx->pipe2[0], ctx->pipe1[1], 0);
  299. }
  300. static int test_splice(struct io_uring *ring, struct test_ctx *ctx)
  301. {
  302. int ret;
  303. if (has_splice) {
  304. ret = check_zero_splice(ring, ctx);
  305. if (ret) {
  306. fprintf(stderr, "check_zero_splice failed %i %i\n",
  307. ret, errno);
  308. return ret;
  309. }
  310. ret = splice_to_pipe(ring, ctx);
  311. if (ret) {
  312. fprintf(stderr, "splice_to_pipe failed %i %i\n",
  313. ret, errno);
  314. return ret;
  315. }
  316. ret = splice_from_pipe(ring, ctx);
  317. if (ret) {
  318. fprintf(stderr, "splice_from_pipe failed %i %i\n",
  319. ret, errno);
  320. return ret;
  321. }
  322. ret = splice_pipe_to_pipe(ring, ctx);
  323. if (ret) {
  324. fprintf(stderr, "splice_pipe_to_pipe failed %i %i\n",
  325. ret, errno);
  326. return ret;
  327. }
  328. ret = fail_splice_pipe_offset(ring, ctx);
  329. if (ret) {
  330. fprintf(stderr, "fail_splice_pipe_offset failed %i %i\n",
  331. ret, errno);
  332. return ret;
  333. }
  334. }
  335. if (has_tee) {
  336. ret = check_zero_tee(ring, ctx);
  337. if (ret) {
  338. fprintf(stderr, "check_zero_tee() failed %i %i\n",
  339. ret, errno);
  340. return ret;
  341. }
  342. ret = fail_tee_nonpipe(ring, ctx);
  343. if (ret) {
  344. fprintf(stderr, "fail_tee_nonpipe() failed %i %i\n",
  345. ret, errno);
  346. return ret;
  347. }
  348. ret = fail_tee_offset(ring, ctx);
  349. if (ret) {
  350. fprintf(stderr, "fail_tee_offset failed %i %i\n",
  351. ret, errno);
  352. return ret;
  353. }
  354. ret = check_tee(ring, ctx);
  355. if (ret) {
  356. fprintf(stderr, "check_tee() failed %i %i\n",
  357. ret, errno);
  358. return ret;
  359. }
  360. }
  361. return 0;
  362. }
  363. int main(int argc, char *argv[])
  364. {
  365. struct io_uring ring;
  366. struct io_uring_params p = { };
  367. struct test_ctx ctx;
  368. int ret;
  369. int reg_fds[6];
  370. if (argc > 1)
  371. return 0;
  372. ret = io_uring_queue_init_params(8, &ring, &p);
  373. if (ret) {
  374. fprintf(stderr, "ring setup failed\n");
  375. return 1;
  376. }
  377. if (!(p.features & IORING_FEAT_FAST_POLL)) {
  378. fprintf(stdout, "No splice support, skipping\n");
  379. return 0;
  380. }
  381. ret = init_splice_ctx(&ctx);
  382. if (ret) {
  383. fprintf(stderr, "init failed %i %i\n", ret, errno);
  384. return 1;
  385. }
  386. check_splice_support(&ring, &ctx);
  387. if (!has_splice)
  388. fprintf(stdout, "skip, doesn't support splice()\n");
  389. check_tee_support(&ring, &ctx);
  390. if (!has_tee)
  391. fprintf(stdout, "skip, doesn't support tee()\n");
  392. ret = test_splice(&ring, &ctx);
  393. if (ret) {
  394. fprintf(stderr, "basic splice tests failed\n");
  395. return ret;
  396. }
  397. reg_fds[0] = ctx.real_pipe1[0];
  398. reg_fds[1] = ctx.real_pipe1[1];
  399. reg_fds[2] = ctx.real_pipe2[0];
  400. reg_fds[3] = ctx.real_pipe2[1];
  401. reg_fds[4] = ctx.real_fd_in;
  402. reg_fds[5] = ctx.real_fd_out;
  403. ret = io_uring_register_files(&ring, reg_fds, 6);
  404. if (ret) {
  405. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  406. return 1;
  407. }
  408. /* remap fds to registered */
  409. ctx.pipe1[0] = 0;
  410. ctx.pipe1[1] = 1;
  411. ctx.pipe2[0] = 2;
  412. ctx.pipe2[1] = 3;
  413. ctx.fd_in = 4;
  414. ctx.fd_out = 5;
  415. splice_flags = SPLICE_F_FD_IN_FIXED;
  416. sqe_flags = IOSQE_FIXED_FILE;
  417. ret = test_splice(&ring, &ctx);
  418. if (ret) {
  419. fprintf(stderr, "registered fds splice tests failed\n");
  420. return ret;
  421. }
  422. return 0;
  423. }