tcpreplay_api.h 8.0 KB

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