probe.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test IORING_REGISTER_PROBE
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include "helpers.h"
  13. #include "liburing.h"
  14. static int no_probe;
  15. static int verify_probe(struct io_uring_probe *p, int full)
  16. {
  17. if (!full && p->ops_len) {
  18. fprintf(stderr, "Got ops_len=%u\n", p->ops_len);
  19. return 1;
  20. }
  21. if (!p->last_op) {
  22. fprintf(stderr, "Got last_op=%u\n", p->last_op);
  23. return 1;
  24. }
  25. if (!full)
  26. return 0;
  27. /* check a few ops that must be supported */
  28. if (!(p->ops[IORING_OP_NOP].flags & IO_URING_OP_SUPPORTED)) {
  29. fprintf(stderr, "NOP not supported!?\n");
  30. return 1;
  31. }
  32. if (!(p->ops[IORING_OP_READV].flags & IO_URING_OP_SUPPORTED)) {
  33. fprintf(stderr, "READV not supported!?\n");
  34. return 1;
  35. }
  36. if (!(p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED)) {
  37. fprintf(stderr, "WRITE not supported!?\n");
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. static int test_probe_helper(struct io_uring *ring)
  43. {
  44. int ret;
  45. struct io_uring_probe *p;
  46. p = io_uring_get_probe_ring(ring);
  47. if (!p) {
  48. fprintf(stderr, "Failed getting probe data\n");
  49. return 1;
  50. }
  51. ret = verify_probe(p, 1);
  52. io_uring_free_probe(p);
  53. return ret;
  54. }
  55. static int test_probe(struct io_uring *ring)
  56. {
  57. struct io_uring_probe *p;
  58. size_t len;
  59. int ret;
  60. len = sizeof(*p) + 256 * sizeof(struct io_uring_probe_op);
  61. p = t_calloc(1, len);
  62. ret = io_uring_register_probe(ring, p, 0);
  63. if (ret == -EINVAL) {
  64. fprintf(stdout, "Probe not supported, skipping\n");
  65. no_probe = 1;
  66. goto out;
  67. } else if (ret) {
  68. fprintf(stdout, "Probe returned %d\n", ret);
  69. goto err;
  70. }
  71. if (verify_probe(p, 0))
  72. goto err;
  73. /* now grab for all entries */
  74. memset(p, 0, len);
  75. ret = io_uring_register_probe(ring, p, 256);
  76. if (ret == -EINVAL) {
  77. fprintf(stdout, "Probe not supported, skipping\n");
  78. goto err;
  79. } else if (ret) {
  80. fprintf(stdout, "Probe returned %d\n", ret);
  81. goto err;
  82. }
  83. if (verify_probe(p, 1))
  84. goto err;
  85. out:
  86. free(p);
  87. return 0;
  88. err:
  89. free(p);
  90. return 1;
  91. }
  92. int main(int argc, char *argv[])
  93. {
  94. struct io_uring ring;
  95. int ret;
  96. if (argc > 1)
  97. return 0;
  98. ret = io_uring_queue_init(8, &ring, 0);
  99. if (ret) {
  100. fprintf(stderr, "ring setup failed\n");
  101. return 1;
  102. }
  103. ret = test_probe(&ring);
  104. if (ret) {
  105. fprintf(stderr, "test_probe failed\n");
  106. return ret;
  107. }
  108. if (no_probe)
  109. return 0;
  110. ret = test_probe_helper(&ring);
  111. if (ret) {
  112. fprintf(stderr, "test_probe failed\n");
  113. return ret;
  114. }
  115. return 0;
  116. }