sleep.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2024 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 "sleep.h"
  19. #include "config.h"
  20. #include "common.h"
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #ifdef HAVE_SYS_EVENT
  24. #include <sys/event.h>
  25. #endif
  26. /* necessary for ioport_sleep() functions */
  27. #if defined HAVE_IOPORT_SLEEP__ && defined HAVE_SYS_IO_H /* Linux */
  28. #include <sys/io.h>
  29. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  30. #include <architecture/i386/pio.h>
  31. #endif
  32. #if defined HAVE_IOPORT_SLEEP__
  33. static int ioport_sleep_value;
  34. #endif
  35. void
  36. ioport_sleep_init(void)
  37. {
  38. #if defined HAVE_IOPORT_SLEEP__
  39. ioperm(0x80, 1, 1);
  40. ioport_sleep_value = inb(0x80);
  41. #else
  42. err(-1, "Platform does not support IO Port for timing");
  43. #endif
  44. }
  45. void
  46. ioport_sleep(sendpacket_t *sp _U_, const struct timespec *nap _U_, struct timespec *now _U_, bool flush _U_)
  47. {
  48. #if defined HAVE_IOPORT_SLEEP__
  49. struct timespec nap_for;
  50. u_int32_t nsec;
  51. time_t i;
  52. TIMESPEC_SET(&nap_for, nap);
  53. /*
  54. * process the seconds, we do this in a loop so we don't have to
  55. * use slower 64bit integers or worry about integer overflows.
  56. */
  57. for (i = 0; i < nap_for.tv_sec; i++) {
  58. nsec = nap_for.tv_sec * 1000000000;
  59. while (usec > 0) {
  60. usec--;
  61. outb(ioport_sleep_value, 0x80);
  62. }
  63. }
  64. /* process the nsec */
  65. nsec = nap->tv_nsec;
  66. nsec--; /* fudge factor for all the above */
  67. while (nsec > 0) {
  68. nsec--;
  69. outb(ioport_sleep_value, 0x80);
  70. }
  71. #else
  72. err(-1, "Platform does not support IO Port for timing");
  73. #endif
  74. }