setup.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/mman.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include "liburing.h"
  8. static int io_uring_mmap(int fd, struct io_uring_params *p,
  9. struct io_uring_sq *sq, struct io_uring_cq *cq)
  10. {
  11. size_t size;
  12. void *ptr;
  13. int ret;
  14. sq->ring_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned);
  15. ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE,
  16. MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
  17. if (ptr == MAP_FAILED)
  18. return -errno;
  19. sq->khead = ptr + p->sq_off.head;
  20. sq->ktail = ptr + p->sq_off.tail;
  21. sq->kring_mask = ptr + p->sq_off.ring_mask;
  22. sq->kring_entries = ptr + p->sq_off.ring_entries;
  23. sq->kflags = ptr + p->sq_off.flags;
  24. sq->kdropped = ptr + p->sq_off.dropped;
  25. sq->array = ptr + p->sq_off.array;
  26. size = p->sq_entries * sizeof(struct io_uring_sqe);
  27. sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE,
  28. MAP_SHARED | MAP_POPULATE, fd,
  29. IORING_OFF_SQES);
  30. if (sq->sqes == MAP_FAILED) {
  31. ret = -errno;
  32. err:
  33. munmap(sq->khead, sq->ring_sz);
  34. return ret;
  35. }
  36. cq->ring_sz = p->cq_off.cqes + p->cq_entries * sizeof(struct io_uring_cqe);
  37. ptr = mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE,
  38. MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
  39. if (ptr == MAP_FAILED) {
  40. ret = -errno;
  41. munmap(sq->sqes, p->sq_entries * sizeof(struct io_uring_sqe));
  42. goto err;
  43. }
  44. cq->khead = ptr + p->cq_off.head;
  45. cq->ktail = ptr + p->cq_off.tail;
  46. cq->kring_mask = ptr + p->cq_off.ring_mask;
  47. cq->kring_entries = ptr + p->cq_off.ring_entries;
  48. cq->koverflow = ptr + p->cq_off.overflow;
  49. cq->cqes = ptr + p->cq_off.cqes;
  50. return 0;
  51. }
  52. /*
  53. * For users that want to specify sq_thread_cpu or sq_thread_idle, this
  54. * interface is a convenient helper for mmap()ing the rings.
  55. * Returns -1 on error, or zero on success. On success, 'ring'
  56. * contains the necessary information to read/write to the rings.
  57. */
  58. int io_uring_queue_mmap(int fd, struct io_uring_params *p, struct io_uring *ring)
  59. {
  60. int ret;
  61. memset(ring, 0, sizeof(*ring));
  62. ret = io_uring_mmap(fd, p, &ring->sq, &ring->cq);
  63. if (!ret)
  64. ring->ring_fd = fd;
  65. return ret;
  66. }
  67. /*
  68. * Returns -1 on error, or zero on success. On success, 'ring'
  69. * contains the necessary information to read/write to the rings.
  70. */
  71. int io_uring_queue_init(unsigned entries, struct io_uring *ring, unsigned flags)
  72. {
  73. struct io_uring_params p;
  74. int fd, ret;
  75. memset(&p, 0, sizeof(p));
  76. p.flags = flags;
  77. fd = io_uring_setup(entries, &p);
  78. if (fd < 0)
  79. return fd;
  80. ret = io_uring_queue_mmap(fd, &p, ring);
  81. if (ret)
  82. close(fd);
  83. return ret;
  84. }
  85. void io_uring_queue_exit(struct io_uring *ring)
  86. {
  87. struct io_uring_sq *sq = &ring->sq;
  88. struct io_uring_cq *cq = &ring->cq;
  89. munmap(sq->sqes, *sq->kring_entries * sizeof(struct io_uring_sqe));
  90. munmap(sq->khead, sq->ring_sz);
  91. munmap(cq->khead, cq->ring_sz);
  92. close(ring->ring_fd);
  93. }