tcpreplay.c 8.1 KB

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