io_uring_passthrough.c 10.0 KB

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