sleep.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  4. *
  5. * The Tcpreplay Suite of tools is free software: you can redistribute it
  6. * and/or modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or with the authors permission any later version.
  9. *
  10. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include "defines.h"
  20. #include "common.h"
  21. #include "sleep.h"
  22. #include <sys/types.h>
  23. #include <sys/time.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #ifdef HAVE_SYS_EVENT
  28. #include <sys/event.h>
  29. #endif
  30. /* necessary for ioport_sleep() functions */
  31. #ifdef HAVE_SYS_IO_H /* Linux */
  32. #include <sys/io.h>
  33. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  34. #include <architecture/i386/pio.h>
  35. #endif
  36. #if defined HAVE_IOPORT_SLEEP__
  37. static int ioport_sleep_value;
  38. #endif
  39. void
  40. ioport_sleep_init(void)
  41. {
  42. #if defined HAVE_IOPORT_SLEEP__
  43. ioperm(0x80, 1, 1);
  44. ioport_sleep_value = inb(0x80);
  45. #else
  46. err(-1, "Platform does not support IO Port for timing");
  47. #endif
  48. }
  49. void
  50. ioport_sleep(sendpacket_t *sp _U_, const struct timespec *nap _U_,
  51. struct timeval *now _U_, bool flush _U_)
  52. {
  53. #if defined HAVE_IOPORT_SLEEP__
  54. struct timeval nap_for;
  55. u_int32_t usec;
  56. time_t i;
  57. TIMESPEC_TO_TIMEVAL(&nap_for, nap);
  58. /*
  59. * process the seconds, we do this in a loop so we don't have to
  60. * use slower 64bit integers or worry about integer overflows.
  61. */
  62. for (i = 0; i < nap_for.tv_sec; i ++) {
  63. usec = SEC_TO_MICROSEC(nap_for.tv_sec);
  64. while (usec > 0) {
  65. usec --;
  66. outb(ioport_sleep_value, 0x80);
  67. }
  68. }
  69. /* process the usec */
  70. usec = nap->tv_nsec / 1000;
  71. usec --; /* fudge factor for all the above */
  72. while (usec > 0) {
  73. usec --;
  74. outb(ioport_sleep_value, 0x80);
  75. }
  76. #else
  77. err(-1, "Platform does not support IO Port for timing");
  78. #endif
  79. #ifdef HAVE_NETMAP
  80. if (flush)
  81. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  82. #endif /* HAVE_NETMAP */
  83. gettimeofday(now, NULL);
  84. }