tcpreplay_api.h 8.4 KB

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