tcpreplay_api.h 8.0 KB

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