send_packets.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <netinet/in.h>
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include "tcpreplay_api.h"
  32. #include "timestamp_trace.h"
  33. #include "../lib/sll.h"
  34. #ifdef HAVE_NETMAP
  35. #ifdef HAVE_SYS_POLL_H
  36. #include <sys/poll.h>
  37. #endif
  38. #include <sys/ioctl.h>
  39. #include <net/netmap.h>
  40. #include <net/netmap_user.h>
  41. #endif /* HAVE_NETMAP */
  42. #ifdef TCPREPLAY
  43. #ifdef TCPREPLAY_EDIT
  44. #include "tcpreplay_edit_opts.h"
  45. #include "tcpedit/tcpedit.h"
  46. extern tcpedit_t *tcpedit;
  47. #else
  48. #include "tcpreplay_opts.h"
  49. #endif /* TCPREPLAY_EDIT */
  50. #endif /* TCPREPLAY */
  51. #include "send_packets.h"
  52. #include "sleep.h"
  53. #ifdef DEBUG
  54. extern int debug;
  55. #endif
  56. static bool calc_sleep_time(tcpreplay_t *ctx, struct timeval *pkt_time,
  57. struct timeval *last, COUNTER len,
  58. sendpacket_t *sp, COUNTER counter, timestamp_t *sent_timestamp,
  59. COUNTER *start_us, COUNTER *skip_length);
  60. static void tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp _U_,
  61. struct timespec *nap_this_time, struct timeval *now,
  62. tcpreplay_accurate accurate);
  63. static u_char *get_next_packet(tcpreplay_t *ctx, pcap_t *pcap,
  64. struct pcap_pkthdr *pkthdr,
  65. int file_idx,
  66. packet_cache_t **prev_packet);
  67. static uint32_t get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter);
  68. /**
  69. * Fast flow packet edit
  70. *
  71. * Attempts to alter the packet IP addresses without
  72. * changing CRC, which will avoid overhead of tcpreplay-edit
  73. *
  74. * This code is a bit bloated but it is the result of
  75. * optimizing. Test performance on 10GigE+ networks if
  76. * modifying.
  77. */
  78. static void
  79. fast_edit_packet_dl(struct pcap_pkthdr *pkthdr, u_char **pktdata,
  80. uint32_t iteration, bool cached, int datalink)
  81. {
  82. int l2_len = 0;
  83. ipv4_hdr_t *ip_hdr;
  84. ipv6_hdr_t *ip6_hdr;
  85. hdlc_hdr_t *hdlc_hdr;
  86. sll_hdr_t *sll_hdr;
  87. struct tcpr_pppserial_hdr *ppp;
  88. uint32_t src_ip, dst_ip;
  89. uint32_t src_ip_orig, dst_ip_orig;
  90. uint16_t ether_type = 0;
  91. if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
  92. dbgx(1, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
  93. return;
  94. }
  95. switch (datalink) {
  96. case DLT_LINUX_SLL:
  97. l2_len = 16;
  98. sll_hdr = (sll_hdr_t *)*pktdata;
  99. ether_type = sll_hdr->sll_protocol;
  100. break;
  101. case DLT_PPP_SERIAL:
  102. l2_len = 4;
  103. ppp = (struct tcpr_pppserial_hdr *)*pktdata;
  104. if (ntohs(ppp->protocol) == 0x0021)
  105. ether_type = htons(ETHERTYPE_IP);
  106. else
  107. ether_type = ppp->protocol;
  108. break;
  109. case DLT_C_HDLC:
  110. l2_len = 4;
  111. hdlc_hdr = (hdlc_hdr_t *)*pktdata;
  112. ether_type = hdlc_hdr->protocol;
  113. break;
  114. case DLT_RAW:
  115. if ((*pktdata[0] >> 4) == 4)
  116. ether_type = ETHERTYPE_IP;
  117. else if ((*pktdata[0] >> 4) == 6)
  118. ether_type = ETHERTYPE_IP6;
  119. break;
  120. default:
  121. warnx("Unable to process unsupported DLT type: %s (0x%x)",
  122. pcap_datalink_val_to_description(datalink), datalink);
  123. return;
  124. }
  125. switch (ether_type) {
  126. case ETHERTYPE_IP:
  127. ip_hdr = (ipv4_hdr_t *)(*pktdata + l2_len);
  128. if (ip_hdr->ip_v != 4) {
  129. dbgx(2, "expected IPv4 but got: %u", ip_hdr->ip_v);
  130. return;
  131. }
  132. if (pkthdr->caplen < (bpf_u_int32)sizeof(*ip_hdr)) {
  133. dbgx(2, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
  134. return;
  135. }
  136. ip_hdr = (ipv4_hdr_t *)(*pktdata + l2_len);
  137. src_ip_orig = src_ip = ntohl(ip_hdr->ip_src.s_addr);
  138. dst_ip_orig = dst_ip = ntohl(ip_hdr->ip_dst.s_addr);
  139. break;
  140. case ETHERTYPE_IP6:
  141. if ((*pktdata[0] >> 4) != 6) {
  142. dbgx(2, "expected IPv6 but got: %u", *pktdata[0] >> 4);
  143. return;
  144. }
  145. if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
  146. dbgx(2, "Packet too short for Unique IPv6 feature: %u", pkthdr->caplen);
  147. return;
  148. }
  149. ip6_hdr = (ipv6_hdr_t *)(*pktdata + l2_len);
  150. src_ip_orig = src_ip = ntohl(ip6_hdr->ip_src.__u6_addr.__u6_addr32[3]);
  151. dst_ip_orig = dst_ip = ntohl(ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3]);
  152. break;
  153. default:
  154. return; /* non-IP */
  155. }
  156. /* swap src/dst IP's in a manner that does not affect CRC */
  157. if ((!cached && dst_ip > src_ip) ||
  158. (cached && (dst_ip - iteration) > (src_ip - 1 - iteration))) {
  159. if (cached) {
  160. --src_ip;
  161. ++dst_ip;
  162. } else {
  163. src_ip -= iteration;
  164. dst_ip += iteration;
  165. }
  166. /* CRC compensations for wrap conditions */
  167. if (src_ip > src_ip_orig && dst_ip > dst_ip_orig) {
  168. dbgx(1, "dst_ip > src_ip(%u): before(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  169. --src_ip;
  170. dbgx(1, "dst_ip > src_ip(%u): after(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  171. } else if (dst_ip < dst_ip_orig && src_ip < src_ip_orig) {
  172. dbgx(1, "dst_ip > src_ip(%u): before(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  173. ++dst_ip;
  174. dbgx(1, "dst_ip > src_ip(%u): after(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  175. }
  176. } else {
  177. if (cached) {
  178. ++src_ip;
  179. --dst_ip;
  180. } else {
  181. src_ip += iteration;
  182. dst_ip -= iteration;
  183. }
  184. /* CRC compensations for wrap conditions */
  185. if (dst_ip > dst_ip_orig && src_ip > src_ip_orig) {
  186. dbgx(1, "src_ip > dst_ip(%u): before(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  187. --dst_ip;
  188. dbgx(1, "src_ip > dst_ip(%u): after(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  189. } else if (src_ip < src_ip_orig && dst_ip < dst_ip_orig) {
  190. dbgx(1, "src_ip > dst_ip(%u): before(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  191. ++src_ip;
  192. dbgx(1, "src_ip > dst_ip(%u): after(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  193. }
  194. }
  195. dbgx(1, "(%u): final src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  196. }
  197. #if defined HAVE_NETMAP
  198. static inline void wake_send_queues(sendpacket_t *sp, tcpreplay_opt_t *options)
  199. {
  200. #ifdef HAVE_NETMAP
  201. if (options->netmap)
  202. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  203. #endif
  204. }
  205. #endif /* HAVE_NETMAP */
  206. static inline void
  207. fast_edit_packet(struct pcap_pkthdr *pkthdr, u_char **pktdata,
  208. uint32_t iteration, bool cached, int datalink)
  209. {
  210. uint16_t ether_type;
  211. vlan_hdr_t *vlan_hdr;
  212. ipv4_hdr_t *ip_hdr = NULL;
  213. ipv6_hdr_t *ip6_hdr = NULL;
  214. uint32_t src_ip, dst_ip;
  215. uint32_t src_ip_orig, dst_ip_orig;
  216. int l2_len;
  217. u_char *packet = *pktdata;
  218. if (datalink != DLT_EN10MB && datalink != DLT_JUNIPER_ETHER)
  219. fast_edit_packet_dl(pkthdr, pktdata, iteration, cached, datalink);
  220. if (pkthdr->caplen < (bpf_u_int32)TCPR_IPV6_H) {
  221. dbgx(2, "Packet too short for Unique IP feature: %u", pkthdr->caplen);
  222. return;
  223. }
  224. l2_len = 0;
  225. if (datalink == DLT_JUNIPER_ETHER) {
  226. if (memcmp(packet, "MGC", 3))
  227. warnx("No Magic Number found: %s (0x%x)",
  228. pcap_datalink_val_to_description(datalink), datalink);
  229. if ((packet[3] & 0x80) == 0x80) {
  230. l2_len = ntohs(*((uint16_t*)&packet[4]));
  231. if (l2_len > 1024) {
  232. warnx("L2 length too long: %u", l2_len);
  233. return;
  234. }
  235. l2_len += 6;
  236. } else
  237. l2_len = 4; /* no header extensions */
  238. }
  239. /* assume Ethernet, IPv4 for now */
  240. ether_type = ntohs(((eth_hdr_t*)(packet + l2_len))->ether_type);
  241. while (ether_type == ETHERTYPE_VLAN) {
  242. vlan_hdr = (vlan_hdr_t *)(packet + l2_len);
  243. ether_type = ntohs(vlan_hdr->vlan_len);
  244. l2_len += 4;
  245. }
  246. l2_len += sizeof(eth_hdr_t);
  247. switch (ether_type) {
  248. case ETHERTYPE_IP:
  249. ip_hdr = (ipv4_hdr_t *)(packet + l2_len);
  250. src_ip_orig = src_ip = ntohl(ip_hdr->ip_src.s_addr);
  251. dst_ip_orig = dst_ip = ntohl(ip_hdr->ip_dst.s_addr);
  252. break;
  253. case ETHERTYPE_IP6:
  254. ip6_hdr = (ipv6_hdr_t *)(packet + l2_len);
  255. src_ip_orig = src_ip = ntohl(ip6_hdr->ip_src.__u6_addr.__u6_addr32[3]);
  256. dst_ip_orig = dst_ip = ntohl(ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3]);
  257. break;
  258. default:
  259. return; /* non-IP */
  260. }
  261. dbgx(2, "Layer 3 protocol type is: 0x%04x", ether_type);
  262. /* swap src/dst IP's in a manner that does not affect CRC */
  263. if ((!cached && dst_ip > src_ip) ||
  264. (cached && (dst_ip - iteration) > (src_ip - 1 - iteration))) {
  265. if (cached) {
  266. --src_ip;
  267. ++dst_ip;
  268. } else {
  269. src_ip -= iteration;
  270. dst_ip += iteration;
  271. }
  272. /* CRC compensations for wrap conditions */
  273. if (src_ip > src_ip_orig && dst_ip > dst_ip_orig) {
  274. dbgx(1, "dst_ip > src_ip(%u): before(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  275. --src_ip;
  276. dbgx(1, "dst_ip > src_ip(%u): after(1) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  277. } else if (dst_ip < dst_ip_orig && src_ip < src_ip_orig) {
  278. dbgx(1, "dst_ip > src_ip(%u): before(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  279. ++dst_ip;
  280. dbgx(1, "dst_ip > src_ip(%u): after(2) src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  281. }
  282. } else {
  283. if (cached) {
  284. ++src_ip;
  285. --dst_ip;
  286. } else {
  287. src_ip += iteration;
  288. dst_ip -= iteration;
  289. }
  290. /* CRC compensations for wrap conditions */
  291. if (dst_ip > dst_ip_orig && src_ip > src_ip_orig) {
  292. dbgx(1, "src_ip > dst_ip(%u): before(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  293. --dst_ip;
  294. dbgx(1, "src_ip > dst_ip(%u): after(1) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  295. } else if (src_ip < src_ip_orig && dst_ip < dst_ip_orig) {
  296. dbgx(1, "src_ip > dst_ip(%u): before(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  297. ++src_ip;
  298. dbgx(1, "src_ip > dst_ip(%u): after(2) dst_ip=0x%08x src_ip=0x%08x", iteration, dst_ip, src_ip);
  299. }
  300. }
  301. dbgx(1, "(%u): final src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  302. switch (ether_type) {
  303. case ETHERTYPE_IP:
  304. ip_hdr->ip_src.s_addr = htonl(src_ip);
  305. ip_hdr->ip_dst.s_addr = htonl(dst_ip);
  306. break;
  307. case ETHERTYPE_IP6:
  308. ip6_hdr->ip_src.__u6_addr.__u6_addr32[3] = htonl(src_ip);
  309. ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3] = htonl(dst_ip);
  310. break;
  311. }
  312. }
  313. /**
  314. * \brief Update flow stats
  315. *
  316. * Finds out if flow is unique and updates stats.
  317. */
  318. static inline void update_flow_stats(tcpreplay_t *ctx, sendpacket_t *sp,
  319. const struct pcap_pkthdr *pkthdr, const u_char *pktdata, int datalink)
  320. {
  321. flow_entry_type_t res = flow_decode(ctx->flow_hash_table,
  322. pkthdr, pktdata, datalink, ctx->options->flow_expiry);
  323. switch (res) {
  324. case FLOW_ENTRY_NEW:
  325. ++ctx->stats.flows;
  326. ++ctx->stats.flows_unique;
  327. ++ctx->stats.flow_packets;
  328. if (sp) {
  329. ++sp->flows;
  330. ++sp->flows_unique;
  331. ++sp->flow_packets;
  332. }
  333. break;
  334. case FLOW_ENTRY_EXISTING:
  335. ++ctx->stats.flow_packets;
  336. if (sp)
  337. ++sp->flow_packets;
  338. break;
  339. case FLOW_ENTRY_EXPIRED:
  340. ++ctx->stats.flows_expired;
  341. ++ctx->stats.flows;
  342. ++ctx->stats.flow_packets;
  343. if (sp) {
  344. ++sp->flows_expired;
  345. ++sp->flows;
  346. ++sp->flow_packets;
  347. }
  348. break;
  349. case FLOW_ENTRY_NON_IP:
  350. ++ctx->stats.flow_non_flow_packets;
  351. if (sp)
  352. ++sp->flow_non_flow_packets;
  353. break;
  354. case FLOW_ENTRY_INVALID:
  355. ++ctx->stats.flows_invalid_packets;
  356. if (sp)
  357. ++sp->flows_invalid_packets;
  358. break;
  359. }
  360. }
  361. /**
  362. * \brief Preloads the memory cache for the given pcap file_idx
  363. *
  364. * Preloading can be used with or without --loop
  365. */
  366. void
  367. preload_pcap_file(tcpreplay_t *ctx, int idx)
  368. {
  369. tcpreplay_opt_t *options = ctx->options;
  370. char *path = options->sources[idx].filename;
  371. pcap_t *pcap = NULL;
  372. char ebuf[PCAP_ERRBUF_SIZE];
  373. const u_char *pktdata = NULL;
  374. struct pcap_pkthdr pkthdr;
  375. packet_cache_t *cached_packet = NULL;
  376. packet_cache_t **prev_packet = &cached_packet;
  377. COUNTER packetnum = 0;
  378. int dlt;
  379. /* close stdin if reading from it (needed for some OS's) */
  380. if (strncmp(path, "-", 1) == 0)
  381. if (close(1) == -1)
  382. warnx("unable to close stdin: %s", strerror(errno));
  383. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  384. errx(-1, "Error opening pcap file: %s", ebuf);
  385. dlt = pcap_datalink(pcap);
  386. /* loop through the pcap. get_next_packet() builds the cache for us! */
  387. while ((pktdata = get_next_packet(ctx, pcap, &pkthdr, idx, prev_packet)) != NULL) {
  388. packetnum++;
  389. if (options->flow_stats)
  390. update_flow_stats(ctx, NULL, &pkthdr, pktdata, dlt);
  391. }
  392. /* mark this file as cached */
  393. options->file_cache[idx].cached = TRUE;
  394. options->file_cache[idx].dlt = dlt;
  395. pcap_close(pcap);
  396. }
  397. static void increment_iteration(tcpreplay_t *ctx)
  398. {
  399. tcpreplay_opt_t *options = ctx->options;
  400. ctx->last_unique_iteration = ctx->unique_iteration;
  401. ++ctx->iteration;
  402. if (options->unique_ip) {
  403. assert(options->unique_loops > 0.0);
  404. ctx->unique_iteration =
  405. (COUNTER)((float)ctx->iteration / options->unique_loops) + 1;
  406. }
  407. }
  408. /**
  409. * the main loop function for tcpreplay. This is where we figure out
  410. * what to do with each packet
  411. */
  412. void
  413. send_packets(tcpreplay_t *ctx, pcap_t *pcap, int idx)
  414. {
  415. struct timeval print_delta, now, first_pkt_ts, pkt_ts_delta;
  416. tcpreplay_opt_t *options = ctx->options;
  417. COUNTER packetnum = 0;
  418. COUNTER limit_send = options->limit_send;
  419. struct pcap_pkthdr pkthdr;
  420. u_char *pktdata = NULL;
  421. sendpacket_t *sp = ctx->intf1;
  422. COUNTER pktlen;
  423. packet_cache_t *cached_packet = NULL;
  424. packet_cache_t **prev_packet = NULL;
  425. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  426. struct pcap_pkthdr *pkthdr_ptr;
  427. #endif
  428. int datalink = options->file_cache[idx].dlt;
  429. COUNTER skip_length = 0;
  430. COUNTER start_us;
  431. COUNTER end_us;
  432. bool preload = options->file_cache[idx].cached;
  433. bool top_speed = (options->speed.mode == speed_topspeed ||
  434. (options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
  435. bool now_is_now;
  436. ctx->skip_packets = 0;
  437. start_us = TIMEVAL_TO_MICROSEC(&ctx->stats.start_time);
  438. timerclear(&first_pkt_ts);
  439. if (options->limit_time > 0)
  440. end_us = start_us + SEC_TO_MICROSEC(options->limit_time);
  441. else
  442. end_us = 0;
  443. if (options->preload_pcap) {
  444. prev_packet = &cached_packet;
  445. } else {
  446. prev_packet = NULL;
  447. }
  448. if (!top_speed) {
  449. gettimeofday(&now, NULL);
  450. now_is_now = true;
  451. } else {
  452. now_is_now = false;
  453. }
  454. /* MAIN LOOP
  455. * Keep sending while we have packets or until
  456. * we've sent enough packets
  457. */
  458. while (!ctx->abort &&
  459. (pktdata = get_next_packet(ctx, pcap, &pkthdr, idx, prev_packet)) != NULL) {
  460. now_is_now = false;
  461. packetnum++;
  462. #if defined TCPREPLAY || defined TCPREPLAY_EDIT
  463. /* do we use the snaplen (caplen) or the "actual" packet len? */
  464. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr.len : (COUNTER)pkthdr.caplen;
  465. #elif TCPBRIDGE
  466. pktlen = (COUNTER)pkthdr.caplen;
  467. #else
  468. #error WTF??? We should not be here!
  469. #endif
  470. dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
  471. /* Dual nic processing */
  472. if (ctx->intf2 != NULL) {
  473. sp = (sendpacket_t *) cache_mode(ctx, options->cachedata, packetnum);
  474. /* sometimes we should not send the packet */
  475. if (sp == TCPR_DIR_NOSEND)
  476. continue;
  477. }
  478. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  479. pkthdr_ptr = &pkthdr;
  480. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  481. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  482. }
  483. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  484. #endif
  485. /* do we need to print the packet via tcpdump? */
  486. #ifdef ENABLE_VERBOSE
  487. if (options->verbose)
  488. tcpdump_print(options->tcpdump, &pkthdr, pktdata);
  489. #endif
  490. if (ctx->options->unique_ip && ctx->unique_iteration &&
  491. ctx->unique_iteration > ctx->last_unique_iteration) {
  492. /* edit packet to ensure every pass has unique IP addresses */
  493. fast_edit_packet(&pkthdr, &pktdata, ctx->unique_iteration - 1,
  494. preload, datalink);
  495. }
  496. /* update flow stats */
  497. if (options->flow_stats && !preload)
  498. update_flow_stats(ctx,
  499. options->cache_packets ? sp : NULL, &pkthdr, pktdata, datalink);
  500. if (ctx->first_time) {
  501. /* get time and timestamp of the first packet */
  502. gettimeofday(&now, NULL);
  503. memcpy(&first_pkt_ts, &pkthdr.ts, sizeof(struct timeval));
  504. }
  505. /*
  506. * this accelerator improves performance by avoiding expensive
  507. * time stamps during periods where we have fallen behind in our
  508. * sending
  509. */
  510. if (skip_length && pktlen < skip_length) {
  511. skip_length -= pktlen;
  512. } else if (ctx->skip_packets) {
  513. --ctx->skip_packets;
  514. } else {
  515. /*
  516. * time stamping is expensive, but now is the
  517. * time to do it.
  518. */
  519. dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr.ts.tv_sec, pkthdr.ts.tv_usec);
  520. skip_length = 0;
  521. ctx->skip_packets = 0;
  522. /*
  523. * Only sleep if we're not in top speed mode (-t)
  524. * or if the current packet is not late.
  525. *
  526. * This also sets skip_length which will avoid timestamping for
  527. * a given number of packets.
  528. */
  529. timersub(&pkthdr.ts, &first_pkt_ts, &pkt_ts_delta);
  530. if (calc_sleep_time(ctx, &pkt_ts_delta, &ctx->stats.last_time, pktlen, sp, packetnum,
  531. &ctx->stats.end_time, &start_us, &skip_length)) {
  532. now_is_now = true;
  533. gettimeofday(&now, NULL);
  534. }
  535. /*
  536. * track the time of the "last packet sent". Again, because of OpenBSD
  537. * we have to do a memcpy rather then assignment.
  538. *
  539. * A number of 3rd party tools generate bad timestamps which go backwards
  540. * in time. Hence, don't update the "last" unless pkthdr.ts > last
  541. */
  542. if (!top_speed && timercmp(&ctx->stats.last_time, &pkt_ts_delta, <))
  543. memcpy(&ctx->stats.last_time, &pkt_ts_delta, sizeof(struct timeval));
  544. /*
  545. * we know how long to sleep between sends, now do it.
  546. */
  547. if (timesisset(&ctx->nap))
  548. tcpr_sleep(ctx, sp, &ctx->nap, &now, options->accurate);
  549. }
  550. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  551. /* write packet out on network */
  552. if (sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen)
  553. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  554. /*
  555. * mark the time when we sent the last packet
  556. *
  557. * we have to cast the ts, since OpenBSD sucks
  558. * had to be special and use bpf_timeval.
  559. */
  560. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  561. #ifdef TIMESTAMP_TRACE
  562. add_timestamp_trace_entry(pktlen, &ctx->stats.end_time, skip_length);
  563. #endif
  564. ctx->stats.pkts_sent++;
  565. ctx->stats.bytes_sent += pktlen;
  566. /* print stats during the run? */
  567. if (options->stats > 0) {
  568. if (! timerisset(&ctx->stats.last_print)) {
  569. memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
  570. } else {
  571. timersub(&now, &ctx->stats.last_print, &print_delta);
  572. if (print_delta.tv_sec >= options->stats) {
  573. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  574. packet_stats(&ctx->stats);
  575. memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
  576. }
  577. }
  578. }
  579. #if defined HAVE_NETMAP
  580. if (sp->first_packet) {
  581. wake_send_queues(sp, options);
  582. sp->first_packet = false;
  583. }
  584. #endif
  585. /* stop sending based on the duration limit... */
  586. if ((end_us > 0 && TIMEVAL_TO_MICROSEC(&now) > end_us) ||
  587. /* ... or stop sending based on the limit -L? */
  588. (limit_send > 0 && ctx->stats.pkts_sent >= limit_send)) {
  589. ctx->abort = true;
  590. }
  591. } /* while */
  592. #ifdef HAVE_NETMAP
  593. /* when completing test, wait until the last packet is sent */
  594. if (options->netmap && (ctx->abort || options->loop == 1)) {
  595. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  596. gettimeofday(&now, NULL);
  597. now_is_now = true;
  598. }
  599. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  600. gettimeofday(&now, NULL);
  601. now_is_now = true;
  602. }
  603. }
  604. #endif /* HAVE_NETMAP */
  605. if (!now_is_now)
  606. gettimeofday(&now, NULL);
  607. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  608. increment_iteration(ctx);
  609. }
  610. /**
  611. * the alternate main loop function for tcpreplay. This is where we figure out
  612. * what to do with each packet when processing two files a the same time
  613. */
  614. void
  615. send_dual_packets(tcpreplay_t *ctx, pcap_t *pcap1, int cache_file_idx1, pcap_t *pcap2, int cache_file_idx2)
  616. {
  617. struct timeval print_delta, now, first_pkt_ts, pkt_ts_delta;
  618. tcpreplay_opt_t *options = ctx->options;
  619. COUNTER packetnum = 0;
  620. COUNTER limit_send = options->limit_send;
  621. int cache_file_idx;
  622. struct pcap_pkthdr pkthdr1, pkthdr2;
  623. u_char *pktdata1 = NULL, *pktdata2 = NULL, *pktdata = NULL;
  624. sendpacket_t *sp;
  625. COUNTER pktlen;
  626. packet_cache_t *cached_packet1 = NULL, *cached_packet2 = NULL;
  627. packet_cache_t **prev_packet1 = NULL, **prev_packet2 = NULL;
  628. struct pcap_pkthdr *pkthdr_ptr;
  629. int datalink;
  630. COUNTER start_us;
  631. COUNTER end_us;
  632. COUNTER skip_length = 0;
  633. bool top_speed = (options->speed.mode == speed_topspeed ||
  634. (options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
  635. bool now_is_now;
  636. ctx->skip_packets = 0;
  637. start_us = TIMEVAL_TO_MICROSEC(&ctx->stats.start_time);
  638. timerclear(&first_pkt_ts);
  639. if (options->limit_time > 0)
  640. end_us = start_us + SEC_TO_MICROSEC(options->limit_time);
  641. else
  642. end_us = 0;
  643. if (options->preload_pcap) {
  644. prev_packet1 = &cached_packet1;
  645. prev_packet2 = &cached_packet2;
  646. } else {
  647. prev_packet1 = NULL;
  648. prev_packet2 = NULL;
  649. }
  650. pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  651. pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  652. if (!top_speed) {
  653. gettimeofday(&now, NULL);
  654. now_is_now = true;
  655. } else {
  656. now_is_now = false;
  657. }
  658. /* MAIN LOOP
  659. * Keep sending while we have packets or until
  660. * we've sent enough packets
  661. */
  662. while (!ctx->abort &&
  663. !(pktdata1 == NULL && pktdata2 == NULL)) {
  664. now_is_now = false;
  665. packetnum++;
  666. /* figure out which pcap file we need to process next
  667. * when get_next_packet() returns null for pktdata, the pkthdr
  668. * will still have the old values from the previous call. This
  669. * means we can't always trust the timestamps to tell us which
  670. * file to process.
  671. */
  672. if (pktdata1 == NULL) {
  673. /* file 2 is next */
  674. sp = ctx->intf2;
  675. datalink = options->file_cache[cache_file_idx2].dlt;
  676. pkthdr_ptr = &pkthdr2;
  677. cache_file_idx = cache_file_idx2;
  678. pktdata = pktdata2;
  679. } else if (pktdata2 == NULL) {
  680. /* file 1 is next */
  681. sp = ctx->intf1;
  682. datalink = options->file_cache[cache_file_idx1].dlt;
  683. pkthdr_ptr = &pkthdr1;
  684. cache_file_idx = cache_file_idx1;
  685. pktdata = pktdata1;
  686. } else if (timercmp(&pkthdr1.ts, &pkthdr2.ts, <=)) {
  687. /* file 1 is next */
  688. sp = ctx->intf1;
  689. datalink = options->file_cache[cache_file_idx1].dlt;
  690. pkthdr_ptr = &pkthdr1;
  691. cache_file_idx = cache_file_idx1;
  692. pktdata = pktdata1;
  693. } else {
  694. /* file 2 is next */
  695. sp = ctx->intf2;
  696. datalink = options->file_cache[cache_file_idx2].dlt;
  697. pkthdr_ptr = &pkthdr2;
  698. cache_file_idx = cache_file_idx2;
  699. pktdata = pktdata2;
  700. }
  701. #if defined TCPREPLAY || defined TCPREPLAY_EDIT
  702. /* do we use the snaplen (caplen) or the "actual" packet len? */
  703. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  704. #elif TCPBRIDGE
  705. pktlen = (COUNTER)pkthdr_ptr->caplen;
  706. #else
  707. #error WTF??? We should not be here!
  708. #endif
  709. dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
  710. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  711. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  712. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  713. }
  714. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  715. #endif
  716. /* do we need to print the packet via tcpdump? */
  717. #ifdef ENABLE_VERBOSE
  718. if (options->verbose)
  719. tcpdump_print(options->tcpdump, pkthdr_ptr, pktdata);
  720. #endif
  721. if (ctx->options->unique_ip && ctx->unique_iteration &&
  722. ctx->unique_iteration > ctx->last_unique_iteration) {
  723. /* edit packet to ensure every pass is unique */
  724. fast_edit_packet(pkthdr_ptr, &pktdata, ctx->unique_iteration - 1,
  725. options->file_cache[cache_file_idx].cached, datalink);
  726. }
  727. /* update flow stats */
  728. if (options->flow_stats && !options->file_cache[cache_file_idx].cached)
  729. update_flow_stats(ctx, sp, pkthdr_ptr, pktdata, datalink);
  730. if (ctx->first_time) {
  731. /* get time and timestamp of the first packet */
  732. gettimeofday(&now, NULL);
  733. memcpy(&first_pkt_ts, &pkthdr_ptr->ts, sizeof(struct timeval));
  734. }
  735. /*
  736. * this accelerator improves performance by avoiding expensive
  737. * time stamps during periods where we have fallen behind in our
  738. * sending
  739. */
  740. if (skip_length && pktlen < skip_length) {
  741. skip_length -= pktlen;
  742. } else if (ctx->skip_packets) {
  743. --ctx->skip_packets;
  744. } else {
  745. /*
  746. * time stamping is expensive, but now is the
  747. * time to do it.
  748. */
  749. dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr_ptr->ts.tv_sec, pkthdr_ptr->ts.tv_usec);
  750. skip_length = 0;
  751. ctx->skip_packets = 0;
  752. /*
  753. * Only sleep if we're not in top speed mode (-t)
  754. * or if the current packet is not late.
  755. *
  756. * This also sets skip_length which will avoid timestamping for
  757. * a given number of packets.
  758. */
  759. timersub(&pkthdr_ptr->ts, &first_pkt_ts, &pkt_ts_delta);
  760. if (calc_sleep_time(ctx, &pkt_ts_delta, &ctx->stats.last_time, pktlen, sp, packetnum,
  761. &ctx->stats.end_time, &start_us, &skip_length)) {
  762. now_is_now = true;
  763. gettimeofday(&now, NULL);
  764. }
  765. /*
  766. * track the time of the "last packet sent". Again, because of OpenBSD
  767. * we have to do a memcpy rather then assignment.
  768. *
  769. * A number of 3rd party tools generate bad timestamps which go backwards
  770. * in time. Hence, don't update the "last" unless pkthdr.ts > last
  771. */
  772. if (!top_speed && timercmp(&ctx->stats.last_time, &pkt_ts_delta, <))
  773. memcpy(&ctx->stats.last_time, &pkt_ts_delta, sizeof(struct timeval));
  774. /*
  775. * we know how long to sleep between sends, now do it.
  776. */
  777. if (timesisset(&ctx->nap))
  778. tcpr_sleep(ctx, sp, &ctx->nap, &now, options->accurate);
  779. }
  780. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  781. /* write packet out on network */
  782. if (sendpacket(sp, pktdata, pktlen, pkthdr_ptr) < (int)pktlen)
  783. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  784. /*
  785. * mark the time when we sent the last packet
  786. *
  787. * we have to cast the ts, since OpenBSD sucks
  788. * had to be special and use bpf_timeval.
  789. */
  790. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  791. ctx->stats.pkts_sent++;
  792. ctx->stats.bytes_sent += pktlen;
  793. /* print stats during the run? */
  794. if (options->stats > 0) {
  795. if (! timerisset(&ctx->stats.last_print)) {
  796. memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
  797. } else {
  798. timersub(&now, &ctx->stats.last_print, &print_delta);
  799. if (print_delta.tv_sec >= options->stats) {
  800. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  801. packet_stats(&ctx->stats);
  802. memcpy(&ctx->stats.last_print, &now, sizeof(ctx->stats.last_print));
  803. }
  804. }
  805. }
  806. #if defined HAVE_NETMAP
  807. if (sp->first_packet) {
  808. wake_send_queues(sp, options);
  809. sp->first_packet = false;
  810. }
  811. #endif
  812. /* get the next packet for this file handle depending on which we last used */
  813. if (sp == ctx->intf2) {
  814. pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  815. } else {
  816. pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  817. }
  818. /* stop sending based on the duration limit... */
  819. if ((end_us > 0 && TIMEVAL_TO_MICROSEC(&now) > end_us) ||
  820. /* ... or stop sending based on the limit -L? */
  821. (limit_send > 0 && ctx->stats.pkts_sent >= limit_send)) {
  822. ctx->abort = true;
  823. }
  824. } /* while */
  825. #ifdef HAVE_NETMAP
  826. /* when completing test, wait until the last packet is sent */
  827. if (options->netmap && (ctx->abort || options->loop == 1)) {
  828. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  829. gettimeofday(&now, NULL);
  830. now_is_now = true;
  831. }
  832. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  833. gettimeofday(&now, NULL);
  834. now_is_now = true;
  835. }
  836. }
  837. #endif /* HAVE_NETMAP */
  838. if (!now_is_now)
  839. gettimeofday(&now, NULL);
  840. memcpy(&ctx->stats.end_time, &now, sizeof(ctx->stats.end_time));
  841. increment_iteration(ctx);
  842. }
  843. /**
  844. * Gets the next packet to be sent out. This will either read from the pcap file
  845. * or will retrieve the packet from the internal cache.
  846. *
  847. * The parameter prev_packet is used as the parent of the new entry in the cache list.
  848. * This should be NULL on the first call to this function for each file and
  849. * will be updated as new entries are added (or retrieved) from the cache list.
  850. */
  851. u_char *
  852. get_next_packet(tcpreplay_t *ctx, pcap_t *pcap, struct pcap_pkthdr *pkthdr, int idx,
  853. packet_cache_t **prev_packet)
  854. {
  855. tcpreplay_opt_t *options = ctx->options;
  856. u_char *pktdata = NULL;
  857. uint32_t pktlen;
  858. /* pcap may be null in cache mode! */
  859. /* packet_cache_t may be null in file read mode! */
  860. assert(pkthdr);
  861. /*
  862. * Check if we're caching files
  863. */
  864. if (options->preload_pcap && (prev_packet != NULL)) {
  865. /*
  866. * Yes we are caching files - has this one been cached?
  867. */
  868. if (options->file_cache[idx].cached) {
  869. if (*prev_packet == NULL) {
  870. /*
  871. * Get the first packet in the cache list directly from the file
  872. */
  873. *prev_packet = options->file_cache[idx].packet_cache;
  874. } else {
  875. /*
  876. * Get the next packet in the cache list
  877. */
  878. *prev_packet = (*prev_packet)->next;
  879. }
  880. if (*prev_packet != NULL) {
  881. pktdata = (*prev_packet)->pktdata;
  882. memcpy(pkthdr, &((*prev_packet)->pkthdr), sizeof(struct pcap_pkthdr));
  883. }
  884. } else {
  885. /*
  886. * We should read the pcap file, and cache the results
  887. */
  888. pktdata = (u_char *)pcap_next(pcap, pkthdr);
  889. if (pktdata != NULL) {
  890. if (*prev_packet == NULL) {
  891. /*
  892. * Create the first packet in the list
  893. */
  894. *prev_packet = safe_malloc(sizeof(packet_cache_t));
  895. options->file_cache[idx].packet_cache = *prev_packet;
  896. } else {
  897. /*
  898. * Add a packet to the end of the list
  899. */
  900. (*prev_packet)->next = safe_malloc(sizeof(packet_cache_t));
  901. *prev_packet = (*prev_packet)->next;
  902. }
  903. if (*prev_packet != NULL) {
  904. (*prev_packet)->next = NULL;
  905. pktlen = pkthdr->len;
  906. (*prev_packet)->pktdata = safe_malloc(pktlen);
  907. memcpy((*prev_packet)->pktdata, pktdata, pktlen);
  908. memcpy(&((*prev_packet)->pkthdr), pkthdr, sizeof(struct pcap_pkthdr));
  909. }
  910. }
  911. }
  912. } else {
  913. /*
  914. * Read pcap file as normal
  915. */
  916. pktdata = (u_char *)pcap_next(pcap, pkthdr);
  917. }
  918. /* this get's casted to a const on the way out */
  919. return pktdata;
  920. }
  921. /**
  922. * determines based upon the cachedata which interface the given packet
  923. * should go out. Also rewrites any layer 2 data we might need to adjust.
  924. * Returns a void cased pointer to the ctx->intfX of the corresponding
  925. * interface or NULL on error
  926. */
  927. void *
  928. cache_mode(tcpreplay_t *ctx, char *cachedata, COUNTER packet_num)
  929. {
  930. tcpreplay_opt_t *options = ctx->options;
  931. void *sp = NULL;
  932. int result;
  933. if (packet_num > options->cache_packets) {
  934. tcpreplay_seterr(ctx, "%s", "Exceeded number of packets in cache file.");
  935. return NULL;
  936. }
  937. result = check_cache(cachedata, packet_num);
  938. if (result == TCPR_DIR_NOSEND) {
  939. dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
  940. return NULL;
  941. }
  942. else if (result == TCPR_DIR_C2S) {
  943. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
  944. sp = ctx->intf1;
  945. }
  946. else if (result == TCPR_DIR_S2C) {
  947. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
  948. sp = ctx->intf2;
  949. }
  950. else {
  951. tcpreplay_seterr(ctx, "Invalid cache value: %i", result);
  952. return NULL;
  953. }
  954. return sp;
  955. }
  956. /**
  957. * Given the timestamp on the current packet and the last packet sent,
  958. * calculate the appropriate amount of time to sleep. Sleep time
  959. * will be in ctx->nap.
  960. */
  961. static bool calc_sleep_time(tcpreplay_t *ctx, struct timeval *pkt_time_delta,
  962. struct timeval *last_delta, COUNTER len,
  963. sendpacket_t *sp, COUNTER counter, timestamp_t *sent_timestamp,
  964. COUNTER *start_us, COUNTER *skip_length)
  965. {
  966. tcpreplay_opt_t *options = ctx->options;
  967. struct timeval nap_for;
  968. COUNTER now_us;
  969. bool update_time = true;
  970. timesclear(&ctx->nap);
  971. /*
  972. * pps_multi accelerator. This uses the existing send accelerator
  973. * and hence requires the funky math to get the expected timings.
  974. */
  975. if (options->speed.mode == speed_packetrate && options->speed.pps_multi) {
  976. ctx->skip_packets = options->speed.pps_multi - 1;
  977. }
  978. /* no last packet sent, just leave */
  979. if (ctx->first_time) {
  980. ctx->first_time = false;
  981. return false;
  982. }
  983. switch(options->speed.mode) {
  984. case speed_multiplier:
  985. /*
  986. * Replay packets a factor of the time they were originally sent.
  987. */
  988. if (timerisset(last_delta)) {
  989. /* make sure the packet is not late */
  990. COUNTER delta_us, delta_pkt_time;
  991. now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp);
  992. delta_pkt_time = TIMEVAL_TO_MICROSEC(pkt_time_delta);
  993. delta_us = now_us - *start_us;
  994. if (timercmp(pkt_time_delta, last_delta, >) && (delta_pkt_time > delta_us)) {
  995. /* pkt_time_delta has increased, so handle normally */
  996. timersub(pkt_time_delta, last_delta, &nap_for);
  997. dbgx(3, "original packet delta pkt_time: " TIMEVAL_FORMAT, nap_for.tv_sec, nap_for.tv_usec);
  998. TIMEVAL_TO_TIMESPEC(&nap_for, &ctx->nap);
  999. dbgx(3, "original packet delta timv: " TIMESPEC_FORMAT, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  1000. timesdiv_float(&ctx->nap, options->speed.multiplier);
  1001. dbgx(3, "original packet delta/div: " TIMESPEC_FORMAT, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  1002. } else {
  1003. /* Don't sleep if this packet is late or in the past */
  1004. update_time = false;
  1005. }
  1006. } else {
  1007. /* Don't sleep if this is our first packet */
  1008. update_time = false;
  1009. }
  1010. break;
  1011. case speed_mbpsrate:
  1012. /*
  1013. * Ignore the time supplied by the capture file and send data at
  1014. * a constant 'rate' (bytes per second).
  1015. */
  1016. now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp);
  1017. if (now_us) {
  1018. COUNTER next_tx_us;
  1019. COUNTER bps = options->speed.speed;
  1020. COUNTER bits_sent = ((ctx->stats.bytes_sent + len) * 8);
  1021. COUNTER tx_us = now_us - *start_us;
  1022. /*
  1023. * bits * 1000000 divided by bps = microseconds
  1024. *
  1025. * ensure there is no overflow in cases where bits_sent is very high
  1026. */
  1027. if (bits_sent > COUNTER_OVERFLOW_RISK && bps > 500000)
  1028. next_tx_us = (bits_sent * 1000) / bps * 1000;
  1029. else
  1030. next_tx_us = (bits_sent * 1000000) / bps;
  1031. if (next_tx_us > tx_us) {
  1032. NANOSEC_TO_TIMESPEC((next_tx_us - tx_us) * 1000, &ctx->nap);
  1033. } else if (tx_us > next_tx_us) {
  1034. tx_us = now_us - *start_us;
  1035. *skip_length = ((tx_us - next_tx_us) * bps) / 8000000;
  1036. }
  1037. update_current_timestamp_trace_entry(ctx->stats.bytes_sent + (COUNTER)len, now_us, tx_us, next_tx_us);
  1038. }
  1039. dbgx(3, "packet size=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT, len,
  1040. ctx->nap.tv_sec, ctx->nap.tv_nsec);
  1041. break;
  1042. case speed_packetrate:
  1043. /*
  1044. * Ignore the time supplied by the capture file and send data at
  1045. * a constant rate (packets per second).
  1046. */
  1047. now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp);
  1048. if (now_us) {
  1049. COUNTER pph = ctx->options->speed.speed * (ctx->options->speed.pps_multi > 0 ? ctx->options->speed.pps_multi : (60 * 60));
  1050. COUNTER pkts_sent = ctx->stats.pkts_sent;
  1051. /*
  1052. * packets * 1000000 divided by pps = microseconds
  1053. * packets per sec (pps) = packets per hour / (60 * 60)
  1054. */
  1055. COUNTER next_tx_us = (pkts_sent * 1000000) * (60 * 60) / pph;
  1056. COUNTER tx_us = now_us - *start_us;
  1057. if (next_tx_us > tx_us)
  1058. NANOSEC_TO_TIMESPEC((next_tx_us - tx_us) * 1000, &ctx->nap);
  1059. else
  1060. ctx->skip_packets = options->speed.pps_multi;
  1061. update_current_timestamp_trace_entry(ctx->stats.bytes_sent + (COUNTER)len, now_us, tx_us, next_tx_us);
  1062. }
  1063. dbgx(3, "packet count=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT, ctx->stats.pkts_sent,
  1064. ctx->nap.tv_sec, ctx->nap.tv_nsec);
  1065. break;
  1066. case speed_oneatatime:
  1067. /* do we skip prompting for a key press? */
  1068. if (ctx->skip_packets == 0) {
  1069. ctx->skip_packets = get_user_count(ctx, sp, counter);
  1070. }
  1071. /* decrement our send counter */
  1072. printf("Sending packet " COUNTER_SPEC " out: %s\n", counter,
  1073. sp == ctx->intf1 ? options->intf1_name : options->intf2_name);
  1074. ctx->skip_packets--;
  1075. /* leave */
  1076. break;
  1077. case speed_topspeed:
  1078. break;
  1079. default:
  1080. errx(-1, "Unknown/supported speed mode: %d", options->speed.mode);
  1081. break;
  1082. }
  1083. return update_time;
  1084. }
  1085. static void tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp,
  1086. struct timespec *nap_this_time, struct timeval *now,
  1087. tcpreplay_accurate accurate)
  1088. {
  1089. tcpreplay_opt_t *options = ctx->options;
  1090. bool flush =
  1091. #ifdef HAVE_NETMAP
  1092. true;
  1093. #else
  1094. false;
  1095. #endif
  1096. /* don't sleep if nap = {0, 0} */
  1097. if (!timesisset(nap_this_time))
  1098. return;
  1099. /* do we need to limit the total time we sleep? */
  1100. if (timesisset(&(options->maxsleep)) && (timescmp(nap_this_time, &(options->maxsleep), >))) {
  1101. dbgx(2, "Was going to sleep for " TIMESPEC_FORMAT " but maxsleeping for " TIMESPEC_FORMAT,
  1102. nap_this_time->tv_sec, nap_this_time->tv_nsec, options->maxsleep.tv_sec,
  1103. options->maxsleep.tv_nsec);
  1104. memcpy(nap_this_time, &(options->maxsleep), sizeof(*nap_this_time));
  1105. }
  1106. dbgx(2, "Sleeping: " TIMESPEC_FORMAT,
  1107. nap_this_time->tv_sec, nap_this_time->tv_nsec);
  1108. /*
  1109. * Depending on the accurate method & packet rate computation method
  1110. * We have multiple methods of sleeping, pick the right one...
  1111. */
  1112. switch (accurate) {
  1113. #ifdef HAVE_SELECT
  1114. case accurate_select:
  1115. select_sleep(nap_this_time);
  1116. gettimeofday(now, NULL);
  1117. break;
  1118. #endif
  1119. case accurate_gtod:
  1120. gettimeofday_sleep(sp, nap_this_time, now, flush);
  1121. break;
  1122. case accurate_nanosleep:
  1123. nanosleep_sleep(nap_this_time);
  1124. gettimeofday(now, NULL);
  1125. break;
  1126. default:
  1127. errx(-1, "Unknown timer mode %d", accurate);
  1128. }
  1129. }
  1130. /**
  1131. * Ask the user how many packets they want to send.
  1132. */
  1133. static uint32_t
  1134. get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter)
  1135. {
  1136. tcpreplay_opt_t *options = ctx->options;
  1137. struct pollfd poller[1]; /* use poll to read from the keyboard */
  1138. char input[EBUF_SIZE];
  1139. unsigned long send = 0;
  1140. printf("**** Next packet #" COUNTER_SPEC " out %s. How many packets do you wish to send? ",
  1141. counter, (sp == ctx->intf1 ? options->intf1_name : options->intf2_name));
  1142. fflush(NULL);
  1143. poller[0].fd = STDIN_FILENO;
  1144. poller[0].events = POLLIN | POLLPRI | POLLNVAL;
  1145. poller[0].revents = 0;
  1146. if (fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK))
  1147. errx(-1, "Unable to clear non-blocking flag on stdin: %s", strerror(errno));
  1148. /* wait for the input */
  1149. if (poll(poller, 1, -1) < 0)
  1150. errx(-1, "Error reading user input from stdin: %s", strerror(errno));
  1151. /*
  1152. * read to the end of the line or EBUF_SIZE,
  1153. * Note, if people are stupid, and type in more text then EBUF_SIZE
  1154. * then the next fgets() will pull in that data, which will have poor
  1155. * results. fuck them.
  1156. */
  1157. if (fgets(input, sizeof(input), stdin) == NULL) {
  1158. errx(-1, "Unable to process user input for fd %d: %s", fileno(stdin), strerror(errno));
  1159. } else if (strlen(input) > 1) {
  1160. send = strtoul(input, NULL, 0);
  1161. }
  1162. /* how many packets should we send? */
  1163. if (send == 0) {
  1164. dbg(1, "Input was less then 1 or non-numeric, assuming 1");
  1165. /* assume send only one packet */
  1166. send = 1;
  1167. }
  1168. return(uint32_t)send;
  1169. }