helpers.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <arpa/inet.h>
  12. enum t_setup_ret {
  13. T_SETUP_OK = 0,
  14. T_SETUP_SKIP,
  15. };
  16. enum t_test_result {
  17. T_EXIT_PASS = 0,
  18. T_EXIT_FAIL = 1,
  19. T_EXIT_SKIP = 77,
  20. };
  21. /*
  22. * Helper for binding socket to an ephemeral port.
  23. * The port number to be bound is returned in @addr->sin_port.
  24. */
  25. int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr);
  26. /*
  27. * Helper for allocating memory in tests.
  28. */
  29. void *t_malloc(size_t size);
  30. /*
  31. * Helper for allocating size bytes aligned on a boundary.
  32. */
  33. void t_posix_memalign(void **memptr, size_t alignment, size_t size);
  34. /*
  35. * Helper for allocating space for an array of nmemb elements
  36. * with size bytes for each element.
  37. */
  38. void *t_calloc(size_t nmemb, size_t size);
  39. /*
  40. * Helper for creating file and write @size byte buf with 0xaa value in the file.
  41. */
  42. void t_create_file(const char *file, size_t size);
  43. /*
  44. * Helper for creating file and write @size byte buf with @pattern value in
  45. * the file.
  46. */
  47. void t_create_file_pattern(const char *file, size_t size, char pattern);
  48. /*
  49. * Helper for creating @buf_num number of iovec
  50. * with @buf_size bytes buffer of each iovec.
  51. */
  52. struct iovec *t_create_buffers(size_t buf_num, size_t buf_size);
  53. /*
  54. * Helper for creating connected socket pairs
  55. */
  56. int t_create_socket_pair(int fd[2], bool stream);
  57. /*
  58. * Helper for setting up a ring and checking for user privs
  59. */
  60. enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
  61. struct io_uring_params *p);
  62. enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
  63. unsigned int flags);
  64. enum t_setup_ret t_register_buffers(struct io_uring *ring,
  65. const struct iovec *iovecs,
  66. unsigned nr_iovecs);
  67. bool t_probe_defer_taskrun(void);
  68. unsigned __io_uring_flush_sq(struct io_uring *ring);
  69. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  70. void t_error(int status, int errnum, const char *format, ...);
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif