tcpreplay.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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. #include "timestamp_trace.h"
  33. #ifdef TCPREPLAY_EDIT
  34. #include "tcpreplay_edit_opts.h"
  35. #include "tcpedit/tcpedit.h"
  36. #include "tcpedit/fuzzing.h"
  37. tcpedit_t *tcpedit;
  38. #else
  39. #include "tcpreplay_opts.h"
  40. #endif
  41. #include "send_packets.h"
  42. #include "replay.h"
  43. #include "signal_handler.h"
  44. #ifdef DEBUG
  45. int debug = 0;
  46. #endif
  47. tcpreplay_t *ctx;
  48. static void flow_stats(const tcpreplay_t *ctx);
  49. int
  50. main(int argc, char *argv[])
  51. {
  52. int i, optct = 0;
  53. int rcode;
  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. char buf[1024];
  125. packet_stats(&ctx->stats);
  126. if (ctx->options->flow_stats)
  127. flow_stats(ctx);
  128. sendpacket_getstat(ctx->intf1, buf, sizeof(buf));
  129. printf("%s", buf);
  130. if (ctx->intf2 != NULL) {
  131. sendpacket_getstat(ctx->intf2, buf, sizeof(buf));
  132. printf("%s", buf);
  133. }
  134. }
  135. #ifdef TIMESTAMP_TRACE
  136. dump_timestamp_trace_array(&ctx->stats.start_time, &ctx->stats.end_time,
  137. ctx->options->speed.speed);
  138. #endif
  139. tcpreplay_close(ctx);
  140. restore_stdin();
  141. return 0;
  142. } /* main() */
  143. /**
  144. * Print various flow statistics
  145. */
  146. static void flow_stats(const tcpreplay_t *ctx)
  147. {
  148. struct timeval diff;
  149. COUNTER diff_us;
  150. const tcpreplay_stats_t *stats = &ctx->stats;
  151. const tcpreplay_opt_t *options = ctx->options;
  152. COUNTER flows_total = stats->flows;
  153. COUNTER flows_unique = stats->flows_unique;
  154. COUNTER flows_expired = stats->flows_expired;
  155. COUNTER flow_packets;
  156. COUNTER flow_non_flow_packets;
  157. COUNTER flows_sec = 0;
  158. u_int32_t flows_sec_100ths = 0;
  159. timersub(&stats->end_time, &stats->start_time, &diff);
  160. diff_us = TIMEVAL_TO_MICROSEC(&diff);
  161. if (!flows_total || !ctx->iteration)
  162. return;
  163. /*
  164. * When packets are read into cache, flows
  165. * are only counted in first iteration
  166. * If flows are unique from one loop iteration
  167. * to the next then multiply by the number of
  168. * successful iterations.
  169. */
  170. if (options->preload_pcap) {
  171. if (ctx->options->unique_ip) {
  172. flows_total *= ctx->last_unique_iteration;
  173. flows_unique *= ctx->last_unique_iteration;
  174. flows_expired *= ctx->last_unique_iteration;
  175. #ifdef TCPREPLAY_EDIT
  176. } else if (tcpedit->seed) {
  177. flows_total *= ctx->iteration;
  178. flows_unique *= ctx->iteration;
  179. flows_expired *= ctx->iteration;
  180. #endif
  181. }
  182. }
  183. flow_packets = stats->flow_packets * ctx->iteration;
  184. flow_non_flow_packets = stats->flow_non_flow_packets * ctx->iteration;
  185. if (diff_us) {
  186. COUNTER flows_sec_X100;
  187. flows_sec_X100 = (flows_total * 100 * 1000 * 1000) / diff_us;
  188. flows_sec = flows_sec_X100 / 100;
  189. flows_sec_100ths = flows_sec_X100 % 100;
  190. }
  191. if (ctx->options->flow_expiry)
  192. printf("Flows: " COUNTER_SPEC " flows, " COUNTER_SPEC " unique, "COUNTER_SPEC " expired, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  193. flows_total, flows_unique, flows_expired, flows_sec, flows_sec_100ths, flow_packets,
  194. flow_non_flow_packets);
  195. else
  196. printf("Flows: " COUNTER_SPEC " flows, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  197. flows_total, flows_sec, flows_sec_100ths, flow_packets,
  198. flow_non_flow_packets);
  199. }
  200. /* vim: set tabstop=8 expandtab shiftwidth=4 softtabstop=4: */