sleep.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include "defines.h"
  21. #include "config.h"
  22. #include "common.h"
  23. #ifdef HAVE_SYS_SELECT /* According to POSIX 1003.1-2001 */
  24. #include <sys/select.h>
  25. #endif
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. #ifdef HAVE_SYS_EVENT
  33. #include <sys/event.h>
  34. #endif
  35. /* necessary for ioport_sleep() functions */
  36. #ifdef HAVE_SYS_IO_H /* Linux */
  37. #include <sys/io.h>
  38. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  39. #include <architecture/i386/pio.h>
  40. #endif
  41. #ifdef HAVE_NETMAP
  42. #include <net/netmap.h>
  43. #include <net/netmap_user.h>
  44. #include <sys/ioctl.h>
  45. #endif /* HAVE_NETMAP */
  46. static inline void
  47. nanosleep_sleep(sendpacket_t *sp _U_, const struct timespec *nap, struct timespec *now, bool flush _U_)
  48. {
  49. #if defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 200112L
  50. struct timespec sleep_until;
  51. timeradd_timespec(now, nap, &sleep_until);
  52. clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &sleep_until, NULL);
  53. #else
  54. nanosleep(nap, NULL);
  55. #endif
  56. #ifdef HAVE_NETMAP
  57. if (flush)
  58. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  59. #endif /* HAVE_NETMAP */
  60. get_current_time(now);
  61. }
  62. /*
  63. * Straight forward... keep calling gettimeofday() until the appropriate amount
  64. * of time has passed. Pretty damn accurate.
  65. *
  66. * Note: make sure "now" has recently been updated.
  67. */
  68. static inline void
  69. gettimeofday_sleep(sendpacket_t *sp, struct timespec *nap, struct timespec *now, bool flush _U_)
  70. {
  71. struct timespec sleep_until;
  72. #ifdef HAVE_NETMAP
  73. struct timespec last;
  74. uint32_t i = 0;
  75. TIMESPEC_SET(&last, now);
  76. #endif /* HAVE_NETMAP */
  77. timeradd_timespec(now, nap, &sleep_until);
  78. while (!sp->abort) {
  79. #ifdef HAVE_NETMAP
  80. if (flush && timescmp(now, &last, !=)) {
  81. TIMESPEC_SET(&last, now);
  82. if ((++i & 0xf) == 0)
  83. /* flush TX buffer every 16 usec */
  84. ioctl(sp->handle.fd, NIOCTXSYNC, NULL);
  85. }
  86. #endif /* HAVE_NETMAP */
  87. if (timescmp(now, &sleep_until, >=))
  88. break;
  89. #ifdef HAVE_SCHED_H
  90. /* yield the CPU so other apps remain responsive */
  91. sched_yield();
  92. #endif
  93. get_current_time(now);
  94. }
  95. }
  96. #ifdef HAVE_SELECT
  97. /*
  98. * sleep for some time using the select() call timeout method. This is
  99. * highly portable for sub-second sleeping, but only for about 1msec
  100. * resolution which is pretty much useless for our needs. Keeping it here
  101. * for future reference
  102. */
  103. static inline void
  104. select_sleep(sendpacket_t *sp _U_, struct timespec *nap, struct timespec *now_ns, bool flush _U_)
  105. {
  106. struct timeval timeout;
  107. timeout.tv_sec = 0;
  108. timeout.tv_usec = 0;
  109. #ifdef HAVE_NETMAP
  110. if (flush)
  111. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  112. #endif /* HAVE_NETMAP */
  113. TIMEVAL_TO_TIMESPEC(&timeout, nap);
  114. if (select(0, NULL, NULL, NULL, &timeout) < 0)
  115. warnx("select_sleep() returned early due to error: %s", strerror(errno));
  116. #ifdef HAVE_NETMAP
  117. if (flush)
  118. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  119. #endif
  120. get_current_time(now_ns);
  121. }
  122. #endif /* HAVE_SELECT */
  123. /*
  124. * ioport_sleep() only works on Intel 32-bit and quite possibly only Linux.
  125. * But the basic idea is to write to the IO Port 0x80 which should
  126. * take exactly 1usec regardless of the CPU speed and without
  127. * calling a sleep method which allows the kernel to service another thread
  128. * Idea stolen from: http://c-faq.com/osdep/sd25.html
  129. */
  130. /* before calling port_sleep(), you have to call port_sleep_init() */
  131. void ioport_sleep_init(void);
  132. void ioport_sleep(sendpacket_t *sp _U_, const struct timespec *nap, struct timespec *now, bool flush);