io_uring_passthrough.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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)
  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. ret = t_create_ring(64, &ring, ring_flags);
  236. if (ret == T_SETUP_SKIP)
  237. return 0;
  238. if (ret != T_SETUP_OK) {
  239. if (ret == -EINVAL) {
  240. no_pt = 1;
  241. return T_SETUP_SKIP;
  242. }
  243. fprintf(stderr, "ring create failed: %d\n", ret);
  244. return 1;
  245. }
  246. ret = __test_io(file, &ring, tc, read, sqthread, fixed, nonvec);
  247. io_uring_queue_exit(&ring);
  248. return ret;
  249. }
  250. /*
  251. * Send a passthrough command that nvme will fail during submission.
  252. * This comes handy for testing error handling.
  253. */
  254. static int test_invalid_passthru_submit(const char *file)
  255. {
  256. struct io_uring ring;
  257. int fd, ret, ring_flags, open_flags;
  258. struct io_uring_cqe *cqe;
  259. struct io_uring_sqe *sqe;
  260. struct nvme_uring_cmd *cmd;
  261. ring_flags = IORING_SETUP_CQE32 | IORING_SETUP_SQE128;
  262. ret = t_create_ring(1, &ring, ring_flags);
  263. if (ret != T_SETUP_OK) {
  264. fprintf(stderr, "ring create failed: %d\n", ret);
  265. return 1;
  266. }
  267. open_flags = O_RDONLY;
  268. fd = open(file, open_flags);
  269. if (fd < 0) {
  270. perror("file open");
  271. goto err;
  272. }
  273. sqe = io_uring_get_sqe(&ring);
  274. io_uring_prep_read(sqe, fd, vecs[0].iov_base, vecs[0].iov_len, 0);
  275. sqe->cmd_op = NVME_URING_CMD_IO;
  276. sqe->opcode = IORING_OP_URING_CMD;
  277. sqe->user_data = 1;
  278. cmd = (struct nvme_uring_cmd *)sqe->cmd;
  279. memset(cmd, 0, sizeof(struct nvme_uring_cmd));
  280. cmd->opcode = nvme_cmd_read;
  281. cmd->addr = (__u64)(uintptr_t)&vecs[0].iov_base;
  282. cmd->data_len = vecs[0].iov_len;
  283. /* populate wrong nsid to force failure */
  284. cmd->nsid = nsid + 1;
  285. ret = io_uring_submit(&ring);
  286. if (ret != 1) {
  287. fprintf(stderr, "submit got %d, wanted %d\n", ret, 1);
  288. goto err;
  289. }
  290. ret = io_uring_wait_cqe(&ring, &cqe);
  291. if (ret) {
  292. fprintf(stderr, "wait_cqe=%d\n", ret);
  293. goto err;
  294. }
  295. if (cqe->res == 0) {
  296. fprintf(stderr, "cqe res %d, wanted failure\n", cqe->res);
  297. goto err;
  298. }
  299. io_uring_cqe_seen(&ring, cqe);
  300. close(fd);
  301. io_uring_queue_exit(&ring);
  302. return 0;
  303. err:
  304. if (fd != -1)
  305. close(fd);
  306. io_uring_queue_exit(&ring);
  307. return 1;
  308. }
  309. /*
  310. * if we are polling io_uring_submit needs to always enter the
  311. * kernel to fetch events
  312. */
  313. static int test_io_uring_submit_enters(const char *file)
  314. {
  315. struct io_uring ring;
  316. int fd, i, ret, ring_flags, open_flags;
  317. unsigned head;
  318. struct io_uring_cqe *cqe;
  319. struct nvme_uring_cmd *cmd;
  320. struct io_uring_sqe *sqe;
  321. ring_flags = IORING_SETUP_IOPOLL;
  322. ring_flags |= IORING_SETUP_SQE128;
  323. ring_flags |= IORING_SETUP_CQE32;
  324. ret = io_uring_queue_init(64, &ring, ring_flags);
  325. if (ret) {
  326. fprintf(stderr, "ring create failed: %d\n", ret);
  327. return 1;
  328. }
  329. open_flags = O_WRONLY;
  330. fd = open(file, open_flags);
  331. if (fd < 0) {
  332. perror("file open");
  333. goto err;
  334. }
  335. for (i = 0; i < BUFFERS; i++) {
  336. off_t offset = BS * (rand() % BUFFERS);
  337. __u64 slba;
  338. __u32 nlb;
  339. sqe = io_uring_get_sqe(&ring);
  340. io_uring_prep_readv(sqe, fd, &vecs[i], 1, offset);
  341. sqe->user_data = i;
  342. sqe->opcode = IORING_OP_URING_CMD;
  343. sqe->cmd_op = NVME_URING_CMD_IO;
  344. cmd = (struct nvme_uring_cmd *)sqe->cmd;
  345. memset(cmd, 0, sizeof(struct nvme_uring_cmd));
  346. slba = offset >> lba_shift;
  347. nlb = (BS >> lba_shift) - 1;
  348. cmd->opcode = nvme_cmd_read;
  349. cmd->cdw10 = slba & 0xffffffff;
  350. cmd->cdw11 = slba >> 32;
  351. cmd->cdw12 = nlb;
  352. cmd->addr = (__u64)(uintptr_t)&vecs[i];
  353. cmd->data_len = 1;
  354. cmd->nsid = nsid;
  355. }
  356. /* submit manually to avoid adding IORING_ENTER_GETEVENTS */
  357. ret = __sys_io_uring_enter(ring.ring_fd, __io_uring_flush_sq(&ring), 0,
  358. 0, NULL);
  359. if (ret < 0)
  360. goto err;
  361. for (i = 0; i < 500; i++) {
  362. ret = io_uring_submit(&ring);
  363. if (ret != 0) {
  364. fprintf(stderr, "still had %d sqes to submit\n", ret);
  365. goto err;
  366. }
  367. io_uring_for_each_cqe(&ring, head, cqe) {
  368. if (cqe->res == -EOPNOTSUPP)
  369. fprintf(stdout, "Device doesn't support polled IO\n");
  370. goto ok;
  371. }
  372. usleep(10000);
  373. }
  374. err:
  375. ret = 1;
  376. if (fd != -1)
  377. close(fd);
  378. ok:
  379. io_uring_queue_exit(&ring);
  380. return ret;
  381. }
  382. int main(int argc, char *argv[])
  383. {
  384. int i, ret;
  385. char *fname;
  386. if (argc < 2)
  387. return T_EXIT_SKIP;
  388. fname = argv[1];
  389. ret = nvme_get_info(fname);
  390. if (ret)
  391. return T_EXIT_SKIP;
  392. vecs = t_create_buffers(BUFFERS, BS);
  393. for (i = 0; i < 16; i++) {
  394. int read = (i & 1) != 0;
  395. int sqthread = (i & 2) != 0;
  396. int fixed = (i & 4) != 0;
  397. int nonvec = (i & 8) != 0;
  398. ret = test_io(fname, i, read, sqthread, fixed, nonvec);
  399. if (no_pt)
  400. break;
  401. if (ret) {
  402. fprintf(stderr, "test_io failed %d/%d/%d/%d\n",
  403. read, sqthread, fixed, nonvec);
  404. goto err;
  405. }
  406. }
  407. if (no_pt)
  408. return T_EXIT_SKIP;
  409. ret = test_io_uring_submit_enters(fname);
  410. if (ret) {
  411. fprintf(stderr, "test_io_uring_submit_enters failed\n");
  412. goto err;
  413. }
  414. ret = test_invalid_passthru_submit(fname);
  415. if (ret) {
  416. fprintf(stderr, "test_invalid_passthru_submit failed\n");
  417. goto err;
  418. }
  419. return T_EXIT_PASS;
  420. err:
  421. return T_EXIT_FAIL;
  422. }