io_uring_passthrough.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: basic read/write tests for io_uring passthrough commands
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "helpers.h"
  12. #include "liburing.h"
  13. #include "../src/syscall.h"
  14. #include "nvme.h"
  15. #define FILE_SIZE (256 * 1024)
  16. #define BS 8192
  17. #define BUFFERS (FILE_SIZE / BS)
  18. static struct iovec *vecs;
  19. static int no_pt;
  20. /*
  21. * Each offset in the file has the ((test_case / 2) * FILE_SIZE)
  22. * + (offset / sizeof(int)) stored for every
  23. * sizeof(int) address.
  24. */
  25. static int verify_buf(int tc, void *buf, off_t off)
  26. {
  27. int i, u_in_buf = BS / sizeof(unsigned int);
  28. unsigned int *ptr;
  29. off /= sizeof(unsigned int);
  30. off += (tc / 2) * FILE_SIZE;
  31. ptr = buf;
  32. for (i = 0; i < u_in_buf; i++) {
  33. if (off != *ptr) {
  34. fprintf(stderr, "Found %u, wanted %llu\n", *ptr,
  35. (unsigned long long) off);
  36. return 1;
  37. }
  38. ptr++;
  39. off++;
  40. }
  41. return 0;
  42. }
  43. static int fill_pattern(int tc)
  44. {
  45. unsigned int val, *ptr;
  46. int i, j;
  47. int u_in_buf = BS / sizeof(val);
  48. val = (tc / 2) * FILE_SIZE;
  49. for (i = 0; i < BUFFERS; i++) {
  50. ptr = vecs[i].iov_base;
  51. for (j = 0; j < u_in_buf; j++) {
  52. *ptr = val;
  53. val++;
  54. ptr++;
  55. }
  56. }
  57. return 0;
  58. }
  59. static int __test_io(const char *file, struct io_uring *ring, int tc, int read,
  60. int sqthread, int fixed, int nonvec)
  61. {
  62. struct io_uring_sqe *sqe;
  63. struct io_uring_cqe *cqe;
  64. struct nvme_uring_cmd *cmd;
  65. int open_flags;
  66. int do_fixed;
  67. int i, ret, fd = -1;
  68. off_t offset;
  69. __u64 slba;
  70. __u32 nlb;
  71. if (read)
  72. open_flags = O_RDONLY;
  73. else
  74. open_flags = O_WRONLY;
  75. if (fixed) {
  76. ret = t_register_buffers(ring, vecs, BUFFERS);
  77. if (ret == T_SETUP_SKIP)
  78. return 0;
  79. if (ret != T_SETUP_OK) {
  80. fprintf(stderr, "buffer reg failed: %d\n", ret);
  81. goto err;
  82. }
  83. }
  84. fd = open(file, open_flags);
  85. if (fd < 0) {
  86. if (errno == EACCES || errno == EPERM)
  87. return T_EXIT_SKIP;
  88. perror("file open");
  89. goto err;
  90. }
  91. if (sqthread) {
  92. ret = io_uring_register_files(ring, &fd, 1);
  93. if (ret) {
  94. fprintf(stderr, "file reg failed: %d\n", ret);
  95. goto err;
  96. }
  97. }
  98. if (!read)
  99. fill_pattern(tc);
  100. offset = 0;
  101. for (i = 0; i < BUFFERS; i++) {
  102. sqe = io_uring_get_sqe(ring);
  103. if (!sqe) {
  104. fprintf(stderr, "sqe get failed\n");
  105. goto err;
  106. }
  107. if (read) {
  108. int use_fd = fd;
  109. do_fixed = fixed;
  110. if (sqthread)
  111. use_fd = 0;
  112. if (fixed && (i & 1))
  113. do_fixed = 0;
  114. if (do_fixed) {
  115. io_uring_prep_read_fixed(sqe, use_fd, vecs[i].iov_base,
  116. vecs[i].iov_len,
  117. offset, i);
  118. sqe->cmd_op = NVME_URING_CMD_IO;
  119. } else if (nonvec) {
  120. io_uring_prep_read(sqe, use_fd, vecs[i].iov_base,
  121. vecs[i].iov_len, offset);
  122. sqe->cmd_op = NVME_URING_CMD_IO;
  123. } else {
  124. io_uring_prep_readv(sqe, use_fd, &vecs[i], 1,
  125. offset);
  126. sqe->cmd_op = NVME_URING_CMD_IO_VEC;
  127. }
  128. } else {
  129. int use_fd = fd;
  130. do_fixed = fixed;
  131. if (sqthread)
  132. use_fd = 0;
  133. if (fixed && (i & 1))
  134. do_fixed = 0;
  135. if (do_fixed) {
  136. io_uring_prep_write_fixed(sqe, use_fd, vecs[i].iov_base,
  137. vecs[i].iov_len,
  138. offset, i);
  139. sqe->cmd_op = NVME_URING_CMD_IO;
  140. } else if (nonvec) {
  141. io_uring_prep_write(sqe, use_fd, vecs[i].iov_base,
  142. vecs[i].iov_len, offset);
  143. sqe->cmd_op = NVME_URING_CMD_IO;
  144. } else {
  145. io_uring_prep_writev(sqe, use_fd, &vecs[i], 1,
  146. offset);
  147. sqe->cmd_op = NVME_URING_CMD_IO_VEC;
  148. }
  149. }
  150. sqe->opcode = IORING_OP_URING_CMD;
  151. if (do_fixed)
  152. sqe->uring_cmd_flags |= IORING_URING_CMD_FIXED;
  153. sqe->user_data = ((uint64_t)offset << 32) | i;
  154. if (sqthread)
  155. sqe->flags |= IOSQE_FIXED_FILE;
  156. cmd = (struct nvme_uring_cmd *)sqe->cmd;
  157. memset(cmd, 0, sizeof(struct nvme_uring_cmd));
  158. cmd->opcode = read ? nvme_cmd_read : nvme_cmd_write;
  159. slba = offset >> lba_shift;
  160. nlb = (BS >> lba_shift) - 1;
  161. /* cdw10 and cdw11 represent starting lba */
  162. cmd->cdw10 = slba & 0xffffffff;
  163. cmd->cdw11 = slba >> 32;
  164. /* cdw12 represent number of lba's for read/write */
  165. cmd->cdw12 = nlb;
  166. if (do_fixed || nonvec) {
  167. cmd->addr = (__u64)(uintptr_t)vecs[i].iov_base;
  168. cmd->data_len = vecs[i].iov_len;
  169. } else {
  170. cmd->addr = (__u64)(uintptr_t)&vecs[i];
  171. cmd->data_len = 1;
  172. }
  173. cmd->nsid = nsid;
  174. offset += BS;
  175. }
  176. ret = io_uring_submit(ring);
  177. if (ret != BUFFERS) {
  178. fprintf(stderr, "submit got %d, wanted %d\n", ret, BUFFERS);
  179. goto err;
  180. }
  181. for (i = 0; i < BUFFERS; i++) {
  182. ret = io_uring_wait_cqe(ring, &cqe);
  183. if (ret) {
  184. fprintf(stderr, "wait_cqe=%d\n", ret);
  185. goto err;
  186. }
  187. if (cqe->res != 0) {
  188. if (!no_pt) {
  189. no_pt = 1;
  190. goto skip;
  191. }
  192. fprintf(stderr, "cqe res %d, wanted 0\n", cqe->res);
  193. goto err;
  194. }
  195. io_uring_cqe_seen(ring, cqe);
  196. if (read) {
  197. int index = cqe->user_data & 0xffffffff;
  198. void *buf = vecs[index].iov_base;
  199. off_t voff = cqe->user_data >> 32;
  200. if (verify_buf(tc, buf, voff))
  201. goto err;
  202. }
  203. }
  204. if (fixed) {
  205. ret = io_uring_unregister_buffers(ring);
  206. if (ret) {
  207. fprintf(stderr, "buffer unreg failed: %d\n", ret);
  208. goto err;
  209. }
  210. }
  211. if (sqthread) {
  212. ret = io_uring_unregister_files(ring);
  213. if (ret) {
  214. fprintf(stderr, "file unreg failed: %d\n", ret);
  215. goto err;
  216. }
  217. }
  218. skip:
  219. close(fd);
  220. return 0;
  221. err:
  222. if (fd != -1)
  223. close(fd);
  224. return 1;
  225. }
  226. static int test_io(const char *file, int tc, int read, int sqthread,
  227. int fixed, int nonvec, int hybrid)
  228. {
  229. struct io_uring ring;
  230. int ret, ring_flags = 0;
  231. ring_flags |= IORING_SETUP_SQE128;
  232. ring_flags |= IORING_SETUP_CQE32;
  233. if (sqthread)
  234. ring_flags |= IORING_SETUP_SQPOLL;
  235. if (hybrid)
  236. ring_flags |= IORING_SETUP_IOPOLL | IORING_SETUP_HYBRID_IOPOLL;
  237. ret = t_create_ring(64, &ring, ring_flags);
  238. if (ret == T_SETUP_SKIP)
  239. return 0;
  240. if (ret != T_SETUP_OK) {
  241. if (ret == -EINVAL) {
  242. no_pt = 1;
  243. return T_SETUP_SKIP;
  244. }
  245. fprintf(stderr, "ring create failed: %d\n", ret);
  246. return 1;
  247. }
  248. ret = __test_io(file, &ring, tc, read, sqthread, fixed, nonvec);
  249. io_uring_queue_exit(&ring);
  250. return ret;
  251. }
  252. /*
  253. * Send a passthrough command that nvme will fail during submission.
  254. * This comes handy for testing error handling.
  255. */
  256. static int test_invalid_passthru_submit(const char *file)
  257. {
  258. struct io_uring ring;
  259. int fd, ret, ring_flags, open_flags;
  260. struct io_uring_cqe *cqe;
  261. struct io_uring_sqe *sqe;
  262. struct nvme_uring_cmd *cmd;
  263. ring_flags = IORING_SETUP_CQE32 | IORING_SETUP_SQE128;
  264. ret = t_create_ring(1, &ring, ring_flags);
  265. if (ret != T_SETUP_OK) {
  266. fprintf(stderr, "ring create failed: %d\n", ret);
  267. return 1;
  268. }
  269. open_flags = O_RDONLY;
  270. fd = open(file, open_flags);
  271. if (fd < 0) {
  272. perror("file open");
  273. goto err;
  274. }
  275. sqe = io_uring_get_sqe(&ring);
  276. io_uring_prep_read(sqe, fd, vecs[0].iov_base, vecs[0].iov_len, 0);
  277. sqe->cmd_op = NVME_URING_CMD_IO;
  278. sqe->opcode = IORING_OP_URING_CMD;
  279. sqe->user_data = 1;
  280. cmd = (struct nvme_uring_cmd *)sqe->cmd;
  281. memset(cmd, 0, sizeof(struct nvme_uring_cmd));
  282. cmd->opcode = nvme_cmd_read;
  283. cmd->addr = (__u64)(uintptr_t)&vecs[0].iov_base;
  284. cmd->data_len = vecs[0].iov_len;
  285. /* populate wrong nsid to force failure */
  286. cmd->nsid = nsid + 1;
  287. ret = io_uring_submit(&ring);
  288. if (ret != 1) {
  289. fprintf(stderr, "submit got %d, wanted %d\n", ret, 1);
  290. goto err;
  291. }
  292. ret = io_uring_wait_cqe(&ring, &cqe);
  293. if (ret) {
  294. fprintf(stderr, "wait_cqe=%d\n", ret);
  295. goto err;
  296. }
  297. if (cqe->res == 0) {
  298. fprintf(stderr, "cqe res %d, wanted failure\n", cqe->res);
  299. goto err;
  300. }
  301. io_uring_cqe_seen(&ring, cqe);
  302. close(fd);
  303. io_uring_queue_exit(&ring);
  304. return 0;
  305. err:
  306. if (fd != -1)
  307. close(fd);
  308. io_uring_queue_exit(&ring);
  309. return 1;
  310. }
  311. /*
  312. * if we are polling io_uring_submit needs to always enter the
  313. * kernel to fetch events
  314. */
  315. static int test_io_uring_submit_enters(const char *file)
  316. {
  317. struct io_uring ring;
  318. int fd, i, ret, ring_flags, open_flags;
  319. unsigned head;
  320. struct io_uring_cqe *cqe;
  321. struct nvme_uring_cmd *cmd;
  322. struct io_uring_sqe *sqe;
  323. ring_flags = IORING_SETUP_IOPOLL;
  324. ring_flags |= IORING_SETUP_SQE128;
  325. ring_flags |= IORING_SETUP_CQE32;
  326. ret = io_uring_queue_init(64, &ring, ring_flags);
  327. if (ret) {
  328. fprintf(stderr, "ring create failed: %d\n", ret);
  329. return 1;
  330. }
  331. open_flags = O_WRONLY;
  332. fd = open(file, open_flags);
  333. if (fd < 0) {
  334. perror("file open");
  335. goto err;
  336. }
  337. for (i = 0; i < BUFFERS; i++) {
  338. off_t offset = BS * (rand() % BUFFERS);
  339. __u64 slba;
  340. __u32 nlb;
  341. sqe = io_uring_get_sqe(&ring);
  342. io_uring_prep_readv(sqe, fd, &vecs[i], 1, offset);
  343. sqe->user_data = i;
  344. sqe->opcode = IORING_OP_URING_CMD;
  345. sqe->cmd_op = NVME_URING_CMD_IO;
  346. cmd = (struct nvme_uring_cmd *)sqe->cmd;
  347. memset(cmd, 0, sizeof(struct nvme_uring_cmd));
  348. slba = offset >> lba_shift;
  349. nlb = (BS >> lba_shift) - 1;
  350. cmd->opcode = nvme_cmd_read;
  351. cmd->cdw10 = slba & 0xffffffff;
  352. cmd->cdw11 = slba >> 32;
  353. cmd->cdw12 = nlb;
  354. cmd->addr = (__u64)(uintptr_t)&vecs[i];
  355. cmd->data_len = 1;
  356. cmd->nsid = nsid;
  357. }
  358. /* submit manually to avoid adding IORING_ENTER_GETEVENTS */
  359. ret = __sys_io_uring_enter(ring.ring_fd, __io_uring_flush_sq(&ring), 0,
  360. 0, NULL);
  361. if (ret < 0)
  362. goto err;
  363. for (i = 0; i < 500; i++) {
  364. ret = io_uring_submit(&ring);
  365. if (ret != 0) {
  366. fprintf(stderr, "still had %d sqes to submit\n", ret);
  367. goto err;
  368. }
  369. io_uring_for_each_cqe(&ring, head, cqe) {
  370. if (cqe->res == -EOPNOTSUPP)
  371. fprintf(stdout, "Device doesn't support polled IO\n");
  372. goto ok;
  373. }
  374. usleep(10000);
  375. }
  376. err:
  377. ret = 1;
  378. if (fd != -1)
  379. close(fd);
  380. ok:
  381. io_uring_queue_exit(&ring);
  382. return ret;
  383. }
  384. int main(int argc, char *argv[])
  385. {
  386. int i, ret;
  387. char *fname;
  388. if (argc < 2)
  389. return T_EXIT_SKIP;
  390. fname = argv[1];
  391. ret = nvme_get_info(fname);
  392. if (ret)
  393. return T_EXIT_SKIP;
  394. vecs = t_create_buffers(BUFFERS, BS);
  395. for (i = 0; i < 32; i++) {
  396. int read = (i & 1) != 0;
  397. int sqthread = (i & 2) != 0;
  398. int fixed = (i & 4) != 0;
  399. int nonvec = (i & 8) != 0;
  400. int hybrid = (i & 16) != 0;
  401. ret = test_io(fname, i, read, sqthread, fixed, nonvec, hybrid);
  402. if (no_pt)
  403. break;
  404. if (ret) {
  405. fprintf(stderr, "test_io failed %d/%d/%d/%d/%d\n",
  406. read, sqthread, fixed, nonvec, hybrid);
  407. goto err;
  408. }
  409. }
  410. if (no_pt)
  411. return T_EXIT_SKIP;
  412. ret = test_io_uring_submit_enters(fname);
  413. if (ret) {
  414. fprintf(stderr, "test_io_uring_submit_enters failed\n");
  415. goto err;
  416. }
  417. ret = test_invalid_passthru_submit(fname);
  418. if (ret) {
  419. fprintf(stderr, "test_invalid_passthru_submit failed\n");
  420. goto err;
  421. }
  422. return T_EXIT_PASS;
  423. err:
  424. return T_EXIT_FAIL;
  425. }