cq-full.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test CQ ring overflow
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include "liburing.h"
  14. #include "helpers.h"
  15. static int queue_n_nops(struct io_uring *ring, int n)
  16. {
  17. struct io_uring_sqe *sqe;
  18. int i, ret;
  19. for (i = 0; i < n; i++) {
  20. sqe = io_uring_get_sqe(ring);
  21. if (!sqe) {
  22. printf("get sqe failed\n");
  23. goto err;
  24. }
  25. io_uring_prep_nop(sqe);
  26. }
  27. ret = io_uring_submit(ring);
  28. if (ret < n) {
  29. printf("Submitted only %d\n", ret);
  30. goto err;
  31. } else if (ret < 0) {
  32. printf("sqe submit failed: %d\n", ret);
  33. goto err;
  34. }
  35. return 0;
  36. err:
  37. return 1;
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. struct io_uring_cqe *cqe;
  42. struct io_uring_params p;
  43. struct io_uring ring;
  44. int i, ret;
  45. if (argc > 1)
  46. return T_EXIT_SKIP;
  47. memset(&p, 0, sizeof(p));
  48. ret = io_uring_queue_init_params(4, &ring, &p);
  49. if (ret) {
  50. printf("ring setup failed\n");
  51. return T_EXIT_FAIL;
  52. }
  53. if (queue_n_nops(&ring, 4))
  54. goto err;
  55. if (queue_n_nops(&ring, 4))
  56. goto err;
  57. if (queue_n_nops(&ring, 4))
  58. goto err;
  59. i = 0;
  60. do {
  61. ret = io_uring_peek_cqe(&ring, &cqe);
  62. if (ret < 0) {
  63. if (ret == -EAGAIN)
  64. break;
  65. printf("wait completion %d\n", ret);
  66. goto err;
  67. }
  68. io_uring_cqe_seen(&ring, cqe);
  69. if (!cqe)
  70. break;
  71. i++;
  72. } while (1);
  73. if (i < 8 ||
  74. ((*ring.cq.koverflow != 4) && !(p.features & IORING_FEAT_NODROP))) {
  75. printf("CQ overflow fail: %d completions, %u overflow\n", i,
  76. *ring.cq.koverflow);
  77. goto err;
  78. }
  79. io_uring_queue_exit(&ring);
  80. return T_EXIT_PASS;
  81. err:
  82. io_uring_queue_exit(&ring);
  83. return T_EXIT_FAIL;
  84. }