sleep.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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(struct timespec *nap)
  48. {
  49. nanosleep(nap, NULL);
  50. }
  51. /*
  52. * Straight forward... keep calling gettimeofday() until the appropriate amount
  53. * of time has passed. Pretty damn accurate.
  54. *
  55. * Note: make sure "now" has recently been updated.
  56. */
  57. static inline void
  58. gettimeofday_sleep(sendpacket_t *sp _U_,
  59. struct timespec *nap, struct timeval *now,
  60. bool flush)
  61. {
  62. struct timeval sleep_until, nap_for;
  63. #ifdef HAVE_NETMAP
  64. struct timeval last;
  65. if (flush)
  66. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  67. memcpy(&last, now, sizeof(last));
  68. #endif /* HAVE_NETMAP */
  69. TIMESPEC_TO_TIMEVAL(&nap_for, nap);
  70. timeradd(now, &nap_for, &sleep_until);
  71. do {
  72. #ifdef HAVE_NETMAP
  73. if (flush && timercmp(now, &last, !=)) {
  74. /* flush TX buffer every usec */
  75. ioctl(sp->handle.fd, NIOCTXSYNC, NULL);
  76. memcpy(&last, now, sizeof(last));
  77. }
  78. #endif /* HAVE_NETMAP */
  79. gettimeofday(now, NULL);
  80. } while (timercmp(now, &sleep_until, <));
  81. }
  82. #ifdef HAVE_SELECT
  83. /*
  84. * sleep for some time using the select() call timeout method. This is
  85. * highly portable for sub-second sleeping, but only for about 1msec
  86. * resolution which is pretty much useless for our needs. Keeping it here
  87. * for future reference
  88. */
  89. static inline void
  90. select_sleep(const struct timespec *nap)
  91. {
  92. struct timeval timeout;
  93. TIMESPEC_TO_TIMEVAL(&timeout, nap);
  94. if (select(0, NULL, NULL, NULL, &timeout) < 0)
  95. warnx("select_sleep() returned early due to error: %s", strerror(errno));
  96. }
  97. #endif /* HAVE_SELECT */
  98. #endif /* __SLEEP_H__ */