send_packets.c 41 KB

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