tcpreplay.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. tcpedit_close(&tcpedit);
  80. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  81. } else if (rcode == 1) {
  82. warnx("%s", tcpedit_geterr(tcpedit));
  83. }
  84. if (tcpedit_validate(tcpedit) < 0) {
  85. tcpedit_close(&tcpedit);
  86. errx(-1, "Unable to edit packets given options:\n%s",
  87. tcpedit_geterr(tcpedit));
  88. }
  89. #endif
  90. if (ctx->options->preload_pcap && ! HAVE_OPT(QUIET)) {
  91. notice("File Cache is enabled");
  92. }
  93. /*
  94. * Setup up the file cache, if required
  95. */
  96. if (ctx->options->preload_pcap) {
  97. /* Initialize each of the file cache structures */
  98. for (i = 0; i < argc; i++) {
  99. ctx->options->file_cache[i].index = i;
  100. ctx->options->file_cache[i].cached = FALSE;
  101. ctx->options->file_cache[i].packet_cache = NULL;
  102. }
  103. }
  104. for (i = 0; i < argc; i++) {
  105. tcpreplay_add_pcapfile(ctx, argv[i]);
  106. /* preload our pcap file? */
  107. if (ctx->options->preload_pcap) {
  108. preload_pcap_file(ctx, i);
  109. }
  110. }
  111. #ifdef TCPREPLAY_EDIT
  112. /* fuzzing init */
  113. fuzzing_init(tcpedit->fuzz_seed, tcpedit->fuzz_factor);
  114. #endif
  115. /* init the signal handlers */
  116. init_signal_handlers();
  117. /* main loop */
  118. rcode = tcpreplay_replay(ctx);
  119. if (rcode < 0) {
  120. notice("\nFailed: %s\n", tcpreplay_geterr(ctx));
  121. #ifdef TCPREPLAY_EDIT
  122. tcpedit_close(&tcpedit);
  123. #endif
  124. exit(rcode);
  125. } else if (rcode == 1) {
  126. notice("\nWarning: %s\n", tcpreplay_getwarn(ctx));
  127. }
  128. if (ctx->stats.bytes_sent > 0) {
  129. char buf[1024];
  130. packet_stats(&ctx->stats);
  131. if (ctx->options->flow_stats)
  132. flow_stats(ctx);
  133. sendpacket_getstat(ctx->intf1, buf, sizeof(buf));
  134. printf("%s", buf);
  135. if (ctx->intf2 != NULL) {
  136. sendpacket_getstat(ctx->intf2, buf, sizeof(buf));
  137. printf("%s", buf);
  138. }
  139. }
  140. #ifdef TIMESTAMP_TRACE
  141. dump_timestamp_trace_array(&ctx->stats.start_time, &ctx->stats.end_time,
  142. ctx->options->speed.speed);
  143. #endif
  144. #ifdef TCPREPLAY_EDIT
  145. tcpedit_close(&tcpedit);
  146. #endif
  147. tcpreplay_close(ctx);
  148. restore_stdin();
  149. return 0;
  150. } /* main() */
  151. /**
  152. * Print various flow statistics
  153. */
  154. static void flow_stats(const tcpreplay_t *ctx)
  155. {
  156. struct timeval diff;
  157. COUNTER diff_us;
  158. const tcpreplay_stats_t *stats = &ctx->stats;
  159. const tcpreplay_opt_t *options = ctx->options;
  160. COUNTER flows_total = stats->flows;
  161. COUNTER flows_unique = stats->flows_unique;
  162. COUNTER flows_expired = stats->flows_expired;
  163. COUNTER flow_packets;
  164. COUNTER flow_non_flow_packets;
  165. COUNTER flows_sec = 0;
  166. u_int32_t flows_sec_100ths = 0;
  167. timersub(&stats->end_time, &stats->start_time, &diff);
  168. diff_us = TIMEVAL_TO_MICROSEC(&diff);
  169. if (!flows_total || !ctx->iteration)
  170. return;
  171. /*
  172. * When packets are read into cache, flows
  173. * are only counted in first iteration
  174. * If flows are unique from one loop iteration
  175. * to the next then multiply by the number of
  176. * successful iterations.
  177. */
  178. if (options->preload_pcap) {
  179. if (ctx->options->unique_ip) {
  180. flows_total *= ctx->last_unique_iteration;
  181. flows_unique *= ctx->last_unique_iteration;
  182. flows_expired *= ctx->last_unique_iteration;
  183. #ifdef TCPREPLAY_EDIT
  184. } else if (tcpedit->seed) {
  185. flows_total *= ctx->iteration;
  186. flows_unique *= ctx->iteration;
  187. flows_expired *= ctx->iteration;
  188. #endif
  189. }
  190. }
  191. flow_packets = stats->flow_packets * ctx->iteration;
  192. flow_non_flow_packets = stats->flow_non_flow_packets * ctx->iteration;
  193. if (diff_us) {
  194. COUNTER flows_sec_X100;
  195. flows_sec_X100 = (flows_total * 100 * 1000 * 1000) / diff_us;
  196. flows_sec = flows_sec_X100 / 100;
  197. flows_sec_100ths = flows_sec_X100 % 100;
  198. }
  199. if (ctx->options->flow_expiry)
  200. printf("Flows: " COUNTER_SPEC " flows, " COUNTER_SPEC " unique, "COUNTER_SPEC " expired, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  201. flows_total, flows_unique, flows_expired, flows_sec, flows_sec_100ths, flow_packets,
  202. flow_non_flow_packets);
  203. else
  204. printf("Flows: " COUNTER_SPEC " flows, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  205. flows_total, flows_sec, flows_sec_100ths, flow_packets,
  206. flow_non_flow_packets);
  207. }
  208. /* vim: set tabstop=8 expandtab shiftwidth=4 softtabstop=4: */