tcpreplay_api.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #pragma once
  20. #include "defines.h"
  21. #include "config.h"
  22. #include <common/interface.h>
  23. #include <common/sendpacket.h>
  24. #include <common/tcpdump.h>
  25. #include <common/utils.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #ifdef ENABLE_DMALLOC
  30. #include <dmalloc.h>
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. struct tcpreplay_s; /* forward declare */
  36. /* in memory packet cache struct */
  37. typedef struct packet_cache_s {
  38. struct pcap_pkthdr pkthdr;
  39. u_char *pktdata;
  40. struct packet_cache_s *next;
  41. } packet_cache_t;
  42. /* packet cache header */
  43. typedef struct file_cache_s {
  44. int index;
  45. int cached;
  46. int dlt;
  47. packet_cache_t *packet_cache;
  48. } file_cache_t;
  49. /* speed mode selector */
  50. typedef enum {
  51. speed_multiplier = 1,
  52. speed_mbpsrate,
  53. speed_packetrate,
  54. speed_topspeed,
  55. speed_oneatatime
  56. } tcpreplay_speed_mode;
  57. /* speed mode configuration */
  58. typedef struct {
  59. /* speed modifiers */
  60. tcpreplay_speed_mode mode;
  61. COUNTER speed;
  62. float multiplier;
  63. int pps_multi;
  64. u_int32_t (*manual_callback)(struct tcpreplay_s *, char *, COUNTER);
  65. } tcpreplay_speed_t;
  66. /* accurate mode selector */
  67. typedef enum {
  68. accurate_gtod,
  69. accurate_select,
  70. accurate_nanosleep,
  71. accurate_ioport,
  72. } tcpreplay_accurate;
  73. typedef enum { source_filename = 1, source_fd = 2, source_cache = 3 } tcpreplay_source_type;
  74. typedef struct {
  75. tcpreplay_source_type type;
  76. int fd;
  77. char *filename;
  78. } tcpreplay_source_t;
  79. /* run-time options */
  80. typedef struct tcpreplay_opt_s {
  81. /* input/output */
  82. char *intf1_name;
  83. char *intf2_name;
  84. tcpreplay_speed_t speed;
  85. COUNTER loop;
  86. u_int32_t loopdelay_ms;
  87. int stats;
  88. bool use_pkthdr_len;
  89. /* tcpprep cache data */
  90. COUNTER cache_packets;
  91. char *cachedata;
  92. char *comment; /* tcpprep comment */
  93. /* deal with MTU/packet len issues */
  94. int mtu;
  95. /* accurate mode to use */
  96. tcpreplay_accurate accurate;
  97. /* limit # of packets to send */
  98. COUNTER limit_send;
  99. COUNTER limit_time;
  100. /* maximum sleep time between packets */
  101. struct timespec maxsleep;
  102. /* pcap file caching */
  103. file_cache_t file_cache[MAX_FILES];
  104. bool preload_pcap;
  105. /* pcap files/sources to replay */
  106. int source_cnt;
  107. tcpreplay_source_t sources[MAX_FILES];
  108. #ifdef ENABLE_VERBOSE
  109. /* tcpdump verbose printing */
  110. bool verbose;
  111. char *tcpdump_args;
  112. tcpdump_t *tcpdump;
  113. #endif
  114. /* dual file mode */
  115. bool dualfile;
  116. #ifdef HAVE_NETMAP
  117. int netmap;
  118. int netmap_delay;
  119. #endif
  120. /* print flow statistic */
  121. bool flow_stats;
  122. int flow_expiry;
  123. int unique_ip;
  124. float unique_loops;
  125. } tcpreplay_opt_t;
  126. /* interface */
  127. typedef enum { intf1 = 1, intf2 } tcpreplay_intf;
  128. /* tcpreplay context variable */
  129. #define TCPREPLAY_ERRSTR_LEN 1024
  130. typedef struct tcpreplay_s {
  131. tcpreplay_opt_t *options;
  132. interface_list_t *intlist;
  133. sendpacket_t *intf1;
  134. sendpacket_t *intf2;
  135. int intf1dlt;
  136. int intf2dlt;
  137. COUNTER iteration;
  138. COUNTER unique_iteration;
  139. COUNTER last_unique_iteration;
  140. sendpacket_type_t sp_type;
  141. char errstr[TCPREPLAY_ERRSTR_LEN];
  142. char warnstr[TCPREPLAY_ERRSTR_LEN];
  143. /* status trackers */
  144. int current_source; /* current source input being replayed */
  145. /* sleep helpers */
  146. struct timespec nap;
  147. uint32_t skip_packets;
  148. bool first_time;
  149. /* counter stats */
  150. tcpreplay_stats_t stats;
  151. tcpreplay_stats_t static_stats; /* stats returned by tcpreplay_get_stats() */
  152. /* flow statistics */
  153. flow_hash_table_t *flow_hash_table;
  154. /* abort, suspend & running flags */
  155. volatile bool abort;
  156. volatile bool suspend;
  157. bool running;
  158. } tcpreplay_t;
  159. /*
  160. * manual callback definition:
  161. * ctx = tcpreplay context
  162. * interface = name of interface current packet will be sent out
  163. * current_packet = packet number to be sent out
  164. *
  165. * Returns number of packets to send. 0 == send all remaining packets
  166. * Note: Your callback method is BLOCKING the main tcpreplay loop. If you
  167. * call tcpreplay_abort() from inside of your callback, you still need to
  168. * return (any value) so that the main loop is released and can abort.
  169. */
  170. typedef u_int32_t (*tcpreplay_manual_callback)(tcpreplay_t *ctx, char *interface, COUNTER current_packet);
  171. char *tcpreplay_geterr(tcpreplay_t *);
  172. char *tcpreplay_getwarn(tcpreplay_t *);
  173. tcpreplay_t *tcpreplay_init();
  174. void tcpreplay_close(tcpreplay_t *);
  175. /* only valid for using with GNU Autogen/AutoOpts */
  176. int tcpreplay_post_args(tcpreplay_t *, int argc);
  177. /* all these configuration functions return 0 on success and < 0 on error. */
  178. int tcpreplay_set_interface(tcpreplay_t *, tcpreplay_intf, char *);
  179. int tcpreplay_set_speed_mode(tcpreplay_t *, tcpreplay_speed_mode);
  180. int tcpreplay_set_speed_speed(tcpreplay_t *, COUNTER);
  181. int tcpreplay_set_speed_pps_multi(tcpreplay_t *, int);
  182. int tcpreplay_set_loop(tcpreplay_t *, u_int32_t);
  183. int tcpreplay_set_unique_ip(tcpreplay_t *, bool);
  184. int tcpreplay_set_unique_ip_loops(tcpreplay_t *, int);
  185. int tcpreplay_set_netmap(tcpreplay_t *, bool);
  186. int tcpreplay_set_use_pkthdr_len(tcpreplay_t *, bool);
  187. int tcpreplay_set_mtu(tcpreplay_t *, int);
  188. int tcpreplay_set_accurate(tcpreplay_t *, tcpreplay_accurate);
  189. int tcpreplay_set_limit_send(tcpreplay_t *, COUNTER);
  190. int tcpreplay_set_dualfile(tcpreplay_t *, bool);
  191. int tcpreplay_set_tcpprep_cache(tcpreplay_t *, char *);
  192. int tcpreplay_add_pcapfile(tcpreplay_t *, char *);
  193. int tcpreplay_set_preload_pcap(tcpreplay_t *, bool);
  194. /* information */
  195. int tcpreplay_get_source_count(tcpreplay_t *);
  196. int tcpreplay_get_current_source(tcpreplay_t *);
  197. int tcpreplay_set_flow_stats(tcpreplay_t *, bool);
  198. int tcpreplay_set_flow_expiry(tcpreplay_t *, int);
  199. bool tcpreplay_get_flow_stats(tcpreplay_t *);
  200. int tcpreplay_get_flow_expiry(tcpreplay_t *);
  201. /* functions controlling execution */
  202. int tcpreplay_prepare(tcpreplay_t *);
  203. int tcpreplay_replay(tcpreplay_t *);
  204. const tcpreplay_stats_t *tcpreplay_get_stats(tcpreplay_t *);
  205. int tcpreplay_abort(tcpreplay_t *);
  206. int tcpreplay_suspend(tcpreplay_t *);
  207. int tcpreplay_restart(tcpreplay_t *);
  208. bool tcpreplay_is_suspended(tcpreplay_t *);
  209. bool tcpreplay_is_running(tcpreplay_t *);
  210. /* set callback for manual stepping */
  211. int tcpreplay_set_manual_callback(tcpreplay_t *ctx, tcpreplay_manual_callback);
  212. /* statistic counts */
  213. COUNTER tcpreplay_get_pkts_sent(tcpreplay_t *ctx);
  214. COUNTER tcpreplay_get_bytes_sent(tcpreplay_t *ctx);
  215. COUNTER tcpreplay_get_failed(tcpreplay_t *ctx);
  216. const struct timeval *tcpreplay_get_start_time(tcpreplay_t *ctx);
  217. const struct timeval *tcpreplay_get_end_time(tcpreplay_t *ctx);
  218. int tcpreplay_set_verbose(tcpreplay_t *, bool);
  219. int tcpreplay_set_tcpdump_args(tcpreplay_t *, char *);
  220. int tcpreplay_set_tcpdump(tcpreplay_t *, tcpdump_t *);
  221. /*
  222. * These functions are seen by the outside world, but nobody should ever use them
  223. * outside of internal tcpreplay API functions
  224. */
  225. #define tcpreplay_seterr(x, y, ...) __tcpreplay_seterr(x, __FUNCTION__, __LINE__, __FILE__, y, __VA_ARGS__)
  226. void __tcpreplay_seterr(tcpreplay_t *ctx, const char *func, const int line, const char *file, const char *fmt, ...);
  227. void tcpreplay_setwarn(tcpreplay_t *ctx, const char *fmt, ...);
  228. #ifdef __cplusplus
  229. }
  230. #endif