send_packets.c 45 KB

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