sleep.h 4.3 KB

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