tcpreplay.c 8.2 KB

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