tcpreplay.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <ctype.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include "tcpreplay.h"
  31. #include "tcpreplay_api.h"
  32. #ifdef TCPREPLAY_EDIT
  33. #include "tcpreplay_edit_opts.h"
  34. #include "tcpedit/tcpedit.h"
  35. #include "tcpedit/fuzzing.h"
  36. tcpedit_t *tcpedit;
  37. #else
  38. #include "tcpreplay_opts.h"
  39. #endif
  40. #include "send_packets.h"
  41. #include "replay.h"
  42. #include "signal_handler.h"
  43. #ifdef DEBUG
  44. int debug = 0;
  45. #endif
  46. tcpreplay_t *ctx;
  47. static void flow_stats(const tcpreplay_t *ctx);
  48. int
  49. main(int argc, char *argv[])
  50. {
  51. int i, optct = 0;
  52. int rcode;
  53. char buf[1024];
  54. fflush(NULL);
  55. ctx = tcpreplay_init();
  56. #ifdef TCPREPLAY
  57. optct = optionProcess(&tcpreplayOptions, argc, argv);
  58. #elif defined TCPREPLAY_EDIT
  59. optct = optionProcess(&tcpreplay_editOptions, argc, argv);
  60. #endif
  61. argc -= optct;
  62. argv += optct;
  63. fflush(NULL);
  64. rcode = tcpreplay_post_args(ctx, argc);
  65. if (rcode <= -2) {
  66. warnx("%s", tcpreplay_getwarn(ctx));
  67. } else if (rcode == -1) {
  68. errx(-1, "Unable to parse args: %s", tcpreplay_geterr(ctx));
  69. }
  70. fflush(NULL);
  71. #ifdef TCPREPLAY_EDIT
  72. /* init tcpedit context */
  73. if (tcpedit_init(&tcpedit, sendpacket_get_dlt(ctx->intf1)) < 0) {
  74. errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  75. }
  76. /* parse the tcpedit args */
  77. rcode = tcpedit_post_args(tcpedit);
  78. if (rcode < 0) {
  79. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  80. } else if (rcode == 1) {
  81. warnx("%s", tcpedit_geterr(tcpedit));
  82. }
  83. if (tcpedit_validate(tcpedit) < 0) {
  84. errx(-1, "Unable to edit packets given options:\n%s",
  85. tcpedit_geterr(tcpedit));
  86. }
  87. #endif
  88. if (ctx->options->preload_pcap && ! HAVE_OPT(QUIET)) {
  89. notice("File Cache is enabled");
  90. }
  91. /*
  92. * Setup up the file cache, if required
  93. */
  94. if (ctx->options->preload_pcap) {
  95. /* Initialise each of the file cache structures */
  96. for (i = 0; i < argc; i++) {
  97. ctx->options->file_cache[i].index = i;
  98. ctx->options->file_cache[i].cached = FALSE;
  99. ctx->options->file_cache[i].packet_cache = NULL;
  100. }
  101. }
  102. for (i = 0; i < argc; i++) {
  103. tcpreplay_add_pcapfile(ctx, argv[i]);
  104. /* preload our pcap file? */
  105. if (ctx->options->preload_pcap) {
  106. preload_pcap_file(ctx, i);
  107. }
  108. }
  109. #ifdef TCPREPLAY_EDIT
  110. /* fuzzing init */
  111. fuzzing_init(tcpedit->fuzz_seed, tcpedit->fuzz_factor);
  112. #endif
  113. /* init the signal handlers */
  114. init_signal_handlers();
  115. /* main loop */
  116. rcode = tcpreplay_replay(ctx);
  117. if (rcode < 0) {
  118. notice("\nFailed: %s\n", tcpreplay_geterr(ctx));
  119. exit(rcode);
  120. } else if (rcode == 1) {
  121. notice("\nWarning: %s\n", tcpreplay_getwarn(ctx));
  122. }
  123. if (ctx->stats.bytes_sent > 0) {
  124. packet_stats(&ctx->stats);
  125. if (ctx->options->flow_stats)
  126. flow_stats(ctx);
  127. sendpacket_getstat(ctx->intf1, buf, sizeof(buf));
  128. printf("%s", buf);
  129. if (ctx->intf2 != NULL) {
  130. sendpacket_getstat(ctx->intf2, buf, sizeof(buf));
  131. printf("%s", buf);
  132. }
  133. }
  134. tcpreplay_close(ctx);
  135. return 0;
  136. } /* main() */
  137. /**
  138. * Print various flow statistics
  139. */
  140. static void flow_stats(const tcpreplay_t *ctx)
  141. {
  142. struct timeval diff;
  143. COUNTER diff_us;
  144. const tcpreplay_stats_t *stats = &ctx->stats;
  145. const tcpreplay_opt_t *options = ctx->options;
  146. COUNTER flows_total = stats->flows;
  147. COUNTER flows_unique = stats->flows_unique;
  148. COUNTER flows_expired = stats->flows_expired;
  149. COUNTER flow_packets;
  150. COUNTER flow_non_flow_packets;
  151. COUNTER flows_sec = 0;
  152. u_int32_t flows_sec_100ths = 0;
  153. timersub(&stats->end_time, &stats->start_time, &diff);
  154. diff_us = TIMEVAL_TO_MICROSEC(&diff);
  155. if (!flows_total || !ctx->iteration)
  156. return;
  157. /*
  158. * When packets are read into cache, flows
  159. * are only counted in first iteration
  160. * If flows are unique from one loop iteration
  161. * to the next then multiply by the number of
  162. * successful iterations.
  163. */
  164. if (options->preload_pcap) {
  165. if (ctx->options->unique_ip) {
  166. flows_total *= ctx->last_unique_iteration;
  167. flows_unique *= ctx->last_unique_iteration;
  168. flows_expired *= ctx->last_unique_iteration;
  169. #ifdef TCPREPLAY_EDIT
  170. } else if (tcpedit->seed) {
  171. flows_total *= ctx->iteration;
  172. flows_unique *= ctx->iteration;
  173. flows_expired *= ctx->iteration;
  174. #endif
  175. }
  176. }
  177. flow_packets = stats->flow_packets * ctx->iteration;
  178. flow_non_flow_packets = stats->flow_non_flow_packets * ctx->iteration;
  179. if (diff_us) {
  180. COUNTER flows_sec_X100;
  181. flows_sec_X100 = (flows_total * 100 * 1000 * 1000) / diff_us;
  182. flows_sec = flows_sec_X100 / 100;
  183. flows_sec_100ths = flows_sec_X100 % 100;
  184. }
  185. if (ctx->options->flow_expiry)
  186. printf("Flows: " COUNTER_SPEC " flows, " COUNTER_SPEC " unique, "COUNTER_SPEC " expired, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  187. flows_total, flows_unique, flows_expired, flows_sec, flows_sec_100ths, flow_packets,
  188. flow_non_flow_packets);
  189. else
  190. printf("Flows: " COUNTER_SPEC " flows, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  191. flows_total, flows_sec, flows_sec_100ths, flow_packets,
  192. flow_non_flow_packets);
  193. }
  194. /* vim: set tabstop=8 expandtab shiftwidth=4 softtabstop=4: */