tcpreplay.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 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. #ifdef HAVE_FTS_H
  28. #include <fts.h>
  29. #endif
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include "tcpreplay.h"
  33. #include "tcpreplay_api.h"
  34. #include "timestamp_trace.h"
  35. #ifdef TCPREPLAY_EDIT
  36. #include "tcpreplay_edit_opts.h"
  37. #include "tcpedit/tcpedit.h"
  38. #include "tcpedit/fuzzing.h"
  39. tcpedit_t *tcpedit;
  40. #else
  41. #include "tcpreplay_opts.h"
  42. #endif
  43. #include "send_packets.h"
  44. #include "replay.h"
  45. #include "signal_handler.h"
  46. #ifdef DEBUG
  47. int debug = 0;
  48. #endif
  49. tcpreplay_t *ctx;
  50. static void flow_stats(const tcpreplay_t *ctx);
  51. int
  52. main(int argc, char *argv[])
  53. {
  54. int i, optct = 0;
  55. int rcode;
  56. fflush(NULL);
  57. ctx = tcpreplay_init();
  58. optct = optionProcess(&tcpreplayOptions, argc, argv);
  59. argc -= optct;
  60. argv += optct;
  61. fflush(NULL);
  62. rcode = tcpreplay_post_args(ctx, argc);
  63. if (rcode <= -2) {
  64. warnx("%s", tcpreplay_getwarn(ctx));
  65. } else if (rcode == -1) {
  66. errx(-1, "Unable to parse args: %s", tcpreplay_geterr(ctx));
  67. }
  68. fflush(NULL);
  69. #ifdef TCPREPLAY_EDIT
  70. /* init tcpedit context */
  71. if (tcpedit_init(&tcpedit, sendpacket_get_dlt(ctx->intf1)) < 0) {
  72. errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  73. }
  74. /* parse the tcpedit args */
  75. rcode = tcpedit_post_args(tcpedit);
  76. if (rcode < 0) {
  77. tcpedit_close(&tcpedit);
  78. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  79. } else if (rcode == 1) {
  80. warnx("%s", tcpedit_geterr(tcpedit));
  81. }
  82. if (tcpedit_validate(tcpedit) < 0) {
  83. tcpedit_close(&tcpedit);
  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. * Check if remaining args are directories or files
  93. */
  94. for (i = 0; i < argc; i++) {
  95. #ifdef HAVE_FTS_H
  96. struct stat statbuf;
  97. if (!strcmp(argv[i], "-")) {
  98. tcpreplay_add_pcapfile(ctx, argv[i]);
  99. continue;
  100. }
  101. if (stat(argv[i], &statbuf) != 0) {
  102. errx(-1,
  103. "Unable to retrieve information from file %s: %s",
  104. argv[i],
  105. strerror(errno));
  106. }
  107. /* If it is a directory, walk the file tree and treat only pcap files */
  108. if (S_ISDIR(statbuf.st_mode)) {
  109. FTSENT *entry = NULL;
  110. FTS *fts = fts_open(&argv[i], FTS_NOCHDIR | FTS_LOGICAL, NULL);
  111. if (fts == NULL) {
  112. errx(-1, "Unable to open %s", argv[1]);
  113. }
  114. while ((entry = fts_read(fts)) != NULL) {
  115. switch (entry->fts_info) {
  116. case FTS_F:
  117. if (entry->fts_path) {
  118. tcpreplay_add_pcapfile(ctx, entry->fts_path);
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. fts_close(fts);
  126. } else {
  127. tcpreplay_add_pcapfile(ctx, argv[i]);
  128. }
  129. #else
  130. tcpreplay_add_pcapfile(ctx, argv[i]);
  131. #endif
  132. }
  133. /*
  134. * Setup up the file cache, if required
  135. */
  136. if (ctx->options->preload_pcap) {
  137. /* Initialize each of the file cache structures */
  138. for (i = 0; i < ctx->options->source_cnt; i++) {
  139. ctx->options->file_cache[i].index = i;
  140. ctx->options->file_cache[i].cached = FALSE;
  141. ctx->options->file_cache[i].packet_cache = NULL;
  142. /* preload our pcap file */
  143. preload_pcap_file(ctx, i);
  144. }
  145. }
  146. #ifdef TCPREPLAY_EDIT
  147. /* fuzzing init */
  148. fuzzing_init(tcpedit->fuzz_seed, tcpedit->fuzz_factor);
  149. #endif
  150. /* init the signal handlers */
  151. init_signal_handlers();
  152. /* main loop */
  153. rcode = tcpreplay_replay(ctx);
  154. if (rcode < 0) {
  155. notice("\nFailed: %s\n", tcpreplay_geterr(ctx));
  156. #ifdef TCPREPLAY_EDIT
  157. tcpedit_close(&tcpedit);
  158. #endif
  159. exit(rcode);
  160. } else if (rcode == 1) {
  161. notice("\nWarning: %s\n", tcpreplay_getwarn(ctx));
  162. }
  163. if (ctx->stats.bytes_sent > 0) {
  164. char buf[1024];
  165. packet_stats(&ctx->stats);
  166. if (ctx->options->flow_stats)
  167. flow_stats(ctx);
  168. sendpacket_getstat(ctx->intf1, buf, sizeof(buf));
  169. printf("%s", buf);
  170. if (ctx->intf2 != NULL) {
  171. sendpacket_getstat(ctx->intf2, buf, sizeof(buf));
  172. printf("%s", buf);
  173. }
  174. }
  175. #ifdef TIMESTAMP_TRACE
  176. dump_timestamp_trace_array(&ctx->stats.start_time, &ctx->stats.end_time,
  177. ctx->options->speed.speed);
  178. #endif
  179. #ifdef TCPREPLAY_EDIT
  180. tcpedit_close(&tcpedit);
  181. #endif
  182. tcpreplay_close(ctx);
  183. restore_stdin();
  184. return 0;
  185. } /* main() */
  186. /**
  187. * Print various flow statistics
  188. */
  189. static void flow_stats(const tcpreplay_t *ctx)
  190. {
  191. struct timeval diff;
  192. COUNTER diff_us;
  193. const tcpreplay_stats_t *stats = &ctx->stats;
  194. const tcpreplay_opt_t *options = ctx->options;
  195. COUNTER flows_total = stats->flows;
  196. COUNTER flows_unique = stats->flows_unique;
  197. COUNTER flows_expired = stats->flows_expired;
  198. COUNTER flow_packets;
  199. COUNTER flow_non_flow_packets;
  200. COUNTER flows_sec = 0;
  201. u_int32_t flows_sec_100ths = 0;
  202. timersub(&stats->end_time, &stats->start_time, &diff);
  203. diff_us = TIMEVAL_TO_MICROSEC(&diff);
  204. if (!flows_total || !ctx->iteration)
  205. return;
  206. /*
  207. * When packets are read into cache, flows
  208. * are only counted in first iteration
  209. * If flows are unique from one loop iteration
  210. * to the next then multiply by the number of
  211. * successful iterations.
  212. */
  213. if (options->preload_pcap) {
  214. if (ctx->options->unique_ip) {
  215. flows_total *= ctx->last_unique_iteration;
  216. flows_unique *= ctx->last_unique_iteration;
  217. flows_expired *= ctx->last_unique_iteration;
  218. #ifdef TCPREPLAY_EDIT
  219. } else if (tcpedit->seed) {
  220. flows_total *= ctx->iteration;
  221. flows_unique *= ctx->iteration;
  222. flows_expired *= ctx->iteration;
  223. #endif
  224. }
  225. }
  226. flow_packets = stats->flow_packets * ctx->iteration;
  227. flow_non_flow_packets = stats->flow_non_flow_packets * ctx->iteration;
  228. if (diff_us) {
  229. COUNTER flows_sec_X100;
  230. flows_sec_X100 = (flows_total * 100 * 1000 * 1000) / diff_us;
  231. flows_sec = flows_sec_X100 / 100;
  232. flows_sec_100ths = flows_sec_X100 % 100;
  233. }
  234. if (ctx->options->flow_expiry)
  235. printf("Flows: " COUNTER_SPEC " flows, " COUNTER_SPEC " unique, "COUNTER_SPEC " expired, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  236. flows_total, flows_unique, flows_expired, flows_sec, flows_sec_100ths, flow_packets,
  237. flow_non_flow_packets);
  238. else
  239. printf("Flows: " COUNTER_SPEC " flows, %llu.%02u fps, " COUNTER_SPEC " flow packets, " COUNTER_SPEC " non-flow\n",
  240. flows_total, flows_sec, flows_sec_100ths, flow_packets,
  241. flow_non_flow_packets);
  242. }
  243. /* vim: set tabstop=8 expandtab shiftwidth=4 softtabstop=4: */