helpers.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Description: Helpers for tests.
  4. */
  5. #ifndef LIBURING_HELPERS_H
  6. #define LIBURING_HELPERS_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "liburing.h"
  11. #include "../src/setup.h"
  12. #include <arpa/inet.h>
  13. #include <sys/time.h>
  14. enum t_setup_ret {
  15. T_SETUP_OK = 0,
  16. T_SETUP_SKIP,
  17. };
  18. enum t_test_result {
  19. T_EXIT_PASS = 0,
  20. T_EXIT_FAIL = 1,
  21. T_EXIT_SKIP = 77,
  22. };
  23. /*
  24. * Helper for binding socket to an ephemeral port.
  25. * The port number to be bound is returned in @addr->sin_port.
  26. */
  27. int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr);
  28. /*
  29. * Helper for allocating memory in tests.
  30. */
  31. void *t_malloc(size_t size);
  32. /*
  33. * Helper for allocating size bytes aligned on a boundary.
  34. */
  35. void t_posix_memalign(void **memptr, size_t alignment, size_t size);
  36. /*
  37. * Helper for allocating space for an array of nmemb elements
  38. * with size bytes for each element.
  39. */
  40. void *t_calloc(size_t nmemb, size_t size);
  41. /*
  42. * Helper for creating file and write @size byte buf with 0xaa value in the file.
  43. */
  44. void t_create_file(const char *file, size_t size);
  45. /*
  46. * Helper for creating file and write @size byte buf with @pattern value in
  47. * the file.
  48. */
  49. void t_create_file_pattern(const char *file, size_t size, char pattern);
  50. /*
  51. * Helper for creating @buf_num number of iovec
  52. * with @buf_size bytes buffer of each iovec.
  53. */
  54. struct iovec *t_create_buffers(size_t buf_num, size_t buf_size);
  55. /*
  56. * Helper for creating connected socket pairs
  57. */
  58. int t_create_socket_pair(int fd[2], bool stream);
  59. /*
  60. * Helper for setting up a ring and checking for user privs
  61. */
  62. enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
  63. struct io_uring_params *p);
  64. enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
  65. unsigned int flags);
  66. enum t_setup_ret t_register_buffers(struct io_uring *ring,
  67. const struct iovec *iovecs,
  68. unsigned nr_iovecs);
  69. bool t_probe_defer_taskrun(void);
  70. unsigned __io_uring_flush_sq(struct io_uring *ring);
  71. static inline int t_io_uring_init_sqarray(unsigned entries, struct io_uring *ring,
  72. struct io_uring_params *p)
  73. {
  74. int ret;
  75. ret = __io_uring_queue_init_params(entries, ring, p, NULL, 0);
  76. return ret >= 0 ? 0 : ret;
  77. }
  78. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  79. void t_error(int status, int errnum, const char *format, ...);
  80. unsigned long long mtime_since(const struct timeval *s, const struct timeval *e);
  81. unsigned long long mtime_since_now(struct timeval *tv);
  82. unsigned long long utime_since(const struct timeval *s, const struct timeval *e);
  83. unsigned long long utime_since_now(struct timeval *tv);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif