send_packets.c 45 KB

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