sleep.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #ifdef HAVE_SYS_SELECT /* According to POSIX 1003.1-2001 */
  23. #include <sys/select.h>
  24. #endif
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #ifdef HAVE_SYS_EVENT
  31. #include <sys/event.h>
  32. #endif
  33. /* necessary for ioport_sleep() functions */
  34. #ifdef HAVE_SYS_IO_H /* Linux */
  35. #include <sys/io.h>
  36. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  37. #include <architecture/i386/pio.h>
  38. #endif
  39. #ifdef HAVE_NETMAP
  40. #include <sys/ioctl.h>
  41. #include <net/netmap.h>
  42. #include <net/netmap_user.h>
  43. #endif /* HAVE_NETMAP */
  44. #ifndef __SLEEP_H__
  45. #define __SLEEP_H__
  46. static inline void
  47. nanosleep_sleep(sendpacket_t *sp _U_, const struct timespec *nap,
  48. struct timeval *now, bool flush _U_)
  49. {
  50. nanosleep(nap, NULL);
  51. #ifdef HAVE_NETMAP
  52. if (flush)
  53. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  54. #endif /* HAVE_NETMAP */
  55. gettimeofday(now, NULL);
  56. }
  57. /*
  58. * Straight forward... keep calling gettimeofday() until the appropriate amount
  59. * of time has passed. Pretty damn accurate.
  60. *
  61. * Note: make sure "now" has recently been updated.
  62. */
  63. static inline void
  64. gettimeofday_sleep(sendpacket_t *sp _U_, struct timespec *nap,
  65. struct timeval *now, bool flush _U_)
  66. {
  67. struct timeval sleep_until, nap_for;
  68. #ifdef HAVE_NETMAP
  69. struct timeval last;
  70. uint32_t i = 0;
  71. TIMEVAL_SET(&last, now);
  72. #endif /* HAVE_NETMAP */
  73. TIMESPEC_TO_TIMEVAL(&nap_for, nap);
  74. timeradd(now, &nap_for, &sleep_until);
  75. while (!sp->abort) {
  76. #ifdef HAVE_NETMAP
  77. if (flush && timercmp(now, &last, !=)) {
  78. TIMEVAL_SET(&last, now);
  79. if ((++i & 0xf) == 0)
  80. /* flush TX buffer every 16 usec */
  81. ioctl(sp->handle.fd, NIOCTXSYNC, NULL);
  82. }
  83. #endif /* HAVE_NETMAP */
  84. if (timercmp(now, &sleep_until, >=))
  85. break;
  86. #ifdef HAVE_SCHED_H
  87. /* yield the CPU so other apps remain responsive */
  88. sched_yield();
  89. #endif
  90. gettimeofday(now, NULL);
  91. }
  92. }
  93. #ifdef HAVE_SELECT
  94. /*
  95. * sleep for some time using the select() call timeout method. This is
  96. * highly portable for sub-second sleeping, but only for about 1msec
  97. * resolution which is pretty much useless for our needs. Keeping it here
  98. * for future reference
  99. */
  100. static inline void
  101. select_sleep(sendpacket_t *sp _U_, const struct timespec *nap,
  102. struct timeval *now, bool flush _U_)
  103. {
  104. struct timeval timeout;
  105. #ifdef HAVE_NETMAP
  106. if (flush)
  107. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  108. #endif /* HAVE_NETMAP */
  109. TIMESPEC_TO_TIMEVAL(&timeout, nap);
  110. if (select(0, NULL, NULL, NULL, &timeout) < 0)
  111. warnx("select_sleep() returned early due to error: %s", strerror(errno));
  112. #ifdef HAVE_NETMAP
  113. if (flush)
  114. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  115. #endif
  116. gettimeofday(now, NULL);
  117. }
  118. #endif /* HAVE_SELECT */
  119. /*
  120. * ioport_sleep() only works on Intel 32-bit and quite possibly only Linux.
  121. * But the basic idea is to write to the IO Port 0x80 which should
  122. * take exactly 1usec regardless of the CPU speed and without
  123. * calling a sleep method which allows the kernel to service another thread
  124. * Idea stolen from: http://c-faq.com/osdep/sd25.html
  125. */
  126. /* before calling port_sleep(), you have to call port_sleep_init() */
  127. void ioport_sleep_init(void);
  128. void ioport_sleep(sendpacket_t *sp _U_, const struct timespec *nap,
  129. struct timeval *now, bool flush);
  130. #endif /* __SLEEP_H__ */